Created
January 5, 2024 21:21
-
-
Save kaellego/3067941ae61cac8b4c331ea7af7c405d to your computer and use it in GitHub Desktop.
Webhook SMS Diafaan for zabbix notifications
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var HttpNotification = { | |
username: null, | |
password: null, | |
to: null, | |
messageType: null, | |
message: null, | |
url: null, | |
sendMessage: function () { | |
var params = { | |
'username': HttpNotification.username, | |
'password': HttpNotification.password, | |
'to': HttpNotification.to, | |
'message-type': HttpNotification.messageType, | |
'message': HttpNotification.message | |
}; | |
var queryString = Object.keys(params).map(function (key) { | |
return key + '=' + encodeURIComponent(params[key]); | |
}).join('&'); | |
var url = HttpNotification.url + queryString; | |
var request = new HttpRequest(); | |
Zabbix.log(4, '[HTTP Notification] URL: ' + url); | |
var response = request.get(url); | |
Zabbix.log(4, '[HTTP Notification] HTTP code: ' + request.getStatus()); | |
if (request.getStatus() !== 200) { | |
throw 'Failed to send message. HTTP status: ' + request.getStatus(); | |
} | |
// Verificar se a resposta é um JSON válido | |
try { | |
var jsonResponse = JSON.parse(response); | |
if (!jsonResponse.success) { | |
throw 'API error: ' + jsonResponse.message; | |
} | |
} catch (error) { | |
// Se não for JSON, apenas logar a resposta | |
Zabbix.log(4, '[HTTP Notification] Response: ' + response); | |
if (response.indexOf('Erro') !== -1) { | |
throw 'API error: ' + response; | |
} | |
} | |
return 'Message sent successfully.'; | |
} | |
}; | |
try { | |
var params = JSON.parse(value); | |
// Validate and assign parameters | |
if (!params.username) throw 'Username is required'; | |
if (!params.password) throw 'Password is required'; | |
if (!params.to) throw 'Recipient number is required'; | |
if (!params.messageType) throw 'Message type is required'; | |
HttpNotification.username = params.username; | |
HttpNotification.password = params.password; | |
HttpNotification.to = params.to; | |
HttpNotification.messageType = params.messageType; | |
HttpNotification.message = params.subject + '\n' + params.message; | |
HttpNotification.url = params.url; | |
// Send the message | |
return HttpNotification.sendMessage(); | |
} | |
catch (error) { | |
Zabbix.log(4, '[HTTP Notification] Error: ' + error); | |
throw 'Notification failed: ' + error + '.'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment