Added ability to embed multiple periods of the same graph (v1.22)

This commit is contained in:
Mark Oudsen 2021-03-10 19:57:42 +01:00
parent 7f6d5ccd03
commit e262f21dc8
7 changed files with 204 additions and 33 deletions

View File

@ -6,17 +6,23 @@
"cli_eventValue": 0,
"cli_duration": 0,
"cli_recipient": "recipient@domain.com",
"cli_subject": "[CLI TEST] {{ HOST_NAME|raw }}: ({{ EVENT_SEVERITY }}) {{ EVENT_NAME|raw }}",
"cli_subject": "[TEST] {{ HOST_NAME|raw }}: ({{ EVENT_SEVERITY }}) {{ EVENT_NAME|raw }}",
"cli_baseURL": "https:\/\/domain.com\/zabbix\/",
"cli_period": "30m",
"cli_period_header": "Last 30 minutes",
"cli_debug": 1,
"zabbix_user": "alogicalusername",
"zabbix_user_pwd": "astrongpassword",
"zabbix_api_user": "alogicalusername",
"zabbix_api_pwd": "astrongpassword",
"mail_from": "sender@domain.com",
"subject": "{{ HOST_NAME|raw }}: ({{ EVENT_SEVERITY }}) {{ EVENT_NAME|raw }}",
"smtp_server": "localhost",
"smtp_port": 25,
"smtp_transport": "none",
"smtp_strict": "yes",
"graph_match": "any",
"period": "2d"
"period": "20m",
"period_header": "Last 20 minutes",
"debug": 0
}

View File

@ -0,0 +1,28 @@
{
"script_baseurl": "https:\/\/domain.com\/",
"cli_itemId": 0,
"cli_triggerId": 0,
"cli_eventId": 0,
"cli_eventValue": 0,
"cli_duration": 0,
"cli_recipient": "recipient@domain.com",
"cli_subject": "[TEST] {{ HOST_NAME|raw }}: ({{ EVENT_SEVERITY }}) {{ EVENT_NAME|raw }}",
"cli_baseURL": "https:\/\/domain.com\/zabbix\/",
"cli_periods": "10m,4h,2d,7d",
"cli_periods_headers": "Last 10 minutes,Last 4 hours,Last 2 days,Last 7 days",
"cli_debug": 1,
"zabbix_user": "alogicalusername",
"zabbix_user_pwd": "astrongpassword",
"zabbix_api_user": "alogicalusername",
"zabbix_api_pwd": "astrongpassword",
"mail_from": "sender@domain.com",
"subject": "{{ HOST_NAME|raw }}: ({{ EVENT_SEVERITY }}) {{ EVENT_NAME|raw }}",
"smtp_server": "localhost",
"smtp_port": 25,
"smtp_transport": "none",
"smtp_strict": "yes",
"graph_match": "any",
"periods": "10m,4h,2d,7d",
"periods_headers": "Last 10 minutes,Last 4 hours,Last 2 days,Last 7 days",
"debug": 0
}

View File

@ -26,6 +26,9 @@ try {
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) {

View File

@ -27,6 +27,7 @@
// 1.20 2021/03/07 - Mark Oudsen - Production level version - leaving BETA from here on ...
// 1.21 2021/03/09 - Mark Oudsen - Reverted graph.get code back to original code as it was not a bug but
// a wrongly typed requested (should be ARRAY, not comma separated)!
// 1.22 2021/03/10 - Mark Oudsen - Added ability to embed multiple periods (1-4) of the same graph
// ------------------------------------------------------------------------------------------------------
//
// (C) M.J.Oudsen, mark.oudsen@puzzl.nl
@ -41,7 +42,7 @@
// MAIN SEQUENCE
// -------------
// 1) Fetch trigger, item, host, graph, event information via Zabbix API via CURL
// 2) Fetch Graph 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
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -49,7 +50,7 @@
// CONSTANTS
$cVersion = 'v1.21';
$cVersion = 'v1.22';
$cCRLF = chr(10).chr(13);
$maskDateTime = 'Y-m-d H:i:s';
@ -156,7 +157,7 @@
// Cookie and image names
$filename_cookie = $z_tmp_cookies ."zabbix_cookie_" .$graphid . "." .$thisTime. ".txt";
$filename = "zabbix_graph_" .$graphid . "." . $thisTime . ".png";
$filename = "zabbix_graph_" .$graphid . "." . $thisTime . "-" . $period . ".png";
$image_name = $z_images_path . $filename;
// Configure CURL
@ -333,6 +334,7 @@
// Assumes that config.json file has the correct information
// MANDATORY
$problemData['itemId'] = $config['cli_itemId'];
$problemData['triggerId'] = $config['cli_triggerId'];
$problemData['eventId'] = $config['cli_eventId'];
@ -343,12 +345,18 @@
$problemData['subject'] = $config['cli_subject'];
$problemData['period'] = $config['cli_period'];
// OPTIONAL
if (isset($config['cli_period_header'])) { $problemData['period_header'] = $config['cli_period_header']; }
if (isset($config['cli_periods'])) { $problemData['periods'] = $config['cli_periods']; }
if (isset($config['cli_periods_headers'])) { $problemData['periods_headers'] = $config['cli_periods_headers']; }
if (isset($config['cli_debug'])) { $problemData['debug'] = $config['cli_debug']; }
// Switch on CLI log display
$showLog = TRUE;
}
}
_log('# Data passed from Zabbix'.$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 ---
// FROM POST OR CLI DATA
@ -516,8 +524,23 @@
switch ($aTag['tag'])
{
case 'mailGraph.period':
$p_period = $aTag['value'];
_log('+ Graph display period override = '.$p_period);
$problemData['period'] = $aTag['value'];
_log('+ Graph display period override = '.$problemData['period']);
break;
case 'mailGraph.period_header':
$problemData['period_header'] = $aTag['value'];
_log('+ Graph display period header override = '.$problemData['period_header']);
break;
case 'mailGraph.periods':
$problemData['periods'] = $aTag['value'];
_log('+ Graph display periods override = '.$problemData['periods']);
break;
case 'mailGraph.periods_headers':
$problemData['periods_headers'] = $aTag['value'];
_log('+ Graph display periods headers override = '.$problemData['periods_headers']);
break;
case 'mailGraph.graph':
@ -539,6 +562,11 @@
$p_graphHeight = intval($aTag['value']);
_log('+ Graph height override = '.$$p_graphHeight);
break;
case 'mailGraph.debug':
$problemData['debug'] = 1;
_log('+ Mail debug log enabled');
break;
}
}
@ -749,7 +777,49 @@
// Fetch Graph //////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////
$graphFile = '';
// Determine number of periods for this graph
$p_periods = array();
$p_periods_headers = array();
if (isset($problemData['periods']))
{
// Multiple periods mode selected
_log('# Multi period graph mode selected');
$p_periods = explode(',',$problemData['periods']);
// If invalid, replace with single graph item
if (sizeof($p_periods)==0) { $p_periods[] = $p_period; }
// --- Determine headers
if (isset($problemData['periods_headers'])) { $p_periods_headers = explode(',',$problemData['periods_headers']); }
// If no headers specified, simply copy the period information
if (sizeof($p_periods_headers)==0) { $p_periods_headers = $p_periods; }
}
else
{
// Single period mode selected
$p_periods[] = $p_period;
if (isset($problemData['period_header']))
{
$p_periods_headers[] = $problemData['period_header'];
}
else
{
$p_periods_headers[] = $p_period;
}
}
while (sizeof($p_periods)>4) { array_pop($p_periods); }
while (sizeof($p_periods_headers)>4) { array_pop($p_periods_headers); }
$graphFiles = array();
$graphURL = '';
// If we have any matching graph, make the embedding information available
@ -784,14 +854,19 @@
_log('# Adding '.strtoupper($theType).' graph #'.$mailData['GRAPH_ID']);
foreach($p_periods as $aKey=>$aPeriod)
{
$graphFile = GraphImageById($mailData['GRAPH_ID'],
$p_graphWidth,$p_graphHeight,
$theGraph['graphtype'],
$p_showLegend,$p_period);
$p_showLegend,$aPeriod);
_log('> Filename = '.$graphFile);
$graphFiles[] = $graphFile;
$mailData['GRAPH_URL'.($aKey+1)] = $z_url_image . $graphFile;
$mailData['GRAPH_HEADER'.($aKey+1)] = $p_periods_headers[$aKey];
}
$mailData['GRAPH_URL'] = $z_url_image . $graphFile;
$mailData['GRAPH_ZABBIXLINK'] = $z_server.'graphs.php?form=update&graphid='.$mailData['GRAPH_ID'];
}
@ -889,12 +964,18 @@
$twig = new \Twig\Environment($loader);
if ($graphFile!='')
if (sizeof($graphFiles)>0)
{
// Embed the image
$mailData['GRAPH_CID'] = $message->embed(Swift_Image::fromPath($z_images_path.$graphFile));
// Embed the image(s)
$graphReference = 1;
foreach($graphFiles as $graphFile)
{
$mailData['GRAPH_CID'.$graphReference++] = $message->embed(Swift_Image::fromPath($z_images_path.$graphFile));
_log('> Embedded graph image '.$z_images_path.$graphFile);
}
}
$bodyHTML = $twig->render('html', $mailData);
$bodyPlain = $twig->render('plain', $mailData);
@ -908,7 +989,7 @@
->setBody($bodyHTML, 'text/html')
->addPart($bodyPlain, 'text/plain');
if ($cDebugMail)
if (($cDebugMail) || (isset($problemData['debug'])))
{
_log('# Attaching logs to mail message');
@ -923,14 +1004,14 @@
$result = $mailer->send($message);
// Return TAG information
// Return Event TAG information for Zabbix
$response = array('messageId.mailGraph'=>$messageId);
echo json_encode($response).$cCRLF;
// Store log?
if ($cDebug)
if (($cDebug) || (isset($problemData['debug'])))
{
unset($mailData['LOG_HTML']);
unset($mailData['LOG_PLAIN']);

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<zabbix_export>
<version>5.0</version>
<date>2021-03-05T10:51:39Z</date>
<date>2021-03-10T18:50:24Z</date>
<media_types>
<media_type>
<name>MailGraph</name>
@ -35,14 +35,18 @@
<name>HTTPProxy</name>
<value/>
</parameter>
<parameter>
<name>infoGiveMeAName</name>
<value>A generic value you want to pass to MailGraph</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>
@ -92,6 +96,9 @@
if (typeof params.graphHeight === 'string') { fields.graphHeight = params.graphHeight; }&#13;
if (typeof params.subject === 'string') { fields.subject = params.subject; }&#13;
if (typeof params.showLegend === 'string') { fields.showLegend = params.showLegend; }&#13;
if (typeof params.periods === 'string') { fields.periods = params.periods; }&#13;
if (typeof params.periods_headers === 'string') { fields.periods_headers = params.periods_headers; }&#13;
if (typeof params.debug === 'string') { fields.debug = params.debug; }&#13;
&#13;
// Add generic fields&#13;
Object.keys(params).forEach(function(key) {&#13;

View File

@ -43,6 +43,12 @@
text-decoration: none;
color: #010059;
}
hr {
border: none;
height: 1px;
color: #333; /* old IE */
background-color: #333;
}
.MsgBody {
width: 100%;
}
@ -68,6 +74,10 @@
font-size: 16px;
font-weight: bold;
}
.graphHeader {
font-size: 14px;
font-weight: bold;
}
.content {
font-size: 14px;
}
@ -104,10 +114,46 @@
</table>
</td>
</tr>
{% if GRAPH_CID|length > 0 %}
{% if GRAPH_CID1|length > 0 %}
<tr>
<div align=justify>
<td align="center"><img id="mainimage" border=0 style="width: 100%; max-width: 790px" alt="Zabbix Graph" src="{{ GRAPH_CID }}" /></td>
<td align="center">
<span class="graphHeader">{{ GRAPH_HEADER1 }}</span><br/>
<img id="mainimage" border=0 style="width: 100%; max-width: 790px" alt="Zabbix Graph" src="{{ GRAPH_CID1 }}" />
</td>
</div>
</tr>
{% endif %}
{% if GRAPH_CID2|length > 0 %}
<tr>
<div align=justify>
<td align="center">
<hr/>
<span class="graphHeader">{{ GRAPH_HEADER2 }}</span><br/>
<img id="mainimage" border=0 style="width: 100%; max-width: 790px" alt="Zabbix Graph" src="{{ GRAPH_CID2 }}" />
</td>
</div>
</tr>
{% endif %}
{% if GRAPH_CID3|length > 0 %}
<tr>
<div align=justify>
<td align="center">
<hr/>
<span class="graphHeader">{{ GRAPH_HEADER3 }}</span><br/>
<img id="mainimage" border=0 style="width: 100%; max-width: 790px" alt="Zabbix Graph" src="{{ GRAPH_CID3 }}" />
</td>
</div>
</tr>
{% endif %}
{% if GRAPH_CID4|length > 0 %}
<tr>
<div align=justify>
<td align="center">
<hr/>
<span class="graphHeader">{{ GRAPH_HEADER4 }}</span><br/>
<img id="mainimage" border=0 style="width: 100%; max-width: 790px" alt="Zabbix Graph" src="{{ GRAPH_CID4 }}" />
</td>
</div>
</tr>
{% endif %}

View File

@ -3,10 +3,10 @@ EVENT INFORMATION #{{ EVENT_ID }}
Severity: {{ EVENT_SEVERITY }}
Description: {{ EVENT_NAME }}
{% if GRAPH_CID|length > 0 %}
GRAPH #{{ GRAPH_ID }}
{{ GRAPH_URL }}
This graph will remain available for approx. the next 2 weeks only; use HTML reader after this period to view the graph.
{% if GRAPH_CID1|length > 0 %}
GRAPH #{{ GRAPH_ID1 }}
{{ GRAPH_URL1 }}
This graph will remain available for approx. the next 2 weeks only; use HTML reader after this period to view the graph that is embedded in the attached HTML version.
{% endif %}
TRIGGER #{{ TRIGGER_ID }}