v2.15 - Fixed issues found in Zabbix 5.4.12 and refactored Javascript for better error handling

This commit is contained in:
Mark Oudsen 2023-08-16 14:18:08 +02:00
parent 16729e0720
commit 624ce29a39
4 changed files with 93 additions and 61 deletions

View File

@ -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)_

View File

@ -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;
}

View File

@ -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'];

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<zabbix_export>
<version>5.4</version>
<date>2023-07-02T12:48:36Z</date>
<date>2023-08-16T12:05:37Z</date>
<media_types>
<media_type>
<name>MailGraph</name>
@ -9,7 +9,7 @@
<parameters>
<parameter>
<name>baseURL</name>
<value>https://myzabbix.example.com/</value>
<value>https://myzabbix.com/zabbix/</value>
</parameter>
<parameter>
<name>duration</name>
@ -57,10 +57,21 @@
</parameter>
<parameter>
<name>URL</name>
<value>https://myzabbix.example.com/mailGraph.php</value>
<value>https://myzabbix.com/mailGraph.php</value>
</parameter>
</parameters>
<script>// mailGraph v2.14
<script>// 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),
@ -78,21 +89,13 @@ try {
// Declare output type
req.addHeader('Content-Type: application/json');
// Pick up relevant fields 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!';
}
@ -118,20 +121,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) == &quot;!&quot;) {
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;
@ -142,7 +155,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;
}</script>
@ -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]</message>
Trigger ID (/url): {TRIGGER.ID} {TRIGGER.URL}</message>
</message_template>
<message_template>
<event_source>TRIGGERS</event_source>
@ -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]</message>
Trigger ID: {TRIGGER.ID} {TRIGGER.URL}</message>
</message_template>
<message_template>
<event_source>TRIGGERS</event_source>
@ -201,11 +206,11 @@ itemId: {ITEM.ID]</message>
Current problem status is {EVENT.STATUS}, age is {EVENT.AGE}, acknowledged: {EVENT.ACK.STATUS}.
eventId: {EVENT.ID}
TriggerId: {TRIGGER.ID}
itemId: {ITEM.ID]</message>
Event ID: {EVENT.ID}
Trigger ID: {TRIGGER.ID}</message>
</message_template>
</message_templates>
</media_type>
</media_types>
</zabbix_export>