Hi!
I have a js script that sends a notification to telegram every time a client's server is down. Currently it sends a notification the very second problem occurs. The problem is, servers tend to go up and down very often, so I have a lot of garbage in our chat, stuff like "problem has been resolved in 2 minutes" I would like zabbix to only to send a notification if a problem persists for more that an hour.
I tried to modify my script, but to no avail.
I know that this can be done with triggers, but that way our graphs are going to have a funny look.
the script
I am using zabbix 5.0.41
I have a js script that sends a notification to telegram every time a client's server is down. Currently it sends a notification the very second problem occurs. The problem is, servers tend to go up and down very often, so I have a lot of garbage in our chat, stuff like "problem has been resolved in 2 minutes" I would like zabbix to only to send a notification if a problem persists for more that an hour.
I tried to modify my script, but to no avail.
I know that this can be done with triggers, but that way our graphs are going to have a funny look.
Code:
var Telegram = {
token: null,
to: null,
message: null,
proxy: null,
parse_mode: null,
escapeMarkup: function (str, mode) {
switch (mode) {
case 'markdown':
return str.replace(/([_*\[`])/g, '\\$&');
case 'markdownv2':
return str.replace(/([_*\[\]()~`>#+\-=|{}.!])/g, '\\$&');
default:
return str;
}
},
sendMessage: function () {
var params = {
chat_id: Telegram.to,
text: Telegram.message,
disable_web_page_preview: true,
disable_notification: false
},
data,
response,
request = new CurlHttpRequest(),
url = 'https://api.telegram.org/bot' + Telegram.token + '/sendMessage';
if (Telegram.parse_mode !== null) {
params['parse_mode'] = Telegram.parse_mode;
}
if (Telegram.proxy) {
request.SetProxy(Telegram.proxy);
}
request.AddHeader('Content-Type: application/json');
data = JSON.stringify(params);
// Remove replace() function if you want to see the exposed token in the log file.
Zabbix.Log(4, '[Telegram Webhook] URL: ' + url.replace(Telegram.token, '<TOKEN>'));
Zabbix.Log(4, '[Telegram Webhook] params: ' + data);
response = request.Post(url, data);
Zabbix.Log(4, '[Telegram Webhook] HTTP code: ' + request.Status());
try {
response = JSON.parse(response);
}
catch (error) {
response = null;
}
if (request.Status() !== 200 || typeof response.ok !== 'boolean' || response.ok !== true) {
if (typeof response.description === 'string') {
throw response.description;
}
else {
throw 'Unknown error. Check debug log for more information.';
}
}
}
};
try {
var params = JSON.parse(value);
if (typeof params.Token === 'undefined') {
throw 'Incorrect value is given for parameter "Token": parameter is missing';
}
Telegram.token = params.Token;
if (params.HTTPProxy) {
Telegram.proxy = params.HTTPProxy;
}
params.ParseMode = params.ParseMode.toLowerCase();
if (['markdown', 'html', 'markdownv2'].indexOf(params.ParseMode) !== -1) {
Telegram.parse_mode = params.ParseMode;
}
Telegram.to = params.To;
Telegram.message = params.Subject + '\n' + params.Message;
if (['markdown', 'markdownv2'].indexOf(params.ParseMode) !== -1) {
Telegram.message = Telegram.escapeMarkup(Telegram.message, params.ParseMode);
}
Telegram.sendMessage();
return 'OK';
}
catch (error) {
Zabbix.Log(4, '[Telegram Webhook] notification failed: ' + error);
throw 'Sending failed: ' + error + '.';
}
I am using zabbix 5.0.41
Comment