mirror of
https://github.com/stylersnico/librenms.git
synced 2026-07-28 00:24:21 +02:00
Added Alerting
This commit is contained in:
+141
-102
@@ -1,112 +1,151 @@
|
||||
<?php
|
||||
/* Copyright (C) 2014 Daniel Preussker <f0o@devilcode.org>
|
||||
* 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
|
||||
/**
|
||||
* Observium Network Management and Monitoring System
|
||||
* Copyright (C) 2006-2012, Adam Armstrong - http://www.observium.org
|
||||
*
|
||||
* @package alerting
|
||||
* @author Adam Armstrong <adama@memetic.org>
|
||||
*
|
||||
* Alerts Tracking
|
||||
* @author Daniel Preussker <f0o@devilcode.org>
|
||||
* @copyright 2014 f0o, LibreNMS
|
||||
* @license GPL
|
||||
* @package LibreNMS
|
||||
* @subpackage Alerts
|
||||
*/
|
||||
|
||||
/**
|
||||
* Generate SQL from Rule
|
||||
* @param string $rule Rule to generate SQL for
|
||||
* @return string
|
||||
*/
|
||||
function GenSQL($rule) {
|
||||
$tmp = explode(" ",$rule);
|
||||
$tables = array();
|
||||
foreach( $tmp as $opt ) {
|
||||
if( strstr($opt,'%') && strstr($opt,'.') ) {
|
||||
$tmpp = explode(".",$opt,2);
|
||||
$tmpp[0] = str_replace("%","",$tmpp[0]);
|
||||
$tables[] = mres($tmpp[0]);
|
||||
$rule = str_replace($opt,$tmpp[0].'.'.$tmpp[1],$rule);
|
||||
}
|
||||
}
|
||||
$tables = array_unique($tables);
|
||||
$x = sizeof($tables);
|
||||
$i = 0;
|
||||
$join = "";
|
||||
while( $i < $x ) {
|
||||
if( isset($tables[$i+1]) ) {
|
||||
$join .= $tables[$i].".device_id = ".$tables[$i+1].".device_id && ";
|
||||
}
|
||||
$i++;
|
||||
}
|
||||
$sql = "SELECT * FROM ".implode(",",$tables)." WHERE (".$join."".$tables[0].".device_id = ?) && (".str_replace(array("*","!~","~"),array("%","NOT LIKE","LIKE"),$rule).")";
|
||||
return $sql;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Build a cache of default alert conditions
|
||||
*
|
||||
* Run all rules for a device
|
||||
* @param int $device Device-ID
|
||||
* @return void
|
||||
*/
|
||||
function RunRules($device) {
|
||||
global $debug;
|
||||
$chk = dbFetchRow("SELECT id FROM alert_schedule WHERE alert_schedule.device_id = ? AND NOW() BETWEEN alert_schedule.start AND alert_schedule.end", array($device));
|
||||
if( $chk['id'] > 0 ) {
|
||||
return false;
|
||||
}
|
||||
foreach( dbFetchRows("SELECT * FROM alert_rules WHERE alert_rules.disabled = 0 && ( alert_rules.device_id = -1 || alert_rules.device_id = ? ) ORDER BY device_id,id",array($device)) as $rule ) {
|
||||
echo " #".$rule['id'].":";
|
||||
$chk = dbFetchRow("SELECT state FROM alerts WHERE rule_id = ? && device_id = ? ORDER BY id DESC LIMIT 1", array($rule['id'], $device));
|
||||
$sql = GenSQL($rule['rule']);
|
||||
$qry = dbFetchRows($sql,array($device));
|
||||
if( sizeof($qry) > 0 ) {
|
||||
if( $chk['state'] === "2" ) {
|
||||
echo " SKIP ";
|
||||
} elseif( $chk['state'] === "1" ) {
|
||||
echo " NOCHG ";
|
||||
} else {
|
||||
$extra = gzcompress(json_encode(array('contacts' => GetContacts($qry), 'rule'=>$qry)),9);
|
||||
if( dbInsert(array('state' => 1, 'device_id' => $device, 'rule_id' => $rule['id'], 'details' => $extra),'alert_log') ) {
|
||||
if( !dbUpdate(array('state' => 1, 'open' => 1),'alerts','device_id = ? && rule_id = ?', array($device,$rule['id'])) ) {
|
||||
dbInsert(array('state' => 1, 'device_id' => $device, 'rule_id' => $rule['id'], 'open' => 1),'alerts');
|
||||
}
|
||||
echo " ALERT ";
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if( $chk['state'] === "0" ) {
|
||||
echo " NOCHG ";
|
||||
} else {
|
||||
if( dbInsert(array('state' => 0, 'device_id' => $device, 'rule_id' => $rule['id']),'alert_log') ){
|
||||
if( !dbUpdate(array('state' => 0, 'open' => 1),'alerts','device_id = ? && rule_id = ?', array($device,$rule['id'])) ) {
|
||||
dbInsert(array('state' => 0, 'device_id' => $device, 'rule_id' => $rule['id'], 'open' => 1),'alerts');
|
||||
}
|
||||
echo " OK ";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Find contacts for alert
|
||||
* @param array $results Rule-Result
|
||||
* @return array
|
||||
*/
|
||||
|
||||
function cache_conditions_global() {
|
||||
$cache = array();
|
||||
foreach (dbFetchRows("SELECT * FROM `alert_conditions_global`") as $entry)
|
||||
{
|
||||
$cache[$entry['type']][$entry['subtype']][$entry['metric']][] = array('operator' => $entry['operator'], 'value' => $entry['value'],
|
||||
'severity' => $entry['severity'], 'alerter' => $entry['alerter'], 'enable' => $entry['enable']);
|
||||
}
|
||||
return $cache;
|
||||
*/
|
||||
function GetContacts($results) {
|
||||
global $config;
|
||||
if( sizeof($results) == 0 ) {
|
||||
return array();
|
||||
}
|
||||
$contacts = array();
|
||||
$uids = array();
|
||||
foreach( $results as $result ) {
|
||||
$tmpa = array();
|
||||
$tmp = NULL;
|
||||
if( is_numeric($result["port_id"]) ) {
|
||||
$tmpa = dbFetchRows("SELECT user_id FROM ports_perms WHERE access_level >= 0 AND port_id = ?",array($result["port_id"]));
|
||||
foreach( $tmpa as $tmp ) {
|
||||
$uids[$tmp['user_id']] = $tmp['user_id'];
|
||||
}
|
||||
}
|
||||
if( is_numeric($result["device_id"]) ) {
|
||||
$tmpa = dbFetchRow("SELECT sysContact FROM devices WHERE device_id = ?",array($result["device_id"]));
|
||||
$contacts[$tmpa["sysContact"]] = "NOC";
|
||||
$tmpa = dbFetchRows("SELECT user_id FROM devices_perms WHERE access_level >= 0 AND device_id = ?", array($result["device_id"]));
|
||||
foreach( $tmpa as $tmp ) {
|
||||
$uids[$tmp['user_id']] = $tmp['user_id'];
|
||||
}
|
||||
}
|
||||
}
|
||||
if( $config["alert"]["globals"] ) {
|
||||
$tmpa = dbFetchRows("SELECT realname,email FROM users WHERE level >= 5 AND level < 10");
|
||||
foreach( $tmpa as $tmp ) {
|
||||
$contacts[$tmp['email']] = $tmp['realname'];
|
||||
}
|
||||
}
|
||||
if( $config["alert"]["admins"] ) {
|
||||
$tmpa = dbFetchRows("SELECT realname,email FROM users WHERE level = 10");
|
||||
foreach( $tmpa as $tmp ) {
|
||||
$contacts[$tmp['email']] = $tmp['realname'];
|
||||
}
|
||||
}
|
||||
if( is_array($uids) ) {
|
||||
foreach( $uids as $uid ) {
|
||||
$tmp = dbFetchRow("SELECT realname,email FROM users WHERE user_id = ?", array($uid));
|
||||
$contacts[$tmp['email']] = $tmp['realname'];
|
||||
}
|
||||
}
|
||||
return $contacts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a cache of device-specific alert conditions
|
||||
*
|
||||
* @return array
|
||||
* @param device_id
|
||||
*/
|
||||
|
||||
function cache_conditions_device($device_id) {
|
||||
|
||||
$cache = array();
|
||||
foreach (dbFetchRows("SELECT * FROM `alert_conditions` WHERE `device_id` = ?", array($device_id)) as $entry)
|
||||
{
|
||||
$cache[$entry['type']][$entry['subtype']][$entry['entity']][$entry['metric']][] = array('condition' => $entry['operator'], 'value' => $entry['value'],
|
||||
'severity' => $entry['severity'], 'alerter' => $entry['alerter'],
|
||||
'enable' => $entry['enable']);
|
||||
}
|
||||
return $cache;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compare two values
|
||||
*
|
||||
* @return integer
|
||||
* @param value_a
|
||||
* @param condition
|
||||
* @param value_b
|
||||
*/
|
||||
|
||||
function test_condition($value_a, $condition, $value_b)
|
||||
{
|
||||
switch($condition)
|
||||
{
|
||||
case ">":
|
||||
if($value_a > $value_b) { $alert = 1; } else { $alert = 0; }
|
||||
break;
|
||||
case "<":
|
||||
if($value_a < $value_b) { $alert = 1; } else { $alert = 0; }
|
||||
break;
|
||||
case "!=":
|
||||
if($value_a != $value_b) { $alert = 1; } else { $alert = 0; }
|
||||
break;
|
||||
case "=":
|
||||
if($value_a = $value_b) { $alert = 1; } else { $alert = 0; }
|
||||
break;
|
||||
default:
|
||||
$alert = -1;
|
||||
break;
|
||||
}
|
||||
return $alert;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check entity data against alert conditions
|
||||
*
|
||||
* @param value_a
|
||||
* @param condition
|
||||
* @param value_b
|
||||
*/
|
||||
|
||||
function check_entity($device, $entity_type, $entity_id, $data)
|
||||
{
|
||||
global $glo_conditions;
|
||||
global $dev_conditions;
|
||||
|
||||
if(!empty($entity_id)) { echo(" $entity_id"); }
|
||||
|
||||
foreach($data as $name => $value)
|
||||
{
|
||||
foreach($dev_conditions[$entity_type][$entity_id][$name] as $condition)
|
||||
{
|
||||
$alert = test_condition($value, $condition['condition'], $condition['value']);
|
||||
if($alert == 1)
|
||||
{
|
||||
echo("ALERT ");
|
||||
} else {
|
||||
echo("OK ");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
?>
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
/* Copyright (C) 2014 Daniel Preussker <f0o@devilcode.org>
|
||||
* 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
|
||||
/**
|
||||
* API Transport
|
||||
* @author f0o <f0o@devilcode.org>
|
||||
* @copyright 2014 f0o, LibreNMS
|
||||
* @license GPL
|
||||
* @package LibreNMS
|
||||
* @subpackage Alerts
|
||||
*/
|
||||
|
||||
foreach( $opts as $method=>$apis ) {
|
||||
// var_dump($method); //FIXME: propper debuging
|
||||
foreach( $apis as $api ) {
|
||||
// var_dump($api); //FIXME: propper debuging
|
||||
list($host, $api) = explode("?",$api,2);
|
||||
foreach( $obj as $k=>$v ) {
|
||||
$api = str_replace("%".$k,$method == "get" ? urlencode($v) : $v, $api);
|
||||
}
|
||||
// var_dump($api); //FIXME: propper debuging
|
||||
$curl = curl_init();
|
||||
curl_setopt($curl, CURLOPT_URL, ($method == "get" ? $host."?".$api : $host) );
|
||||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
|
||||
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, strtoupper($method));
|
||||
curl_setopt($curl, CURLOPT_POSTFIELDS, $api);
|
||||
$ret = curl_exec($curl);
|
||||
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
|
||||
if( $code != 200 ) {
|
||||
var_dump("API '$host' returnd Error"); //FIXME: propper debuging
|
||||
var_dump("Params: ".$api); //FIXME: propper debuging
|
||||
var_dump("Return: ".$ret); //FIXME: propper debuging
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
@@ -0,0 +1,25 @@
|
||||
/* Copyright (C) 2014 Daniel Preussker <f0o@devilcode.org>
|
||||
* 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
|
||||
/**
|
||||
* Dummy Transport
|
||||
* @author f0o <f0o@devilcode.org>
|
||||
* @copyright 2014 f0o, LibreNMS
|
||||
* @license GPL
|
||||
* @package LibreNMS
|
||||
* @subpackage Alerts
|
||||
*/
|
||||
|
||||
var_dump($obj);
|
||||
return true;
|
||||
@@ -0,0 +1,24 @@
|
||||
/* Copyright (C) 2014 Daniel Preussker <f0o@devilcode.org>
|
||||
* 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
|
||||
/**
|
||||
* IRC Transport
|
||||
* @author f0o <f0o@devilcode.org>
|
||||
* @copyright 2014 f0o, LibreNMS
|
||||
* @license GPL
|
||||
* @package LibreNMS
|
||||
* @subpackage Alerts
|
||||
*/
|
||||
|
||||
return file_put_contents($config['install_dir']."/.ircbot.alert", json_encode($obj)."\n", FILE_APPEND);
|
||||
@@ -0,0 +1,24 @@
|
||||
/* Copyright (C) 2014 Daniel Preussker <f0o@devilcode.org>
|
||||
* 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
|
||||
/**
|
||||
* Mail Transport
|
||||
* @author f0o <f0o@devilcode.org>
|
||||
* @copyright 2014 f0o, LibreNMS
|
||||
* @license GPL
|
||||
* @package LibreNMS
|
||||
* @subpackage Alerts
|
||||
*/
|
||||
|
||||
return send_mail($obj['contacts'], $obj['title'], $obj['msg']);
|
||||
@@ -0,0 +1,48 @@
|
||||
/* Copyright (C) 2014 Daniel Preussker <f0o@devilcode.org>
|
||||
* 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
|
||||
/**
|
||||
* Nagios Transport
|
||||
* @author f0o <f0o@devilcode.org>
|
||||
* @copyright 2014 f0o, LibreNMS
|
||||
* @license GPL
|
||||
* @package LibreNMS
|
||||
* @subpackage Alerts
|
||||
*/
|
||||
|
||||
/*
|
||||
host_perfdata_file_template=
|
||||
[HOSTPERFDATA]\t
|
||||
$TIMET$\t
|
||||
$HOSTNAME$\t
|
||||
HOST\t
|
||||
$HOSTSTATE$\t
|
||||
$HOSTEXECUTIONTIME$\t
|
||||
$HOSTLATENCY$\t
|
||||
$HOSTOUTPUT$\t
|
||||
$HOSTPERFDATA$
|
||||
*/
|
||||
|
||||
$format = '';
|
||||
$format .= "[HOSTPERFDATA]\t";
|
||||
$format .= $obj['timestamp']."\t";
|
||||
$format .= $obj['hostname']."\t";
|
||||
$format .= md5($obj['rule'])."\t"; //FIXME: Better entity
|
||||
$format .= ($obj['state'] ? $obj['severity'] : "ok")."\t";
|
||||
$format .= 0."\t";
|
||||
$format .= 0."\t";
|
||||
$format .= str_replace("\n","",nl2br($obj['msg']))."\t";
|
||||
$format .= "NULL"; //FIXME: What's the HOSTPERFDATA equivalent for LibreNMS? Oo
|
||||
$format .= "\n";
|
||||
return file_put_contents($opts, $format);
|
||||
Reference in New Issue
Block a user