mirror of
https://github.com/moudsen/mailGraph
synced 2025-10-28 08:07:39 +01:00
Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8597658670 | ||
|
|
b7e2062486 | ||
|
|
2955ffc404 | ||
|
|
2976a4af9c | ||
|
|
2882367308 | ||
|
|
8b4885038a | ||
|
|
f541719fbe | ||
|
|
9edab245d8 | ||
|
|
341bd561ab | ||
|
|
e2f657f3dd | ||
|
|
82a8fe4121 | ||
|
|
bee76b63c0 |
17
README.md
17
README.md
@@ -1,9 +1,20 @@
|
||||
## mailGraph (v1.28)
|
||||
## 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.
|
||||
|
||||
**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
|
||||
### 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 ...).
|
||||
|
||||
@@ -11,6 +22,6 @@ If you upgrade to v1.27 please be aware of the additional features for adding Ta
|
||||
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") however 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 used ("Twig") allows for a fully customized message creation to your needs!
|
||||
|
||||
[](images/Example-mail-message-v122.png)
|
||||
|
||||
@@ -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;
|
||||
@@ -138,4 +138,4 @@
|
||||
default:
|
||||
echo 'Unknown command?'.$cCRLF;
|
||||
}
|
||||
?>
|
||||
?>
|
||||
|
||||
BIN
images/Example-Zabbix-MediaType-Config.png
Normal file
BIN
images/Example-Zabbix-MediaType-Config.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 85 KiB |
121
mailGraph.php
121
mailGraph.php
@@ -36,6 +36,12 @@
|
||||
// 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
|
||||
@@ -52,13 +58,14 @@
|
||||
// 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.28';
|
||||
$cVersion = 'v1.31';
|
||||
$cCRLF = chr(10).chr(13);
|
||||
$maskDateTime = 'Y-m-d H:i:s';
|
||||
$maxGraphs = 4;
|
||||
@@ -66,10 +73,15 @@
|
||||
// DEBUG SETTINGS
|
||||
// -- Should be FALSE for production level use
|
||||
|
||||
$cDebug = TRUE; // Extended debug logging mode
|
||||
$cDebug = FALSE; // 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
|
||||
|
||||
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)
|
||||
// -- swiftmailer/swiftmailer https://swiftmailer.symfony.com/docs/introduction.html
|
||||
@@ -99,23 +111,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 +137,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
|
||||
{
|
||||
@@ -404,6 +418,10 @@
|
||||
{
|
||||
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
|
||||
@@ -423,9 +441,6 @@
|
||||
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_proxy'])) { $problemData['HTTPProxy'] = $config['cli_proxy']; }
|
||||
|
||||
// Switch on CLI log display
|
||||
$showLog = TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -465,7 +480,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
|
||||
|
||||
@@ -498,18 +513,24 @@
|
||||
$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 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_images_path = $z_path.'images/';
|
||||
$z_template_path = $z_path.'templates/';
|
||||
$z_tmp_cookies = $z_path.'tmp/';
|
||||
$z_log_path = $z_path.'log/';
|
||||
$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
|
||||
|
||||
// Zabbix user (requires Super Admin access rights to access image generator script)
|
||||
$z_user = $config['zabbix_user'];
|
||||
@@ -531,9 +552,9 @@
|
||||
// Check accessibility of paths and template files //////////////////////////////////////////////////////
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
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 (!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 (!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; }
|
||||
|
||||
@@ -564,7 +585,13 @@
|
||||
$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 (check username/password)? ('.$z_url_api.'): '.$cCRLF;
|
||||
echo var_dump($request).$cCRLF;
|
||||
echo var_dump($result).$cCRLF;
|
||||
die;
|
||||
}
|
||||
|
||||
_log('> Token = '.$token);
|
||||
|
||||
@@ -1183,7 +1210,7 @@
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Prepare HTML LOG content /////////////////////////////////////////////////////////////////////////////
|
||||
// Prepare LOG related content //////////////////////////////////////////////////////////////////////////
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
$mailData['LOG_HTML'] = implode('</br/>',$logging);
|
||||
@@ -1196,16 +1223,17 @@
|
||||
'</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;
|
||||
|
||||
@@ -1341,6 +1369,37 @@
|
||||
// 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'])))
|
||||
@@ -1354,6 +1413,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