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 .
*/
2018-03-14 20:25:19 +00:00
use LibreNMS\Alerting\QueryBuilderParser ;
2018-04-07 15:55:28 -05:00
use LibreNMS\Authentication\Auth ;
2018-03-14 20:25:19 +00:00
2018-04-07 15:55:28 -05:00
if ( ! Auth :: user () -> hasGlobalAdmin ()) {
2016-08-18 20:28:22 -05:00
header ( 'Content-type: text/plain' );
2014-11-30 17:49:52 +00:00
die ( 'ERROR: You need to be admin' );
}
$alert_id = $_POST [ 'alert_id' ];
2016-10-26 06:54:48 +00:00
$template_id = $_POST [ 'template_id' ];
2014-11-30 17:49:52 +00:00
2015-07-13 20:10:26 +02:00
if ( is_numeric ( $alert_id ) && $alert_id > 0 ) {
2018-03-14 20:25:19 +00:00
$rule = dbFetchRow ( 'SELECT * FROM `alert_rules` WHERE `id` = ? LIMIT 1' , [ $alert_id ]);
$maps = [];
$devices = dbFetchRows ( 'SELECT `device_id`, `hostname`, `sysName` FROM `alert_device_map` LEFT JOIN `devices` USING (`device_id`) WHERE `rule_id`=?' , [ $alert_id ]);
foreach ( $devices as $device ) {
$maps [] = [ 'id' => $device [ 'device_id' ], 'text' => format_hostname ( $device )];
}
$groups = dbFetchRows ( 'SELECT `group_id`, `name` FROM `alert_group_map` LEFT JOIN `device_groups` ON `device_groups`.`id`=`alert_group_map`.`group_id` WHERE `rule_id`=?' , [ $alert_id ]);
foreach ( $groups as $group ) {
$maps [] = [ 'id' => 'g' . $group [ 'group_id' ], 'text' => $group [ 'name' ]];
}
2016-10-26 06:54:48 +00:00
} elseif ( is_numeric ( $template_id ) && $template_id >= 0 ) {
2016-11-01 16:43:12 +00:00
$tmp_rules = get_rules_from_json ();
$rule = $tmp_rules [ $template_id ];
2018-03-14 20:25:19 +00:00
$maps = [];
2016-10-26 06:54:48 +00:00
}
2018-03-14 20:25:19 +00:00
2016-10-26 06:54:48 +00:00
if ( is_array ( $rule )) {
2018-03-14 20:25:19 +00:00
if ( empty ( $rule [ 'builder' ])) {
// convert old rules when editing
$builder = QueryBuilderParser :: fromOld ( $rule [ 'rule' ]) -> toArray ();
} else {
$builder = json_decode ( $rule [ 'builder' ]);
}
header ( 'Content-type: application/json' );
echo json_encode ([
'extra' => isset ( $rule [ 'extra' ]) ? json_decode ( $rule [ 'extra' ]) : null ,
'maps' => $maps ,
2015-07-13 20:10:26 +02:00
'name' => $rule [ 'name' ],
2016-06-06 11:49:50 +02:00
'proc' => $rule [ 'proc' ],
2018-03-14 20:25:19 +00:00
'builder' => $builder ,
'severity' => $rule [ 'severity' ],
]);
2014-11-30 17:49:52 +00:00
}