mirror of
https://github.com/moudsen/mailGraph
synced 2025-10-26 07:07:39 +01:00
v2.15 - Fixed issues found in Zabbix 5.4.12 and refactored Javascript for better error handling
This commit is contained in:
@@ -1,4 +1,15 @@
|
||||
// mailGraph v2.14
|
||||
// mailGraph v2.15
|
||||
|
||||
// Function to test string
|
||||
function isJSON(str) {
|
||||
try {
|
||||
JSON.stringify(JSON.parse(str));
|
||||
return true;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
// Pickup parameters
|
||||
params = JSON.parse(value),
|
||||
@@ -16,21 +27,13 @@ try {
|
||||
// Declare output type
|
||||
req.addHeader('Content-Type: application/json');
|
||||
|
||||
// Pick up fields relevant for mailGraph API level call
|
||||
// Pick up fields relevant for mailGraph API level call while parsing/casting fields that should be integer
|
||||
fields.itemId = params.itemId * 1;
|
||||
fields.eventId = params.eventId * 1;
|
||||
fields.recipient = params.recipient;
|
||||
fields.baseURL = params.baseURL;
|
||||
fields.duration = params.duration * 1;
|
||||
|
||||
if (isNaN(fields.eventId)) {
|
||||
throw '[MailGraph Webhook] Invalid event ID? Integer required (use actual event ID from Zabbix!)';
|
||||
}
|
||||
|
||||
if (isNaN(fields.duration)) {
|
||||
throw '[MailGraph Webhook] Invalid duration? Integer required (set to zero if unknown)!';
|
||||
}
|
||||
|
||||
if (fields.recipient.charAt(0) == '{') {
|
||||
throw '[MailGraph Webhook] Please define recipient for the test message!';
|
||||
}
|
||||
@@ -56,20 +59,30 @@ try {
|
||||
var resp = req.post(params.URL,JSON.stringify(fields));
|
||||
Zabbix.Log(4, '[Mailgraph Webhook] Received response:' + resp);
|
||||
|
||||
// If blank the email address is likely incorrect
|
||||
// The response can be
|
||||
// - did not receive status 200 as result (contains HTTP server response)
|
||||
// - null (no response received at all)
|
||||
// - empty string (likely no e-mail sent due to recipient issue)
|
||||
// - not json (debugging message for troubleshooting or configuration hints)
|
||||
// - json (contains the mail message ID sent)
|
||||
|
||||
if (req.getStatus() != 200) {
|
||||
throw '[MailGraph Webhook] Processing of mailGraph.php failed: ' + resp;
|
||||
}
|
||||
if (resp==null) {
|
||||
throw '[MailGraph Webhook] No data received from mailGraph! Likely the email processing failed? Check mailGraph logging';
|
||||
throw '[MailGraph Webhook] No response received from mailGraph.php? This should not occur (check URL and your webserver!)';
|
||||
}
|
||||
|
||||
// If there was an error, report it and stop
|
||||
if (req.getStatus() != 200) { throw JSON.parse(resp).errors[0]; }
|
||||
|
||||
// If no datas returned, report it and stop
|
||||
if (resp.charAt(0) == "!") {
|
||||
throw '[MailGraph Webhook] No data received from mailGraph! Likely the event ID provided does not exist? Check mailGraph logging';
|
||||
if (resp=='') {
|
||||
throw '[MailGraph Webhook] No data received from mailGraph - please check recipient address or mailGraph log and retry.';
|
||||
}
|
||||
|
||||
// We expect the message id back from the processing script
|
||||
// Check if JSON was returned
|
||||
if (!isJSON(resp)) {
|
||||
throw '[MailGraph Webhook] An error has occurred during processing: ' + resp;
|
||||
}
|
||||
|
||||
// We expect the message id back from the processing script response in JSON format
|
||||
msg = JSON.parse(resp);
|
||||
|
||||
result.tags.__message_id = msg.messageId;
|
||||
@@ -80,7 +93,7 @@ try {
|
||||
}
|
||||
catch (error)
|
||||
{
|
||||
// In case something went wrong in the processing, pass the error back to Zabbix
|
||||
// In case something else went wrong in the processing, pass the error back to Zabbix
|
||||
Zabbix.Log(127, 'MailGraph notification failed: '+error);
|
||||
throw 'MailGraph notification failed : '+error;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user