Skip to content

Instantly share code, notes, and snippets.

@isaqueprofeta
Last active July 6, 2022 23:42
Show Gist options
  • Save isaqueprofeta/be0de6b37d3b7c12b225c93af88b5508 to your computer and use it in GitHub Desktop.
Save isaqueprofeta/be0de6b37d3b7c12b225c93af88b5508 to your computer and use it in GitHub Desktop.
Zabbix JS Script item for count of day-before Alerts
  • Where? https://t.me/ZabbixTech

  • Question: Hi team...is there any query where we can get the count of alarms raised per day?

  • Answer: Since version 6.0 you can create an API key and then do an JavaScript Script Item to check the information from Zabbix API using time_from and time_till parameters to filter the day before. Didn't tested, just prototyped the idea below, and before trying the code, remember to add the "url" and "apikey" to the item Parameters, (and please use a macro with vault for your apikey), in this case I'd configure the item interval as an scheduled one to run everyday at 1 or 2 AM:

try {
var parameters = JSON.parse(value);
var req = new CurlHttpRequest();
var yesterdayStart = new Date();
yesterdayStart.setHours(0, 0, 0);
var start = yesterdayStart.setDate(yesterdayStart.getDate() - 1);
var yesterdayEnd = new Date();
yesterdayEnd.setHours(23, 59, 59);
var end = yesterdayEnd.setDate(yesterdayEnd.getDate() - 1);
var jsonZabbix = {
"jsonrpc": "2.0",
"method": "event.get",
"params": {
"countOutput": true,
"time_from": start,
"time_till": end,
},
"auth": parameters.apikey,
"id": 1
};
req.AddHeader('Content-Type: application/json');
resp = req.Post(
parameters.url,
JSON.stringify(jsonZabbix)
);
if (resp.Status() != 200) {
throw 'Response code: '+resp.Status();
}
var results = JSON.parse(resp);
return(JSON.stringify(results));
} catch (error) {
Zabbix.Log(3, "Error 'warning': "+error);
return "";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment