mirror of
https://github.com/moudsen/mailGraph
synced 2025-10-28 08:07:39 +01:00
Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e80a2a56a3 | ||
|
|
4b28d3f066 | ||
|
|
93535d4006 | ||
|
|
b9622da72f | ||
|
|
56fb68ae9c | ||
|
|
fa51c3e59e | ||
|
|
4ae62b3a1f | ||
|
|
0731b87d44 | ||
|
|
bdf4b05a6b | ||
|
|
df16444665 | ||
|
|
bccd134523 | ||
|
|
27c1d64511 |
36
README.md
36
README.md
@@ -1,27 +1,25 @@
|
||||
## 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").
|
||||
## Zabbix 6.4.x testing ##
|
||||
_(2023/06/29)_
|
||||
|
||||
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.
|
||||
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.
|
||||
|
||||
## mailGraph (v1.31)
|
||||
Zabbix Media module and scripts for sending e-mail alerts with graphs.
|
||||
## Zabbix 6.2.x testing ##
|
||||
_(2022/10/10)_
|
||||
|
||||
Testing completed. No immediate issues found.
|
||||
|
||||
_(2022/08/22)_
|
||||
|
||||
Zabbix 6.2.x testing is in progress. Once completed the next effort will be to deliver on automatic configuration detection and automatic updates.
|
||||
|
||||
## IMPORTANT NOTE ##
|
||||
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**
|
||||
**Please use the Wiki for information on how to install, configure and use MailGraph in Zabbix**
|
||||
|
||||
## UPGRADE NOTES
|
||||
### 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
|
||||
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!
|
||||
The below message is just an example of what MailGraph is capable of. The template engine ("Twig") allows for a fully customized message creation to your needs! It is also possible to add more Zabbix fields. If you need additional fields just raise an issue ticket and ask and I'll see what I can do.
|
||||
|
||||
[](images/Example-mail-message-v122.png)
|
||||
|
||||
7
javascript/READ.ME
Normal file
7
javascript/READ.ME
Normal file
@@ -0,0 +1,7 @@
|
||||
As per the details captured in issue #32 the javascript code portion of this Media Type has changed due to a platform/library change at Zabbix side.
|
||||
A distinction is now made for the following Zabbix versions:
|
||||
|
||||
Zabbix <6.2 - use "zabbix.mailGraph.js"
|
||||
Zabbix >=6.2 - <coming soon>
|
||||
|
||||
Please make sure you install the right javascript in your environment!
|
||||
169
mailGraph.php
169
mailGraph.php
@@ -39,9 +39,11 @@
|
||||
// 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
|
||||
// ------------------------------------------------------------------------------------------------------
|
||||
// 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
|
||||
// ------------------------------------------------------------------------------------------------------
|
||||
//
|
||||
// (C) M.J.Oudsen, mark.oudsen@puzzl.nl
|
||||
@@ -58,14 +60,12 @@
|
||||
// 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
|
||||
// 3) Build and send mail message from template using Swift/TWIG
|
||||
// 4) Cleanup images, retention logs
|
||||
//
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// CONSTANTS
|
||||
|
||||
$cVersion = 'v1.31';
|
||||
$cVersion = 'v2.020';
|
||||
$cCRLF = chr(10).chr(13);
|
||||
$maskDateTime = 'Y-m-d H:i:s';
|
||||
$maxGraphs = 4;
|
||||
@@ -73,12 +73,11 @@
|
||||
// DEBUG SETTINGS
|
||||
// -- Should be FALSE for production level use
|
||||
|
||||
$cDebug = FALSE; // Extended debug logging mode
|
||||
$cDebug = TRUE; // Extended debug logging mode
|
||||
$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; this will ensure no error messages are passed to Zabbix which
|
||||
// otherwise will block mailGraph from functioning correctly
|
||||
// Limit server level output errors
|
||||
|
||||
error_reporting(E_ERROR | E_PARSE);
|
||||
|
||||
@@ -391,6 +390,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 ///////////////////////////////////////////////////////////////////////////////////////////
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
@@ -442,6 +475,31 @@
|
||||
if (isset($config['cli_debug'])) { $problemData['debug'] = $config['cli_debug']; }
|
||||
if (isset($config['cli_proxy'])) { $problemData['HTTPProxy'] = $config['cli_proxy']; }
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
_log('# Data passed to MailGraph main routine and used for processing'.
|
||||
@@ -450,8 +508,8 @@
|
||||
// --- 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']);
|
||||
// if (!isset($problemData['itemId'])) { echo "Missing ITEM ID?\n"; die; }
|
||||
// $p_itemId = intval($problemData['itemId']);
|
||||
|
||||
if (!isset($problemData['eventId'])) { echo "Missing EVENT ID?\n"; die; }
|
||||
$p_eventId = intval($problemData['eventId']);
|
||||
@@ -513,24 +571,18 @@
|
||||
$p_graph_match = 'any';
|
||||
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 ---
|
||||
|
||||
// Script related settings
|
||||
$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 as text)
|
||||
$z_url_image = $z_url.'images/'; // Images URL (included in plain message text)
|
||||
|
||||
// Absolute paths for processing
|
||||
// Absolute path where to store the generated images - note: script does not take care of clearing out old images!
|
||||
$z_path = getcwd().'/'; // Absolute base path on the filesystem for this url
|
||||
$z_images_path = $z_path.'images/'; // Storing images here
|
||||
$z_template_path = $z_path.'templates/'; // Expecting templates here
|
||||
$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/'; // Where logs are stored of mailGraph
|
||||
$z_images_path = $z_path.'images/';
|
||||
$z_template_path = $z_path.'templates/';
|
||||
$z_tmp_cookies = $z_path.'tmp/';
|
||||
$z_log_path = $z_path.'log/';
|
||||
|
||||
// Zabbix user (requires Super Admin access rights to access image generator script)
|
||||
$z_user = $config['zabbix_user'];
|
||||
@@ -552,9 +604,9 @@
|
||||
// Check accessibility of paths and template files //////////////////////////////////////////////////////
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
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 (should have read/write rights)?'.$cCRLF; die; }
|
||||
if (!is_writable($z_log_path)) { echo 'Log path inaccessible (should have read/write rights)?'.$cCRLF; die; }
|
||||
if (!is_writable($z_images_path)) { echo 'Image path inaccessible?'.$cCRLF; die; }
|
||||
if (!is_writable($z_tmp_cookies)) { echo 'Cookies temporary path inaccessible?'.$cCRLF; die; }
|
||||
if (!is_writable($z_log_path)) { echo 'Log path inaccessible?'.$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; }
|
||||
|
||||
@@ -587,7 +639,7 @@
|
||||
|
||||
if ($token=='')
|
||||
{
|
||||
echo 'Error logging in to Zabbix (check username/password)? ('.$z_url_api.'): '.$cCRLF;
|
||||
echo 'Error logging in to Zabbix? ('.$z_url_api.'): '.$cCRLF;
|
||||
echo var_dump($request).$cCRLF;
|
||||
echo var_dump($result).$cCRLF;
|
||||
die;
|
||||
@@ -648,10 +700,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());
|
||||
|
||||
@@ -737,6 +789,16 @@
|
||||
}
|
||||
}
|
||||
|
||||
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 ---
|
||||
// ---------------------
|
||||
@@ -793,7 +855,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?
|
||||
@@ -1038,6 +1100,7 @@
|
||||
_log('> Graphs found = '.sizeof($triggerGraphs));
|
||||
}
|
||||
|
||||
$hostGraphs = array();
|
||||
|
||||
if ($hostScreen>0)
|
||||
{
|
||||
@@ -1210,7 +1273,7 @@
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Prepare LOG related content //////////////////////////////////////////////////////////////////////////
|
||||
// Prepare HTML LOG content /////////////////////////////////////////////////////////////////////////////
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
$mailData['LOG_HTML'] = implode('</br/>',$logging);
|
||||
@@ -1223,17 +1286,16 @@
|
||||
'</body>'.$cCRLF.
|
||||
'</html>';
|
||||
|
||||
// Prepare PLAIN LOG content
|
||||
|
||||
$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['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['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;
|
||||
|
||||
@@ -1369,37 +1431,6 @@
|
||||
// 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?
|
||||
|
||||
if (($cDebug) || (isset($problemData['debug'])))
|
||||
@@ -1413,6 +1444,6 @@
|
||||
$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);
|
||||
}
|
||||
?>
|
||||
|
||||
Reference in New Issue
Block a user