TRUE)) - display for devices down * * Another statuses: * devices, uptime, ports, errors, services, bgp * * @param array $options * @return none * */ function print_status_old($options) { global $config; $max_interval = filter_var($options['max']['interval'], FILTER_VALIDATE_INT, array('options' => array('default' => 24, 'min_range' => 1))); $max_count = filter_var($options['max']['count'], FILTER_VALIDATE_INT, array('options' => array('default' => 200, 'min_range' => 1))); $string = '' . PHP_EOL; /* $string .= ' ' . PHP_EOL; $string .= ' ' . PHP_EOL; $string .= ' ' . PHP_EOL; $string .= ' ' . PHP_EOL; $string .= ' ' . PHP_EOL; $string .= ' ' . PHP_EOL; // $string .= ' ' . PHP_EOL; $string .= ' ' . PHP_EOL; $string .= ' ' . PHP_EOL; $string .= ' ' . PHP_EOL; $string .= ' ' . PHP_EOL; */ $query_device_permitted = generate_query_permitted(array('device'), array('device_table' => 'D', 'hide_ignored' => TRUE)); $query_port_permitted = generate_query_permitted(array('port'), array('port_table' => 'I', 'hide_ignored' => TRUE)); // Show Device Status if ($options['devices']) { $query = 'SELECT * FROM `devices` AS D'; $query .= ' WHERE D.`status` = 0' . $query_device_permitted; $query .= ' ORDER BY D.`hostname` ASC'; $entries = dbFetchRows($query); foreach ($entries as $device) { $string .= ' ' . PHP_EOL; $string .= ' ' . PHP_EOL; // $string .= ' ' . PHP_EOL; $string .= ' ' . PHP_EOL; $string .= ' ' . PHP_EOL; // $string .= ' ' . PHP_EOL; $string .= ' ' . PHP_EOL; $string .= ' ' . PHP_EOL; } } // Uptime if ($options['uptime']) { if (filter_var($config['uptime_warning'], FILTER_VALIDATE_FLOAT) !== FALSE && $config['uptime_warning'] > 0) { $query = 'SELECT * FROM `devices` AS D'; // Since reboot event more complicated than just device uptime less than some time //$query .= ' WHERE D.`status` = 1 AND D.`uptime` > 0 AND D.`uptime` < ' . $config['uptime_warning']; $query .= ' WHERE D.`status` = 1 AND D.`uptime` > 0 AND D.`last_rebooted` > ?'; $query .= $query_device_permitted; $query .= 'ORDER BY D.`hostname` ASC'; $entries = dbFetchRows($query, array($config['time']['now'] - $config['uptime_warning'] - 10)); foreach ($entries as $device) { $string .= ' ' . PHP_EOL; $string .= ' ' . PHP_EOL; // $string .= ' ' . PHP_EOL; $string .= ' ' . PHP_EOL; $string .= ' ' . PHP_EOL; // $string .= ' ' . PHP_EOL; $string .= ' ' . PHP_EOL; $string .= ' ' . PHP_EOL; } } } // Ports Down if ($options['ports'] || $options['neighbours'] || $options['links']) { $options['neighbours'] = $options['neighbours'] && !$options['ports']; // Disable 'neighbours' if 'ports' already enabled $query = 'SELECT * FROM `ports` AS I '; if ($options['neighbours']) { $query .= 'INNER JOIN `neighbours` AS L ON I.`port_id` = L.`port_id` '; } $query .= 'LEFT JOIN `devices` AS D ON I.`device_id` = D.`device_id` '; $query .= "WHERE D.`status` = 1 AND D.ignore = 0 AND I.ignore = 0 AND I.deleted = 0 AND I.`ifAdminStatus` = 'up' AND (I.`ifOperStatus` = 'lowerLayerDown' OR I.`ifOperStatus` = 'down') "; if ($options['neighbours']) { $query .= ' AND L.`active` = 1 '; } $query .= $query_port_permitted; $query .= ' AND I.`ifLastChange` >= DATE_SUB(NOW(), INTERVAL '.$max_interval.' HOUR) '; if ($options['neighbours']) { $query .= 'GROUP BY L.`port_id` '; } $query .= 'ORDER BY I.`ifLastChange` DESC, D.`hostname` ASC, I.`ifDescr` * 1 ASC '; $entries = dbFetchRows($query); $i = 1; foreach ($entries as $port) { if ($i > $max_count) { $string .= ' '; $string .= ''; $string .= '' . PHP_EOL; break; } humanize_port($port); $string .= ' ' . PHP_EOL; $string .= ' ' . PHP_EOL; // $string .= ' ' . PHP_EOL; $string .= ' ' . PHP_EOL; $string .= ' ' . PHP_EOL; // $string .= ' ' . PHP_EOL; $string .= ' ' . PHP_EOL; $string .= ' ' . PHP_EOL; $i++; } } // Ports Errors (only deltas) if ($options['errors']) { $query = 'SELECT * FROM `ports` AS I '; //$query .= 'LEFT JOIN `ports-state` AS E ON I.`port_id` = E.`port_id` '; $query .= 'LEFT JOIN `devices` AS D ON I.`device_id` = D.`device_id` '; $query .= "WHERE D.`status` = 1 AND I.`ifOperStatus` = 'up' AND (I.`ifInErrors_delta` > 0 OR I.`ifOutErrors_delta` > 0)"; $query .= $query_port_permitted; $query .= 'ORDER BY D.`hostname` ASC, I.`ifDescr` * 1 ASC'; $entries = dbFetchRows($query); foreach ($entries as $port) { humanize_port($port); $string .= ' ' . PHP_EOL; $string .= ' ' . PHP_EOL; // $string .= ' ' . PHP_EOL; $string .= ' ' . PHP_EOL; $string .= ' ' . PHP_EOL; // $string .= ' ' . PHP_EOL; $string .= ' ' . PHP_EOL; $string .= ' ' . PHP_EOL; } } // BGP if ($options['bgp']) { if (isset($config['enable_bgp']) && $config['enable_bgp']) { // Description for BGP states $bgpstates = 'IDLE - Router is searching routing table to see whether a route exists to reach the neighbor. '; $bgpstates .= 'CONNECT - Router found a route to the neighbor and has completed the three-way TCP handshake. '; $bgpstates .= 'OPEN SENT - Open message sent, with parameters for the BGP session. '; $bgpstates .= 'OPEN CONFIRM - Router received agreement on the parameters for establishing session. '; $bgpstates .= 'ACTIVE - Router did not receive agreement on parameters of establishment. '; //$bgpstates .= 'ESTABLISHED - Peering is established; routing begins.'; $query = 'SELECT * FROM `devices` AS D '; $query .= 'LEFT JOIN `bgpPeers` AS B ON B.`device_id` = D.`device_id` '; $query .= "WHERE D.`status` = 1 AND (`bgpPeerAdminStatus` = 'start' OR `bgpPeerAdminStatus` = 'running') AND `bgpPeerState` != 'established' "; $query .= $query_device_permitted; $query .= 'ORDER BY D.`hostname` ASC'; $entries = dbFetchRows($query); foreach ($entries as $peer) { humanize_bgp($peer); $peer_ip = generate_entity_link("bgp_peer", $peer, $peer['human_remoteip']); $string .= ' ' . PHP_EOL; $string .= ' ' . PHP_EOL; // $string .= ' ' . PHP_EOL; $string .= ' ' . PHP_EOL; $string .= ' ' . PHP_EOL; // $string .= ' ' . PHP_EOL; $string .= ' ' . PHP_EOL; $string .= ' ' . PHP_EOL; } } } // $string .= ' ' . PHP_EOL; $string .= '
DeviceTypeStatusEntityLocationInformation
' . generate_device_link_short($device) . 'DeviceDevice Down ' . generate_device_link_short($device) . '' . escape_html(truncate($device['location'], 30)) . '' . deviceUptime($device, 'short') . '
' . generate_device_link_short($device) . 'DeviceDevice Rebooted ' . generate_device_link_short($device) . '' . escape_html(truncate($device['location'], 30)) . 'Uptime ' . format_uptime($device['uptime'], 'short') . '
PortPort DownToo many ports down. See All DOWN ports.
' . generate_device_link_short($port) . 'PortPort Down' . get_icon('port') . ' ' . generate_port_link_short($port) . '' . escape_html(truncate($port['location'], 30)) . 'Down for ' . format_uptime($config['time']['now'] - strtotime($port['ifLastChange']), 'short'); // This is like deviceUptime() if ($options['links']) { $string .= ' ('.nicecase($port['protocol']).': ' .$port['remote_hostname'].' / ' .$port['remote_port'] .')'; } $string .= '
' . generate_device_link_short($port) . 'PortPort Errors' . get_icon('port') . ' ' . generate_port_link_short($port, NULL, 'port_errors') . '' . escape_html(truncate($port['location'], 30)) . 'Errors '; if ($port['ifInErrors_delta']) { $string .= 'In: ' . $port['ifInErrors_delta']; } if ($port['ifInErrors_delta'] && $port['ifOutErrors_delta']) { $string .= ', '; } if ($port['ifOutErrors_delta']) { $string .= 'Out: ' . $port['ifOutErrors_delta']; } $string .= '
' . generate_device_link_short($peer, [ 'tab' => 'routing', 'proto' => 'bgp' ]) . 'BGPBGP ' . nicecase($peer['bgpPeerState']) . ' ' . $peer_ip . '' . escape_html(truncate($peer['location'], 30)) . 'AS' . $peer['human_remote_as'] . ' : ' . $peer['astext'] . '
'; // Final print all statuses echo($string); } function generate_alert_entries($vars) { global $alert_rules; global $config; // This should be set outside, but do it here if it isn't if (!is_array($alert_rules)) { $alert_rules = cache_alert_rules(); } /// WARN HERE $vars['sort'] = 'alert_last_changed'; list($query, $param, $query_count) = build_alert_table_query($vars); // Fetch alerts $count = dbFetchCell($query_count, $param); $alerts = dbFetchRows($query, $param); foreach ($alerts as $alert) { $alert_rule = &$alert_rules[$alert['alert_test_id']]; $alert['severity'] = $alert_rule['severity']; humanize_alert_entry($alert); $device = device_by_id_cache($alert['device_id']); $array[] = array('sev' => 100, 'icon_tag' => '', 'alert_test_id' => $alert['alert_test_id'], 'event' => ''. escape_html($alert_rule['alert_name']). '', 'entity_type' => $alert['entity_type'], 'entity_id' => $alert['entity_id'], 'entity_link' => ($alert['entity_type'] != "device" ? generate_entity_link($alert['entity_type'], $alert['entity_id'],NULL, NULL, TRUE, TRUE) : NULL), 'device_id' => $device['device_id'], 'device_link' => generate_device_link_short($device), 'time' => $alert['changed']); } return $array; } /** * Display status alerts. * * Display pages with alerts about device troubles. * Examples: * print_status(array('devices' => TRUE)) - display for devices down * * Another statuses: * devices, uptime, ports, errors, services, bgp * * @param array $options * * @return void|true * */ function print_status_boxes($options, $limit = NULL) { if (isset($options['widget_type']) && $options['widget_type'] === 'alert_boxes') { $status_array = generate_alert_entries(array('status' => 'failed')); //, 'entity_id' => '1')); } elseif ($status_array = get_status_array($options)) { $status_array = array_sort($status_array, 'sev', 'SORT_DESC'); } $count = safe_count($status_array); if ($count === 0) { echo '
'; echo '
There are currently no ongoing alert events.
'; echo '
'; return; } $i = 1; foreach ($status_array as $entry) { if ($i >= $limit) { echo('
'); echo '
' . $count . ' more...
'; echo('
'); return; } if ($entry['entity_link']) { $entry['entity_link'] = preg_replace('/()(.{0,20}).*(<\/a>)/', '\\1\\2\\3', $entry['entity_link']); } if ($entry['sev'] > 51) { $class = "alert-danger"; } elseif ($entry['sev'] > 20) { $class = "alert-warning"; } else { $class = "alert-info"; } //if ($entry['wide']) { $class .= ' statusbox-wide'; } echo('

'); echo '

' . $entry['device_link'] . '

'; echo '' . $entry['event'] . '
'; echo '

' . ($entry['entity_link'] ? $entry['icon_tag'] . $entry['entity_link'] : 'Device') . '

'; echo '' . $entry['time'] . ''; echo('

'); $count--; $i++; } return TRUE; } function generate_status_table($options, $print = FALSE) { $status_array = get_status_array($options); $status_array = array_sort($status_array, 'sev', 'SORT_DESC'); $i = 1; $string = ''; $string .= '' . PHP_EOL; foreach ($status_array as $entry) { if ($entry['sev'] > 51) { $class = "danger"; $row_class = 'error'; } elseif ($entry['sev'] > 20) { $class = "warning"; $row_class = 'warning'; } else { $class = "info"; } $string .= ' ' . PHP_EOL; $string .= ' '; $string .= ' ' . PHP_EOL; $string .= ' ' . PHP_EOL; $string .= ' ' . PHP_EOL; $string .= ' ' . PHP_EOL; $string .= ' ' . PHP_EOL; } $string .= '
' . $entry['device_link'] . ''.$entry['class'].' '.$entry['event'].''. $entry['icon_tag'] . ($entry['entity_link'] ? $entry['entity_link'] : $entry['device_link']) . '' . $entry['time'] . '
'; if($print == TRUE) { echo $string; return TRUE; } else { return $string; } } // DOCME needs phpdoc block function get_status_array($options) { // Mike: I know that there are duplicated variables, but later will remove global global $config; $max_interval = filter_var($options['max']['interval'], FILTER_VALIDATE_INT, array('options' => array('default' => 24, 'min_range' => 1))); $max_count = filter_var($options['max']['count'], FILTER_VALIDATE_INT, array('options' => array('default' => 200, 'min_range' => 1))); $query_device_permitted = generate_query_permitted(array('device'), array('device_table' => 'D', 'hide_ignored' => TRUE, 'hide_disabled' => TRUE)); $query_port_permitted = generate_query_permitted(array('port'), array('port_table' => 'I', 'hide_ignored' => TRUE)); // Show Device Status if ($options['devices']) { $query = 'SELECT * FROM `devices` AS D '; $query .= 'WHERE D.`status` = 0' . $query_device_permitted; $query .= 'ORDER BY D.`hostname` ASC'; $entries = dbFetchRows($query); foreach ($entries as $device) { $boxes[] = array('sev' => 100, 'class' => 'Device', 'event' => 'Down', 'device_link' => generate_device_link_short($device), 'time' => deviceUptime($device, 'short-3'), 'icon_tag' => ''); } } // Uptime if ($options['uptime']) { if (filter_var($config['uptime_warning'], FILTER_VALIDATE_FLOAT) !== FALSE && $config['uptime_warning'] > 0) { $query = 'SELECT * FROM `devices` AS D '; // Since reboot event more complicated than just device uptime less than some time //$query .= ' WHERE D.`status` = 1 AND D.`uptime` > 0 AND D.`uptime` < ' . $config['uptime_warning']; $query .= ' WHERE D.`status` = 1 AND D.`uptime` > 0 AND D.`last_rebooted` > ?'; $query .= $query_device_permitted; $query .= 'ORDER BY D.`hostname` ASC'; $entries = dbFetchRows($query, array($config['time']['now'] - $config['uptime_warning'] - 10)); foreach ($entries as $device) { $boxes[] = array('sev' => 10, 'class' => 'Device', 'event' => 'Rebooted', 'device_link' => generate_device_link_short($device), 'time' => deviceUptime($device, 'short-3'), 'location' => $device['location'], 'icon_tag' => ''); } } } // Ports Down if ($options['ports'] || $options['neighbours']) { $options['neighbours'] = $options['neighbours'] && !$options['ports']; // Disable 'neighbours' if 'ports' already enabled $query = 'SELECT * FROM `ports` AS I '; if ($options['neighbours']) { $query .= 'INNER JOIN `neighbours` as L ON I.`port_id` = L.`port_id` '; } $query .= 'LEFT JOIN `devices` AS D ON I.`device_id` = D.`device_id` '; $query .= "WHERE D.`status` = 1 AND D.ignore = 0 AND I.ignore = 0 AND I.deleted = 0 AND I.`ifAdminStatus` = 'up' AND (I.`ifOperStatus` = 'lowerLayerDown' OR I.`ifOperStatus` = 'down') "; if ($options['neighbours']) { $query .= ' AND L.`active` = 1 '; } $query .= $query_port_permitted; $query .= ' AND I.`ifLastChange` >= DATE_SUB(NOW(), INTERVAL '.$max_interval.' HOUR) '; if ($options['neighbours']) { $query .= 'GROUP BY L.`port_id` '; } $query .= 'ORDER BY I.`ifLastChange` DESC, D.`hostname` ASC, I.`ifDescr` * 1 ASC '; $entries = dbFetchRows($query); $i = 1; foreach ($entries as $port) { if ($i > $max_count) { // Limit to 200 ports on overview page break; } //humanize_port($port); $boxes[] = array('sev' => 50, 'class' => 'Port', 'event' => 'Down', 'device_link' => generate_device_link_short($port), 'entity_link' => generate_port_link_short($port), 'time' => format_uptime($config['time']['now'] - strtotime($port['ifLastChange'])), 'location' => $device['location'], 'icon_tag' => ''); } } // Ports Errors (only deltas) if ($options['errors']) { foreach ($GLOBALS['cache']['ports']['errored'] as $port_id) { if (in_array($port_id, $GLOBALS['cache']['ports']['ignored'])) { continue; } // Skip ignored ports if (in_array($port['ifType'], $config['ports']['ignore_errors_iftype'])) { continue; } // Skip iftypes we ignore $port = get_port_by_id($port_id); $device = device_by_id_cache($port['device_id']); humanize_port($port); $port['text'] = []; if ($port['ifInErrors_delta']) { $port['text'][] = 'Rx: ' . format_number($port['ifInErrors_delta']) . ' (' . format_number($port['ifInErrors_rate']) . '/s)'; } if ($port['ifOutErrors_delta']) { $port['text'][] = 'Tx: ' . format_number($port['ifOutErrors_delta']) . ' (' . format_number($port['ifOutErrors_rate']) . '/s)'; } $port['string'] = implode(', ', $port['text']); if($port['ifInErrors_rate'] > 1 || $port['ifOutErrors_rate'] > 1) { $sev = 70; } else { $sev = 45; } $boxes[] = array('sev' => $sev, 'class' => 'Port', 'event' => 'Errors', 'device_link' => generate_device_link_short($device), 'entity_link' => generate_port_link_short($port), 'time' => $port['string'], 'location' => $device['location'], 'icon_tag' => ''); } } // BGP if ($options['bgp']) { if (isset($config['enable_bgp']) && $config['enable_bgp']) { $query = 'SELECT * FROM `bgpPeers` AS B '; $query .= 'LEFT JOIN `devices` AS D ON B.`device_id` = D.`device_id` '; //$query .= 'LEFT JOIN `bgpPeers-state` AS BS ON B.`bgpPeer_id` = BS.`bgpPeer_id` '; $query .= "WHERE D.`status` = 1 AND (`bgpPeerAdminStatus` = 'start' OR `bgpPeerAdminStatus` = 'running') AND `bgpPeerState` != 'established' "; $query .= $query_device_permitted; $query .= 'ORDER BY D.`hostname` ASC'; $entries = dbFetchRows($query); foreach ($entries as $peer) { humanize_bgp($peer); $peer_ip = generate_entity_link("bgp_peer", $peer, $peer['human_remoteip']); $peer['wide'] = str_contains($peer['bgpPeerRemoteAddr'], ':'); $boxes[] = array('sev' => 75, 'class' => 'BGP Session', 'event' => 'Down', 'device_link' => generate_device_link_short($peer), 'entity_link' => $peer_ip, 'wide' => $peer['wide'], 'time' => format_uptime($peer['bgpPeerFsmEstablishedTime'], 'short-3'), 'location' => $device['location'], 'icon_tag' => ''); } } } // Return boxes array return $boxes; } // EOF