52 Commits
v1.28 ... v2.15

Author SHA1 Message Date
Mark Oudsen
624ce29a39 v2.15 - Fixed issues found in Zabbix 5.4.12 and refactored Javascript for better error handling 2023-08-16 14:18:08 +02:00
moudsen
16729e0720 v2.14 - Added special thank you section 2023-07-15 15:12:24 +02:00
Mark Oudsen
97dba8f197 v2.14 - Fixed missing flag for fetching web url related items 2023-07-15 00:51:55 +02:00
Mark Oudsen
288b0adaf7 v2.14 - Adding ability to set 'From' and 'Reply-to' address details in configuration - logic refactor on itemId (obsolete) 2023-07-10 23:59:33 +02:00
Mark Oudsen
2859d427f7 v2.14 - Adding ACK_URL for direct jump to Acknowledge edit windows of Zabbix 2023-07-10 14:34:55 +02:00
Mark Oudsen
0cd169a9d2 v2.14 - Adding ability to set 'From' and 'Reply-to' address details in configuration 2023-07-10 14:32:47 +02:00
Mark Oudsen
8269195795 v2.14 - Adding ability to set 'From' address details in configuration 2023-07-10 11:21:48 +02:00
moudsen
6da6da77fb v2.14 - Configuration updates
itemId no longer required/possibel per Zabbix 6.2. T
new items for SMTP from
keeping "mail_from" for backwards compatibility.
2023-07-10 11:02:20 +02:00
Mark Oudsen
00a67cbb65 v2.14 - Adding ability to set 'From' address details in configuration 2023-07-10 10:14:52 +02:00
Mark Oudsen
bf970029ea Merge branch 'main' of github.com:moudsen/mailGraph 2023-07-10 10:09:53 +02:00
Mark Oudsen
9f3bfb5c69 v2.14 - Adding ability to set 'From' address in configuration 2023-07-10 10:09:39 +02:00
moudsen
504422f452 v2.13 updates 2023-07-06 01:22:40 +02:00
Mark Oudsen
3ef16ae597 v2.13 - Bugfixes speciifally on links into Zabbix (missing context or info) 2023-07-03 21:54:27 +02:00
Mark Oudsen
a597ce6ab3 v2.13 - Bugfixes speciifally on links into Zabbix (missing context or info) 2023-07-03 21:50:14 +02:00
moudsen
a04050c3bc v2.12 updates 2023-07-02 15:00:47 +02:00
Mark Oudsen
b81414e06d v2.12 - Replaced SwiftMailer with PHPMailer (based on AutoTLS) 2023-07-02 14:54:00 +02:00
moudsen
accb8f40d6 v2.11.1 updates 2023-07-02 14:09:00 +02:00
moudsen
bf5bbcb778 v2.11.1 updates 2023-07-02 14:07:37 +02:00
moudsen
702725e5f7 v2.11.1 updates 2023-07-02 14:06:59 +02:00
moudsen
0725e01bc0 v2.11 updates
Amended README with context and background while cleaning up historical items.
2023-07-02 13:52:59 +02:00
moudsen
a8858784a5 v2.11 - Corrected XML for Zabbix 5.4 and up 2023-07-02 13:19:16 +02:00
moudsen
39ada5d6ff v2.11 - mailGraph.xml now with correct format 2023-07-02 12:36:17 +02:00
Mark Oudsen
32b8582757 v2.11 - Improved error handling and ability to pick random problem via Zabbix for testing 2023-07-01 13:26:29 +02:00
Mark Oudsen
9c42ee4b89 v2.10 - Corrected commit due to incorrect source copy from different host 2023-06-30 10:35:17 +02:00
Mark Oudsen
fa7fba040a Merge branch 'main' of github.com:moudsen/mailGraph 2023-06-30 10:19:40 +02:00
Mark Oudsen
3a4a7ab23e v2.10 - Correction of incorrectly copied source file from different host 2023-06-30 10:16:29 +02:00
moudsen
7075b40ad1 v2.10 - Code conversion typo detected (replaced & with &&) 2023-06-30 10:05:53 +02:00
Mark Oudsen
8bc476ed63 v2.10 - Removed deprecated function dependencies 2023-06-30 08:48:59 +02:00
moudsen
e80a2a56a3 Update README.md, June 29, 2023 2023-06-29 16:16:52 +02:00
moudsen
4b28d3f066 Announcing different javascript code for the Media Type for Zabbix 6.2+ 2022-09-26 12:27:00 +02:00
moudsen
93535d4006 Update README.md 2022-08-22 11:58:34 +02:00
moudsen
b9622da72f Update README.md 2022-03-16 23:05:50 +01:00
moudsen
56fb68ae9c Rework scope change to include Zabbix 6.0 LTS testing 2022-02-22 17:35:28 +01:00
Mark Oudsen
fa51c3e59e Merge branch 'main' of github.com:moudsen/mailGraph into main 2022-01-30 16:44:58 +01:00
Mark Oudsen
4ae62b3a1f v2.02 - Added cleanup option 2022-01-30 16:44:43 +01:00
moudsen
0731b87d44 v2.02 updates 2022-01-30 16:43:14 +01:00
moudsen
bdf4b05a6b Update README.md 2021-12-16 13:57:57 +01:00
moudsen
df16444665 Update README.md 2021-12-16 13:55:03 +01:00
Mark Oudsen
bccd134523 Merge branch 'main' of github.com:moudsen/mailGraph into main 2021-12-16 13:46:43 +01:00
Mark Oudsen
27c1d64511 v2.0 - Bugfixes and dealing with API changes in Zabbix 5.4 such itemId no longer passed by Zabbix itself 2021-12-16 13:46:18 +01:00
moudsen
8597658670 v1.31 updates 2021-10-06 13:54:11 +02:00
Mark Oudsen
b7e2062486 Merge branch 'main' of github.com:moudsen/mailGraph into main 2021-10-06 11:15:33 +02:00
Mark Oudsen
2955ffc404 (v1.31) Removal of trival errors and fix for 'json error' messages in Zabbix on clean install and default PHP settings 2021-10-06 11:15:17 +02:00
moudsen
2976a4af9c Fixed a bug when configuration file is empty 2021-07-17 23:45:50 +02:00
moudsen
2882367308 Minor bugfixes notification (fixed in current v1.29) 2021-07-07 19:54:46 +02:00
moudsen
8b4885038a Minor bugfix where HTTPProxy variable was not picked up correctly due to type 2021-07-07 19:39:02 +02:00
moudsen
f541719fbe Added example to show how Zabbix Media Type configuration looks like 2021-07-07 12:37:01 +02:00
Mark Oudsen
9edab245d8 Merge branch 'main' of github.com:moudsen/mailGraph into main 2021-07-05 22:47:37 +02:00
Mark Oudsen
341bd561ab Now shows mailGraph version when invoked from CLI (for testing) 2021-07-05 22:47:05 +02:00
moudsen
e2f657f3dd Updated to v1.29 2021-04-03 16:31:10 +02:00
Mark Oudsen
82a8fe4121 Merge branch 'main' of github.com:moudsen/mailGraph into main 2021-04-03 16:29:40 +02:00
Mark Oudsen
bee76b63c0 Bugfix - Stricter JSONRPC version check by Zabbix (v1.29) 2021-04-03 16:29:26 +02:00
10 changed files with 719 additions and 296 deletions

133
README.md
View File

@@ -1,16 +1,129 @@
## mailGraph (v1.28)
Zabbix Media module and scripts for sending e-mail alerts with graphs.
## Introduction ##
Over the years I've been using Zabbix as both personal and business solution for monitoring. One of the missing features in Zabbix itself was the ability to have graphs generated and associated to monitoring messages as well as an easy way to jump back into Zabbix when searching for monitoring information for specific events, problems or to have a direct link to the associated Zabbix item or host.
**Please use the Wiki for information on how to install, configure and use MailGraph in Zabbix.**
This initiated the development of mailGraph v1 in Zabbix 5.4 delivering an elementary solution for sending HTML enriched messages from Zabbix including graphs.
To facilitate in a very flexible way to setup and format messages including the graphs, Twig made its introduction towards mailGraph v2, the current release branch of mailGraph.
## UPGRADE NOTES
### v1.27
If you upgrade to v1.27 please be aware of the additional features for adding Tags to Trigger and Host to add additional graphs and the associated `html.template` updates that come alone with it (otherwise the new graphs will not show ...).
The below message is just an example of what MailGraph is capable of. The template engine in Twig allows for a fully customized message creation to your needs! It is also possible to add more Zabbix fields as any field is passed to Twig when accessible in Zabbix through the macro mechanism. Example: `{ITEM.ID}`.
### v1.25 and higher
Template data provisioning and code has fundamentally changed. If you upgrade from an earlier version as v1.25, make sure you understand the changes in templates/html.template (now making use of arrays for lists of items).
Note that all Zabbix host, item or screen related information is made available to Twig automatically.
## Example message
The below message is just an example of what MailGraph is capable of. The template engine used ("Twig") however allows for a fully customized message creation to your needs!
Example message:
[![](images/Example-mail-message-v122.png?raw=true)](images/Example-mail-message-v122.png)
mailGraph is capable of adding several series of graphs into a single message delivering a unique experience when and how groups of graph images per the requested periods of time are added.
Currently mailGraph supports hosts, (one or more related) items and screens (applicable to Zabbix 5.4 only).
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)_
_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
- Adding the ability to define FROM details for the emails generated by mailGraph
- `smtp_from_address` to set the from mail address (if not set uses mailing system default). Note: obsolete `mail_from` but retained for backwards compatibility.
- `smtp_from_name` to set the name that goes with the mail address (if not set uses "mailGraph").
- Adding the ability to define REPLY_TO details for the emails generated by mailGraph
- `smtp_reply_address` to set the reply-to mail address (if not set, no reply-to will be added to the message).
- `smtp_reply_name` to set the reply-to name of the mailbox (if not set defaults to "mailGraph feedback").
- Adding a new URL for use in the template:
- `ACK_URL` points to the function in Zabbix for editing and submitting an Acknowledge statement.
## mailGraph v2.13 release ##
_(2023/07/03)_
_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 references into Zabbix
- Trigger was missing "context" parameter
- Item was missing "context" parameter
- Problems reference to this host was not generated at all
## mailGraph v2.12 release ##
_(2023/07/02)_
_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
- Replaced Swiftmailer with PHPMailer
- Configuration option "smtp_transport" removed (using PHPMailer AutoTLS)
- Code cleanup (markup and obsolete related parts)
- Addressed any PHP message (deprecation or error) - mailGraph debug output is now "clean" in PHP 7.4.x and PHP 8.2.x
- Increased maximum number of graphs from 4 to 8
**Please consider to move to PHPMailer as soon as possible as Swiftmailer is abandoned. Installation instructions are included on the wiki.**
Principal approach: `composer require phpmailer/phpmailer`, install mailGraph v2.12 or higher.
In case Swiftmailer is no longer used elsewhere you can consider to remove this package with `swiftmailer/swiftmailer`.
## mailGraph v2.11 release ##
_(2023/07/01)_
_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
- Added pre- and postchecking of variables to the Zabbix javascript - this will prevent the 'invalid JSON' messages and provide better feedback for errors
- When testing MailGraph it is now possible to set the eventId to zero - a random problem will be picked up via the API
- Zabbix Media Type XML reverted back to version 5.4 (for backwards compatibility)
Modified files
- mailGraph.php
- mailGraph.xml
- javascript/zabbix.mailGraph.js
For those upgrading to the latest release without installing the media type:
- copy new mailGraph.php over existing mailGraph.php
- open the Media type MailGraph in Zabbix and edit the javascript
-- replace the script contents with the contents of javascript/zabbix.mailGraph.js
Changes are in effect immediately, no need to restart any services.
## mailGraph v2.10 release ##
_(2023/06/30)_
_This version has been verified with Zabbix 5.4, 6.0 LTS and 6.4, PHP 7.4 and 8.2 and recent versions of libraries used via composer._
Minor updates to the mailGraph code
- When not defining zabbix_api_user and zabbix_api_pwd in the configuration file the zbx_user and zbx_user_pwd wll be used
## Zabbix 6.4.x testing ##
_(2023/06/30)_
Zabbix 6.4 verification has succesfully completed.
- Refactored code to remove deprecated and removed functions since Zabbix 6.4.0
-- Zabbix Javascript now using HttpRequest instead of CurlHttpRequest (function name changes implemented)
-- Zabbix API user.login is now based on "username" (instead of "user")
Sidenotes
- Zabbix logging still shows deprecation messages however it is believed these are internal to Zabbix and not related to mailGraph
_(2023/06/29)_
Zabbix 6.4.x verification is in progress. Required intermediate release to fix one major issue (Zabbix login parameters deprecation) and some minor coding updates.
Expect to continue with automatic configuration within the next 2 months.
## Special thank you ##
I would like to express my gratitude to the following people that have actively contributed to bring bugs and improvements to my attention:
- [pqvindesland](https://github.com/pqvindesland)
## IMPORTANT NOTE for users of mailGraph v1 and Zabbix versions under 5.4 ##
As a result of a major functional change in Zabbix 5.4 (Screens no longer exist and are all moved into Dashboards) the mailGraph.screen macro no longer functions under Zabbix 5.4+. A code rewrite is in progress to deal with detecting the Zabbix version and to pick dashboard.get instead of screen.get as a source list for the graphs that should be included in the mail message.
**v1.x is no longer supported; please upgrade to the current v2 release**

View File

@@ -1,7 +1,5 @@
{
"script_baseurl": "https:\/\/mydomain.com\/",
"cli_itemId": 0,
"cli_triggerId": 0,
"cli_eventId": 0,
"cli_duration": 0,
"cli_recipient": "recipient@mydomain.com",
@@ -15,14 +13,19 @@
"zabbix_user_pwd": "astrongpassword",
"zabbix_api_user": "alogicalusername",
"zabbix_api_pwd": "astrongpassword",
"mail_from": "sender@mydomain.com",
"subject": "{{ HOST_NAME|raw }}: ({{ EVENT_SEVERITY }}) {{ EVENT_NAME|raw }}",
"smtp_server": "localhost",
"smtp_port": 25,
"smtp_transport": "none",
"smtp_from_address": "mailgraph@mydomain.com",
"smtp_from_name": "mailGraph",
"smtp_reply_address": "feedback@mydomain.com",
"smtp_from_name": "mailGraph response mailbox",
"smtp_strict": "yes",
"graph_match": "any",
"period": "20m",
"period_header": "Last 20 minutes",
"retention_images": 7,
"retention_logs": 14,
"debug": 0
}

View File

@@ -1,7 +1,5 @@
{
"script_baseurl": "https:\/\/mydomain.com\/",
"cli_itemId": 0,
"cli_triggerId": 0,
"cli_eventId": 0,
"cli_duration": 0,
"cli_recipient": "recipient@mydomain.com",
@@ -15,12 +13,15 @@
"zabbix_user_pwd": "astrongpassword",
"zabbix_api_user": "alogicalusername",
"zabbix_api_pwd": "astrongpassword",
"mail_from": "sender@mydomain.com",
"subject": "{{ HOST_NAME|raw }}: ({{ EVENT_SEVERITY }}) {{ EVENT_NAME|raw }}",
"smtp_server": "localhost",
"smtp_port": 25,
"smtp_transport": "none",
"smtp_strict": "yes",
"smtp_from_address": "mailgraph@mydomain.com",
"smtp_from_name": "mailGraph",
"smtp_reply_address": "feedback@mydomain.com",
"smtp_from_name": "mailGraph response mailbox",
"graph_match": "any",
"periods": "10m,4h,2d,7d",
"periods_headers": "Last 10 minutes,Last 4 hours,Last 2 days,Last 7 days",

View File

@@ -27,7 +27,7 @@
$content = file_get_contents($argv[1]);
$data = json_decode($content,TRUE);
if ($data==NULL)
if (($data==NULL) && (sizeof($content)>2))
{
echo 'Invalid JSON format in config file?!'.$cCRLF;
die;

Binary file not shown.

After

Width:  |  Height:  |  Size: 85 KiB

1
javascript/READ.ME Normal file
View File

@@ -0,0 +1 @@
Issue #32 has been resolved. The javascript code now works for Zabbix 5.4, 6.0 LTS and 6.4.

View File

@@ -1,7 +1,19 @@
// 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),
req = new CurlHttpRequest(),
req = new HttpRequest(),
fields = {},
resp = '',
result = { tags: {} };
@@ -13,14 +25,18 @@ try {
}
// Declare output type
req.AddHeader('Content-Type: application/json');
req.addHeader('Content-Type: application/json');
// Must have fields
fields.itemId = params.itemId;
fields.eventId = params.eventId;
// 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;
fields.duration = params.duration * 1;
if (fields.recipient.charAt(0) == '{') {
throw '[MailGraph Webhook] Please define recipient for the test message!';
}
// Optional fields
if (typeof params.graphWidth === 'string') { fields.graphWidth = params.graphWidth; }
@@ -40,22 +56,44 @@ try {
// Post information to the processing script
Zabbix.Log(4, '[MailGraph Webhook] Sending request: ' + params.URL + '?' + JSON.stringify(fields));
var resp = req.Post(params.URL,JSON.stringify(fields));
Zabbix.Log(4, '[Mailgraph Webhook] Receiving response:' + resp);
var resp = req.post(params.URL,JSON.stringify(fields));
Zabbix.Log(4, '[Mailgraph Webhook] Received response:' + resp);
// If there was an error, report it
if (req.Status() != 200) { throw JSON.parse(resp).errors[0]; }
// 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)
// We expect the message id back from the processing script
resp = JSON.parse(resp);
result.tags.__message_id = resp.messageId;
if (req.getStatus() != 200) {
throw '[MailGraph Webhook] Processing of mailGraph.php failed: ' + resp;
}
if (resp==null) {
throw '[MailGraph Webhook] No response received from mailGraph.php? This should not occur (check URL and your webserver!)';
}
if (resp=='') {
throw '[MailGraph Webhook] No data received from mailGraph - please check recipient address or mailGraph log and retry.';
}
// 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;
Zabbix.Log(4, '[MailGraph Webhook] Message sent with identification "' + msg.messageId + '"');
// Pass the result back to Zabbix
return JSON.stringify(result);
}
catch (error)
{
// In case something went wrong in the processing, pass the error back to Zabbix
Zabbix.Log(127, 'MailGraph notification failed : '+error);
// 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

@@ -8,6 +8,8 @@
// upon an alert message.
//
// ------------------------------------------------------------------------------------------------------
// Release 1 tested with Zabbix 5.4 and 6.0 LTS
// ------------------------------------------------------------------------------------------------------
// 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.10 2021/02/27 - Mark Oudsen - Moved all configuration outside code
@@ -36,6 +38,28 @@
// 1.27 2021/03/19 - Mark Oudsen - Added ability to define "mailGraph.screen" tag to embed graphs from
// Added PHP informational and warnings to log for easier debug/spotting
// 1.28 2021/03/24 - Mark Oudsen - Added ability to specify username/password for TLS/SSL
// 1.29 2021/04/03 - Mark Oudsen - Bugfix due to stricter JSONRPC version check since Zabbix 5.0.10
// 2021/07/05 - Mark Oudsen - Minor detail added: CLI debug mode now also outputs version
// 2021/07/07 - Mark Oudsen - Fixed HTTPProxy typo in code (instead of HTTPPRoxy)
// ------------------------------------------------------------------------------------------------------
// Release 2 tested with Zabbix 5.4, 6.0 LTS and 6.4 - tested with latest Composer libraries
// ------------------------------------------------------------------------------------------------------
// 2.00 2021/12/16 - Mark Oudsen - itemId not always provisioned by Zabbix
// Several fixes on warning - several small bug fixes
// 2.01 2021/12/16 - Mark Oudsen - Screens are no longer available - reverting to using Dashboards now
// 2.02 2022/01/30 - Mark Oudsen - Added cleanup routine for old logs and images
// 2.10 2023/06/30 - Mark Oudsen - Refactored deprecated code - now compatible with Zabbix 6.0 LTS, 6.4
// 2.11 2023/07/01 - Mark Oudsen - Refactored Zabbix javascript - now capturing obvious errors
// Added ability to locate latest problems for testing purposes
// 2.12 2023/07/02 - Mark Oudsen - Replaced SwiftMailer with PHPMailer (based on AutoTLS)
// 2.13 2023/07/03 - Mark Oudsen - Bugfixes speciifally on links into Zabbix (missing context or info)
// 2.14 2023/07/10 - Mark Oudsen - Adding ability to set 'From' and 'ReplyTo' addresses in configuration
// Adding ACK_URL for utilization in the template to point to Ack page
// 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
@@ -44,6 +68,19 @@
/////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Roadmap
// -------
// - Automatic setup and configuration of mailGraph
// - Automatic code updates (CLI triggered)
// - Add DASHBOARD facility (SCREEN was abandoned in Zabbix 5.4
// - Extract Graph API functionality to seperate code unit/object
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////
//
@@ -57,27 +94,35 @@
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// CONSTANTS
$cVersion = 'v1.28';
$cVersion = 'v2.15';
$cCRLF = chr(10).chr(13);
$maskDateTime = 'Y-m-d H:i:s';
$maxGraphs = 4;
$maxGraphs = 8;
// DEBUG SETTINGS
// -- Should be FALSE for production level use
$cDebug = TRUE; // Extended debug logging mode
$cDebug = TRUE; // Extended debug logging mode, switch to FALSE for production environment
$cDebugMail = FALSE; // If TRUE, includes log in the mail message (html and plain text attachments)
$showLog = FALSE; // Display the log - !!! only use in combination with CLI mode !!!
// Limit server level output errors
error_reporting(E_ERROR | E_PARSE);
// INCLUDE REQUIRED LIBRARIES (Composer)
// (configure at same location as the script is running or load in your own central library)
// -- phpmailer/phpmailer https://github.com/PHPMailer/PHPMailer
// -- swiftmailer/swiftmailer https://swiftmailer.symfony.com/docs/introduction.html
// -- twig/twig https://twig.symfony.com/doc/3.x/templates.html
// Change only required if you decide to use a local/central library, otherwise leave as is
include(getcwd().'/vendor/autoload.php');
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
/////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// Fetch the HTML source of the given URL
@@ -99,23 +144,24 @@
$ch = curl_init();
// Set options
curl_setopt($ch, CURLOPT_USERAGENT, 'Zabbix-mailGraph - '.$cVersion);
if ((isset($HTTPProxy)) && ($HTTPProxy!=''))
{
_log('% Using proxy: '.$HTTPProxy);
if ($cDebug) { _log('% Using proxy: '.$HTTPProxy); }
curl_setopt($ch, CURLOPT_PROXY, $HTTPProxy);
}
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS,
json_encode($data,JSON_PRETTY_PRINT|JSON_NUMERIC_CHECK));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, 'Zabbix-mailGraph - '.$cVersion);
// Execute Curl
$data = curl_exec($ch);
@@ -124,9 +170,10 @@
{
_log('! Failed: '.curl_error($ch));
$data = 'An error occurred while retreiving the requested page.'.$cCRLF;
$data .= 'Requested page = '.$url.$cCRLF;
$data .= 'Error = '.curl_error($ch).$cCRLF;
$data = array();
$data[] = 'An error occurred while retreiving the requested page.';
$data[] .= 'Requested page = '.$url;
$data[] .= 'Error = '.curl_error($ch);
}
else
{
@@ -238,8 +285,12 @@
curl_close($ch);
// Delete cookie
unlink($filename_cookie);
// Delete cookie (if exists)
if (file_exists($filename_cookie))
{
unlink($filename_cookie);
_log('- Removed cookie '.$filename_cookie);
}
// Write file
$fp = fopen($image_name, 'w');
@@ -377,6 +428,40 @@
return($info);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// Cleanup 'path': remove files with 'ext' older than 'daysOld'
function cleanupDir($path, $ext, $daysOld)
{
_log('- Scanning "'.$path.'"');
$files = glob($path.'/*'.$ext);
$now = time();
$filesRemoved = 0;
$filesKept = 0;
foreach ($files as $file)
{
if (is_file($file))
{
if ($now - filemtime($file) >= (60 * 60 * 24 * $daysOld))
{
_log('> Removing "'.$file.'"');
unlink($file);
$filesRemoved++;
}
else
{
$filesKept++;
}
}
}
_log(': Done. Cleaned up '.$filesRemoved.' file(s), kept '.$filesKept.' file(s)');
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// Initialize ///////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -404,18 +489,25 @@
{
if (($argc>1) && ($argv[1]=='test'))
{
// Switch on CLI log display
$showLog = TRUE;
_log('<<< mailGraph '.$cVersion.' >>>');
_log('# Invoked from CLI');
// Assumes that config.json file has the correct information
// Assumes that config.json file has the correct information for MANDATORY information
// DEFAULTS
$problemData['eventId'] = 0;
$problemData['duration'] = 0;
// MANDATORY
$problemData['itemId'] = $config['cli_itemId'];
$problemData['eventId'] = $config['cli_eventId'];
$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']; }
@@ -424,8 +516,34 @@
if (isset($config['cli_debug'])) { $problemData['debug'] = $config['cli_debug']; }
if (isset($config['cli_proxy'])) { $problemData['HTTPProxy'] = $config['cli_proxy']; }
// BACKWARDS COMPATIBILITY - obsolete from Zabbix 6.2 onwards
$problemData['itemId'] = 0;
if (isset($config['cli_itemId'])) { $problemData['itemId'] = $config['cli_itemId']; }
}
if (($argc>1) && ($argv[1]=='cleanup'))
{
// Switch on CLI log display
$showLog = TRUE;
// Check for configuration of retention period for images and logs
_log('<<< mailGraph '.$cVersion.' >>>');
_log('# Invoked from CLI');
// Set defaults
$retImages = 30;
$retLogs = 14;
// Check if configured settings
if (isset($config['retention_images'])) { $retImages = intval($config['retention_images']); }
if (isset($config['retention_logs'])) { $retLogs = intval($config['retention_logs']); }
_log('Cleaning up IMAGES ('.$retImages.' days) and LOGS ('.$retLogs.' days)');
cleanupDir(getcwd().'/images', '.png', $retImages);
cleanupDir(getcwd().'/log', '.dump', $retLogs);
exit(0);
}
}
@@ -433,10 +551,8 @@
$cCRLF.json_encode($problemData,JSON_PRETTY_PRINT|JSON_NUMERIC_CHECK));
// --- CHECK AND SET P_ VARIABLES ---
// FROM POST OR CLI DATA
if (!isset($problemData['itemId'])) { echo "Missing ITEM ID?\n"; die; }
$p_itemId = intval($problemData['itemId']);
// FROM POST OR CLI DATA
if (!isset($problemData['eventId'])) { echo "Missing EVENT ID?\n"; die; }
$p_eventId = intval($problemData['eventId']);
@@ -444,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'];
@@ -465,7 +581,7 @@
$p_period = '48h';
if (isset($problemData['period'])) { $p_period = $problemData['period']; }
if (isset($problemData['HTTPProxy'])) { $HTTPProxy = $problemData['HTTPPRoxy']; }
if (isset($problemData['HTTPProxy'])) { $HTTPProxy = $problemData['HTTPProxy']; }
// DYNAMIC VARIABLES FROM ZABBIX
@@ -482,10 +598,6 @@
$p_smtp_port = 25;
if (isset($config['smtp_port'])) { $p_smtp_port = $config['smtp_port']; }
$p_smtp_transport = 'none';
if ((isset($config['smtp_transport'])) && ($config['smtp_transport']=='tls')) { $p_smtp_transport = 'tls'; }
if ((isset($config['smtp_transport'])) && ($config['smtp_transport']=='ssl')) { $p_smtp_transport = 'ssl'; }
$p_smtp_strict = 'yes';
if ((isset($config['smtp_strict'])) && ($config['smtp_strict']=='no')) { $p_smtp_strict = 'no'; }
@@ -495,6 +607,27 @@
$p_smtp_password = '';
if (isset($config['smtp_password'])) { $p_smtp_password = $config['smtp_password']; }
$p_smtp_from_address = '';
if (isset($config['smtp_from_address'])) { $p_smtp_from_address = $config['smtp_from_address']; }
$p_smtp_from_name = 'mailGraph';
if (isset($config['smtp_from_name'])) { $p_smtp_from_name = $config['smtp_from_name']; }
$p_smtp_reply_address = '';
if (isset($config['smtp_reply_address'])) { $p_smtp_reply_address = $config['smtp_reply_address']; }
$p_smtp_reply_name = 'mailGraph feedback';
if (isset($config['smtp_reply_name'])) { $p_smtp_reply_name = $config['smtp_reply_name']; }
// >>> Backwards compatibility but smtp_from_address is leading (<v2.14)
$mailFrom = '';
if (isset($config['mail_from'])) { $mailFrom = $config['mail_from']; }
if (($p_smtp_from_address=='') && ($mailFrom!=''))
{
$p_smtp_from_address = $mailFrom;
}
$p_graph_match = 'any';
if ((isset($config['graph_match'])) && ($config['graph_match']=='exact')) { $p_graph_match = 'exact'; }
@@ -511,17 +644,38 @@
$z_tmp_cookies = $z_path.'tmp/';
$z_log_path = $z_path.'log/';
// If tmp or log does not exist, create them
if (!is_dir($z_tmp_cookies))
{
mkdir($z_tmp_cookies);
_log('+ created TMP directory "'.$z_tmp_cookies.'"');
}
if (!is_dir($z_log_path))
{
mkdir($z_log_path);
_log('+ created LOG directory "'.$z_log_path.'"');
}
// Zabbix user (requires Super Admin access rights to access image generator script)
$z_user = $config['zabbix_user'];
$z_pass = $config['zabbix_user_pwd'];
// Zabbix API user (requires Super Admin access rights)
// --- Copy from Zabbix user and override when defined in configuration
// TODO: Check if information retreival can be done with less rigths
$z_api_user = $config['zabbix_api_user'];
$z_api_pass = $config['zabbix_api_pwd'];
$z_api_user = $z_user;
$z_api_pass = $z_pass;
// Mail sender
$mailFrom = array($config['mail_from']=>'Zabbix Mailgraph');
if (isset($config['zabbix_api_user']))
{
$z_api_user = $config['zabbix_api_user'];
}
if (isset($config['zabbix_api_pwd']))
{
$z_api_pass = $config['zabbix_api_pwd'];
}
// Derived variables - do not change!
$z_server = $p_URL; // Zabbix server URL from config
@@ -554,7 +708,7 @@
$request = array('jsonrpc'=>'2.0',
'method'=>'user.login',
'params'=>array('user'=>$z_api_user,
'params'=>array('username'=>$z_api_user,
'password'=>$z_api_pass),
'id'=>nextRequestID(),
'auth'=>null);
@@ -564,10 +718,78 @@
$token = '';
if (isset($result['result'])) { $token = $result['result']; }
if ($token=='') { echo 'Error logging in to Zabbix? ('.$z_url_api.')'; die; }
if ($token=='')
{
echo 'Error logging in to Zabbix? ('.$z_url_api.'): '.$cCRLF;
echo var_dump($request).$cCRLF;
echo var_dump($result).$cCRLF;
die;
}
_log('> Token = '.$token);
// -----------------------
// --- LOG API VERSION ---
// -----------------------
_log('# Record Zabbix API version');
$request = array('jsonrpc'=>'2.0',
'method'=>'apiinfo.version',
'params'=>[],
'id'=>nextRequestID());
$result = postJSON($z_url_api, $request);
$apiVersion = $result['result'];
_log('> API version '.$apiVersion);
// -----------------------------------
// --- IF NO EVENT ID FETCH LATEST ---
// -----------------------------------
if ($p_eventId=="0")
{
_log('# No event ID given, picking up random event from Zabbix');
$request = array('jsonrpc'=>'2.0',
'method'=>'problem.get',
'params'=>array('output'=>'extend',
'recent'=>'true',
'limit'=>1),
'auth'=>$token,
'id'=>nextRequestID());
$thisProblems = postJSON($z_url_api, $request);
_log('> Problem data (recent)'.$cCRLF.json_encode($thisProblems,JSON_PRETTY_PRINT|JSON_NUMERIC_CHECK));
if (!isset($thisProblems['result'][0]))
{
_log('- No response data received. Retrying with less recent problems ... ');
$request = array('jsonrpc'=>'2.0',
'method'=>'problem.get',
'params'=>array('output'=>'extend',
'recent'=>'false',
'limit'=>1),
'auth'=>$token,
'id'=>nextRequestID());
$thisProblems = postJSON($z_url_api, $request);
_log('> Problem data (not recent)'.$cCRLF.json_encode($thisProblems,JSON_PRETTY_PRINT|JSON_NUMERIC_CHECK));
if (!isset($thisProblems['result'][0]))
{
_log('! Cannot continue: mailGraph is unable to pick a random event via the Zabbix API. It is highly likely that no active problems exist? Please retry or determine and set an event ID manually and retry.');
die;
}
}
$p_eventId = $thisProblems['result'][0]['eventid'];
_log('> Picked up random last event #'.$p_eventId);
}
// ------------------------------
// --- READ EVENT INFORMATION ---
// ------------------------------
@@ -621,10 +843,10 @@
'params'=>array('triggerids'=>$p_triggerId,
'output'=>'extend',
'selectFunctions'=>'extend',
'selectTags'=>'extend'),
'expandComment'=>1,
'expandDescription'=>1,
'expandExpression'=>1,
'selectTags'=>'extend',
'expandComment'=>1,
'expandDescription'=>1,
'expandExpression'=>1),
'auth'=>$token,
'id'=>nextRequestID());
@@ -710,6 +932,17 @@
}
}
// If no specific itemId is requested take the first item found on the items list from the host
if (!isset($p_itemId))
{
foreach($thisTrigger['result'][0]['functions'] as $aFunction)
{
$p_itemId = $aFunction['itemid'];
_log('- Item ID taken from trigger (first) function = '.$p_itemId);
break;
}
}
// ---------------------
// --- GET ITEM INFO ---
// ---------------------
@@ -719,7 +952,8 @@
$request = array('jsonrpc'=>'2.0',
'method'=>'item.get',
'params'=>array('itemids'=>$p_itemId,
'output'=>'extend'),
'webitems'=>'true',
'output'=>'extend'),
'auth'=>$token,
'id'=>nextRequestID());
@@ -766,7 +1000,7 @@
$mailData['HOST_ID'] = $thisHost['result'][0]['hostid'];
$mailData['HOST_NAME'] = $thisHost['result'][0]['name'];
$mailData['HOST_ERROR'] = $thisHost['result'][0]['error'];
if (isset($thisHost['result'][0]['error'])) { $mailData['HOST_ERROR'] = $thisHost['result'][0]['error']; }
$mailData['HOST_DESCRIPTION'] = $thisHost['result'][0]['description'];
// --- Custom settings?
@@ -1011,6 +1245,7 @@
_log('> Graphs found = '.sizeof($triggerGraphs));
}
$hostGraphs = array();
if ($hostScreen>0)
{
@@ -1062,7 +1297,7 @@
}
}
// Strip off any excessive elements from the end
// Strip off any excessive elements from the end (protection of graph generation overload on system)
while (sizeof($p_periods)>$maxGraphs) { array_pop($p_periods); }
while (sizeof($p_periods_headers)>$maxGraphs) { array_pop($p_periods_headers); }
@@ -1114,6 +1349,7 @@
$graphFiles[] = $graphFile;
$mailData['GRAPHS_I'][$aKey]['PATH'] = $z_images_path . $graphFile;
$mailData['GRAPHS_I'][$aKey]['CID'] = 'images/'.$graphFile;
$mailData['GRAPHS_I'][$aKey]['URL'] = $z_url_image . $graphFile;
$mailData['GRAPHS_I'][$aKey]['HEADER'] = $p_periods_headers[$aKey];
}
@@ -1144,6 +1380,7 @@
$mailData['GRAPHS_'.$varName][$aKey]['URL'] = $z_url_image . $graphFile;
$mailData['GRAPHS_'.$varName][$aKey]['PATH'] = $z_images_path . $graphFile;
$mailData['GRAPHS_'.$varName][$aKey]['CID'] = 'images/'.$graphFile;
}
$mailData['GRAPHS_'.$varName.'_LINK'] = $z_server.'screens.php?elementid='.$info[0]['screen']['screenid'];
@@ -1190,7 +1427,10 @@
$mailData['LOG_HTML'] = str_replace($cCRLF,'<br/>',$mailData['LOG_HTML']);
$mailData['LOG_HTML'] = str_replace('<br/>','<br/>'.$cCRLF,$mailData['LOG_HTML']);
$mailData['LOG_HTML'] = '<html lang="en"><head><meta http-equiv=Content-Type content="text/html; charset=UTF-8">'.$cCRLF.
$mailData['LOG_HTML'] = '<html lang="en"><head><meta http-equiv=Content-Type content="text/html; charset=UTF-8"></head>'.$cCRLF.
'<style type="text/css">'.$cCRLF.
'body { font-family: courier, courier new, serif; font-size: 12px; }'.$cCRLF.
'</style>'.$cCRLF.
'<body>'.$cCRLF.
$mailData['LOG_HTML'].$cCRLF.
'</body>'.$cCRLF.
@@ -1202,141 +1442,139 @@
// Prepare others
$mailData['TRIGGER_URL'] = $z_server.'triggers.php?form=update&triggerid='.$mailData['TRIGGER_ID'];
$mailData['ITEM_URL'] = $z_server.'items.php?form=update&hostid='.$mailData['HOST_ID'].'&itemid='.$mailData['ITEM_ID'];
$mailData['TRIGGER_URL'] = $z_server.'triggers.php?form=update&triggerid='.$mailData['TRIGGER_ID'].'&context=host';
$mailData['ITEM_URL'] = $z_server.'items.php?form=update&hostid='.$mailData['HOST_ID'].'&itemid='.$mailData['ITEM_ID'].'&context=host';
$mailData['HOST_URL'] = $z_server.'hosts.php?form=update&hostid='.$mailData['HOST_ID'];
$mailData['ACK_URL'] = $z_server.'zabbix.php?action=popup&popup_action=acknowledge.edit&eventids[]='.$mailData['EVENT_ID'];
$mailData['EVENTDETAILS_URL'] = $z_server.'tr_events.php?triggerid='.$mailData['TRIGGER_ID'].'&eventid='.$mailData['EVENT_ID'];
$mailData['EVENT_DURATION'] = $p_duration;
$mailData['HOST_PROBLEMS_URL'] = $z_server.'zabbix.php?show=1&name=&inventory%5B0%5D%5Bfield%5D=type&inventory%5B0%5D%5Bvalue%5D=&evaltype=0&tags%5B0%5D%5Btag%5D=&tags%5B0%5D%5Boperator%5D=0&tags%5B0%5D%5Bvalue%5D=&show_tags=3&tag_name_format=0&tag_priority=&show_opdata=0&show_timeline=1&filter_name=&filter_show_counter=0&filter_custom_time=0&sort=clock&sortorder=DESC&age_state=0&show_suppressed=0&unacknowledged=0&compact_view=0&details=0&highlight_row=0&action=problem.view&hostids%5B%5D='.$mailData['HOST_ID'];
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// Compose & Send Message ///////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////
_log('# Setting up mailer');
_log('# Configuring Mailer');
// Do we need TLS or SSL?
$mail = new PHPMailer(true);
if (($p_smtp_transport=='tls') || ($p_smtp_transport=='ssl'))
try
{
$transport = (new Swift_SmtpTransport($p_smtp_server, $p_smtp_port, $p_smtp_transport));
$mail->SMTPDebug = 0;
$mail->isSMTP();
$mail->Host = $p_smtp_server;
$mail->Port = $p_smtp_port;
// --- Authentication required?
if ($p_smtp_username!="")
{
$mail->SMTPAuth = true;
$mail->Username = $p_smtp_username;
$mail->Password = $p_smtp_password;
}
// --- Disable strict certificate checking?
if ($p_smtp_strict=='no')
{
if ($transport instanceof \Swift_Transport_EsmtpTransport)
$mail->SMTPOptions = [
'ssl' => [ 'verify_peer' => false, 'verify_peer_name' => false, 'allow_self_signed' => true ]
];
}
// --- Define from
$mail->Sender = $p_smtp_from_address;
$mail->SetFrom($p_smtp_from_address, $p_smtp_from_name, FALSE);
// --- Define reply-to
if ($p_smtp_reply_address!='')
{
$mail->clearReplyTos();
$mail->addReplyTo($p_smtp_reply_address, $p_smtp_reply_name);
}
// --- Add recipient
$mail->addAddress($p_recipient);
// --- Prepare embedding of the graphs by attaching and generating "cid" (content-id) information
function embedGraphs($graphs,$varName,$type)
{
global $mail;
global $mailData;
foreach($graphs as $aKey=>$anItem)
{
$transport->setStreamOptions([
'ssl' => ['allow_self_signed' => true,
'verify_peer' => false,
'verify_peer_name' => false]
]);
$mail->AddEmbeddedImage($mailData['GRAPHS_'.$varName][$aKey]['PATH'],
$mailData['GRAPHS_'.$varName][$aKey]['CID']);
// Add content-id marker to the identifier for ease of use in Twig
$mailData['GRAPHS_'.$varName][$aKey]['CID'] = 'cid:'.$mailData['GRAPHS_'.$varName][$aKey]['CID'];
_log('> Embedded graph image ('.$type.') '.$mailData['GRAPHS_'.$varName][$aKey]['URL']);
}
}
else
embedGraphs($graphFiles,'I','ITEM');
embedGraphs($triggerGraphs,'T','TRIGGER');
embedGraphs($hostGraphs,'H','HOST');
// --- Render the content with TWIG for HTML, Plain text and the Subject of the message
$loader = new \Twig\Loader\ArrayLoader([
'html' => file_get_contents($z_template_path.'html.template'),
'plain' => file_get_contents($z_template_path.'plain.template'),
'subject' => $mailData['SUBJECT'],
]);
$twig = new \Twig\Environment($loader);
$bodyHTML = $twig->render('html', $mailData);
$bodyPlain = $twig->render('plain', $mailData);
$mailSubject = $twig->render('subject', $mailData);
// --- Attach debug log processing?
if (($cDebugMail) || (isset($problemData['debug'])))
{
if ($transport instanceof \Swift_Transport_EsmtpTransport)
{
$transport->setStreamOptions([
'ssl' => ['allow_self_signed' => false,
'verify_peer' => true,
'verify_peer_name' => true]
]);
}
_log('# Attaching logs to mail message');
$mail->addStringAttachment($mailData['LOG_HTML'],'log.html');
$mail->addStringAttachment($mailData['LOG_PLAIN'],'log.txt');
}
}
else
{
$transport = (new Swift_SmtpTransport($p_smtp_server, $p_smtp_port));
}
// Username/password?
// ---Fill body and subject and mark as HTML while also supplying plain text option alternative
// Note: Not using PHPMailer option for automatic text/plain generation (by design)
$mail->Body = $bodyHTML;
$mail->isHTML(true);
$mail->AltBody = $bodyPlain;
$mail->Subject = $mailSubject;
if ($p_smtp_username!='') { $transport->setUsername($p_smtp_username); }
if ($p_smtp_password!='') { $transport->setPassword($p_smtp_password); }
// Start actual mail(er)
$mailer = new Swift_Mailer($transport);
$message = (new Swift_Message());
// --- Fetch mailer ID from this message (no Swift function available for it ...)
// --- "Message-ID: <...id...@swift.generated>"
$content = $message->toString();
$lines = explode(chr(10),$content);
$firstLine = $lines[0];
$idParts = explode(' ',$firstLine);
$messageId = substr($idParts[1],1,-2);
_log('# Message ID = '.$messageId);
// Build parts for HTML and PLAIN
_log('# Processing templates');
_log('+ '.$z_template_path.'html.template');
_log('+ '.$z_template_path.'plain.template');
$loader = new \Twig\Loader\ArrayLoader([
'html' => file_get_contents($z_template_path.'html.template'),
'plain' => file_get_contents($z_template_path.'plain.template'),
'subject' => $mailData['SUBJECT'],
]);
$twig = new \Twig\Environment($loader);
// --- Embed the images
function embedGraphs($graphs,$varName,$type)
{
global $message;
global $mailData;
foreach($graphs as $aKey=>$anItem)
// --- Send the message
if (!$mail->send())
{
$mailData['GRAPHS_'.$varName][$aKey]['CID'] = $message->embed(Swift_Image::fromPath($mailData['GRAPHS_'.$varName][$aKey]['PATH']));
_log('> Embedded graph image ('.$type.') '.$mailData['GRAPHS_'.$varName][$aKey]['PATH']);
_log("! Failed to send mail message");
echo "! Failed to send mail message. Likely an issue with the recipient email address?".$cCRLF;
echo "+ Mailer error: ".$mail->ErrorInfo.$cCRLF;
}
}
embedGraphs($graphFiles,'I','ITEM');
embedGraphs($triggerGraphs,'T','TRIGGER');
embedGraphs($hostGraphs,'H','HOST');
// --- Obtain message ID
$messageId = $mail->getlastMessageID();
_log('# Message ID = '.$messageId);
// --- Render the content
$bodyHTML = $twig->render('html', $mailData);
$bodyPlain = $twig->render('plain', $mailData);
$mailSubject = $twig->render('subject', $mailData);
// Prepare message
$message->setSubject($mailSubject)
->setFrom($mailFrom)
->setTo($p_recipient)
->setBody($bodyHTML, 'text/html')
->addPart($bodyPlain, 'text/plain');
if (($cDebugMail) || (isset($problemData['debug'])))
// --- Return Event TAG information for Zabbix to store with Zabbix event
$response = array('messageId'=>$messageId);
echo json_encode($response).$cCRLF;
} catch (phpmailerException $e)
{
_log('# Attaching logs to mail message');
$attachLogHTML = new Swift_Attachment($mailData['LOG_HTML'], 'log.html', 'text/html');
$message->attach($attachLogHTML);
$attachLogPlain = new Swift_Attachment($mailData['LOG_PLAIN'], 'log.txt', '/text/plain');
$message->attach($attachLogPlain);
echo "! Failed to send message".$cCRLF;
echo "! phpMailer error message: ".$e->getMessage().$cCRLF;
_log("! phpMailer failed: ".$e->getMessage());
} catch (Exception $e)
{
echo "! Failed to send message".$cCRLF;
echo "! Error message: ".$e->getMessage().$cCRLF;
_log("! Failed: ".$e->getMessage());
}
// Send message
$result = $mailer->send($message);
// Return Event TAG information for Zabbix
$response = array('messageId.mailGraph'=>$messageId);
echo json_encode($response).$cCRLF;
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// Wrap up //////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -1345,15 +1583,17 @@
if (($cDebug) || (isset($problemData['debug'])))
{
// Prevent duplicate dump of log information
unset($mailData['LOG_HTML']);
unset($mailData['LOG_PLAIN']);
// Attach the collected information
$content = implode(chr(10),$logging).$cCRLF.$cCRLF.'=== VALUES AVAILABLE FOR TWIG TEMPLATE ==='.$cCRLF.$cCRLF.json_encode($mailData,JSON_PRETTY_PRINT|JSON_NUMERIC_CHECK);
$content = str_replace(chr(13),'',$content);
// Save to unique log file
$logName = 'log.'.$p_eventId.'.'.date('YmdHis').'.dump';
file_put_contents($z_log_path.$logName,$content);
_log('= Log stored to '.$z_log_path.$logName);
_log('= Log stored to '.$z_log_path.$logName);
}
?>

View File

@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<zabbix_export>
<version>5.0</version>
<date>2021-03-17T12:55:43Z</date>
<version>5.4</version>
<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://mydomain.com/zabbix/</value>
<value>https://myzabbix.com/zabbix/</value>
</parameter>
<parameter>
<name>duration</name>
@@ -35,10 +35,6 @@
<name>infoTest</name>
<value>Test</value>
</parameter>
<parameter>
<name>itemId</name>
<value>{ITEM.ID}</value>
</parameter>
<parameter>
<name>periods</name>
<value>10m,4h,1d,7d</value>
@@ -61,130 +57,160 @@
</parameter>
<parameter>
<name>URL</name>
<value>https://mydomain.com/mailGraph.php</value>
<value>https://myzabbix.com/mailGraph.php</value>
</parameter>
</parameters>
<script>try {&#13;
// Pickup parameters&#13;
params = JSON.parse(value),&#13;
req = new CurlHttpRequest(),&#13;
fields = {},&#13;
resp = '',&#13;
result = { tags: {} };&#13;
&#13;
// Set HTTP proxy if required&#13;
if (typeof params.HTTPProxy === 'string' &amp;&amp; params.HTTPProxy.trim() !== '') {&#13;
req.setProxy(params.HTTPProxy);&#13;
fields.HTTPProxy = params.HTTPProxy;&#13;
}&#13;
&#13;
// Declare output type&#13;
req.AddHeader('Content-Type: application/json');&#13;
&#13;
// Must have fields&#13;
fields.itemId = params.itemId;&#13;
fields.eventId = params.eventId;&#13;
fields.recipient = params.recipient;&#13;
fields.baseURL = params.baseURL;&#13;
fields.duration = params.duration;&#13;
&#13;
// Optional fields&#13;
if (typeof params.graphWidth === 'string') { fields.graphWidth = params.graphWidth; }&#13;
if (typeof params.graphHeight === 'string') { fields.graphHeight = params.graphHeight; }&#13;
if (typeof params.subject === 'string') { fields.subject = params.subject; }&#13;
if (typeof params.showLegend === 'string') { fields.showLegend = params.showLegend; }&#13;
if (typeof params.periods === 'string') { fields.periods = params.periods; }&#13;
if (typeof params.periods_headers === 'string') { fields.periods_headers = params.periods_headers; }&#13;
if (typeof params.debug === 'string') { fields.debug = params.debug; }&#13;
&#13;
// Add generic fields&#13;
Object.keys(params).forEach(function(key) {&#13;
if (key.substring(0, 4) == 'info') {&#13;
fields[key] = params[key];&#13;
}&#13;
});&#13;
&#13;
// Post information to the processing script&#13;
Zabbix.Log(4, '[MailGraph Webhook] Sending request: ' + params.URL + '?' + JSON.stringify(fields));&#13;
var resp = req.Post(params.URL,JSON.stringify(fields));&#13;
Zabbix.Log(4, '[Mailgraph Webhook] Receiving response:' + resp);&#13;
&#13;
// If there was an error, report it&#13;
if (req.Status() != 200) { throw JSON.parse(resp).errors[0]; }&#13;
&#13;
// We expect the message id back from the processing script&#13;
resp = JSON.parse(resp);&#13;
result.tags.__message_id = resp.messageId;&#13;
&#13;
// Pass the result back to Zabbix&#13;
return JSON.stringify(result);&#13;
}&#13;
catch (error)&#13;
{&#13;
// In case something went wrong in the processing, pass the error back to Zabbix&#13;
Zabbix.Log(127, 'MailGraph notification failed : '+error);&#13;
throw 'MailGraph notification failed : '+error;&#13;
<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),
req = new HttpRequest(),
fields = {},
resp = '',
result = { tags: {} };
// Set HTTP proxy if required
if (typeof params.HTTPProxy === 'string' &amp;&amp; params.HTTPProxy.trim() !== '') {
req.setProxy(params.HTTPProxy);
fields.HTTPProxy = params.HTTPProxy;
}
// Declare output type
req.addHeader('Content-Type: application/json');
// 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 (fields.recipient.charAt(0) == '{') {
throw '[MailGraph Webhook] Please define recipient for the test message!';
}
// Optional fields
if (typeof params.graphWidth === 'string') { fields.graphWidth = params.graphWidth; }
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) {
if (key.substring(0, 4) == 'info') {
fields[key] = params[key];
}
});
// Post information to the processing script
Zabbix.Log(4, '[MailGraph Webhook] Sending request: ' + params.URL + '?' + JSON.stringify(fields));
var resp = req.post(params.URL,JSON.stringify(fields));
Zabbix.Log(4, '[Mailgraph Webhook] Received response:' + resp);
// 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 response received from mailGraph.php? This should not occur (check URL and your webserver!)';
}
if (resp=='') {
throw '[MailGraph Webhook] No data received from mailGraph - please check recipient address or mailGraph log and retry.';
}
// 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;
Zabbix.Log(4, '[MailGraph Webhook] Message sent with identification &quot;' + msg.messageId + '&quot;');
// Pass the result back to Zabbix
return JSON.stringify(result);
}
catch (error)
{
// 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>
<process_tags>YES</process_tags>
<description>The &quot;URL&quot; must point to the location of the processing script. If a proxy is required, define &quot;HTTPProxy&quot; for the proxy address.&#13;
&#13;
Customization:&#13;
- &quot;graphWidth&quot; and &quot;graphWidth&quot; can be defined for the image size&#13;
- &quot;showLegend&quot; can be defined to show or hide the legend of the graph&#13;
- &quot;subject&quot; can be defined for a customized subject for the mail message&#13;
- &quot;periods&quot; and &quot;periods_headers&quot; can be defined for displaying multiple periods of the same graph, or&#13;
- &quot;period&quot; and &quot;period_header&quot; can be defined to display a single graph&#13;
&#13;
The html.template and plain.template files can be adjusted (TWIG format).&#13;
&#13;
<description>The &quot;URL&quot; must point to the location of the processing script. If a proxy is required, define &quot;HTTPProxy&quot; for the proxy address.
Customization:
- &quot;graphWidth&quot; and &quot;graphWidth&quot; can be defined for the image size
- &quot;showLegend&quot; can be defined to show or hide the legend of the graph
- &quot;subject&quot; can be defined for a customized subject for the mail message
- &quot;periods&quot; and &quot;periods_headers&quot; can be defined for displaying multiple periods of the same graph, or
- &quot;period&quot; and &quot;period_header&quot; can be defined to display a single graph
The html.template and plain.template files can be adjusted (TWIG format).
More details are available at https://github.com/moudsen/mailGraph</description>
<message_templates>
<message_template>
<event_source>TRIGGERS</event_source>
<operation_mode>PROBLEM</operation_mode>
<subject>Problem: {EVENT.NAME}</subject>
<message>Problem started at {EVENT.TIME} on {EVENT.DATE}&#13;
Problem name: {EVENT.NAME}&#13;
Host: {HOST.NAME}&#13;
Severity: {EVENT.SEVERITY}&#13;
Operational data: {EVENT.OPDATA}&#13;
Original problem ID: {EVENT.ID}&#13;
{TRIGGER.URL}&#13;
&#13;
eventId: {EVENT.ID}&#13;
TriggerId: {TRIGGER.ID}&#13;
itemId: {ITEM.ID]</message>
<message>Problem started at {EVENT.TIME} on {EVENT.DATE}
Problem name: {EVENT.NAME}
Host: {HOST.NAME}
Severity: {EVENT.SEVERITY}
Operational data: {EVENT.OPDATA}
Original problem ID: {EVENT.ID}
Trigger ID (/url): {TRIGGER.ID} {TRIGGER.URL}</message>
</message_template>
<message_template>
<event_source>TRIGGERS</event_source>
<operation_mode>RECOVERY</operation_mode>
<subject>Resolved in {EVENT.DURATION}: {EVENT.NAME}</subject>
<message>Problem has been resolved at {EVENT.RECOVERY.TIME} on {EVENT.RECOVERY.DATE}&#13;
Problem name: {EVENT.NAME}&#13;
Problem duration: {EVENT.DURATION}&#13;
Host: {HOST.NAME}&#13;
Severity: {EVENT.SEVERITY}&#13;
Original problem ID: {EVENT.ID}&#13;
{TRIGGER.URL}&#13;
&#13;
eventId: {EVENT.ID}&#13;
TriggerId: {TRIGGER.ID}&#13;
itemId: {ITEM.ID]</message>
<subject>Resolved in {EVENT.DURATION}: {EVENT.RECOVERY.NAME}</subject>
<message>Problem has been resolved at {EVENT.RECOVERY.TIME} on {EVENT.RECOVERY.DATE}
Problem name: {EVENT.RECOVERY.NAME}
Problem duration: {EVENT.DURATION}
Host: {HOST.NAME}
Severity: {EVENT.SEVERITY}
Original problem ID: {EVENT.ID}
Trigger ID: {TRIGGER.ID} {TRIGGER.URL}</message>
</message_template>
<message_template>
<event_source>TRIGGERS</event_source>
<operation_mode>UPDATE</operation_mode>
<subject>Updated problem in {EVENT.AGE}: {EVENT.NAME}</subject>
<message>{USER.FULLNAME} {EVENT.UPDATE.ACTION} problem at {EVENT.UPDATE.DATE} {EVENT.UPDATE.TIME}.&#13;
{EVENT.UPDATE.MESSAGE}&#13;
&#13;
Current problem status is {EVENT.STATUS}, age is {EVENT.AGE}, acknowledged: {EVENT.ACK.STATUS}.&#13;
&#13;
eventId: {EVENT.ID}&#13;
TriggerId: {TRIGGER.ID}&#13;
itemId: {ITEM.ID]</message>
<message>{USER.FULLNAME} {EVENT.UPDATE.ACTION} problem at {EVENT.UPDATE.DATE} {EVENT.UPDATE.TIME}.
{EVENT.UPDATE.MESSAGE}
Current problem status is {EVENT.STATUS}, age is {EVENT.AGE}, acknowledged: {EVENT.ACK.STATUS}.
Event ID: {EVENT.ID}
Trigger ID: {TRIGGER.ID}</message>
</message_template>
</message_templates>
</media_type>
</media_types>
</zabbix_export>

View File

@@ -124,6 +124,7 @@
{% if GRAPH_ZABBIXLINK|length > 0 %}
// Graph ID: <a href="{{ GRAPH_ZABBIXLINK }}">{{ GRAPH_ID }}</a>
{% endif %}
// <a href="{{ ACK_URL }}">Ack</a>
</div>
</td>
</tr>