mirror of
https://github.com/moudsen/mailGraph
synced 2025-10-28 08:07:39 +01:00
Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a04050c3bc | ||
|
|
b81414e06d | ||
|
|
accb8f40d6 | ||
|
|
bf5bbcb778 | ||
|
|
702725e5f7 | ||
|
|
0725e01bc0 | ||
|
|
a8858784a5 | ||
|
|
39ada5d6ff |
53
README.md
53
README.md
@@ -1,3 +1,37 @@
|
||||
## 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.
|
||||
|
||||
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.
|
||||
|
||||
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}`.
|
||||
|
||||
Note that all Zabbix host, item or screen related information is made available to Twig automatically.
|
||||
|
||||
Example message:
|
||||
|
||||
[](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.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
|
||||
|
||||
## mailGraph v2.11 release ##
|
||||
_(2023/07/01)_
|
||||
|
||||
@@ -23,7 +57,7 @@ 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._
|
||||
_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
|
||||
@@ -44,22 +78,7 @@ _(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.
|
||||
|
||||
## 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 ##
|
||||
## 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**
|
||||
**Please use the Wiki for information on how to install, configure and use MailGraph in Zabbix**
|
||||
|
||||
## Example message
|
||||
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)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// mailGraph v2.11
|
||||
// mailGraph v2.12
|
||||
try {
|
||||
// Pickup parameters
|
||||
params = JSON.parse(value),
|
||||
|
||||
268
mailGraph.php
268
mailGraph.php
@@ -51,6 +51,7 @@
|
||||
// 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/xx - Mark Oudsen - Replaced SwiftMailer with PHPMailer (based on AutoTLS)
|
||||
// ------------------------------------------------------------------------------------------------------
|
||||
//
|
||||
// (C) M.J.Oudsen, mark.oudsen@puzzl.nl
|
||||
@@ -59,6 +60,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
|
||||
//
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
@@ -72,15 +86,15 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// CONSTANTS
|
||||
$cVersion = 'v2.11';
|
||||
$cVersion = 'v2.12';
|
||||
$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 !!!
|
||||
|
||||
@@ -90,12 +104,17 @@
|
||||
|
||||
// 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
|
||||
@@ -258,8 +277,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');
|
||||
@@ -515,9 +538,6 @@
|
||||
// --- 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['eventId'])) { echo "Missing EVENT ID?\n"; die; }
|
||||
$p_eventId = intval($problemData['eventId']);
|
||||
|
||||
@@ -562,10 +582,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'; }
|
||||
|
||||
@@ -612,7 +628,7 @@
|
||||
}
|
||||
|
||||
// Mail sender
|
||||
$mailFrom = array($config['mail_from']=>'Zabbix Mailgraph');
|
||||
$mailFrom = $config['mail_from'];
|
||||
|
||||
// Derived variables - do not change!
|
||||
$z_server = $p_URL; // Zabbix server URL from config
|
||||
@@ -665,6 +681,23 @@
|
||||
|
||||
_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 ---
|
||||
// -----------------------------------
|
||||
@@ -1195,7 +1228,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); }
|
||||
@@ -1247,6 +1280,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];
|
||||
}
|
||||
@@ -1277,6 +1311,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'];
|
||||
@@ -1346,135 +1381,114 @@
|
||||
// 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 and recipient
|
||||
$mail->setFrom($mailFrom, 'mailGraph');
|
||||
$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'])))
|
||||
{
|
||||
_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);
|
||||
}
|
||||
|
||||
// Send message
|
||||
|
||||
$result = $mailer->send($message);
|
||||
|
||||
// Return Event TAG information for Zabbix
|
||||
|
||||
$response = array('messageId'=>$messageId);
|
||||
|
||||
if ($response=="")
|
||||
{
|
||||
_log("! Failed to send mail message");
|
||||
echo "! Failed to send mail message. Likely an issue with the recipient email address?".$cCRLF;
|
||||
} else {
|
||||
// --- Return Event TAG information for Zabbix to store with Zabbix event
|
||||
$response = array('messageId'=>$messageId);
|
||||
echo json_encode($response).$cCRLF;
|
||||
} catch (phpmailerException $e)
|
||||
{
|
||||
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());
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
@@ -1485,14 +1499,16 @@
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
411
mailGraph.xml
411
mailGraph.xml
@@ -1,198 +1,215 @@
|
||||
zabbix_export:
|
||||
version: '5.4'
|
||||
date: '2023-07-01T10:54:39Z'
|
||||
media_types:
|
||||
-
|
||||
name: MailGraph
|
||||
type: WEBHOOK
|
||||
parameters:
|
||||
-
|
||||
name: baseURL
|
||||
value: 'https://myzabbix.example.com/'
|
||||
-
|
||||
name: duration
|
||||
value: '{EVENT.DURATION}'
|
||||
-
|
||||
name: eventId
|
||||
value: '{EVENT.ID}'
|
||||
-
|
||||
name: graphHeight
|
||||
value: '120'
|
||||
-
|
||||
name: graphWidth
|
||||
value: '300'
|
||||
-
|
||||
name: HTTPProxy
|
||||
value: ''
|
||||
-
|
||||
name: infoTest
|
||||
value: Test
|
||||
-
|
||||
name: itemId
|
||||
value: '{ITEM.ID}'
|
||||
-
|
||||
name: periods
|
||||
value: '10m,4h,1d,7d'
|
||||
-
|
||||
name: periods_headers
|
||||
value: 'Last 10 minutes,Last 4 hours,Last day,Last 7 days'
|
||||
-
|
||||
name: recipient
|
||||
value: '{ALERT.SENDTO}'
|
||||
-
|
||||
name: showLegend
|
||||
value: '0'
|
||||
-
|
||||
name: subject
|
||||
value: '{{ HOST_NAME|raw }}: ({{ EVENT_SEVERITY }}) {{ EVENT_NAME|raw }}'
|
||||
-
|
||||
name: URL
|
||||
value: 'https://myzabbix.example.com/mailGraph.php'
|
||||
script: |
|
||||
// mailGraph v2.11
|
||||
try {
|
||||
// Pickup parameters
|
||||
params = JSON.parse(value),
|
||||
req = new HttpRequest(),
|
||||
fields = {},
|
||||
resp = '',
|
||||
result = { tags: {} };
|
||||
|
||||
// Set HTTP proxy if required
|
||||
if (typeof params.HTTPProxy === 'string' && params.HTTPProxy.trim() !== '') {
|
||||
req.setProxy(params.HTTPProxy);
|
||||
fields.HTTPProxy = params.HTTPProxy;
|
||||
}
|
||||
|
||||
// Declare output type
|
||||
req.addHeader('Content-Type: application/json');
|
||||
|
||||
// Must have fields
|
||||
fields.itemId = params.itemId * 1;
|
||||
fields.eventId = params.eventId * 1;
|
||||
fields.recipient = params.recipient;
|
||||
fields.baseURL = params.baseURL;
|
||||
fields.duration = params.duration * 1;
|
||||
|
||||
if (isNaN(fields.eventId)) {
|
||||
throw '[MailGraph Webhook] Invalid event ID? Integer required (use actual event ID from Zabbix!)';
|
||||
}
|
||||
|
||||
if (isNaN(fields.duration)) {
|
||||
throw '[MailGraph Webhook] Invalid duration? Integer required (set to zero if unknown)!';
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
// If blank the email address is likely incorrect
|
||||
if (resp==null) {
|
||||
throw '[MailGraph Webhook] No data received from mailGraph! Likely the email processing failed? Check mailGraph logging';
|
||||
}
|
||||
|
||||
// If there was an error, report it and stop
|
||||
if (req.getStatus() != 200) { throw JSON.parse(resp).errors[0]; }
|
||||
|
||||
// If no datas returned, report it and stop
|
||||
if (resp.charAt(0) == "!") {
|
||||
throw '[MailGraph Webhook] No data received from mailGraph! Likely the event ID provided does not exist? Check mailGraph logging';
|
||||
}
|
||||
|
||||
// We expect the message id back from the processing script
|
||||
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);
|
||||
throw 'MailGraph notification failed : '+error;
|
||||
}
|
||||
process_tags: 'YES'
|
||||
description: |
|
||||
The "URL" must point to the location of the processing script. If a proxy is required, define "HTTPProxy" for the proxy address.
|
||||
|
||||
Customization:
|
||||
- "graphWidth" and "graphWidth" can be defined for the image size
|
||||
- "showLegend" can be defined to show or hide the legend of the graph
|
||||
- "subject" can be defined for a customized subject for the mail message
|
||||
- "periods" and "periods_headers" can be defined for displaying multiple periods of the same graph, or
|
||||
- "period" and "period_header" 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
|
||||
message_templates:
|
||||
-
|
||||
event_source: TRIGGERS
|
||||
operation_mode: PROBLEM
|
||||
subject: 'Problem: {EVENT.NAME}'
|
||||
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.URL}
|
||||
|
||||
eventId: {EVENT.ID}
|
||||
TriggerId: {TRIGGER.ID}
|
||||
itemId: {ITEM.ID]
|
||||
-
|
||||
event_source: TRIGGERS
|
||||
operation_mode: RECOVERY
|
||||
subject: 'Resolved in {EVENT.DURATION}: {EVENT.RECOVERY.NAME}'
|
||||
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.URL}
|
||||
|
||||
eventId: {EVENT.ID}
|
||||
TriggerId: {TRIGGER.ID}
|
||||
itemId: {ITEM.ID]
|
||||
-
|
||||
event_source: TRIGGERS
|
||||
operation_mode: UPDATE
|
||||
subject: 'Updated problem in {EVENT.AGE}: {EVENT.NAME}'
|
||||
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}.
|
||||
|
||||
eventId: {EVENT.ID}
|
||||
TriggerId: {TRIGGER.ID}
|
||||
itemId: {ITEM.ID]
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<zabbix_export>
|
||||
<version>5.4</version>
|
||||
<date>2023-07-02T12:48:36Z</date>
|
||||
<media_types>
|
||||
<media_type>
|
||||
<name>MailGraph</name>
|
||||
<type>WEBHOOK</type>
|
||||
<parameters>
|
||||
<parameter>
|
||||
<name>baseURL</name>
|
||||
<value>https://myzabbix.example.com/</value>
|
||||
</parameter>
|
||||
<parameter>
|
||||
<name>duration</name>
|
||||
<value>{EVENT.DURATION}</value>
|
||||
</parameter>
|
||||
<parameter>
|
||||
<name>eventId</name>
|
||||
<value>{EVENT.ID}</value>
|
||||
</parameter>
|
||||
<parameter>
|
||||
<name>graphHeight</name>
|
||||
<value>120</value>
|
||||
</parameter>
|
||||
<parameter>
|
||||
<name>graphWidth</name>
|
||||
<value>300</value>
|
||||
</parameter>
|
||||
<parameter>
|
||||
<name>HTTPProxy</name>
|
||||
<value/>
|
||||
</parameter>
|
||||
<parameter>
|
||||
<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>
|
||||
</parameter>
|
||||
<parameter>
|
||||
<name>periods_headers</name>
|
||||
<value>Last 10 minutes,Last 4 hours,Last day,Last 7 days</value>
|
||||
</parameter>
|
||||
<parameter>
|
||||
<name>recipient</name>
|
||||
<value>{ALERT.SENDTO}</value>
|
||||
</parameter>
|
||||
<parameter>
|
||||
<name>showLegend</name>
|
||||
<value>0</value>
|
||||
</parameter>
|
||||
<parameter>
|
||||
<name>subject</name>
|
||||
<value>{{ HOST_NAME|raw }}: ({{ EVENT_SEVERITY }}) {{ EVENT_NAME|raw }}</value>
|
||||
</parameter>
|
||||
<parameter>
|
||||
<name>URL</name>
|
||||
<value>https://myzabbix.example.com/mailGraph.php</value>
|
||||
</parameter>
|
||||
</parameters>
|
||||
<script>// mailGraph v2.12
|
||||
try {
|
||||
// Pickup parameters
|
||||
params = JSON.parse(value),
|
||||
req = new HttpRequest(),
|
||||
fields = {},
|
||||
resp = '',
|
||||
result = { tags: {} };
|
||||
|
||||
// Set HTTP proxy if required
|
||||
if (typeof params.HTTPProxy === 'string' && params.HTTPProxy.trim() !== '') {
|
||||
req.setProxy(params.HTTPProxy);
|
||||
fields.HTTPProxy = params.HTTPProxy;
|
||||
}
|
||||
|
||||
// Declare output type
|
||||
req.addHeader('Content-Type: application/json');
|
||||
|
||||
// Must have fields
|
||||
fields.itemId = params.itemId * 1;
|
||||
fields.eventId = params.eventId * 1;
|
||||
fields.recipient = params.recipient;
|
||||
fields.baseURL = params.baseURL;
|
||||
fields.duration = params.duration * 1;
|
||||
|
||||
if (isNaN(fields.eventId)) {
|
||||
throw '[MailGraph Webhook] Invalid event ID? Integer required (use actual event ID from Zabbix!)';
|
||||
}
|
||||
|
||||
if (isNaN(fields.duration)) {
|
||||
throw '[MailGraph Webhook] Invalid duration? Integer required (set to zero if unknown)!';
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
// If blank the email address is likely incorrect
|
||||
if (resp==null) {
|
||||
throw '[MailGraph Webhook] No data received from mailGraph! Likely the email processing failed? Check mailGraph logging';
|
||||
}
|
||||
|
||||
// If there was an error, report it and stop
|
||||
if (req.getStatus() != 200) { throw JSON.parse(resp).errors[0]; }
|
||||
|
||||
// If no datas returned, report it and stop
|
||||
if (resp.charAt(0) == "!") {
|
||||
throw '[MailGraph Webhook] No data received from mailGraph! Likely the event ID provided does not exist? Check mailGraph logging';
|
||||
}
|
||||
|
||||
// We expect the message id back from the processing script
|
||||
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);
|
||||
throw 'MailGraph notification failed : '+error;
|
||||
}</script>
|
||||
<process_tags>YES</process_tags>
|
||||
<description>The "URL" must point to the location of the processing script. If a proxy is required, define "HTTPProxy" for the proxy address.
|
||||
|
||||
Customization:
|
||||
- "graphWidth" and "graphWidth" can be defined for the image size
|
||||
- "showLegend" can be defined to show or hide the legend of the graph
|
||||
- "subject" can be defined for a customized subject for the mail message
|
||||
- "periods" and "periods_headers" can be defined for displaying multiple periods of the same graph, or
|
||||
- "period" and "period_header" 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}
|
||||
Problem name: {EVENT.NAME}
|
||||
Host: {HOST.NAME}
|
||||
Severity: {EVENT.SEVERITY}
|
||||
Operational data: {EVENT.OPDATA}
|
||||
Original problem ID: {EVENT.ID}
|
||||
{TRIGGER.URL}
|
||||
|
||||
eventId: {EVENT.ID}
|
||||
TriggerId: {TRIGGER.ID}
|
||||
itemId: {ITEM.ID]</message>
|
||||
</message_template>
|
||||
<message_template>
|
||||
<event_source>TRIGGERS</event_source>
|
||||
<operation_mode>RECOVERY</operation_mode>
|
||||
<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.URL}
|
||||
|
||||
eventId: {EVENT.ID}
|
||||
TriggerId: {TRIGGER.ID}
|
||||
itemId: {ITEM.ID]</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}.
|
||||
{EVENT.UPDATE.MESSAGE}
|
||||
|
||||
Current problem status is {EVENT.STATUS}, age is {EVENT.AGE}, acknowledged: {EVENT.ACK.STATUS}.
|
||||
|
||||
eventId: {EVENT.ID}
|
||||
TriggerId: {TRIGGER.ID}
|
||||
itemId: {ITEM.ID]</message>
|
||||
</message_template>
|
||||
</message_templates>
|
||||
</media_type>
|
||||
</media_types>
|
||||
</zabbix_export>
|
||||
|
||||
Reference in New Issue
Block a user