1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
|
/*
** Copyright (C) 2001-2025 Zabbix SIA
**
** This program is free software: you can redistribute it and/or modify it under the terms of
** the GNU Affero General Public License as published by the Free Software Foundation, version 3.
**
** This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
** without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
** See the GNU Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License along with this program.
** If not, see <https://www.gnu.org/licenses/>.
**/
class CSoftwareVersionCheck {
static URL = 'https://services.zabbix.com/updates/v1';
static TYPE = 'software_update_check';
static DELAY_ON_ERROR = 60; // 1 minute
#data = {};
constructor() {
this.#startUpdating(ZBX_SOFTWARE_VERSION_CHECK_DELAY);
}
#startUpdating(delay) {
setTimeout(() => this.#getSavedData(), delay * 1000);
}
#getSavedData() {
const curl = new Curl('zabbix.php');
curl.setArgument('action', 'softwareversioncheck.get');
fetch(curl.getUrl())
.then(response => response.json())
.then(response => {
if ('error' in response) {
throw {error: response.error};
}
if (!response.is_software_update_check_enabled) {
return;
}
if ('delay' in response) {
this.#startUpdating(response.delay);
}
else {
this.#data.versions = [];
this.#data.version = response.version;
this.#data.check_hash = response.check_hash;
this.#data.csrf_token = response.csrf_token;
this.#getCurrentData();
}
})
.catch(exception => {
console.log('Could not get data', exception);
this.#startUpdating(CSoftwareVersionCheck.DELAY_ON_ERROR);
});
}
#getCurrentData() {
const curl = new Curl(CSoftwareVersionCheck.URL);
curl.setArgument('type', CSoftwareVersionCheck.TYPE);
curl.setArgument('version', this.#data.version);
curl.setArgument('software_update_check_hash', this.#data.check_hash);
fetch(curl.getUrl(), {cache: 'no-store'})
.then(response => response.json())
.then(response => {
if ('versions' in response) {
this.#data.versions = response.versions;
}
})
.catch(exception => {
console.log('Could not get data', exception);
})
.finally(() => {
this.#update();
});
}
#update() {
const curl = new Curl('zabbix.php');
curl.setArgument('action', 'softwareversioncheck.update');
fetch(curl.getUrl(), {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
versions: this.#data.versions,
[CSRF_TOKEN_NAME]: this.#data.csrf_token
})
})
.then(response => response.json())
.then(response => {
if ('error' in response) {
console.log('Could not update data', {error: response.error});
}
this.#startUpdating(response.delay || CSoftwareVersionCheck.DELAY_ON_ERROR);
})
.catch(exception => {
console.log('Could not update data', exception);
this.#startUpdating(CSoftwareVersionCheck.DELAY_ON_ERROR);
});
}
}
|