diff --git a/README.md b/README.md index 89bb10f..67694c5 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,15 @@ More information can be found in the Wiki. ## Installation ## Please refer to the Wiki how to get mailGraph installed and configured on your system. +## mailGraph v2.15 release ## +_(2023/08/16)_ + +_This version has been verified with Zabbix 5.4 and 6.0 LTS and is expected to work with 6.4 and later (based on v2.10 testing)_ + +Release notes +- Fixed new error condition found in Zabbix 5.4.12 where zeroed or blank parameters for a webhook are no longer added as parameters in the Javascript. +- Refactored error handling inside the Javascript to distinguish automatically between 'email ID response' or a debug or warning level message (this prevents the code of throwing an unknown error like 'Invalid JSON'). + ## mailGraph v2.14 release ## _(2023/07/10)_ diff --git a/javascript/zabbix.mailGraph.js b/javascript/zabbix.mailGraph.js index d81a341..36490e6 100644 --- a/javascript/zabbix.mailGraph.js +++ b/javascript/zabbix.mailGraph.js @@ -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; } diff --git a/mailGraph.php b/mailGraph.php index 8ad1244..5779d4b 100644 --- a/mailGraph.php +++ b/mailGraph.php @@ -58,6 +58,8 @@ // Small refactor on itemId variable processing (no longer mandatory) // Additional logic added to random eventId to explain in case of issues // Fixed missing flag for fetching web url related items + // 2.15 2023/08/16 - Mark Oudsen - Bugfix for Zabbix 5.4 where empty or zeroed variables are no longer + // passed by Zabbix (hence resulting in weird errors) // ------------------------------------------------------------------------------------------------------ // // (C) M.J.Oudsen, mark.oudsen@puzzl.nl @@ -92,7 +94,7 @@ ///////////////////////////////////////////////////////////////////////////////////////////////////////// // CONSTANTS - $cVersion = 'v2.14'; + $cVersion = 'v2.15'; $cCRLF = chr(10).chr(13); $maskDateTime = 'Y-m-d H:i:s'; $maxGraphs = 8; @@ -495,15 +497,17 @@ // Assumes that config.json file has the correct information for MANDATORY information - // MANDATORY + // DEFAULTS $problemData['eventId'] = 0; + $problemData['duration'] = 0; + // MANDATORY $problemData['recipient'] = $config['cli_recipient']; $problemData['baseURL'] = $config['cli_baseURL']; - $problemData['duration'] = $config['cli_duration']; // OPTIONAL if (isset($config['cli_eventId'])) { $problemData['eventId'] = $config['cli_eventId']; } + if (isset($config['cli_duration'])) { $problemData['duration'] = $config['cli_duration']; } if (isset($config['cli_subject'])) { $problemData['subject'] = $config['cli_subject']; } if (isset($config['cli_period'])) { $problemData['period'] = $config['cli_period']; } if (isset($config['cli_period_header'])) { $problemData['period_header'] = $config['cli_period_header']; } @@ -547,6 +551,7 @@ $cCRLF.json_encode($problemData,JSON_PRETTY_PRINT|JSON_NUMERIC_CHECK)); // --- CHECK AND SET P_ VARIABLES --- + // FROM POST OR CLI DATA if (!isset($problemData['eventId'])) { echo "Missing EVENT ID?\n"; die; } @@ -555,8 +560,8 @@ if (!isset($problemData['recipient'])) { echo "Missing RECIPIENT?\n"; die; } $p_recipient = $problemData['recipient']; - if (!isset($problemData['duration'])) { echo "Missing DURATION?\n"; die; } - $p_duration = intval($problemData['duration']); + $p_duration = 0; + if (isset($problemData['duration'])) { $p_duration = intval($problemData['duration']); } if (!isset($problemData['baseURL'])) { echo "Missing URL?\n"; die; } $p_URL = $problemData['baseURL']; diff --git a/mailGraph.xml b/mailGraph.xml index 94359bd..021a0fe 100755 --- a/mailGraph.xml +++ b/mailGraph.xml @@ -1,7 +1,7 @@ 5.4 - 2023-07-02T12:48:36Z + 2023-08-16T12:05:37Z MailGraph @@ -9,7 +9,7 @@ baseURL - https://myzabbix.example.com/ + https://myzabbix.com/zabbix/ duration @@ -57,10 +57,21 @@ URL - https://myzabbix.example.com/mailGraph.php + https://myzabbix.com/mailGraph.php - @@ -170,11 +183,7 @@ Host: {HOST.NAME} Severity: {EVENT.SEVERITY} Operational data: {EVENT.OPDATA} Original problem ID: {EVENT.ID} -{TRIGGER.URL} - -eventId: {EVENT.ID} -TriggerId: {TRIGGER.ID} -itemId: {ITEM.ID] +Trigger ID (/url): {TRIGGER.ID} {TRIGGER.URL} TRIGGERS @@ -186,11 +195,7 @@ Problem duration: {EVENT.DURATION} Host: {HOST.NAME} Severity: {EVENT.SEVERITY} Original problem ID: {EVENT.ID} -{TRIGGER.URL} - -eventId: {EVENT.ID} -TriggerId: {TRIGGER.ID} -itemId: {ITEM.ID] +Trigger ID: {TRIGGER.ID} {TRIGGER.URL} TRIGGERS @@ -201,11 +206,11 @@ itemId: {ITEM.ID] Current problem status is {EVENT.STATUS}, age is {EVENT.AGE}, acknowledged: {EVENT.ACK.STATUS}. -eventId: {EVENT.ID} -TriggerId: {TRIGGER.ID} -itemId: {ITEM.ID] +Event ID: {EVENT.ID} +Trigger ID: {TRIGGER.ID} +