21 Commits
v1.26 ... v1.31

Author SHA1 Message Date
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
moudsen
ba0d474bdb Update README.md 2021-03-24 12:35:49 +01:00
moudsen
a5a0f0ace1 Update README.md 2021-03-24 12:34:34 +01:00
moudsen
8655d4ca2c Updated to v1.28 2021-03-24 12:33:10 +01:00
moudsen
76321be3f8 Update README.md 2021-03-24 12:28:39 +01:00
Mark Oudsen
9d4f00ce87 Merge branch 'main' of github.com:moudsen/mailGraph into main 2021-03-24 12:27:54 +01:00
Mark Oudsen
1278d43acb Added ability to specify SMTP username/password (v1.28) 2021-03-24 12:27:44 +01:00
moudsen
18c22abf17 Updated to v1.27 2021-03-20 14:09:51 +01:00
moudsen
0a623f3137 Updated to v1.27 2021-03-20 14:08:57 +01:00
Mark Oudsen
634134b423 Ability to add additional grapgs through Tagging of Trigger of Host and associate Template update (v1.27) 2021-03-20 14:06:08 +01:00
5 changed files with 456 additions and 65 deletions

View File

@@ -1,10 +1,27 @@
## mailGraph (v1.26) ## Minor bugfixes to the code (2021/10/06)
Code improvements to prevent possible errors leading into a non-functional mailGraph ... (typical error within log or Zabbix: "json error").
Please inform me (raise an issue) in case you have PHP related errors in your logs - this should no longer be the case with v1.31.
## mailGraph (v1.31)
Zabbix Media module and scripts for sending e-mail alerts with graphs. Zabbix Media module and scripts for sending e-mail alerts with graphs.
**Please use the Wiki for information on how to install, configure and use MailGraph in Zabbix.** **Please use the Wiki for information on how to install, configure and use MailGraph in Zabbix**
## Upgrade notes ## UPGRADE NOTES
Per v1.25 and higher the 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). ### v1.31
Updated: mailGraph.pgp - Bugfixes and c
### v1.29
Updated: mailGraph.php
### 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 ...).
### 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).
## Example message ## Example message
The below message is just an example of what MailGraph is capable of. The template engine used ("Twig") allows for a fully customized message creation to your needs!
[![](images/Example-mail-message-v122.png?raw=true)](images/Example-mail-message-v122.png) [![](images/Example-mail-message-v122.png?raw=true)](images/Example-mail-message-v122.png)

View File

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

Binary file not shown.

After

Width:  |  Height:  |  Size: 85 KiB

View File

@@ -33,6 +33,15 @@
// 1.25 2021/03/16 - Mark Oudsen - Refactoring for optimized flow and relevant data retrieval // 1.25 2021/03/16 - Mark Oudsen - Refactoring for optimized flow and relevant data retrieval
// 1.26 2021/03/19 - Mark Oudsen - Bugfixes after refactor (wrong itemId and incorrect eventValue) // 1.26 2021/03/19 - Mark Oudsen - Bugfixes after refactor (wrong itemId and incorrect eventValue)
// Suppressing Zabbix username-password in log // Suppressing Zabbix username-password in log
// 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)
// 2021/07/07 - Mark Oudsen - #20 - Added problem URL as ready to use TWIG macro
// 1.30 2021/07/07 - Mark Oudsen - Adding auto-cleaning of images, log retention and some more macros
// 1.31 2021/10/06 - Mark Oudsen - Bugfixes, PHP error suppression and prevention of trivial errors
// ------------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------------
// //
// (C) M.J.Oudsen, mark.oudsen@puzzl.nl // (C) M.J.Oudsen, mark.oudsen@puzzl.nl
@@ -49,15 +58,17 @@
// 1) Fetch trigger, item, host, graph, event information via Zabbix API via CURL // 1) Fetch trigger, item, host, graph, event information via Zabbix API via CURL
// 2) Fetch Graph(s) 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 // 3) Build and send mail message from template using Swift/TWIG
// 4) Cleanup images, retention logs
// //
///////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////
// CONSTANTS // CONSTANTS
$cVersion = 'v1.26'; $cVersion = 'v1.31';
$cCRLF = chr(10).chr(13); $cCRLF = chr(10).chr(13);
$maskDateTime = 'Y-m-d H:i:s'; $maskDateTime = 'Y-m-d H:i:s';
$maxGraphs = 4;
// DEBUG SETTINGS // DEBUG SETTINGS
// -- Should be FALSE for production level use // -- Should be FALSE for production level use
@@ -66,6 +77,11 @@
$cDebugMail = FALSE; // If TRUE, includes log in the mail message (html and plain text attachments) $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 !!! $showLog = FALSE; // Display the log - !!! only use in combination with CLI mode !!!
// Limit server level output errors; this will ensure no error messages are passed to Zabbix which
// otherwise will block mailGraph from functioning correctly
error_reporting(E_ERROR | E_PARSE);
// INCLUDE REQUIRED LIBRARIES (Composer) // INCLUDE REQUIRED LIBRARIES (Composer)
// (configure at same location as the script is running or load in your own central library) // (configure at same location as the script is running or load in your own central library)
// -- swiftmailer/swiftmailer https://swiftmailer.symfony.com/docs/introduction.html // -- swiftmailer/swiftmailer https://swiftmailer.symfony.com/docs/introduction.html
@@ -95,23 +111,24 @@
$ch = curl_init(); $ch = curl_init();
// Set options // Set options
curl_setopt($ch, CURLOPT_USERAGENT, 'Zabbix-mailGraph - '.$cVersion);
if ((isset($HTTPProxy)) && ($HTTPProxy!='')) if ((isset($HTTPProxy)) && ($HTTPProxy!=''))
{ {
_log('% Using proxy: '.$HTTPProxy); if ($cDebug) { _log('% Using proxy: '.$HTTPProxy); }
curl_setopt($ch, CURLOPT_PROXY, $HTTPProxy); curl_setopt($ch, CURLOPT_PROXY, $HTTPProxy);
} }
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20); 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_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json')); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($ch, CURLOPT_POST, TRUE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS,
json_encode($data,JSON_PRETTY_PRINT|JSON_NUMERIC_CHECK)); curl_setopt($ch, CURLOPT_USERAGENT, 'Zabbix-mailGraph - '.$cVersion);
// Execute Curl // Execute Curl
$data = curl_exec($ch); $data = curl_exec($ch);
@@ -120,9 +137,10 @@
{ {
_log('! Failed: '.curl_error($ch)); _log('! Failed: '.curl_error($ch));
$data = 'An error occurred while retreiving the requested page.'.$cCRLF; $data = array();
$data .= 'Requested page = '.$url.$cCRLF; $data[] = 'An error occurred while retreiving the requested page.';
$data .= 'Error = '.curl_error($ch).$cCRLF; $data[] .= 'Requested page = '.$url;
$data[] .= 'Error = '.curl_error($ch);
} }
else else
{ {
@@ -268,6 +286,22 @@
if ($showLog) { echo $logString.$cCRLF; } if ($showLog) { echo $logString.$cCRLF; }
} }
/////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// Catch PHP warnings/notices/errors
function catchPHPerrors($errno, $errstr, $errfile, $errline)
{
// --- Just log ...
_log('!! ('.$errno.') "'.$errstr.'" at line #'.$errline.' of "'.$errfile.'"');
// --- We do not take care of any errors, etc.
return FALSE;
}
set_error_handler("catchPHPerrors");
///////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////
// Read configuration file // Read configuration file
@@ -384,6 +418,10 @@
{ {
if (($argc>1) && ($argv[1]=='test')) if (($argc>1) && ($argv[1]=='test'))
{ {
// Switch on CLI log display
$showLog = TRUE;
_log('<<< mailGraph '.$cVersion.' >>>');
_log('# Invoked from CLI'); _log('# Invoked from CLI');
// Assumes that config.json file has the correct information // Assumes that config.json file has the correct information
@@ -403,13 +441,11 @@
if (isset($config['cli_periods_headers'])) { $problemData['periods_headers'] = $config['cli_periods_headers']; } if (isset($config['cli_periods_headers'])) { $problemData['periods_headers'] = $config['cli_periods_headers']; }
if (isset($config['cli_debug'])) { $problemData['debug'] = $config['cli_debug']; } if (isset($config['cli_debug'])) { $problemData['debug'] = $config['cli_debug']; }
if (isset($config['cli_proxy'])) { $problemData['HTTPProxy'] = $config['cli_proxy']; } if (isset($config['cli_proxy'])) { $problemData['HTTPProxy'] = $config['cli_proxy']; }
// Switch on CLI log display
$showLog = TRUE;
} }
} }
_log('# Data passed to MailGraph main routine and used for processing'.$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 --- // --- CHECK AND SET P_ VARIABLES ---
// FROM POST OR CLI DATA // FROM POST OR CLI DATA
@@ -444,7 +480,7 @@
$p_period = '48h'; $p_period = '48h';
if (isset($problemData['period'])) { $p_period = $problemData['period']; } 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 // DYNAMIC VARIABLES FROM ZABBIX
@@ -468,21 +504,33 @@
$p_smtp_strict = 'yes'; $p_smtp_strict = 'yes';
if ((isset($config['smtp_strict'])) && ($config['smtp_strict']=='no')) { $p_smtp_strict = 'no'; } if ((isset($config['smtp_strict'])) && ($config['smtp_strict']=='no')) { $p_smtp_strict = 'no'; }
$p_smtp_username = '';
if (isset($config['smtp_username'])) { $p_smtp_username = $config['smtp_username']; }
$p_smtp_password = '';
if (isset($config['smtp_password'])) { $p_smtp_password = $config['smtp_password']; }
$p_graph_match = 'any'; $p_graph_match = 'any';
if ((isset($config['graph_match'])) && ($config['graph_match']=='exact')) { $p_graph_match = 'exact'; } if ((isset($config['graph_match'])) && ($config['graph_match']=='exact')) { $p_graph_match = 'exact'; }
$p_remove_images = 'yes';
if ((isset($config['remove_images'])) && ($config['remove_images']=='no')) { $p_remove_images = 'no'; }
$p_log_retention = 365;
if (isset($config['log_retention'])) { $p_log_retention =intval($config['log_retention']); }
// --- GLOBAL CONFIGURATION --- // --- GLOBAL CONFIGURATION ---
// Script related settings // Script related settings
$z_url = $config['script_baseurl']; // Script URL location (for relative paths to images, templates, log, tmp) $z_url = $config['script_baseurl']; // Script URL location (for relative paths to images, templates, log, tmp)
$z_url_image = $z_url.'images/'; // Images URL (included in plain message text) $z_url_image = $z_url.'images/'; // Images URL (included in plain message as text)
// Absolute path where to store the generated images - note: script does not take care of clearing out old images! // Absolute paths for processing
$z_path = getcwd().'/'; // Absolute base path on the filesystem for this url $z_path = getcwd().'/'; // Absolute base path on the filesystem for this url
$z_images_path = $z_path.'images/'; $z_images_path = $z_path.'images/'; // Storing images here
$z_template_path = $z_path.'templates/'; $z_template_path = $z_path.'templates/'; // Expecting templates here
$z_tmp_cookies = $z_path.'tmp/'; $z_tmp_cookies = $z_path.'tmp/'; // Temporary path required for Cookies (front-end login to Zabbix for image generation)
$z_log_path = $z_path.'log/'; $z_log_path = $z_path.'log/'; // Where logs are stored of mailGraph
// Zabbix user (requires Super Admin access rights to access image generator script) // Zabbix user (requires Super Admin access rights to access image generator script)
$z_user = $config['zabbix_user']; $z_user = $config['zabbix_user'];
@@ -504,9 +552,9 @@
// Check accessibility of paths and template files ////////////////////////////////////////////////////// // Check accessibility of paths and template files //////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////
if (!is_writable($z_images_path)) { echo 'Image path inaccessible?'.$cCRLF; die; } if (!is_writable($z_images_path)) { echo 'Image path inaccessible (should have read/write rights)?'.$cCRLF; die; }
if (!is_writable($z_tmp_cookies)) { echo 'Cookies temporary path inaccessible?'.$cCRLF; die; } if (!is_writable($z_tmp_cookies)) { echo 'Cookies temporary path inaccessible (should have read/write rights)?'.$cCRLF; die; }
if (!is_writable($z_log_path)) { echo 'Log path inaccessible?'.$cCRLF; die; } if (!is_writable($z_log_path)) { echo 'Log path inaccessible (should have read/write rights)?'.$cCRLF; die; }
if (!file_exists($z_template_path.'html.template')) { echo 'HTML template missing?'.$cCRLF; die; } if (!file_exists($z_template_path.'html.template')) { echo 'HTML template missing?'.$cCRLF; die; }
if (!file_exists($z_template_path.'plain.template')) { echo 'PLAIN template missing?'.$cCRLF; die; } if (!file_exists($z_template_path.'plain.template')) { echo 'PLAIN template missing?'.$cCRLF; die; }
@@ -537,7 +585,13 @@
$token = ''; $token = '';
if (isset($result['result'])) { $token = $result['result']; } 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 (check username/password)? ('.$z_url_api.'): '.$cCRLF;
echo var_dump($request).$cCRLF;
echo var_dump($result).$cCRLF;
die;
}
_log('> Token = '.$token); _log('> Token = '.$token);
@@ -613,6 +667,9 @@
// --- Custom settings? // --- Custom settings?
$forceGraph = 0; $forceGraph = 0;
$triggerScreen = 0;
$triggerScreenPeriod = '';
$triggerScreenPeriodHeader = '';
foreach($thisTrigger['result'][0]['tags'] as $aTag) foreach($thisTrigger['result'][0]['tags'] as $aTag)
{ {
@@ -662,6 +719,21 @@
$problemData['debug'] = 1; $problemData['debug'] = 1;
_log('+ Mail debug log enabled'); _log('+ Mail debug log enabled');
break; break;
case 'mailGraph.screen':
$triggerScreen = intval($aTag['value']);
_log('+ Trigger screen = '.$triggerScreen);
break;
case 'mailGraph.screenPeriod':
$triggerScreenPeriod = $aTag['value'];
_log('+ Trigger screen period = '.$triggerScreenPeriod);
break;
case 'mailGraph.screenPeriodHeader':
$triggerScreenPeriodHeader = $aTag['value'];
_log('+ Trigger screen header = '.$triggerScreenPeriodHeader);
break;
} }
} }
@@ -709,7 +781,8 @@
$request = array('jsonrpc'=>'2.0', $request = array('jsonrpc'=>'2.0',
'method'=>'host.get', 'method'=>'host.get',
'params'=>array('hostids'=>$hostId, 'params'=>array('hostids'=>$hostId,
'output'=>'extend'), 'output'=>'extend',
'selectTags'=>'extend'),
'auth'=>$token, 'auth'=>$token,
'id'=>nextRequestID()); 'id'=>nextRequestID());
@@ -723,6 +796,33 @@
$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'];
// --- Custom settings?
$hostScreen = 0;
$hostScreenPeriod = '';
$hostScreenPeriodHeader = '';
foreach($thisHost['result'][0]['tags'] as $aTag)
{
switch ($aTag['tag'])
{
case 'mailGraph.screen':
$hostScreen = intval($aTag['value']);
_log('+ Host screen (from TAG) = '.$hostScreen);
break;
case 'mailGraph.screenPeriod':
$hostScreenPeriod = $aTag['value'];
_log('+ Host screen period (from TAG) = '.$hostScreenPeriod);
break;
case 'mailGraph.screenPeriodHeader':
$hostScreenPeriodHeader = $aTag['value'];
_log('+ Host screen period header (from TAG) = '.$hostScreenPeriodHeader);
break;
}
}
_log('# Retreive HOST macro information'); _log('# Retreive HOST macro information');
$request = array('jsonrpc'=>'2.0', $request = array('jsonrpc'=>'2.0',
@@ -735,9 +835,30 @@
$thisMacros = postJSON($z_url_api,$request); $thisMacros = postJSON($z_url_api,$request);
_log('> Host macro data'.$cCRLF.json_encode($thisMacros,JSON_PRETTY_PRINT|JSON_NUMERIC_CHECK)); _log('> Host macro data'.$cCRLF.json_encode($thisMacros,JSON_PRETTY_PRINT|JSON_NUMERIC_CHECK));
// -------------------------------------------- foreach($thisMacros['result'] as $aMacro)
// --- GET GRAPHS ASSOCIATED WITH THIS HOST --- {
// -------------------------------------------- switch($aMacro['macro'])
{
case 'mailGraph.screen':
$hostScreen = intval($aMacro['value']);
_log('+ Host screen (from MACRO) = '.$hostScreen);
break;
case 'mailGraph.screenPeriod':
$hostScreenPeriod = $aMacro['value'];
_log('+ Host screen period (from MACRO) = '.$hostScreenPeriod);
break;
case 'mailGraph.screenPeriodHeader':
$hostScreenPeriodHeader = $aMacro['value'];
_log('+ Host screen header (from MACRO) = '.$hostScreenPeriodHeader);
break;
}
}
// ------------------------------------------------------------------
// --- GET GRAPHS ASSOCIATED WITH THIS HOST AND THE TRIGGER ITEMS ---
// ------------------------------------------------------------------
_log('# Retrieve associated graphs to this HOST and the TRIGGER ITEMS'); _log('# Retrieve associated graphs to this HOST and the TRIGGER ITEMS');
@@ -781,7 +902,8 @@
'id'=>nextRequestID()); 'id'=>nextRequestID());
$forceGraphInfo = postJSON($z_url_api,$request); $forceGraphInfo = postJSON($z_url_api,$request);
_log('> Forced graph data'.$cCRLF.json_encode($forceGraphInfo,JSON_PRETTY_PRINT|JSON_NUMERIC_CHECK)); _log('> Forced graph data'.$cCRLF.
json_encode($forceGraphInfo,JSON_PRETTY_PRINT|JSON_NUMERIC_CHECK));
if (!isset($forceGraphInfo['result'][0])) if (!isset($forceGraphInfo['result'][0]))
{ {
@@ -840,11 +962,95 @@
_log('> Graphs found (matching/partial) = '.sizeof($matchedGraphs).' / '.sizeof($otherGraphs)); _log('> Graphs found (matching/partial) = '.sizeof($matchedGraphs).' / '.sizeof($otherGraphs));
// ---------------------------------------------------------------------------
// --- FIND MATCHING GRAPH ITEMS WITH TRIGGER AND/OR HOST SCREEN REFERENCE ---
// ---------------------------------------------------------------------------
function _sort($a,$b)
{
if ($a['screen']['y']>$b['screen']['y']) { return(1); }
if ($a['screen']['y']<$b['screen']['y']) { return(-1); }
if ($a['screen']['x']>$b['screen']['x']) { return(1); }
if ($a['screen']['x']<$b['screen']['x']) { return(-1); }
return(0);
}
function fetchGraphsFromScreen($screenId)
{
global $token;
global $z_url_api;
global $cCRLF;
// --- Pick up the SCREEN ITEMS associated to the SCREEN
$request = array('jsonrpc'=>'2.0',
'method'=>'screen.get',
'params'=>array('screenids'=>$screenId,
'output'=>'extend',
'selectScreenItems'=>'extend'),
'auth'=>$token,
'id'=>nextRequestID());
$screenGraphs = postJSON($z_url_api,$request);
_log('> Screen items data for screen #'.$screenId.$cCRLF.json_encode($screenGraphs,JSON_PRETTY_PRINT|JSON_NUMERIC_CHECK));
// --- Filter on specific type(s) and enrich the graph data
$result = array();
foreach($screenGraphs['result'][0]['screenitems'] as $anItem)
{
switch($anItem['resourcetype'])
{
case 0: // Graph
$request = array('jsonrpc'=>'2.0',
'method'=>'graph.get',
'params'=>array('graphids'=>$anItem['resourceid'],
'expandName'=>1,
'output'=>'extend'),
'auth'=>$token,
'id'=>nextRequestID());
$screenGraph = postJSON($z_url_api,$request);
_log('+ Graph data for screen item #'.$anItem['screenitemid'].$cCRLF.
json_encode($screenGraph,JSON_PRETTY_PRINT|JSON_NUMERIC_CHECK));
$result[] = array('screen'=>$anItem,'name'=>$screenGraphs['result'][0]['name'],'graph'=>$screenGraph['result'][0]);
break;
}
}
// --- Sort the result according to SCREEN x,y position
usort($result,"_sort");
// --- Done
return($result);
}
$triggerGraphs = array();
if ($triggerScreen>0)
{
_log('# Fetching graph information for TRIGGER for screen #'.$hostScreen);
$triggerGraphs = fetchGraphsFromScreen($triggerScreen);
_log('> Graphs found = '.sizeof($triggerGraphs));
}
if ($hostScreen>0)
{
_log('# Fetching graph information for HOST for screen #'.$hostScreen);
$hostGraphs = fetchGraphsFromScreen($hostScreen);
_log('> Graphs found = '.sizeof($hostGraphs));
}
///////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////
// Fetch Graph ////////////////////////////////////////////////////////////////////////////////////////// // Fetch Graph(s) ///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////
// Determine number of periods for this graph // Determine number of periods for the ITEM graphs
$p_periods = array(); $p_periods = array();
$p_periods_headers = array(); $p_periods_headers = array();
@@ -883,8 +1089,12 @@
} }
} }
while (sizeof($p_periods)>4) { array_pop($p_periods); } // Strip off any excessive elements from the end
while (sizeof($p_periods_headers)>4) { array_pop($p_periods_headers); }
while (sizeof($p_periods)>$maxGraphs) { array_pop($p_periods); }
while (sizeof($p_periods_headers)>$maxGraphs) { array_pop($p_periods_headers); }
// Fetching of the ITEM graphs
$graphFiles = array(); $graphFiles = array();
$graphURL = ''; $graphURL = '';
@@ -930,14 +1140,78 @@
$graphFiles[] = $graphFile; $graphFiles[] = $graphFile;
$mailData['GRAPHS'][$aKey]['URL'] = $z_url_image . $graphFile; $mailData['GRAPHS_I'][$aKey]['PATH'] = $z_images_path . $graphFile;
$mailData['GRAPHS'][$aKey]['HEADER'] = $p_periods_headers[$aKey]; $mailData['GRAPHS_I'][$aKey]['URL'] = $z_url_image . $graphFile;
$mailData['GRAPHS_I'][$aKey]['HEADER'] = $p_periods_headers[$aKey];
} }
$mailData['GRAPH_ZABBIXLINK'] = $z_server.'graphs.php?form=update&graphid='.$mailData['GRAPH_ID']; $mailData['GRAPH_ZABBIXLINK'] = $z_server.'graphs.php?form=update&graphid='.$mailData['GRAPH_ID'];
} }
// Prepare HTML LOG content // Fetch graphs associated to TRIGGER or HOST screen references obtained earlier
function addGraphs($varName,$info,$period,$periodHeader)
{
global $p_graphWidth;
global $p_graphHeight;
global $p_showLegend;
global $z_url_image;
global $z_images_path;
global $z_server;
global $mailData;
$files = array();
foreach($info as $aKey=>$anItem)
{
$graphFile = GraphImageById($anItem['graph']['graphid'],
$p_graphWidth,$p_graphHeight,
$anItem['graph']['graphtype'],
$p_showLegend,$period);
$mailData['GRAPHS_'.$varName][$aKey]['URL'] = $z_url_image . $graphFile;
$mailData['GRAPHS_'.$varName][$aKey]['PATH'] = $z_images_path . $graphFile;
}
$mailData['GRAPHS_'.$varName.'_LINK'] = $z_server.'screens.php?elementid='.$info[0]['screen']['screenid'];
$mailData['GRAPHS_'.$varName.'_HEADER'] = $info[0]['name'];
$mailData['GRAPHS_'.$varName.'_PERIODHEADER'] = $periodHeader;
}
if (sizeof($triggerGraphs)>0)
{
if ($triggerScreenPeriod=='')
{
$triggerScreenPeriod = $p_periods[0];
$triggerScreenPeriodHeader = $p_periods_headers[0];
}
if ($triggerScreenPeriodHeader=='') { $triggerScreenPeriodHeader = $triggerScreenPeriod; }
addGraphs('T',$triggerGraphs,$triggerScreenPeriod,$triggerScreenPeriodHeader);
$mailData['TRIGGER_SCREEN'] = $triggerScreen;
}
if (sizeof($hostGraphs)>0)
{
if ($hostScreenPeriod=='')
{
$hostScreenPeriod = $p_periods[0];
$hostScreenPeriodHeader = $p_periods_headers[0];
}
if ($hostScreenPeriodHeader=='') { $hostScreenPeriodHeader = $hostScreenPeriod; }
addGraphs('H',$hostGraphs,$hostScreenPeriod,$hostScreenPeriodHeader);
$mailData['HOST_SCREEN'] = $hostScreen;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// Prepare LOG related content //////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////
$mailData['LOG_HTML'] = implode('</br/>',$logging); $mailData['LOG_HTML'] = implode('</br/>',$logging);
$mailData['LOG_HTML'] = str_replace($cCRLF,'<br/>',$mailData['LOG_HTML']); $mailData['LOG_HTML'] = str_replace($cCRLF,'<br/>',$mailData['LOG_HTML']);
@@ -949,16 +1223,17 @@
'</body>'.$cCRLF. '</body>'.$cCRLF.
'</html>'; '</html>';
// Prepare PLAIN LOG content
$mailData['LOG_PLAIN'] = implode(chr(10),$logging); $mailData['LOG_PLAIN'] = implode(chr(10),$logging);
// Prepare others /////////////////////////////////////////////////////////////////////////////////////////////////////////
// Prepare others ///////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////
$mailData['TRIGGER_URL'] = $z_server.'triggers.php?form=update&triggerid='.$mailData['TRIGGER_ID']; $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['ITEM_URL'] = $z_server.'items.php?form=update&hostid='.$mailData['HOST_ID'].'&itemid='.$mailData['ITEM_ID'];
$mailData['HOST_URL'] = $z_server.'hosts.php?form=update&hostid='.$mailData['HOST_ID']; $mailData['HOST_URL'] = $z_server.'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['HOST_PROBLEMS_URL'] = $z_server.'zabbix.php?action=problem.view&filter_hostids%5B%5D='.$mailData['HOST_ID'].'&filter_set=1';
$mailData['EVENT_DURATION'] = $p_duration; $mailData['EVENT_DURATION'] = $p_duration;
@@ -1002,11 +1277,18 @@
$transport = (new Swift_SmtpTransport($p_smtp_server, $p_smtp_port)); $transport = (new Swift_SmtpTransport($p_smtp_server, $p_smtp_port));
} }
// Username/password?
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); $mailer = new Swift_Mailer($transport);
$message = (new Swift_Message()); $message = (new Swift_Message());
// Fetch mailer ID from this message (no Swift function available for it ...) // --- Fetch mailer ID from this message (no Swift function available for it ...)
// --- "Message-ID: <...id...@swift.generated>" // --- "Message-ID: <...id...@swift.generated>"
$content = $message->toString(); $content = $message->toString();
@@ -1031,19 +1313,26 @@
$twig = new \Twig\Environment($loader); $twig = new \Twig\Environment($loader);
if (sizeof($graphFiles)>0) // --- Embed the images
function embedGraphs($graphs,$varName,$type)
{ {
// Embed the image(s) global $message;
global $mailData;
$graphReference = 0; foreach($graphs as $aKey=>$anItem)
foreach($graphFiles as $graphFile)
{ {
$mailData['GRAPHS'][$graphReference++]['CID'] = $message->embed(Swift_Image::fromPath($z_images_path.$graphFile)); $mailData['GRAPHS_'.$varName][$aKey]['CID'] = $message->embed(Swift_Image::fromPath($mailData['GRAPHS_'.$varName][$aKey]['PATH']));
_log('> Embedded graph image '.$z_images_path.$graphFile); _log('> Embedded graph image ('.$type.') '.$mailData['GRAPHS_'.$varName][$aKey]['PATH']);
} }
} }
embedGraphs($graphFiles,'I','ITEM');
embedGraphs($triggerGraphs,'T','TRIGGER');
embedGraphs($hostGraphs,'H','HOST');
// --- Render the content
$bodyHTML = $twig->render('html', $mailData); $bodyHTML = $twig->render('html', $mailData);
$bodyPlain = $twig->render('plain', $mailData); $bodyPlain = $twig->render('plain', $mailData);
$mailSubject = $twig->render('subject', $mailData); $mailSubject = $twig->render('subject', $mailData);
@@ -1076,6 +1365,41 @@
$response = array('messageId.mailGraph'=>$messageId); $response = array('messageId.mailGraph'=>$messageId);
echo json_encode($response).$cCRLF; echo json_encode($response).$cCRLF;
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// Wrap up //////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// Remove generated images? (default=yes)
function removeGraphs($graphs,$varName)
{
global $mailData;
foreach($graphs as $aKey=>$anItem)
{
unlink($mailData['GRAPHS_'.$varName][$aKey]['PATH']);
_log('> Removed generated image: '.$mailData['GRAPHS_'.$varName][$aKey]['PATH']);
}
}
if ($p_remove_images=='yes')
{
if (!$cDebug)
{
removeGraphs($graphFiles,'I');
removeGraphs($triggerGraphs,'T');
removeGraphs($hostGraphs,'H');
}
else
{
_log('> Not removing generated images (debug mode active)');
}
}
else
{
_log('> Not removing generated images');
}
// Store log? // Store log?
if (($cDebug) || (isset($problemData['debug']))) if (($cDebug) || (isset($problemData['debug'])))
@@ -1089,6 +1413,6 @@
$logName = 'log.'.$p_eventId.'.'.date('YmdHis').'.dump'; $logName = 'log.'.$p_eventId.'.'.date('YmdHis').'.dump';
file_put_contents($z_log_path.$logName,$content); 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

@@ -114,16 +114,6 @@
</table> </table>
</td> </td>
</tr> </tr>
{% for aGraph in GRAPHS %}
<tr>
<div align=justify>
<td align="center">
<span class="graphHeader">{{ aGraph.HEADER }}</span><br/>
<img id="mainimage" border=0 style="width: 100%; max-width: 790px" alt="Zabbix Graph" src="{{ aGraph.CID }}" />
</td>
</div>
</tr>
{% endfor %}
<tr> <tr>
<td class="links"> <td class="links">
<div align=center> <div align=center>
@@ -137,6 +127,66 @@
</div> </div>
</td> </td>
</tr> </tr>
{% for aGraph in GRAPHS_I %}
<tr>
<div align=justify>
<td align="center">
<span class="graphHeader">{{ aGraph.HEADER }}</span><br/>
<img id="mainimage" border=0 style="width: 100%; max-width: 790px" alt="Zabbix Graph" src="{{ aGraph.CID }}" />
</td>
</div>
</tr>
{% endfor %}
{% if TRIGGER_SCREEN > 0 %}
<tr>
<div align=justify>
<td align="center">
<span class="graphHeader">{{ GRAPHS_T_HEADER }} - {{ GRAPHS_T_PERIODHEADER }}</span>
</td>
</div>
</tr>
<tr>
<td class="links">
<div align="center">
Screen ID: <a href="{{ GRAPHS_T_LINK }}">{{ TRIGGER_SCREEN }}</a>
</div>
</td>
</tr>
{% for aGraph in GRAPHS_T %}
<tr>
<div align=justify>
<td align="center">
<img id="mainimage" border=0 style="width: 100%; max-width: 790px" alt="Zabbix Graph" src="{{ aGraph.CID }}" />
</td>
</div>
</tr>
{% endfor %}
{% endif %}
{% if HOST_SCREEN > 0 %}
<tr>
<div align=justify>
<td align="center">
<span class="graphHeader">{{ GRAPHS_H_HEADER }} - {{ GRAPHS_H_PERIODHEADER }}</span>
</td>
</div>
</tr>
<tr>
<td class="links">
<div align="center">
Screen ID: <a href="{{ GRAPHS_H_LINK }}">{{ HOST_SCREEN }}</a>
</div>
</td>
</tr>
{% for aGraph in GRAPHS_H %}
<tr>
<div align=justify>
<td align="center">
<img id="mainimage" border=0 style="width: 100%; max-width: 790px" alt="Zabbix Graph" src="{{ aGraph.CID }}" />
</td>
</div>
</tr>
{% endfor %}
{% endif %}
</table> </table>
</body> </body>
</html> </html>