Although still under development (consider the current releases "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.
1. Finding "beta testers" to assist me in further enhancing the use cases => awaiting the first users/feedback!
2. Disassociate fetching the graph from the main routine; this is mainly driven by the fact that other Zabbix Media types face the same challenge for picking up graphs hence my work could also be of great use for including graphs in Telegraph, Slack, etc.
Main ticket asking how to get this Media type onboarded in the Media type section of the manual and the associated Zabbix GitHub directory. Although the request to get added has been kind of 'declined', I'm still pushing forward with my development as the original ask for such functionality is from **2010** (!).
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).
I've tested my code with Zabbix 5.0.x (LTS) on Linux with local Postfix. Not sure if it can/will run in any Zabbix versions under 5 or on other environments as I have no facilities nor time available to test on any lower versions.
- Two ways to configure the config.json file: 1) with config.php or 2) with your favorite text editor (note that you must have knowledge of JSON format to use this option)'
- List the available configuration options with "php config.php config.json list"
- Change any option with "php config.php config.json replace 'key_name' 'new_value'" (note the usage of the single quotes from the command-line!)
**"zabbix_user"** and **zabbix_user_pwd** must be a Zabbix SuperAdmin user/password you need to have/create to login to Zabbix (this is for grabbing the images via the regular Zabbix routines).
**"zabbix_user_api"** and **zabbix_user_pwd** must also be a Zabbix SuperAdmin user/password you need to have/create to login to the Zabbix API (this is for grabbing the information of the event via the Zabbix API).
**"mail_from"** must be a valid e-mail address which represents the 'from' address in the mails that are sent (ie. "zabbix.mailgraph.noreply@domain.com") that is acceptable by your mail server.
"baseURL" must contain your Zabbix URL (ie. "https://mydomain.com/zabbix/"). Note the ending '/'!
You can set your custom "graphWidth" and "GraphHeight" to your convenience.
You can switch the graph legend on/off with "showLegend" (0=off,1=on).
You can change the "subject" of the e-mail that is sent (note that the markup can be a combination of Zabbix MACRO or TWIG notation!).
"URL" is the url to the mailGraph script (ie. "https://mydomain.com/mailGraph.php").
# Actions configuration
At this point the Media type is ready for configuration under "actions" as per the regular way of Zabbix alert processing. Please refer to the manual how to configure.
There is no immediate relationship between a trigger and a graph. This is why the script uses the following technique to find graphs to are associated to the trigger:
1. The trigger API call returns a list of "functions". Each functions holds an "item id".
2. Via the graph API call we can figure out which graphs are associated to the "host id" and to any of the items we've found in the previous step.
3. Traversing this set of grahps, we are looking for graphs that have the actual "item id" associated. If there is no association found, we can still use any graph as it is still relevant to the trigger, but it will have an indirect relationship.
4. Most ideally we pick the graph that has the actual "item id" matched. If not, we pick the first grapgh we've managed to find.
In reality this may means that there are items (like simple "interface up/down" configured) that have no graphs defined. In this occasion there is no graph attached to the message. If you wish a graph to appear at this point, just add a graph using that item and next time this graph will show in the message or add the tag "mailGraph.graph" to the trigger pointing at a graph you would like to display for this trigger.
I've picked TWIG as the template processor, where the following macros are available for your convenience. Feel free to adjust the html.template and plain.template files as you see fit for your situation!
You can define _CUSTOM_ information that is passed from Zabbix by introducing a Macro of which the name starts with 'info'. Each 'infoXXX' will be made available for use in the template as {{ infoXXX }}. This allows you to add _any_ other information you want to pass from Zabbix straight into the template.
In general if something goes wrong (no output), use the following sequence to identify where the error has occured (and raise an issue in this repository so I can take a look at it):
- Goto Zabbix => Reports => Action Log and search for events with Status "Failed"
- Note the itemId, triggerId and eventId from this event for testing the Media Type manually
- If the popup message says "Syntax Error" something went wrong with the processing during the script. In this case you have to investigate a bit more what is happening.
The easiest way to test what is happening is to now goto Administration => Media types and hit the "Test" at the right hand side for MailGraph.
- Replace relevant macros with information (eventId, triggerId, itemId, recipient, baseUrl and URL) and hit "Test"