. * * @link https://www.librenms.org * * @copyright 2020 The LibreNMS Community * @author Original Author * @author Joseph Tingiris */ if (! Auth::user()->hasGlobalAdmin()) { exit('ERROR: You need to be admin'); } use App\Facades\DeviceCache; use LibreNMS\Alerting\QueryBuilderParser; use LibreNMS\Enum\AlertState; $no_refresh = true; ?>
false, 'count' => -1, 'delay' => 300, 'invert' => false, 'interval' => 300, ]; foreach ($default_rules as $add_rule) { $extra = $default_extra; if (isset($add_rule['extra'])) { $extra = array_replace($extra, json_decode($add_rule['extra'], true)); } $qb = QueryBuilderParser::fromOld($add_rule['rule']); $insert = [ 'rule' => '', 'builder' => json_encode($qb), 'query' => $qb->toSql(), 'severity' => 'critical', 'extra' => json_encode($extra), 'disabled' => 0, 'name' => $add_rule['name'], ]; dbInsert($insert, 'alert_rules'); } unset($qb); } require_once 'includes/html/modal/new_alert_rule.inc.php'; require_once 'includes/html/modal/delete_alert_rule.inc.php'; // Also dies if !Auth::user()->hasGlobalAdmin() require_once 'includes/html/modal/alert_rule_collection.inc.php'; // Also dies if !Auth::user()->hasGlobalAdmin() require_once 'includes/html/modal/alert_rule_list.inc.php'; // Also dies if !Auth::user()->hasGlobalAdmin() require_once 'includes/html/modal/edit_transport_group.inc.php'; require_once 'includes/html/modal/edit_alert_transport.inc.php'; echo '
'; echo csrf_field(); if (isset($_POST['results_amount']) && $_POST['results_amount'] > 0) { $results = $_POST['results']; } else { $results = 50; } echo '
'; echo '
'; echo ''; echo ' - OR - '; echo ''; echo '
'; echo '
'; echo ''; echo '
'; echo '
'; echo '
'; $param = []; if (isset($device['device_id']) && $device['device_id'] > 0) { //device selected $global_rules = 'SELECT ar1.* FROM alert_rules AS ar1 WHERE ar1.id NOT IN (SELECT agm1.rule_id FROM alert_group_map AS agm1 UNION DISTINCT SELECT adm1.rule_id FROM alert_device_map AS adm1)'; $device_rules = 'SELECT ar2.* FROM alert_rules AS ar2 WHERE ar2.id IN (SELECT adm2.rule_id FROM alert_device_map AS adm2 WHERE adm2.device_id=?)'; $param[] = $device['device_id']; $device_group_rules = 'SELECT ar3.* FROM alert_rules AS ar3 WHERE ar3.id IN (SELECT agm3.rule_id FROM alert_group_map AS agm3 LEFT JOIN device_group_device AS dgd3 ON agm3.group_id=dgd3.device_group_id WHERE dgd3.device_id=?)'; $param[] = $device['device_id']; $device_location_rules = 'SELECT ar4.* FROM alert_rules AS ar4 WHERE ar4.id IN (SELECT alm4.rule_id FROM alert_location_map AS alm4 LEFT JOIN devices AS d4 ON alm4.location_id=d4.location_id WHERE d4.device_id=?)'; $param[] = $device['device_id']; $full_query = '(' . $global_rules . ') UNION DISTINCT (' . $device_rules . ') UNION DISTINCT (' . $device_group_rules . ') UNION DISTINCT (' . $device_location_rules . ')'; } else { // no device selected $full_query = 'SELECT alert_rules.* FROM alert_rules'; } $full_query .= ' ORDER BY name ASC'; $rule_list = dbFetchRows($full_query, $param); $count = count($rule_list); if (isset($_POST['page_number']) && $_POST['page_number'] > 0 && $_POST['page_number'] <= $count) { $page_number = $_POST['page_number']; } else { $page_number = 1; } $start = (($page_number - 1) * $results); ?>
$start + $results) { break; } $sub = dbFetchRows('SELECT * FROM alerts WHERE rule_id = ? ORDER BY `state` DESC, `id` DESC LIMIT 1', [$rule['id']]); $severity = dbFetchCell('SELECT severity FROM alert_rules where id = ?', [$rule['id']]); $ico = 'check'; $col = 'success'; $extra = ''; $status_msg = ''; if (sizeof($sub) == 1) { $sub = $sub[0]; if ((int) $sub['state'] === AlertState::CLEAR) { $ico = 'check'; $col = 'success'; $status_msg = 'All devices matching ' . $rule['name'] . ' are OK'; } if ((int) $sub['state'] === AlertState::ACTIVE || (int) $sub['state'] === AlertState::ACKNOWLEDGED) { $alert_style = alert_layout($severity); $ico = $alert_style['icon']; $col = $alert_style['icon_color']; $extra = $alert_style['background_color']; $status_msg = 'Some devices matching ' . $rule['name'] . ' are currently alerting'; } } $alert_checked = ''; $orig_ico = $ico; $orig_col = $col; $orig_class = $extra; if ($rule['disabled']) { $ico = 'pause'; $col = ''; $extra = 'active'; $status_msg = $rule['name'] . ' is OFF'; } else { $alert_checked = 'checked'; } $rule_extra = json_decode($rule['extra'], true); $device_count = dbFetchCell('SELECT COUNT(*) FROM alert_device_map WHERE rule_id=?', [$rule['id']]); $group_count = dbFetchCell('SELECT COUNT(*) FROM alert_group_map WHERE rule_id=?', [$rule['id']]); $location_count = dbFetchCell('SELECT COUNT(*) FROM alert_location_map WHERE rule_id=?', [$rule['id']]); $popover_msg_parts = []; $icon_indicator = 'fa fa-globe fa-fw text-success'; if ($device_count) { $popover_msg_parts[] = 'Device'; $icon_indicator = 'fa fa-server fa-fw text-primary'; } if ($group_count) { $popover_msg_parts[] = 'Group'; $icon_indicator = 'fa fa-th fa-fw text-primary'; } if ($location_count) { $popover_msg_parts[] = 'Location'; $icon_indicator = 'fa fa-th fa-fw text-primary'; } if (count($popover_msg_parts)) { $popover_msg = implode(', ', $popover_msg_parts); } else { $popover_msg = 'Global'; } $popover_msg .= ' alert Rule #' . $rule['id']; echo ""; // Type echo ""; // Name echo ''; // Devices (and Groups) if ($rule['invert_map'] == 0) { $groups_msg = 'Only devices in this group.'; $devices_msg = 'Only this device.'; $except_device_or_group = null; } if ($rule['invert_map'] == 1) { $devices_msg = 'All devices EXCEPT this device. '; $groups_msg = 'All devices EXCEPT this group.'; $except_device_or_group = 'EXCEPT '; } $popover_position = 'right'; $locations = null; if ($location_count) { $location_query = 'SELECT locations.location, locations.id FROM alert_location_map, locations WHERE alert_location_map.rule_id=? and alert_location_map.location_id = locations.id ORDER BY location'; $location_maps = dbFetchRows($location_query, [$rule['id']]); foreach ($location_maps as $location_map) { $locations .= $except_device_or_group . '' . htmlentities($location_map['location']) . '
'; } } $groups = null; if ($group_count) { $group_query = 'SELECT device_groups.name, device_groups.id FROM alert_group_map, device_groups WHERE alert_group_map.rule_id=? and alert_group_map.group_id = device_groups.id ORDER BY name'; $group_maps = dbFetchRows($group_query, [$rule['id']]); foreach ($group_maps as $group_map) { $groups .= $except_device_or_group . '' . htmlentities($group_map['name']) . '
'; } } $devices = null; if ($device_count) { $device_query = 'SELECT devices.device_id,devices.hostname FROM alert_device_map, devices WHERE alert_device_map.rule_id=? and alert_device_map.device_id = devices.device_id ORDER BY hostname'; $device_maps = dbFetchRows($device_query, [$rule['id']]); foreach ($device_maps as $device_map) { $devices .= $except_device_or_group . '' . htmlentities(DeviceCache::get($device_map['device_id'])->displayName()) . '
'; } } echo "'; // Transports $transport_count = dbFetchCell('SELECT COUNT(*) FROM alert_transport_map WHERE rule_id=?', [$rule['id']]); $transports_popover = 'right'; $transports = null; if ($transport_count) { $transport_maps = dbFetchRows('SELECT transport_or_group_id,target_type FROM alert_transport_map WHERE alert_transport_map.rule_id=? ORDER BY target_type', [$rule['id']]); foreach ($transport_maps as $transport_map) { $transport_name = null; if ($transport_map['target_type'] == 'group') { $transport_name = dbFetchCell('SELECT transport_group_name FROM alert_transport_groups WHERE transport_group_id=?', [$transport_map['transport_or_group_id']]); $transport_edit = "" . $transport_name . ''; } if ($transport_map['target_type'] == 'single') { $transport_name = dbFetchCell('SELECT transport_name FROM alert_transports WHERE transport_id=?', [$transport_map['transport_or_group_id']]); $transport_edit = "" . $transport_name . ''; } $transports .= $transport_edit . '
'; } } if (! $transport_count || ! $transports) { $default_transports = dbFetchRows('SELECT transport_id, transport_name FROM alert_transports WHERE is_default=1 ORDER BY transport_name', []); foreach ($default_transports as $default_transport) { $transport_edit = "" . $default_transport['transport_name'] . ''; $transports .= $transport_edit . '
'; } } if (! $transports) { $transports = 'none'; } echo ""; // Extra echo ''; // Rule echo "'; // Severity echo ''; // Status $status_popover = 'top'; echo "'; // Enabled $enabled_popover = 'top'; echo ''; // Action $action_popover = 'left'; echo ''; echo "\r\n"; }//end foreach ?>
Type Name Devices Transports Notification Settings Rule Severity Status Enabled Action
' . $rule['name'] . '"; if ($locations) { echo $locations; } if ($groups) { echo $groups; } if ($devices) { echo $devices; } if (! $devices && ! $groups && ! $locations) { // All Devices echo 'All Devices
'; } echo '
$transportsMax: ' . $rule_extra['count'] . '
Delay: ' . $rule_extra['delay'] . '
Interval: ' . $rule_extra['interval'] . '
"; if ($rule_extra['invert'] === true) { echo 'Inverted '; } if (empty($rule['builder'])) { $rule_display = $rule['rule']; } elseif ($rule_extra['options']['override_query'] === 'on') { $rule_display = 'Custom SQL Query'; } else { $rule_display = QueryBuilderParser::fromJson($rule['builder'])->toSql(false); } echo '' . htmlentities($rule_display) . '' . ($rule['severity'] == 'ok' ? strtoupper($rule['severity']) : ucwords($rule['severity'])) . '"; if ($rule_extra['mute'] === true) { echo ""; } if ($sub['state'] == AlertState::ACKNOWLEDGED) { echo ""; } echo ''; if ($rule['disabled']) { $enabled_msg = $rule['name'] . ' is OFF'; } if (! $rule['disabled']) { $enabled_msg = $rule['name'] . ' is ON'; } echo "
"; echo ""; echo '
'; echo '
'; echo "
"; echo " "; echo ""; echo '
$results) { echo '
'; echo '
'; echo generate_pagination($count, $results, $page_number); echo '
'; echo '
'; $showing_start = ($page_number * $results) - $results + 1; $showing_end = $page_number * $results; if ($showing_end > $count) { $showing_end = $count; } echo "

Showing $showing_start to $showing_end of $count alert rules

"; echo '
'; echo '
'; } echo '
'; if ($count < 1) { echo '
' . csrf_field() . '

'; } ?>