From e262f21dc822f90cc8432cb2de073d0dfb88a67f Mon Sep 17 00:00:00 2001 From: Mark Oudsen Date: Wed, 10 Mar 2021 19:57:42 +0100 Subject: [PATCH] Added ability to embed multiple periods of the same graph (v1.22) --- config/config.json.template | 10 +- config/config.json.template.multigraph | 28 ++++++ javascript/zabbix.mailGraph.js | 3 + mailGraph.php | 121 +++++++++++++++++++++---- mailGraph.xml | 17 +++- templates/html.template | 50 +++++++++- templates/plain.template | 8 +- 7 files changed, 204 insertions(+), 33 deletions(-) create mode 100644 config/config.json.template.multigraph diff --git a/config/config.json.template b/config/config.json.template index ffe15a4..8e3dcfc 100644 --- a/config/config.json.template +++ b/config/config.json.template @@ -6,17 +6,23 @@ "cli_eventValue": 0, "cli_duration": 0, "cli_recipient": "recipient@domain.com", - "cli_subject": "[CLI TEST] {{ HOST_NAME|raw }}: ({{ EVENT_SEVERITY }}) {{ EVENT_NAME|raw }}", + "cli_subject": "[TEST] {{ HOST_NAME|raw }}: ({{ EVENT_SEVERITY }}) {{ EVENT_NAME|raw }}", "cli_baseURL": "https:\/\/domain.com\/zabbix\/", + "cli_period": "30m", + "cli_period_header": "Last 30 minutes", + "cli_debug": 1, "zabbix_user": "alogicalusername", "zabbix_user_pwd": "astrongpassword", "zabbix_api_user": "alogicalusername", "zabbix_api_pwd": "astrongpassword", "mail_from": "sender@domain.com", + "subject": "{{ HOST_NAME|raw }}: ({{ EVENT_SEVERITY }}) {{ EVENT_NAME|raw }}", "smtp_server": "localhost", "smtp_port": 25, "smtp_transport": "none", "smtp_strict": "yes", "graph_match": "any", - "period": "2d" + "period": "20m", + "period_header": "Last 20 minutes", + "debug": 0 } diff --git a/config/config.json.template.multigraph b/config/config.json.template.multigraph new file mode 100644 index 0000000..441233d --- /dev/null +++ b/config/config.json.template.multigraph @@ -0,0 +1,28 @@ +{ + "script_baseurl": "https:\/\/domain.com\/", + "cli_itemId": 0, + "cli_triggerId": 0, + "cli_eventId": 0, + "cli_eventValue": 0, + "cli_duration": 0, + "cli_recipient": "recipient@domain.com", + "cli_subject": "[TEST] {{ HOST_NAME|raw }}: ({{ EVENT_SEVERITY }}) {{ EVENT_NAME|raw }}", + "cli_baseURL": "https:\/\/domain.com\/zabbix\/", + "cli_periods": "10m,4h,2d,7d", + "cli_periods_headers": "Last 10 minutes,Last 4 hours,Last 2 days,Last 7 days", + "cli_debug": 1, + "zabbix_user": "alogicalusername", + "zabbix_user_pwd": "astrongpassword", + "zabbix_api_user": "alogicalusername", + "zabbix_api_pwd": "astrongpassword", + "mail_from": "sender@domain.com", + "subject": "{{ HOST_NAME|raw }}: ({{ EVENT_SEVERITY }}) {{ EVENT_NAME|raw }}", + "smtp_server": "localhost", + "smtp_port": 25, + "smtp_transport": "none", + "smtp_strict": "yes", + "graph_match": "any", + "periods": "10m,4h,2d,7d", + "periods_headers": "Last 10 minutes,Last 4 hours,Last 2 days,Last 7 days", + "debug": 0 +} diff --git a/javascript/zabbix.mailGraph.js b/javascript/zabbix.mailGraph.js index caa831e..feb4527 100644 --- a/javascript/zabbix.mailGraph.js +++ b/javascript/zabbix.mailGraph.js @@ -26,6 +26,9 @@ try { if (typeof params.graphHeight === 'string') { fields.graphHeight = params.graphHeight; } if (typeof params.subject === 'string') { fields.subject = params.subject; } if (typeof params.showLegend === 'string') { fields.showLegend = params.showLegend; } + if (typeof params.periods === 'string') { fields.periods = params.periods; } + if (typeof params.periods_headers === 'string') { fields.periods_headers = params.periods_headers; } + if (typeof params.debug === 'string') { fields.debug = params.debug; } // Add generic fields Object.keys(params).forEach(function(key) { diff --git a/mailGraph.php b/mailGraph.php index 85bb45a..c480d57 100644 --- a/mailGraph.php +++ b/mailGraph.php @@ -27,6 +27,7 @@ // 1.20 2021/03/07 - Mark Oudsen - Production level version - leaving BETA from here on ... // 1.21 2021/03/09 - Mark Oudsen - Reverted graph.get code back to original code as it was not a bug but // a wrongly typed requested (should be ARRAY, not comma separated)! + // 1.22 2021/03/10 - Mark Oudsen - Added ability to embed multiple periods (1-4) of the same graph // ------------------------------------------------------------------------------------------------------ // // (C) M.J.Oudsen, mark.oudsen@puzzl.nl @@ -41,7 +42,7 @@ // MAIN SEQUENCE // ------------- // 1) Fetch trigger, item, host, graph, event information via Zabbix API via CURL - // 2) Fetch Graph associated to the item/trigger (if any) via Zabbix URL login via CURL + // 2) Fetch Graph(s) associated to the item/trigger (if any) via Zabbix URL login via CURL // 3) Build and send mail message from template using Swift/TWIG // ///////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -49,7 +50,7 @@ // CONSTANTS - $cVersion = 'v1.21'; + $cVersion = 'v1.22'; $cCRLF = chr(10).chr(13); $maskDateTime = 'Y-m-d H:i:s'; @@ -156,7 +157,7 @@ // Cookie and image names $filename_cookie = $z_tmp_cookies ."zabbix_cookie_" .$graphid . "." .$thisTime. ".txt"; - $filename = "zabbix_graph_" .$graphid . "." . $thisTime . ".png"; + $filename = "zabbix_graph_" .$graphid . "." . $thisTime . "-" . $period . ".png"; $image_name = $z_images_path . $filename; // Configure CURL @@ -333,6 +334,7 @@ // Assumes that config.json file has the correct information + // MANDATORY $problemData['itemId'] = $config['cli_itemId']; $problemData['triggerId'] = $config['cli_triggerId']; $problemData['eventId'] = $config['cli_eventId']; @@ -343,12 +345,18 @@ $problemData['subject'] = $config['cli_subject']; $problemData['period'] = $config['cli_period']; + // OPTIONAL + if (isset($config['cli_period_header'])) { $problemData['period_header'] = $config['cli_period_header']; } + if (isset($config['cli_periods'])) { $problemData['periods'] = $config['cli_periods']; } + if (isset($config['cli_periods_headers'])) { $problemData['periods_headers'] = $config['cli_periods_headers']; } + if (isset($config['cli_debug'])) { $problemData['debug'] = $config['cli_debug']; } + // Switch on CLI log display $showLog = TRUE; } } - _log('# Data passed from Zabbix'.$cCRLF.json_encode($problemData,JSON_PRETTY_PRINT|JSON_NUMERIC_CHECK)); + _log('# Data passed to MailGraph main routine and used for processing'.$cCRLF.json_encode($problemData,JSON_PRETTY_PRINT|JSON_NUMERIC_CHECK)); // --- CHECK AND SET P_ VARIABLES --- // FROM POST OR CLI DATA @@ -516,8 +524,23 @@ switch ($aTag['tag']) { case 'mailGraph.period': - $p_period = $aTag['value']; - _log('+ Graph display period override = '.$p_period); + $problemData['period'] = $aTag['value']; + _log('+ Graph display period override = '.$problemData['period']); + break; + + case 'mailGraph.period_header': + $problemData['period_header'] = $aTag['value']; + _log('+ Graph display period header override = '.$problemData['period_header']); + break; + + case 'mailGraph.periods': + $problemData['periods'] = $aTag['value']; + _log('+ Graph display periods override = '.$problemData['periods']); + break; + + case 'mailGraph.periods_headers': + $problemData['periods_headers'] = $aTag['value']; + _log('+ Graph display periods headers override = '.$problemData['periods_headers']); break; case 'mailGraph.graph': @@ -539,6 +562,11 @@ $p_graphHeight = intval($aTag['value']); _log('+ Graph height override = '.$$p_graphHeight); break; + + case 'mailGraph.debug': + $problemData['debug'] = 1; + _log('+ Mail debug log enabled'); + break; } } @@ -749,7 +777,49 @@ // Fetch Graph ////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////// - $graphFile = ''; + // Determine number of periods for this graph + + $p_periods = array(); + $p_periods_headers = array(); + + if (isset($problemData['periods'])) + { + // Multiple periods mode selected + + _log('# Multi period graph mode selected'); + + $p_periods = explode(',',$problemData['periods']); + + // If invalid, replace with single graph item + if (sizeof($p_periods)==0) { $p_periods[] = $p_period; } + + // --- Determine headers + + if (isset($problemData['periods_headers'])) { $p_periods_headers = explode(',',$problemData['periods_headers']); } + + // If no headers specified, simply copy the period information + if (sizeof($p_periods_headers)==0) { $p_periods_headers = $p_periods; } + } + else + { + // Single period mode selected + + $p_periods[] = $p_period; + + if (isset($problemData['period_header'])) + { + $p_periods_headers[] = $problemData['period_header']; + } + else + { + $p_periods_headers[] = $p_period; + } + } + + while (sizeof($p_periods)>4) { array_pop($p_periods); } + while (sizeof($p_periods_headers)>4) { array_pop($p_periods_headers); } + + $graphFiles = array(); $graphURL = ''; // If we have any matching graph, make the embedding information available @@ -784,14 +854,19 @@ _log('# Adding '.strtoupper($theType).' graph #'.$mailData['GRAPH_ID']); - $graphFile = GraphImageById($mailData['GRAPH_ID'], - $p_graphWidth,$p_graphHeight, - $theGraph['graphtype'], - $p_showLegend,$p_period); + foreach($p_periods as $aKey=>$aPeriod) + { + $graphFile = GraphImageById($mailData['GRAPH_ID'], + $p_graphWidth,$p_graphHeight, + $theGraph['graphtype'], + $p_showLegend,$aPeriod); - _log('> Filename = '.$graphFile); + $graphFiles[] = $graphFile; + + $mailData['GRAPH_URL'.($aKey+1)] = $z_url_image . $graphFile; + $mailData['GRAPH_HEADER'.($aKey+1)] = $p_periods_headers[$aKey]; + } - $mailData['GRAPH_URL'] = $z_url_image . $graphFile; $mailData['GRAPH_ZABBIXLINK'] = $z_server.'graphs.php?form=update&graphid='.$mailData['GRAPH_ID']; } @@ -889,11 +964,17 @@ $twig = new \Twig\Environment($loader); - if ($graphFile!='') + if (sizeof($graphFiles)>0) { - // Embed the image - $mailData['GRAPH_CID'] = $message->embed(Swift_Image::fromPath($z_images_path.$graphFile)); - _log('> Embedded graph image '.$z_images_path.$graphFile); + // Embed the image(s) + + $graphReference = 1; + + foreach($graphFiles as $graphFile) + { + $mailData['GRAPH_CID'.$graphReference++] = $message->embed(Swift_Image::fromPath($z_images_path.$graphFile)); + _log('> Embedded graph image '.$z_images_path.$graphFile); + } } $bodyHTML = $twig->render('html', $mailData); @@ -908,7 +989,7 @@ ->setBody($bodyHTML, 'text/html') ->addPart($bodyPlain, 'text/plain'); - if ($cDebugMail) + if (($cDebugMail) || (isset($problemData['debug']))) { _log('# Attaching logs to mail message'); @@ -923,14 +1004,14 @@ $result = $mailer->send($message); - // Return TAG information + // Return Event TAG information for Zabbix $response = array('messageId.mailGraph'=>$messageId); echo json_encode($response).$cCRLF; // Store log? - if ($cDebug) + if (($cDebug) || (isset($problemData['debug']))) { unset($mailData['LOG_HTML']); unset($mailData['LOG_PLAIN']); diff --git a/mailGraph.xml b/mailGraph.xml index b6669ba..d8be653 100755 --- a/mailGraph.xml +++ b/mailGraph.xml @@ -1,7 +1,7 @@ 5.0 - 2021-03-05T10:51:39Z + 2021-03-10T18:50:24Z MailGraph @@ -35,14 +35,18 @@ HTTPProxy - - infoGiveMeAName - A generic value you want to pass to MailGraph - itemId {ITEM.ID} + + periods + 10m,4h,1d,7d + + + periods_headers + Last 10 minutes,Last 4 hours,Last day,Last 7 days + recipient {ALERT.SENDTO} @@ -92,6 +96,9 @@ if (typeof params.graphHeight === 'string') { fields.graphHeight = params.graphHeight; } if (typeof params.subject === 'string') { fields.subject = params.subject; } if (typeof params.showLegend === 'string') { fields.showLegend = params.showLegend; } + if (typeof params.periods === 'string') { fields.periods = params.periods; } + if (typeof params.periods_headers === 'string') { fields.periods_headers = params.periods_headers; } + if (typeof params.debug === 'string') { fields.debug = params.debug; } // Add generic fields Object.keys(params).forEach(function(key) { diff --git a/templates/html.template b/templates/html.template index 993c322..9c63623 100644 --- a/templates/html.template +++ b/templates/html.template @@ -43,6 +43,12 @@ text-decoration: none; color: #010059; } + hr { + border: none; + height: 1px; + color: #333; /* old IE */ + background-color: #333; + } .MsgBody { width: 100%; } @@ -68,6 +74,10 @@ font-size: 16px; font-weight: bold; } + .graphHeader { + font-size: 14px; + font-weight: bold; + } .content { font-size: 14px; } @@ -104,10 +114,46 @@ -{% if GRAPH_CID|length > 0 %} +{% if GRAPH_CID1|length > 0 %}
- Zabbix Graph + + {{ GRAPH_HEADER1 }}
+ Zabbix Graph + +
+ +{% endif %} +{% if GRAPH_CID2|length > 0 %} + +
+ +
+ {{ GRAPH_HEADER2 }}
+ Zabbix Graph + +
+ +{% endif %} +{% if GRAPH_CID3|length > 0 %} + +
+ +
+ {{ GRAPH_HEADER3 }}
+ Zabbix Graph + +
+ +{% endif %} +{% if GRAPH_CID4|length > 0 %} + +
+ +
+ {{ GRAPH_HEADER4 }}
+ Zabbix Graph +
{% endif %} diff --git a/templates/plain.template b/templates/plain.template index 129bfe6..5e18d51 100644 --- a/templates/plain.template +++ b/templates/plain.template @@ -3,10 +3,10 @@ EVENT INFORMATION #{{ EVENT_ID }} Severity: {{ EVENT_SEVERITY }} Description: {{ EVENT_NAME }} -{% if GRAPH_CID|length > 0 %} -GRAPH #{{ GRAPH_ID }} -{{ GRAPH_URL }} -This graph will remain available for approx. the next 2 weeks only; use HTML reader after this period to view the graph. +{% if GRAPH_CID1|length > 0 %} +GRAPH #{{ GRAPH_ID1 }} +{{ GRAPH_URL1 }} +This graph will remain available for approx. the next 2 weeks only; use HTML reader after this period to view the graph that is embedded in the attached HTML version. {% endif %} TRIGGER #{{ TRIGGER_ID }}