2010-09-03 18:26:59 +00:00
#!/usr/bin/env php
2007-04-03 14:10:23 +00:00
< ? php
2015-07-13 20:10:26 +02:00
/*
* Copyright ( C ) 2014 Daniel Preussker < f0o @ devilcode . org >
2014-12-15 11:10:26 +00:00
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
2015-07-13 20:10:26 +02:00
*
2014-12-15 11:10:26 +00:00
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
2015-07-13 20:10:26 +02:00
*
2014-12-15 11:10:26 +00:00
* You should have received a copy of the GNU General Public License
2015-07-13 20:10:26 +02:00
* along with this program . If not , see < http :// www . gnu . org / licenses />.
*/
2007-04-03 14:10:23 +00:00
2012-05-09 10:01:42 +00:00
/**
2014-12-15 11:10:26 +00:00
* Alerts Cronjob
* @ author f0o < f0o @ devilcode . org >
* @ copyright 2014 f0o , LibreNMS
* @ license GPL
* @ package LibreNMS
* @ subpackage Alerts
2012-05-09 10:01:42 +00:00
*/
2015-07-13 20:10:26 +02:00
require_once 'includes/defaults.inc.php' ;
require_once 'config.php' ;
2014-12-22 18:17:43 +00:00
2016-01-17 14:39:17 +00:00
$options = getopt ( 'd::' );
2014-12-22 18:17:43 +00:00
$lock = false ;
2015-07-13 20:10:26 +02:00
if ( file_exists ( $config [ 'install_dir' ] . '/.alerts.lock' )) {
$pids = explode ( " \n " , trim ( `ps -e | grep php | awk '{print $1}'` ));
$lpid = trim ( file_get_contents ( $config [ 'install_dir' ] . '/.alerts.lock' ));
if ( in_array ( $lpid , $pids )) {
$lock = true ;
}
2014-12-22 18:17:43 +00:00
}
2015-07-13 20:10:26 +02:00
if ( $lock === true ) {
exit ( 1 );
}
else {
file_put_contents ( $config [ 'install_dir' ] . '/.alerts.lock' , getmypid ());
2014-12-22 18:17:43 +00:00
}
2015-07-13 20:10:26 +02:00
require_once $config [ 'install_dir' ] . '/includes/definitions.inc.php' ;
require_once $config [ 'install_dir' ] . '/includes/functions.php' ;
require_once $config [ 'install_dir' ] . '/includes/alerts.inc.php' ;
2016-01-17 14:39:17 +00:00
if ( isset ( $options [ 'd' ])) {
echo " DEBUG! \n " ;
$debug = true ;
ini_set ( 'display_errors' , 1 );
ini_set ( 'display_startup_errors' , 1 );
ini_set ( 'log_errors' , 1 );
ini_set ( 'error_reporting' , 1 );
}
else {
$debug = false ;
// ini_set('display_errors', 0);
ini_set ( 'display_startup_errors' , 0 );
ini_set ( 'log_errors' , 0 );
// ini_set('error_reporting', 0);
}
2015-11-04 20:29:02 +00:00
if ( ! defined ( 'TEST' ) && $config [ 'alert' ][ 'disable' ] != 'true' ) {
2015-07-13 20:10:26 +02:00
echo 'Start: ' . date ( 'r' ) . " \r \n " ;
echo " RunFollowUp(): \r \n " ;
RunFollowUp ();
echo " RunAlerts(): \r \n " ;
RunAlerts ();
echo " RunAcks(): \r \n " ;
RunAcks ();
echo 'End : ' . date ( 'r' ) . " \r \n " ;
2015-01-25 21:39:33 +00:00
}
2007-04-03 14:10:23 +00:00
2015-07-13 20:10:26 +02:00
unlink ( $config [ 'install_dir' ] . '/.alerts.lock' );
2014-12-22 18:17:43 +00:00
2015-04-04 17:13:22 +00:00
/**
* Re - Validate Rule - Mappings
2015-07-13 20:10:26 +02:00
* @ param integer $device Device - ID
* @ param integer $rule Rule - ID
2015-04-04 17:13:22 +00:00
* @ return boolean
*/
2015-07-13 20:10:26 +02:00
function IsRuleValid ( $device , $rule ) {
global $rulescache ;
if ( empty ( $rulescache [ $device ]) || ! isset ( $rulescache [ $device ])) {
foreach ( GetRules ( $device ) as $chk ) {
$rulescache [ $device ][ $chk [ 'id' ]] = true ;
}
}
if ( $rulescache [ $device ][ $rule ] === true ) {
return true ;
}
return false ;
} //end IsRuleValid()
2015-04-04 17:13:22 +00:00
2015-05-14 14:37:59 +00:00
/**
* Issue Alert - Object
* @ param array $alert
* @ return boolean
*/
function IssueAlert ( $alert ) {
2015-07-13 20:10:26 +02:00
global $config ;
if ( dbFetchCell ( 'SELECT attrib_value FROM devices_attribs WHERE attrib_type = "disable_notify" && device_id = ?' , array ( $alert [ 'device_id' ])) == '1' ) {
return true ;
}
if ( $config [ 'alert' ][ 'fixed-contacts' ] == false ) {
$alert [ 'details' ][ 'contacts' ] = GetContacts ( $alert [ 'details' ][ 'rule' ]);
}
$obj = DescribeAlert ( $alert );
if ( is_array ( $obj )) {
echo 'Issuing Alert-UID #' . $alert [ 'id' ] . '/' . $alert [ 'state' ] . ': ' ;
if ( ! empty ( $config [ 'alert' ][ 'transports' ])) {
ExtTransports ( $obj );
}
echo " \r \n " ;
}
return true ;
} //end IssueAlert()
2015-05-14 14:37:59 +00:00
/**
* Issue ACK notification
* @ return void
*/
function RunAcks () {
2015-07-13 20:10:26 +02:00
foreach ( dbFetchRows ( 'SELECT alerts.device_id, alerts.rule_id, alerts.state FROM alerts WHERE alerts.state = 2 && alerts.open = 1' ) as $alert ) {
$tmp = array (
$alert [ 'rule_id' ],
$alert [ 'device_id' ],
);
$alert = dbFetchRow ( 'SELECT alert_log.id,alert_log.rule_id,alert_log.device_id,alert_log.state,alert_log.details,alert_log.time_logged,alert_rules.rule,alert_rules.severity,alert_rules.extra,alert_rules.name FROM alert_log,alert_rules WHERE alert_log.rule_id = alert_rules.id && alert_log.device_id = ? && alert_log.rule_id = ? && alert_rules.disabled = 0 ORDER BY alert_log.id DESC LIMIT 1' , array ( $alert [ 'device_id' ], $alert [ 'rule_id' ]));
if ( empty ( $alert [ 'rule' ]) || ! IsRuleValid ( $tmp [ 1 ], $tmp [ 0 ])) {
// Alert-Rule does not exist anymore, let's remove the alert-state.
echo 'Stale-Rule: #' . $tmp [ 0 ] . '/' . $tmp [ 1 ] . " \r \n " ;
dbDelete ( 'alerts' , 'rule_id = ? && device_id = ?' , array ( $tmp [ 0 ], $tmp [ 1 ]));
continue ;
}
$alert [ 'details' ] = json_decode ( gzuncompress ( $alert [ 'details' ]), true );
$alert [ 'state' ] = 2 ;
IssueAlert ( $alert );
dbUpdate ( array ( 'open' => 0 ), 'alerts' , 'rule_id = ? && device_id = ?' , array ( $alert [ 'rule_id' ], $alert [ 'device_id' ]));
}
} //end RunAcks()
2015-05-14 14:37:59 +00:00
2015-01-25 21:39:33 +00:00
/**
* Run Follow - Up alerts
* @ return void
*/
function RunFollowUp () {
2015-07-13 20:10:26 +02:00
global $config ;
foreach ( dbFetchRows ( 'SELECT alerts.device_id, alerts.rule_id, alerts.state FROM alerts WHERE alerts.state != 2 && alerts.state > 0 && alerts.open = 0' ) as $alert ) {
$tmp = array (
$alert [ 'rule_id' ],
$alert [ 'device_id' ],
);
$alert = dbFetchRow ( 'SELECT alert_log.id,alert_log.rule_id,alert_log.device_id,alert_log.state,alert_log.details,alert_log.time_logged,alert_rules.rule,alert_rules.severity,alert_rules.extra,alert_rules.name FROM alert_log,alert_rules WHERE alert_log.rule_id = alert_rules.id && alert_log.device_id = ? && alert_log.rule_id = ? && alert_rules.disabled = 0 ORDER BY alert_log.id DESC LIMIT 1' , array ( $alert [ 'device_id' ], $alert [ 'rule_id' ]));
if ( empty ( $alert [ 'rule' ]) || ! IsRuleValid ( $tmp [ 1 ], $tmp [ 0 ])) {
// Alert-Rule does not exist anymore, let's remove the alert-state.
echo 'Stale-Rule: #' . $tmp [ 0 ] . '/' . $tmp [ 1 ] . " \r \n " ;
dbDelete ( 'alerts' , 'rule_id = ? && device_id = ?' , array ( $tmp [ 0 ], $tmp [ 1 ]));
continue ;
}
$alert [ 'details' ] = json_decode ( gzuncompress ( $alert [ 'details' ]), true );
2016-02-17 13:59:58 +00:00
$rextra = json_decode ( $alert [ 'extra' ], true );
2015-07-13 20:10:26 +02:00
if ( $rextra [ 'invert' ]) {
continue ;
}
$chk = dbFetchRows ( GenSQL ( $alert [ 'rule' ]), array ( $alert [ 'device_id' ]));
$o = sizeof ( $alert [ 'details' ][ 'rule' ]);
$n = sizeof ( $chk );
$ret = 'Alert #' . $alert [ 'id' ];
$state = 0 ;
if ( $n > $o ) {
$ret .= ' Worsens' ;
$state = 3 ;
2016-03-02 16:53:53 +00:00
$alert [ 'details' ][ 'diff' ] = array_diff ( $chk , $alert [ 'details' ][ 'rule' ]);
2015-07-13 20:10:26 +02:00
}
2016-03-04 16:25:51 +00:00
elseif ( $n < $o ) {
2015-07-13 20:10:26 +02:00
$ret .= ' Betters' ;
$state = 4 ;
2016-03-02 16:53:53 +00:00
$alert [ 'details' ][ 'diff' ] = array_diff ( $alert [ 'details' ][ 'rule' ], $chk );
2015-07-13 20:10:26 +02:00
}
2015-08-27 14:05:47 +01:00
if ( $state > 0 && $n > 0 ) {
2015-07-13 20:10:26 +02:00
$alert [ 'details' ][ 'rule' ] = $chk ;
if ( dbInsert ( array ( 'state' => $state , 'device_id' => $alert [ 'device_id' ], 'rule_id' => $alert [ 'rule_id' ], 'details' => gzcompress ( json_encode ( $alert [ 'details' ]), 9 )), 'alert_log' )) {
dbUpdate ( array ( 'state' => $state , 'open' => 1 , 'alerted' => 1 ), 'alerts' , 'rule_id = ? && device_id = ?' , array ( $alert [ 'rule_id' ], $alert [ 'device_id' ]));
}
echo $ret . ' (' . $o . '/' . $n . " ) \r \n " ;
}
} //end foreach
} //end RunFollowUp()
2015-01-25 21:39:33 +00:00
2014-12-15 11:10:26 +00:00
/**
* Run all alerts
* @ return void
*/
function RunAlerts () {
2015-07-13 20:10:26 +02:00
global $config ;
foreach ( dbFetchRows ( 'SELECT alerts.device_id, alerts.rule_id, alerts.state FROM alerts WHERE alerts.state != 2 && alerts.open = 1' ) as $alert ) {
$tmp = array (
$alert [ 'rule_id' ],
$alert [ 'device_id' ],
);
$alert = dbFetchRow ( 'SELECT alert_log.id,alert_log.rule_id,alert_log.device_id,alert_log.state,alert_log.details,alert_log.time_logged,alert_rules.rule,alert_rules.severity,alert_rules.extra,alert_rules.name FROM alert_log,alert_rules WHERE alert_log.rule_id = alert_rules.id && alert_log.device_id = ? && alert_log.rule_id = ? && alert_rules.disabled = 0 ORDER BY alert_log.id DESC LIMIT 1' , array ( $alert [ 'device_id' ], $alert [ 'rule_id' ]));
if ( empty ( $alert [ 'rule_id' ]) || ! IsRuleValid ( $tmp [ 1 ], $tmp [ 0 ])) {
echo 'Stale-Rule: #' . $tmp [ 0 ] . '/' . $tmp [ 1 ] . " \r \n " ;
// Alert-Rule does not exist anymore, let's remove the alert-state.
dbDelete ( 'alerts' , 'rule_id = ? && device_id = ?' , array ( $tmp [ 0 ], $tmp [ 1 ]));
continue ;
}
$alert [ 'details' ] = json_decode ( gzuncompress ( $alert [ 'details' ]), true );
$noiss = false ;
$noacc = false ;
$updet = false ;
2016-02-17 13:59:58 +00:00
$rextra = json_decode ( $alert [ 'extra' ], true );
2015-07-13 20:10:26 +02:00
$chk = dbFetchRow ( 'SELECT alerts.alerted,devices.ignore,devices.disabled FROM alerts,devices WHERE alerts.device_id = ? && devices.device_id = alerts.device_id && alerts.rule_id = ?' , array ( $alert [ 'device_id' ], $alert [ 'rule_id' ]));
if ( $chk [ 'alerted' ] == $alert [ 'state' ]) {
$noiss = true ;
}
if ( ! empty ( $rextra [ 'count' ]) && empty ( $rextra [ 'interval' ])) {
// This check below is for compat-reasons
if ( ! empty ( $rextra [ 'delay' ])) {
2015-09-03 15:15:17 +01:00
if (( time () - strtotime ( $alert [ 'time_logged' ]) + $config [ 'alert' ][ 'tolerance_window' ]) < $rextra [ 'delay' ] || ( ! empty ( $alert [ 'details' ][ 'delay' ]) && ( time () - $alert [ 'details' ][ 'delay' ] + $config [ 'alert' ][ 'tolerance_window' ]) < $rextra [ 'delay' ])) {
2015-07-13 20:10:26 +02:00
continue ;
}
else {
$alert [ 'details' ][ 'delay' ] = time ();
$updet = true ;
}
}
if ( $alert [ 'state' ] == 1 && ! empty ( $rextra [ 'count' ]) && ( $rextra [ 'count' ] == - 1 || $alert [ 'details' ][ 'count' ] ++ < $rextra [ 'count' ])) {
if ( $alert [ 'details' ][ 'count' ] < $rextra [ 'count' ]) {
$noacc = true ;
}
$updet = true ;
$noiss = false ;
}
}
else {
// This is the new way
2015-09-03 15:15:17 +01:00
if ( ! empty ( $rextra [ 'delay' ]) && ( time () - strtotime ( $alert [ 'time_logged' ]) + $config [ 'alert' ][ 'tolerance_window' ]) < $rextra [ 'delay' ]) {
2015-07-13 20:10:26 +02:00
continue ;
}
if ( ! empty ( $rextra [ 'interval' ])) {
2015-09-03 15:15:17 +01:00
if ( ! empty ( $alert [ 'details' ][ 'interval' ]) && ( time () - $alert [ 'details' ][ 'interval' ] + $config [ 'alert' ][ 'tolerance_window' ]) < $rextra [ 'interval' ]) {
2015-07-13 20:10:26 +02:00
continue ;
}
else {
$alert [ 'details' ][ 'interval' ] = time ();
$updet = true ;
}
}
if ( $alert [ 'state' ] == 1 && ! empty ( $rextra [ 'count' ]) && ( $rextra [ 'count' ] == - 1 || $alert [ 'details' ][ 'count' ] ++ < $rextra [ 'count' ])) {
if ( $alert [ 'details' ][ 'count' ] < $rextra [ 'count' ]) {
$noacc = true ;
}
$updet = true ;
$noiss = false ;
}
} //end if
if ( $chk [ 'ignore' ] == 1 || $chk [ 'disabled' ] == 1 ) {
$noiss = true ;
$updet = false ;
$noacc = false ;
}
if ( IsMaintenance ( $alert [ 'device_id' ]) > 0 ) {
$noiss = true ;
$noacc = true ;
}
if ( $updet ) {
dbUpdate ( array ( 'details' => gzcompress ( json_encode ( $alert [ 'details' ]), 9 )), 'alert_log' , 'id = ?' , array ( $alert [ 'id' ]));
}
if ( ! empty ( $rextra [ 'mute' ])) {
echo 'Muted Alert-UID #' . $alert [ 'id' ] . " \r \n " ;
$noiss = true ;
}
if ( ! $noiss ) {
IssueAlert ( $alert );
dbUpdate ( array ( 'alerted' => $alert [ 'state' ]), 'alerts' , 'rule_id = ? && device_id = ?' , array ( $alert [ 'rule_id' ], $alert [ 'device_id' ]));
}
if ( ! $noacc ) {
dbUpdate ( array ( 'open' => 0 ), 'alerts' , 'rule_id = ? && device_id = ?' , array ( $alert [ 'rule_id' ], $alert [ 'device_id' ]));
}
} //end foreach
} //end RunAlerts()
2011-05-13 11:42:26 +00:00
2014-12-15 11:10:26 +00:00
/**
* Run external transports
* @ param array $obj Alert - Array
* @ return void
*/
function ExtTransports ( $obj ) {
2015-07-13 20:10:26 +02:00
global $config ;
$tmp = false ;
// To keep scrutinizer from naging because it doesnt understand eval
foreach ( $config [ 'alert' ][ 'transports' ] as $transport => $opts ) {
2015-11-23 15:57:43 +00:00
if ( is_array ( $opts )) {
$opts = array_filter ( $opts );
}
2015-07-13 20:10:26 +02:00
if (( $opts === true || ! empty ( $opts )) && $opts != false && file_exists ( $config [ 'install_dir' ] . '/includes/alerts/transport.' . $transport . '.php' )) {
2016-03-01 11:39:06 -07:00
$obj [ 'transport' ] = $transport ;
$msg = FormatAlertTpl ( $obj );
$obj [ 'msg' ] = $msg ;
2015-07-13 20:10:26 +02:00
echo $transport . ' => ' ;
2015-08-27 14:28:46 +01:00
eval ( '$tmp = function($obj,$opts) { global $config; ' . file_get_contents ( $config [ 'install_dir' ] . '/includes/alerts/transport.' . $transport . '.php' ) . ' return false; };' );
2015-07-13 20:10:26 +02:00
$tmp = $tmp ( $obj , $opts );
2015-08-13 20:36:34 +00:00
$prefix = array ( 0 => " recovery " , 1 => $obj [ 'severity' ] . " alert " , 2 => " acknowledgment " );
$prefix [ 3 ] = & $prefix [ 0 ];
$prefix [ 4 ] = & $prefix [ 0 ];
2016-03-23 16:50:16 +00:00
if ( $tmp === true ) {
2015-07-13 20:10:26 +02:00
echo 'OK' ;
2015-08-13 20:36:34 +00:00
log_event ( 'Issued ' . $prefix [ $obj [ 'state' ]] . " for rule ' " . $obj [ 'name' ] . " ' to transport ' " . $transport . " ' " , $obj [ 'device_id' ]);
2015-07-13 20:10:26 +02:00
}
2016-03-23 16:50:16 +00:00
elseif ( $tmp === false ) {
2015-07-13 20:10:26 +02:00
echo 'ERROR' ;
2015-08-13 20:36:34 +00:00
log_event ( 'Could not issue ' . $prefix [ $obj [ 'state' ]] . " for rule ' " . $obj [ 'name' ] . " ' to transport ' " . $transport . " ' " , $obj [ 'device_id' ]);
2015-07-13 20:10:26 +02:00
}
2016-03-23 16:50:16 +00:00
else {
echo 'ERROR: ' . $tmp . " \r \n " ;
log_event ( 'Could not issue ' . $prefix [ $obj [ 'state' ]] . " for rule ' " . $obj [ 'name' ] . " ' to transport ' " . $transport . " ' Error: " . $tmp , $obj [ 'device_id' ]);
}
2015-07-13 20:10:26 +02:00
}
echo '; ' ;
}
} //end ExtTransports()
2007-04-03 14:10:23 +00:00
2014-12-15 11:10:26 +00:00
/**
* Format Alert
* @ param array $obj Alert - Array
* @ return string
*/
2015-08-30 16:53:34 +01:00
function FormatAlertTpl ( $obj ) {
2015-08-30 17:07:02 +01:00
$tpl = $obj [ " template " ];
$msg = '$ret .= "' . str_replace ( array ( '{else}' , '{/if}' , '{/foreach}' ), array ( '"; } else { $ret .= "' , '"; } $ret .= "' , '"; } $ret .= "' ), addslashes ( $tpl )) . '";' ;
2015-07-13 20:10:26 +02:00
$parsed = $msg ;
$s = strlen ( $msg );
$x = $pos = - 1 ;
$buff = '' ;
2016-03-04 14:36:47 +00:00
$if = $for = $calc = false ;
2015-07-13 20:10:26 +02:00
while ( ++ $x < $s ) {
if ( $msg [ $x ] == '{' && $buff == '' ) {
$buff .= $msg [ $x ];
}
2016-03-04 16:25:51 +00:00
elseif ( $buff == '{ ' ) {
2015-07-13 20:10:26 +02:00
$buff = '' ;
}
2016-03-04 16:25:51 +00:00
elseif ( $buff != '' ) {
2015-07-13 20:10:26 +02:00
$buff .= $msg [ $x ];
}
if ( $buff == '{if' ) {
$pos = $x ;
$if = true ;
}
2016-03-04 16:25:51 +00:00
elseif ( $buff == '{foreach' ) {
2015-07-13 20:10:26 +02:00
$pos = $x ;
$for = true ;
}
2016-03-04 16:25:51 +00:00
elseif ( $buff == '{calc' ) {
2016-03-02 16:33:46 +00:00
$pos = $x ;
$calc = true ;
}
2015-07-13 20:10:26 +02:00
if ( $pos != - 1 && $msg [ $x ] == '}' ) {
$orig = $buff ;
$buff = '' ;
$pos = - 1 ;
if ( $if ) {
$if = false ;
$o = 3 ;
$native = array (
'"; if( ' ,
' ) { $ret .= "' ,
);
}
2016-03-04 16:25:51 +00:00
elseif ( $for ) {
2015-07-13 20:10:26 +02:00
$for = false ;
$o = 8 ;
$native = array (
'"; foreach( ' ,
' as $key=>$value) { $ret .= "' ,
);
}
2016-03-04 16:25:51 +00:00
elseif ( $calc ) {
2016-03-02 16:33:46 +00:00
$calc = false ;
$o = 5 ;
$native = array (
'"; $ret .= (float) (0+(' ,
')); $ret .= "' ,
);
}
2015-07-13 20:10:26 +02:00
else {
continue ;
}
$cond = trim ( populate ( substr ( $orig , $o , - 1 ), false ));
$native = $native [ 0 ] . $cond . $native [ 1 ];
$parsed = str_replace ( $orig , $native , $parsed );
unset ( $cond , $o , $orig , $native );
} //end if
} //end while
$parsed = populate ( $parsed );
return RunJail ( $parsed , $obj );
} //end FormatAlertTpl()
2014-12-15 11:10:26 +00:00
/**
* Describe Alert
* @ param array $alert Alert - Result from DB
* @ return array
*/
function DescribeAlert ( $alert ) {
2015-07-13 20:10:26 +02:00
$obj = array ();
$i = 0 ;
2016-07-28 21:04:00 +01:00
$device = dbFetchRow ( 'SELECT hostname, sysName, location, uptime FROM devices WHERE device_id = ?' , array ( $alert [ 'device_id' ]));
2015-08-30 16:53:34 +01:00
$tpl = dbFetchRow ( 'SELECT `template`,`title`,`title_rec` FROM `alert_templates` JOIN `alert_template_map` ON `alert_template_map`.`alert_templates_id`=`alert_templates`.`id` WHERE `alert_template_map`.`alert_rule_id`=?' , array ( $alert [ 'rule_id' ]));
$default_tpl = " %title \r \n Severity: %severity \r \n { if %state == 0}Time elapsed: %elapsed \r \n { /if}Timestamp: %timestamp \r \n Unique-ID: %uid \r \n Rule: { if %name}%name { else}%rule { /if} \r \n { if %faults}Faults: \r \n { foreach %faults} #%key: %value.string \r \n { /foreach} { /if}Alert sent to: { foreach %contacts}%value <%key> { /foreach} " ;
2015-07-13 20:10:26 +02:00
$obj [ 'hostname' ] = $device [ 'hostname' ];
2016-05-02 19:15:58 +00:00
$obj [ 'sysName' ] = $device [ 'sysName' ];
2016-06-13 22:03:17 +00:00
$obj [ 'location' ] = $device [ 'location' ];
2016-07-28 21:04:00 +01:00
$obj [ 'uptime' ] = $device [ 'uptime' ];
2015-07-13 20:10:26 +02:00
$obj [ 'device_id' ] = $alert [ 'device_id' ];
$extra = $alert [ 'details' ];
2015-08-30 16:53:34 +01:00
if ( ! isset ( $tpl [ 'template' ])) {
2015-08-30 17:07:02 +01:00
$obj [ 'template' ] = $default_tpl ;
} else {
$obj [ 'template' ] = $tpl [ 'template' ];
2015-08-30 16:53:34 +01:00
}
2015-07-13 20:10:26 +02:00
if ( $alert [ 'state' ] >= 1 ) {
2015-08-30 16:53:34 +01:00
if ( ! empty ( $tpl [ 'title' ])) {
2016-03-04 11:30:11 +01:00
$obj [ 'title' ] = $tpl [ 'title' ];
2015-08-30 16:53:34 +01:00
}
else {
$obj [ 'title' ] = 'Alert for device ' . $device [ 'hostname' ] . ' - ' . ( $alert [ 'name' ] ? $alert [ 'name' ] : $alert [ 'rule' ]);
}
2015-07-13 20:10:26 +02:00
if ( $alert [ 'state' ] == 2 ) {
$obj [ 'title' ] .= ' got acknowledged' ;
}
2016-03-04 16:25:51 +00:00
elseif ( $alert [ 'state' ] == 3 ) {
2015-07-13 20:10:26 +02:00
$obj [ 'title' ] .= ' got worse' ;
}
2016-03-04 16:25:51 +00:00
elseif ( $alert [ 'state' ] == 4 ) {
2015-07-13 20:10:26 +02:00
$obj [ 'title' ] .= ' got better' ;
}
foreach ( $extra [ 'rule' ] as $incident ) {
$i ++ ;
$obj [ 'faults' ][ $i ] = $incident ;
foreach ( $incident as $k => $v ) {
if ( ! empty ( $v ) && $k != 'device_id' && ( stristr ( $k , 'id' ) || stristr ( $k , 'desc' ) || stristr ( $k , 'msg' )) && substr_count ( $k , '_' ) <= 1 ) {
$obj [ 'faults' ][ $i ][ 'string' ] .= $k . ' => ' . $v . '; ' ;
}
}
}
2016-03-02 16:53:53 +00:00
$obj [ 'elapsed' ] = TimeFormat ( time () - strtotime ( $alert [ 'time_logged' ]));
if ( ! empty ( $extra [ 'diff' ]) ) {
$obj [ 'diff' ] = $extra [ 'diff' ];
}
2015-07-13 20:10:26 +02:00
}
2016-03-04 16:25:51 +00:00
elseif ( $alert [ 'state' ] == 0 ) {
2015-07-13 20:10:26 +02:00
$id = dbFetchRow ( 'SELECT alert_log.id,alert_log.time_logged,alert_log.details FROM alert_log WHERE alert_log.state != 2 && alert_log.state != 0 && alert_log.rule_id = ? && alert_log.device_id = ? && alert_log.id < ? ORDER BY id DESC LIMIT 1' , array ( $alert [ 'rule_id' ], $alert [ 'device_id' ], $alert [ 'id' ]));
if ( empty ( $id [ 'id' ])) {
return false ;
}
$extra = json_decode ( gzuncompress ( $id [ 'details' ]), true );
2016-03-04 11:30:11 +01:00
if ( ! empty ( $tpl [ 'title_rec' ])) {
$obj [ 'title' ] = $tpl [ 'title_rec' ];
}
else {
2015-08-30 16:53:34 +01:00
$obj [ 'title' ] = 'Device ' . $device [ 'hostname' ] . ' recovered from ' . ( $alert [ 'name' ] ? $alert [ 'name' ] : $alert [ 'rule' ]);
}
2015-07-13 20:10:26 +02:00
$obj [ 'elapsed' ] = TimeFormat ( strtotime ( $alert [ 'time_logged' ]) - strtotime ( $id [ 'time_logged' ]));
$obj [ 'id' ] = $id [ 'id' ];
$obj [ 'faults' ] = false ;
}
else {
return 'Unknown State' ;
} //end if
$obj [ 'uid' ] = $alert [ 'id' ];
$obj [ 'severity' ] = $alert [ 'severity' ];
$obj [ 'rule' ] = $alert [ 'rule' ];
$obj [ 'name' ] = $alert [ 'name' ];
$obj [ 'timestamp' ] = $alert [ 'time_logged' ];
$obj [ 'contacts' ] = $extra [ 'contacts' ];
$obj [ 'state' ] = $alert [ 'state' ];
2015-08-30 18:02:29 +01:00
if ( strstr ( $obj [ 'title' ], '%' )) {
$obj [ 'title' ] = RunJail ( '$ret = "' . populate ( addslashes ( $obj [ 'title' ])) . '";' , $obj );
}
2015-07-13 20:10:26 +02:00
return $obj ;
} //end DescribeAlert()
2007-04-03 14:10:23 +00:00
2014-12-15 11:10:26 +00:00
/**
* Format Elapsed Time
2015-07-13 20:10:26 +02:00
* @ param integer $secs Seconds elapsed
2014-12-15 11:10:26 +00:00
* @ return string
*/
2015-07-13 20:10:26 +02:00
function TimeFormat ( $secs ) {
$bit = array (
'y' => $secs / 31556926 % 12 ,
'w' => $secs / 604800 % 52 ,
'd' => $secs / 86400 % 7 ,
'h' => $secs / 3600 % 24 ,
'm' => $secs / 60 % 60 ,
's' => $secs % 60 ,
);
$ret = array ();
foreach ( $bit as $k => $v ) {
if ( $v > 0 ) {
$ret [] = $v . $k ;
}
}
if ( empty ( $ret )) {
return 'none' ;
}
return join ( ' ' , $ret );
} //end TimeFormat()
2015-01-25 21:39:33 +00:00
/**
* " Safely " run eval
* @ param string $code Code to run
2015-07-13 20:10:26 +02:00
* @ param array $obj Object with variables
2015-01-25 21:39:33 +00:00
* @ return string | mixed
*/
2015-07-13 20:10:26 +02:00
function RunJail ( $code , $obj ) {
$ret = '' ;
eval ( $code );
return $ret ;
} //end RunJail()
2015-01-25 21:39:33 +00:00
/**
* Populate variables
2015-07-13 20:10:26 +02:00
* @ param string $txt Text with variables
* @ param boolean $wrap Wrap variable for text - usage ( default : true )
2015-01-25 21:39:33 +00:00
* @ return string
*/
2015-07-13 20:10:26 +02:00
function populate ( $txt , $wrap = true ) {
preg_match_all ( '/%([\w\.]+)/' , $txt , $m );
foreach ( $m [ 1 ] as $tmp ) {
$orig = $tmp ;
$rep = false ;
if ( $tmp == 'key' || $tmp == 'value' ) {
$rep = '$' . $tmp ;
}
else {
if ( strstr ( $tmp , '.' )) {
$tmp = explode ( '.' , $tmp , 2 );
$pre = '$' . $tmp [ 0 ];
$tmp = $tmp [ 1 ];
}
else {
$pre = '$obj' ;
}
$rep = $pre . " [' " . str_replace ( '.' , " '][' " , $tmp ) . " '] " ;
if ( $wrap ) {
$rep = '{' . $rep . '}' ;
}
}
$txt = str_replace ( '%' . $orig , $rep , $txt );
} //end foreach
return $txt ;
} //end populate()