Device-Groups Draft

This commit is contained in:
f0o
2015-04-03 18:22:29 +00:00
parent 1a8c76268c
commit ba99eb25a2
20 changed files with 952 additions and 2 deletions

View File

@@ -55,6 +55,15 @@ if(empty($rule)) {
} else {
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>";
}

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;

View File

@@ -0,0 +1,59 @@
<?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($target) && $target[0] != "g" ) {
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($target) && $target[0] != "g" ) {
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>");
}
}
}
}
}
foreach( $ret as $msg ) {
echo $msg."<br/>";
}

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['map_id'])) {
echo('ERROR: No map selected');
exit;
} else {
if(dbDelete('alert_map', "`id` = ?", array($_POST['map_id']))) {
echo('Map has been deleted.');
exit;
} else {
echo('ERROR: Map has not been deleted.');
exit;
}
}

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;
}
}

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);
}

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);
}