librenms-librenms/html/includes/forms/create-alert-item.inc.php

99 lines
3.2 KiB
PHP
Raw Normal View History

2014-11-30 17:49:52 +00:00
<?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.
*/
2015-07-13 20:10:26 +02:00
if (is_admin() === false) {
2014-11-30 17:49:52 +00:00
die('ERROR: You need to be admin');
}
2015-07-13 20:10:26 +02:00
$rule = implode(' ', $_POST['rules']);
$rule = rtrim($rule, '&&');
$rule = rtrim($rule, '||');
2014-11-30 17:49:52 +00:00
$alert_id = $_POST['alert_id'];
2015-07-13 20:10:26 +02:00
$count = mres($_POST['count']);
$delay = mres($_POST['delay']);
$interval = mres($_POST['interval']);
2015-07-13 20:10:26 +02:00
$mute = mres($_POST['mute']);
$invert = mres($_POST['invert']);
$name = mres($_POST['name']);
2016-05-27 16:13:51 +02:00
if ($_POST['proc'] != "") { $proc = $_POST['proc']; }
else { $proc = ""; }
2014-11-30 17:49:52 +00:00
2015-07-13 20:10:26 +02:00
if (empty($rule)) {
$update_message = 'ERROR: No rule was generated - did you forget to click and / or?';
}
else if (validate_device_id($_POST['device_id']) || $_POST['device_id'] == '-1' || $_POST['device_id'][0] == ':') {
2014-11-30 17:49:52 +00:00
$device_id = $_POST['device_id'];
2015-07-13 20:10:26 +02:00
if (!is_numeric($count)) {
$count = '-1';
2014-11-30 17:49:52 +00:00
}
2015-07-13 20:10:26 +02:00
$delay_sec = convert_delay($delay);
$interval_sec = convert_delay($interval);
2015-07-13 20:10:26 +02:00
if ($mute == 'on') {
2014-11-30 17:49:52 +00:00
$mute = true;
2015-07-13 20:10:26 +02:00
}
else {
2014-11-30 17:49:52 +00:00
$mute = false;
}
2015-07-13 20:10:26 +02:00
if ($invert == 'on') {
$invert = true;
2015-07-13 20:10:26 +02:00
}
else {
$invert = false;
}
2015-07-13 20:10:26 +02:00
$extra = array(
'mute' => $mute,
'count' => $count,
'delay' => $delay_sec,
'invert' => $invert,
'interval' => $interval_sec,
);
2014-11-30 17:49:52 +00:00
$extra_json = json_encode($extra);
2015-07-13 20:10:26 +02:00
if (is_numeric($alert_id) && $alert_id > 0) {
2016-05-27 16:13:51 +02:00
if (dbUpdate(array('rule' => $rule, 'severity' => mres($_POST['severity']), 'extra' => $extra_json, 'name' => $name, 'proc' => $proc), 'alert_rules', 'id=?', array($alert_id)) >= 0) {
2014-12-21 15:44:50 +00:00
$update_message = "Edited Rule: <i>$name: $rule</i>";
2014-11-30 17:49:52 +00:00
}
2015-07-13 20:10:26 +02:00
else {
$update_message = 'ERROR: Failed to edit Rule: <i>'.$rule.'</i>';
}
}
else {
if (is_array($_POST['maps'])) {
$device_id = ':'.$device_id;
}
2015-07-13 20:10:26 +02:00
2016-05-27 16:13:51 +02:00
if (dbInsert(array('device_id' => $device_id, 'rule' => $rule, 'severity' => mres($_POST['severity']), 'extra' => $extra_json, 'disabled' => 0, 'name' => $name, 'proc' => $proc), 'alert_rules')) {
2014-12-21 15:44:50 +00:00
$update_message = "Added Rule: <i>$name: $rule</i>";
2015-07-13 20:10:26 +02:00
if (is_array($_POST['maps'])) {
foreach ($_POST['maps'] as $target) {
$_POST['rule'] = $name;
2015-04-03 18:22:29 +00:00
$_POST['target'] = $target;
$_POST['map_id'] = '';
include 'create-map-item.inc.php';
2015-04-03 18:22:29 +00:00
unset($ret,$target,$raw,$rule,$msg,$map_id);
}
}
2014-11-30 17:49:52 +00:00
}
2015-07-13 20:10:26 +02:00
else {
$update_message = 'ERROR: Failed to add Rule: <i>'.$rule.'</i>';
}
}//end if
2014-11-30 17:49:52 +00:00
}
2015-07-13 20:10:26 +02:00
else {
$update_message = 'ERROR: invalid device ID or not a global alert';
}//end if
2014-11-30 17:49:52 +00:00
echo $update_message;