11 Commits
v1.10 ... v1.11

Author SHA1 Message Date
Mark Oudsen
6289a1cbdb Bug fixes (v1.11) 2021-02-28 21:31:46 +01:00
moudsen
3ec9004db9 Update README.md 2021-02-28 14:51:10 +01:00
Mark Oudsen
dcb523d1cb Example mail message picture 2021-02-27 23:26:01 +01:00
moudsen
f4d7adca16 Delete images directory 2021-02-27 23:24:14 +01:00
moudsen
617df995b4 Update README.md 2021-02-27 23:23:28 +01:00
moudsen
2d93ce0425 Update README.md 2021-02-27 23:22:02 +01:00
moudsen
06eebd02e1 Update README.md 2021-02-27 23:17:56 +01:00
moudsen
010bce74bc Update README.md 2021-02-27 23:16:05 +01:00
Mark Oudsen
931ce2ca9b Adding example mail message (v1.10) 2021-02-27 23:07:59 +01:00
Mark Oudsen
b2ebf778ac Fixed issue with duration - not passed in seconds but formatted already - v1.10 2021-02-27 22:44:23 +01:00
moudsen
e3cd9815d2 Update README.md 2021-02-27 22:39:21 +01:00
7 changed files with 54 additions and 22 deletions

View File

@@ -1,8 +1,23 @@
# mailGraph # mailGraph
Zabbix Media module and scripts for sending e-mail alerts with graphs. Zabbix Media module and scripts for sending e-mail alerts with graphs.
[![](images/Example-mail-message.png?raw=true)](images/Example-mail-message.png)
# WORK IN PROGRESS # WORK IN PROGRESS
Getting the code ready for further distribution. Although still under development (consider the current release "beta"), I need feedback and interaction with other users of Zabbix that are looking for the functionality I've developed hence I'm releasing my code to the world.
**List of item to-do**
1. Resolve macro information.
1. Passing dynamic parameters via the Webhook for template/output usage.
1. Finding "beta testers" to assist me in further enhancing the use cases.
# Zabbix enhancements
https://support.zabbix.com/browse/ZBXNEXT-6534
Main ticket asking how to get this Media type onboarded in the Media type section of the manual and the associated Zabbix GitHub directory.
https://support.zabbix.com/browse/ZBXNEXT-6535
One of the major items that needs to be tackled is to resolve Macros that are in names, descriptions, etc. As this does work for the Trigger and TriggerPrototype I've examined the Zabbix source code and from here I would say it's quite "easy" to add the "expandXXX" flags to other types as well (found many existing functions to perform this).
I'm adding this to my testlab setup for Item and ItemPrototype and will share the outcome. If it is succesfull I will also arrange for other types (like Host) eventually giving back the additional code lines to Zabbix for incorporation into a release.
# Installation pre-requisites # Installation pre-requisites
The suggested installation path of this script is on the same host where Zabbix lives but outside the actual Zabbix directory, although it is possible to run the script entirely somewhere else (the code is webhook based, picking up information from Zabbix is via the front-end login and API). The suggested installation path of this script is on the same host where Zabbix lives but outside the actual Zabbix directory, although it is possible to run the script entirely somewhere else (the code is webhook based, picking up information from Zabbix is via the front-end login and API).
@@ -17,8 +32,8 @@ The suggested installation path of this script is on the same host where Zabbix
- Create the directories "config", "images", "log", "templates" and "tmp" inside this directory - Create the directories "config", "images", "log", "templates" and "tmp" inside this directory
- Copy .htaccess to the main directory _(if not using Apache make sure your webserver denies access to /config!)_ - Copy .htaccess to the main directory _(if not using Apache make sure your webserver denies access to /config!)_
- Copy mailGraph.php to the main directory - Copy mailGraph.php to the main directory
- Install SwiftMailer: "composer require swiftmailer/swiftmailer" - Install SwiftMailer: `composer require swiftmailer/swiftmailer`
- Install TWIG: "composer require twig/twig" - Install TWIG: `composer require twig/twig`
- Copy config/config.php to your /config directory - Copy config/config.php to your /config directory
- Copy config/config.json.template to your /config/ directory and rename to "config.json" - Copy config/config.json.template to your /config/ directory and rename to "config.json"
- Copy templates/html.template and templates/plain.template to your templates directory - Copy templates/html.template and templates/plain.template to your templates directory

View File

@@ -6,6 +6,7 @@
"cli_eventValue": 0, "cli_eventValue": 0,
"cli_duration": 0, "cli_duration": 0,
"cli_recipient": "recipient@domain.com", "cli_recipient": "recipient@domain.com",
"cli_subject": "\"{{ HOST_NAME }}\": ({{ EVENT_SEVERITY }}) {{ EVENT_NAME }}"
"cli_baseURL": "https:\/\/domain.com\/zabbix\/", "cli_baseURL": "https:\/\/domain.com\/zabbix\/",
"zabbix_user": "alogicalusername", "zabbix_user": "alogicalusername",
"zabbix_pwd": "astrongpassword", "zabbix_pwd": "astrongpassword",

BIN
images/Example-mail-message.png Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 67 KiB

View File

@@ -11,6 +11,7 @@
// 1.00 2021/02/26 - Mark Oudsen - MVP version, ready for distribution // 1.00 2021/02/26 - Mark Oudsen - MVP version, ready for distribution
// 1.01 2021/02/27 - Mark Oudsen - Enhanced search for associated graphs to an item // bug fixes // 1.01 2021/02/27 - Mark Oudsen - Enhanced search for associated graphs to an item // bug fixes
// 1.10 2021/02/27 - Mark Oudsen - Moved all configuration outside code // 1.10 2021/02/27 - Mark Oudsen - Moved all configuration outside code
// 1.11 2021/02/28 - Mark Oudsen - Bug fixes
// ------------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------------
// //
// (C) M.J.Oudsen, mark.oudsen@puzzl.nl // (C) M.J.Oudsen, mark.oudsen@puzzl.nl
@@ -36,7 +37,7 @@
// CONSTANTS // CONSTANTS
$cVersion = 'v1.01'; $cVersion = 'v1.11';
$cCRLF = chr(10).chr(13); $cCRLF = chr(10).chr(13);
$maskDateTime = 'Y-m-d H:i:s'; $maskDateTime = 'Y-m-d H:i:s';
@@ -315,6 +316,7 @@
$problemData['recipient'] = $config['cli_recipient']; $problemData['recipient'] = $config['cli_recipient'];
$problemData['baseURL'] = $config['cli_baseURL']; $problemData['baseURL'] = $config['cli_baseURL'];
$problemData['duration'] = $config['cli_duration']; $problemData['duration'] = $config['cli_duration'];
$problemData['subject'] = $config['cli_subject'];
// Switch on CLI log display // Switch on CLI log display
$showLog = TRUE; $showLog = TRUE;
@@ -343,7 +345,7 @@
$p_URL = $problemData['baseURL']; $p_URL = $problemData['baseURL'];
$p_subject = '{{ EVENT_SEVERITY }}: {{ EVENT_NAME }}'; $p_subject = '{{ EVENT_SEVERITY }}: {{ EVENT_NAME }}';
if ((isset($problemData['subject'])) && ($problemData['subject']!='')) { $p_subject = $problemData['subject']; } if (isset($problemData['subject'])) { $p_subject = $problemData['subject']; }
$p_graphWidth = 450; $p_graphWidth = 450;
if (isset($problemData['graphWidth'])) { $p_graphWidth = intval($problemData['graphWidth']); } if (isset($problemData['graphWidth'])) { $p_graphWidth = intval($problemData['graphWidth']); }
@@ -423,7 +425,9 @@
///////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////
$mailData = array(); $mailData = array();
$mailData['baseURL'] = $p_URL;
$mailData['BASE_URL'] = $p_URL;
$mailData['SUBJECT'] = $p_subject;
// --- LOGIN --- // --- LOGIN ---
@@ -496,7 +500,7 @@
// --- GET HOST INFO --- // --- GET HOST INFO ---
_log('# Retrieve HOST information '); _log('# Retrieve HOST information');
$hostId = $thisItem['result'][0]['hostid']; $hostId = $thisItem['result'][0]['hostid'];
@@ -515,6 +519,18 @@
$mailData['HOST_ERROR'] = $thisHost['result'][0]['error']; $mailData['HOST_ERROR'] = $thisHost['result'][0]['error'];
$mailData['HOST_DESCRIPTION'] = $thisHost['result'][0]['description']; $mailData['HOST_DESCRIPTION'] = $thisHost['result'][0]['description'];
_log('# Retreive HOST macro information');
$request = array('jsonrpc'=>'2.0',
'method'=>'usermacro.get',
'params'=>array('hostids'=>$hostId,
'output'=>'extend'),
'auth'=>$token,
'id'=>nextRequestID());
$thisMacros = postJSON($z_url_api,$request);
_log('> Host macro data'.$cCRLF.json_encode($thisMacros,JSON_PRETTY_PRINT|JSON_NUMERIC_CHECK));
// --- GET GRAPHS ASSOCIATED WITH THIS ITEM --- // --- GET GRAPHS ASSOCIATED WITH THIS ITEM ---
_log('# Retrieve associated graphs to this item'); _log('# Retrieve associated graphs to this item');
@@ -652,7 +668,7 @@
$mailData['HOST_URL'] = $z_server.'/zabbix/hosts.php?form=update&hostid='.$mailData['HOST_ID']; $mailData['HOST_URL'] = $z_server.'/zabbix/hosts.php?form=update&hostid='.$mailData['HOST_ID'];
$mailData['EVENTDETAILS_URL'] = $z_server.'/tr_events.php?triggerid='.$mailData['TRIGGER_ID'].'&eventid='.$mailData['EVENT_ID']; $mailData['EVENTDETAILS_URL'] = $z_server.'/tr_events.php?triggerid='.$mailData['TRIGGER_ID'].'&eventid='.$mailData['EVENT_ID'];
$mailData['EVENT_DURATION'] = getNiceDuration($p_duration); $mailData['EVENT_DURATION'] = $p_duration;
///////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////
// Compose & Send Message /////////////////////////////////////////////////////////////////////////////// // Compose & Send Message ///////////////////////////////////////////////////////////////////////////////
@@ -685,7 +701,7 @@
$loader = new \Twig\Loader\ArrayLoader([ $loader = new \Twig\Loader\ArrayLoader([
'html' => file_get_contents($z_template_path.'html.template'), 'html' => file_get_contents($z_template_path.'html.template'),
'plain' => file_get_contents($z_template_path.'plain.template'), 'plain' => file_get_contents($z_template_path.'plain.template'),
'subject' => $p_subject, 'subject' => $mailData['SUBJECT'],
]); ]);
$twig = new \Twig\Environment($loader); $twig = new \Twig\Environment($loader);
@@ -703,7 +719,7 @@
// Prepare message // Prepare message
$message->setSubject($thisEvent['result'][0]['name']) $message->setSubject($mailSubject)
->setFrom($mailFrom) ->setFrom($mailFrom)
->setTo($p_recipient) ->setTo($p_recipient)
->setBody($bodyHTML, 'text/html') ->setBody($bodyHTML, 'text/html')
@@ -739,6 +755,6 @@
$content = implode(chr(10),$logging).$cCRLF.$cCRLF.'=== MAILDATA ==='.$cCRLF.$cCRLF.json_encode($mailData,JSON_PRETTY_PRINT|JSON_NUMERIC_CHECK); $content = implode(chr(10),$logging).$cCRLF.$cCRLF.'=== MAILDATA ==='.$cCRLF.$cCRLF.json_encode($mailData,JSON_PRETTY_PRINT|JSON_NUMERIC_CHECK);
$content = str_replace(chr(13),'',$content); $content = str_replace(chr(13),'',$content);
file_put_contents($z_log_path.'log.'.$p_eventId.'.dump',$content); file_put_contents($z_log_path.'log.'.$p_eventId.'.'.date('YmdHis').'.dump',$content);
} }
?> ?>

View File

@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<zabbix_export> <zabbix_export>
<version>5.0</version> <version>5.0</version>
<date>2021-02-27T16:34:16Z</date> <date>2021-02-28T20:27:03Z</date>
<media_types> <media_types>
<media_type> <media_type>
<name>MailGraph</name> <name>MailGraph</name>
@@ -9,7 +9,7 @@
<parameters> <parameters>
<parameter> <parameter>
<name>baseURL</name> <name>baseURL</name>
<value>https://zbx.puzzl.nl/zabbix/</value> <value>https://domain.com/zabbix/</value>
</parameter> </parameter>
<parameter> <parameter>
<name>duration</name> <name>duration</name>
@@ -49,7 +49,7 @@
</parameter> </parameter>
<parameter> <parameter>
<name>subject</name> <name>subject</name>
<value>{{ EVENT_SEVERITY }} --- {{ EVENT_NAME }}</value> <value>{{ HOST_NAME }}: ({{ EVENT_SEVERITY }}) {{ EVENT_NAME }}</value>
</parameter> </parameter>
<parameter> <parameter>
<name>triggerId</name> <name>triggerId</name>
@@ -57,7 +57,7 @@
</parameter> </parameter>
<parameter> <parameter>
<name>URL</name> <name>URL</name>
<value>https://zbx.puzzl.nl/mailGraph.php</value> <value>https://domain.com/mailGraph.php</value>
</parameter> </parameter>
</parameters> </parameters>
<script>try {&#13; <script>try {&#13;

View File

@@ -51,9 +51,9 @@
{% if EVENT_SEVERITY == "Resolved" %} {% if EVENT_SEVERITY == "Resolved" %}
Duration: <b>{{ EVENT_DURATION }}</b><br/> Duration: <b>{{ EVENT_DURATION }}</b><br/>
{% endif %} {% endif %}
Item: <b>{{ ITEM_NAME }}</b> ({{ ITEM_KEY }})<br/> Item: <b>{{ ITEM_NAME }}</b><br/>
Previous/Last: {{ ITEM_PREVIOUSVALUE }} ==&gt; {{ ITEM_LASTVALUE }} Previous/Last: {{ ITEM_PREVIOUSVALUE }} ==&gt; {{ ITEM_LASTVALUE }}
<p><a href="{{ EVENT_URL }}">Event Details</a><br/></p> <p><a href="{{ EVENTDETAILS_URL }}">Event Details</a><br/></p>
</td></tr> </td></tr>
</table> </table>
</td> </td>
@@ -65,10 +65,10 @@
{% endif %} {% endif %}
</table> </table>
<p style="font-size:10px"> <p style="font-size:10px">
Event ID: <a href="{{ EVENT_URL }}">{{ EVENT_ID }}</a> // Event ID: <a href="{{ EVENTDETAILS_URL }}">{{ EVENT_ID }}</a> //
Trigger ID: <a href="{{ TRIGGER_URL }}">{{ TRIGGER_ID }}</a> // Trigger ID: <a href="{{ TRIGGER_URL }}">{{ TRIGGER_ID }}</a> //
Item ID: <a href="{{ ITEM_URL }}">{{ ITEM_ID }}</a> // Item ID: <a href="{{ ITEM_URL }}">{{ ITEM_ID }}</a> //
Host ID: <a href="{{ HOST_URL }}">{{ HOST_ID }}</a> Host ID: <a href="{{ HOST_URL }}">{{ HOST_ID }}</a>
</p> </p>
</body> </body>
</html> </html>

View File

@@ -18,12 +18,12 @@ Description: {{ TRIGGER_DESCRIPTION }}
ITEM #{{ ITEM_ID }} ITEM #{{ ITEM_ID }}
Name: {{ ITEM_NAME }} Name: {{ ITEM_NAME }}
Key: {{ ITEM_KEY }}
Value: {{ ITEM_LASTVALUE }} Value: {{ ITEM_LASTVALUE }}
Previous: {{ ITEM_PREVIOUSVALUE }}
HOST HOST
Name: {{ HOST_NAME }} Name: {{ HOST_NAME }}
EVENT DETAILS EVENT DETAILS
{{ baseURL }}/tr_events.php?triggerid={TRIGGER_ID}&eventid={EVENT_ID} {{ EVENTDETAILS_URL }}