1<?php
2
3/*
4 * LibreNMS
5 *
6 * Copyright (c) 2014 Neil Lathwood <https://github.com/laf/ http://www.lathwood.co.uk/fa>
7 *
8 * This program is free software: you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation, either version 3 of the License, or (at your
11 * option) any later version.  Please see LICENSE.txt at the top level of
12 * the source code distribution for details.
13 */
14
15use LibreNMS\Alerting\QueryBuilderParser;
16
17if (! Auth::user()->hasGlobalAdmin()) {
18    header('Content-type: text/plain');
19    exit('ERROR: You need to be admin');
20}
21$alert_id = $vars['alert_id'];
22$template_id = $vars['template_id'];
23
24if (is_numeric($alert_id) && $alert_id > 0) {
25    $rule = dbFetchRow('SELECT * FROM `alert_rules` WHERE `id` = ? LIMIT 1', [$alert_id]);
26
27    $maps = [];
28
29    $devices = dbFetchRows('SELECT `device_id`, `hostname`, `sysName` FROM `alert_device_map` LEFT JOIN `devices` USING (`device_id`) WHERE `rule_id`=?', [$alert_id]);
30    foreach ($devices as $device) {
31        $maps[] = ['id' => $device['device_id'], 'text' => format_hostname($device)];
32    }
33
34    $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]);
35    foreach ($groups as $group) {
36        $maps[] = ['id' => 'g' . $group['group_id'], 'text' => $group['name']];
37    }
38    $locations = dbFetchRows('SELECT `location_id`, `location` FROM `alert_location_map` LEFT JOIN `locations` ON `locations`.`id`=`alert_location_map`.`location_id` WHERE `rule_id`=?', [$alert_id]);
39    foreach ($locations as $location) {
40        $maps[] = ['id' => 'l' . $location['location_id'], 'text' => $location['location']];
41    }
42
43    $transports = [];
44    $members = dbFetchRows('SELECT `transport_or_group_id`, `transport_name`, `transport_type` FROM `alert_transport_map` LEFT JOIN `alert_transports` ON `transport_or_group_id` = `transport_id` WHERE `target_type`="single" AND `rule_id`=?', [$alert_id]);
45
46    foreach ($members as $member) {
47        $transports[] = [
48            'id' => $member['transport_or_group_id'],
49            'text' => ucfirst($member['transport_type']) . ': ' . $member['transport_name'],
50        ];
51    }
52
53    $t_groups = dbFetchRows('SELECT `transport_or_group_id`, `transport_group_name` FROM `alert_transport_map` LEFT JOIN `alert_transport_groups` ON `transport_or_group_id`=`transport_group_id` WHERE `target_type`="group" AND `rule_id`=?', [$alert_id]);
54    foreach ($t_groups as $group) {
55        $transports[] = [
56            'id' => 'g' . $group['transport_or_group_id'],
57            'text' => 'Group: ' . $group['transport_group_name'],
58        ];
59    }
60} elseif (is_numeric($template_id) && $template_id >= 0) {
61    $tmp_rules = get_rules_from_json();
62    $rule = $tmp_rules[$template_id];
63    $maps = [];
64}
65
66if (is_array($rule)) {
67    if (empty($rule['builder'])) {
68        // convert old rules when editing
69        $builder = QueryBuilderParser::fromOld($rule['rule'])->toArray();
70    } else {
71        $builder = json_decode($rule['builder']);
72    }
73
74    header('Content-type: application/json');
75    echo json_encode([
76        'extra'      => isset($rule['extra']) ? json_decode($rule['extra']) : null,
77        'maps'       => $maps,
78        'transports' => $transports,
79        'name'       => $rule['name'],
80        'proc'       => $rule['proc'],
81        'builder'    => $builder,
82        'severity'   => $rule['severity'],
83        'adv_query'  => $rule['query'],
84        'invert_map'  => $rule['invert_map'],
85    ]);
86}
87