Merge pull request #722 from f0o/issue-616-dynamic

Device-Groups + Alerting + Management
This commit is contained in:
Neil Lathwood
2015-04-04 19:36:26 +01:00
22 changed files with 1008 additions and 9 deletions
+13 -1
View File
@@ -28,7 +28,7 @@ $name = mres($_POST['name']);
if(empty($rule)) {
$update_message = "ERROR: No rule was generated";
} elseif(validate_device_id($_POST['device_id']) || $_POST['device_id'] == '-1') {
} elseif(validate_device_id($_POST['device_id']) || $_POST['device_id'] == '-1' || $_POST['device_id'][0] == ':') {
$device_id = $_POST['device_id'];
if(!is_numeric($count)) {
$count='-1';
@@ -53,8 +53,20 @@ if(empty($rule)) {
$update_message = "ERROR: Failed to edit Rule: <i>".$rule."</i>";
}
} else {
if( is_array($_POST['maps']) ) {
$device_id = ':'.$device_id;
}
if( dbInsert(array('device_id'=>$device_id,'rule'=>$rule,'severity'=>mres($_POST['severity']),'extra'=>$extra_json,'name'=>$name),'alert_rules') ) {
$update_message = "Added Rule: <i>$name: $rule</i>";
if( is_array($_POST['maps']) ) {
foreach( $_POST['maps'] as $target ) {
$_POST['rule'] = $name;
$_POST['target'] = $target;
$_POST['map_id'] = '';
include('forms/create-map-item.inc.php');
unset($ret,$target,$raw,$rule,$msg,$map_id);
}
}
} else {
$update_message = "ERROR: Failed to add Rule: <i>".$rule."</i>";
}
+52
View File
@@ -0,0 +1,52 @@
<?php
/*
* LibreNMS
*
* Copyright (c) 2014 Neil Lathwood <https://github.com/laf/ http://www.lathwood.co.uk/fa>
*
* 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. Please see LICENSE.txt at the top level of
* the source code distribution for details.
*/
if(is_admin() === false) {
die('ERROR: You need to be admin');
}
$pattern = $_POST['patterns'];
$group_id = $_POST['group_id'];
$name = mres($_POST['name']);
$desc = mres($_POST['desc']);
if( is_array($pattern) ) {
$pattern = implode(" ", $pattern);
$pattern = rtrim($pattern,'&&');
$pattern = rtrim($pattern,'||');
} elseif( !empty($_POST['pattern']) && !empty($_POST['condition']) && !empty($_POST['value']) ) {
$pattern = '%'.$_POST['pattern'].' '.$_POST['condition'].' ';
if( is_numeric($_POST['value']) ) {
$pattern .= $_POST['value'];
} else {
$pattern .= '"'.$_POST['value'].'"';
}
}
if(empty($pattern)) {
$update_message = "ERROR: No group was generated";
} elseif(is_numeric($group_id) && $group_id > 0) {
if(dbUpdate(array('pattern' => $pattern,'name'=>$name,'desc'=>$desc), 'device_groups', 'id=?',array($group_id)) >= 0) {
$update_message = "Edited Group: <i>$name: $pattern</i>";
} else {
$update_message = "ERROR: Failed to edit Group: <i>".$pattern."</i>";
}
} else {
if( dbInsert(array('pattern'=>$pattern,'name'=>$name,'desc'=>$desc),'device_groups') ) {
$update_message = "Added Group: <i>$name: $pattern</i>";
} else {
$update_message = "ERROR: Failed to add Group: <i>".$pattern."</i>";
}
}
echo $update_message;
+66
View File
@@ -0,0 +1,66 @@
<?php
/*
* LibreNMS
*
* Copyright (c) 2014 Neil Lathwood <https://github.com/laf/ http://www.lathwood.co.uk/fa>
*
* 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. Please see LICENSE.txt at the top level of
* the source code distribution for details.
*/
if(is_admin() === false) {
die('ERROR: You need to be admin');
}
$rule = mres($_POST['rule']);
$target = mres($_POST['target']);
$map_id = mres($_POST['map_id']);
$ret = array();
if( empty($rule) || empty($target) ) {
$ret[] = "ERROR: No map was generated";
} else {
$raw = $rule;
$rule = dbFetchCell('SELECT id FROM alert_rules WHERE name = ?',array($rule));
if( !is_numeric($rule) ) {
array_unshift($ret, "ERROR: Could not find rule for '".$raw."'");
} else {
$raw = $target;
if( $target[0].$target[1] == "g:" ) {
$target = "g".dbFetchCell('SELECT id FROM device_groups WHERE name = ?',array(substr($target,2)));
} else {
$target = dbFetchCell('SELECT device_id FROM devices WHERE hostname = ?',array($target));
}
if( !is_numeric(str_replace('g','',$target)) ) {
array_unshift($ret, "ERROR: Could not find entry for '".$raw."'");
} else {
if(is_numeric($map_id) && $map_id > 0) {
if(dbUpdate(array('rule' => $rule,'target'=>$target), 'alert_map', 'id=?',array($map_id)) >= 0) {
$ret[] = "Edited Map: <i>".$map_id.": ".$rule." = ".$target."</i>";
} else {
array_unshift($ret,"ERROR: Failed to edit Map: <i>".$map_id.": ".$rule." = ".$target."</i>");
}
} else {
if( dbInsert(array('rule'=>$rule,'target'=>$target),'alert_map') ) {
$ret[] = "Added Map: <i>".$rule." = ".$target."</i>";
} else {
array_unshift($ret,"ERROR: Failed to add Map: <i>".$rule." = ".$target."</i>");
}
}
if( ($tmp=dbFetchCell('SELECT device_id FROM alert_rules WHERE id = ?',array($rule))) && $tmp[0] != ":" ) {
if(dbUpdate(array('device_id' => ':'.$tmp), 'alert_rules', 'id=?',array($rule)) >= 0) {
$ret[] = "Edited Rule: <i>".$rule." device_id = ':".$tmp."'</i>";
} else {
array_unshift($ret,"ERROR: Failed to edit Rule: <i>".$rule.": device_id = ':".$tmp."'</i>");
}
}
}
}
}
foreach( $ret as $msg ) {
echo $msg."<br/>";
}
+41
View File
@@ -0,0 +1,41 @@
<?php
/*
* LibreNMS
*
* Copyright (c) 2014 Neil Lathwood <https://github.com/laf/ http://www.lathwood.co.uk/fa>
*
* 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. Please see LICENSE.txt at the top level of
* the source code distribution for details.
*/
if(is_admin() === false) {
die('ERROR: You need to be admin');
}
$ret = array();
$brk = false;
if( !is_numeric($_POST['map_id']) ) {
array_unshift($ret,'ERROR: No map selected');
} else {
if( dbFetchCell('SELECT COUNT(B.id) FROM alert_map,alert_map AS B WHERE alert_map.rule=B.rule && alert_map.id = ?',array($_POST['map_id'])) <= 1 ) {
$rule = dbFetchRow('SELECT alert_rules.id,alert_rules.device_id FROM alert_map,alert_rules WHERE alert_map.rule=alert_rules.id && alert_map.id = ?',array($_POST['map_id']));
$rule['device_id'] = str_replace(":",'',$rule['device_id']);
if( dbUpdate(array('device_id'=>$rule['device_id']),'alert_rules','id = ?',array($rule['id'])) >= 0 ) {
$ret[] = "Restored Rule: <i>".$rule['id'].": device_id = '".$rule['device_id']."'</i>";
} else {
array_unshift($ret, 'ERROR: Rule '.$rule['id'].' has not been restored.');
$brk = true;
}
}
if( $brk === false && dbDelete('alert_map', "`id` = ?", array($_POST['map_id'])) ) {
$ret[] = 'Map has been deleted.';
} else {
array_unshift($ret, 'ERROR: Map has not been deleted.');
}
}
foreach( $ret as $msg ) {
echo $msg."<br/>";
}
+5
View File
@@ -21,6 +21,11 @@ if(!is_numeric($_POST['alert_id'])) {
exit;
} else {
if(dbDelete('alert_rules', "`id` = ?", array($_POST['alert_id']))) {
if(dbDelete('alert_map', "rule = ?", array($_POST['alert_id'])) || dbFetchCell('COUNT(id) FROM alert_map WHERE rule = ?',array($_POST['alert_id'])) == 0) {
echo('Maps has been deleted.');
} else {
echo('WARNING: Maps could not be deleted.');
}
echo('Alert rule has been deleted.');
exit;
} else {
+31
View File
@@ -0,0 +1,31 @@
<?php
/*
* LibreNMS
*
* Copyright (c) 2014 Neil Lathwood <https://github.com/laf/ http://www.lathwood.co.uk/fa>
*
* 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. Please see LICENSE.txt at the top level of
* the source code distribution for details.
*/
if(is_admin() === false) {
die('ERROR: You need to be admin');
}
if(!is_numeric($_POST['group_id'])) {
echo('ERROR: No group selected');
exit;
} else {
if(dbDelete('device_groups', "`id` = ?", array($_POST['group_id']))) {
echo('group has been deleted.');
exit;
} else {
echo('ERROR: group has not been deleted.');
exit;
}
}
+30
View File
@@ -0,0 +1,30 @@
<?php
/*
* LibreNMS
*
* Copyright (c) 2014 Neil Lathwood <https://github.com/laf/ http://www.lathwood.co.uk/fa>
*
* 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. Please see LICENSE.txt at the top level of
* the source code distribution for details.
*/
if(is_admin() === false) {
die('ERROR: You need to be admin');
}
$map_id = $_POST['map_id'];
if(is_numeric($map_id) && $map_id > 0) {
$map = dbFetchRow("SELECT alert_rules.name,alert_map.target FROM alert_map,alert_rules WHERE alert_map.rule=alert_rules.id && alert_map.id = ?",array($map_id));
if( $map['target'][0] == "g" ) {
$map['target'] = 'g:'.dbFetchCell("SELECT name FROM device_groups WHERE id = ?",array(substr($map['target'],1)));
} else {
$map['target'] = dbFetchCell("SELECT hostname FROM devices WHERE device_id = ?",array($map['target']));
}
$output = array('rule'=>$map['name'],'target'=>$map['target']);
echo _json_encode($output);
}
+28
View File
@@ -0,0 +1,28 @@
<?php
/*
* LibreNMS
*
* Copyright (c) 2014 Neil Lathwood <https://github.com/laf/ http://www.lathwood.co.uk/fa>
*
* 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. Please see LICENSE.txt at the top level of
* the source code distribution for details.
*/
if(is_admin() === false) {
die('ERROR: You need to be admin');
}
$group_id = $_POST['group_id'];
if(is_numeric($group_id) && $group_id > 0) {
$group = dbFetchRow("SELECT * FROM `device_groups` WHERE `id` = ? LIMIT 1",array($group_id));
$group_split = preg_split('/([a-zA-Z0-9_\-\.\=\%\<\>\ \"\'\!\~\(\)\*\/\@]+[&&\|\|]+)/',$group['pattern'], -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
$count = count($group_split) - 1;
$group_split[$count] = $group_split[$count].' &&';
$output = array('name'=>$group['name'],'desc'=>$group['desc'],'pattern'=>$group_split);
echo _json_encode($output);
}