} if ($vars['adminstatus'] == "stop") { $where .= " AND bgpPeerAdminStatus = 'stop'"; } elseif ($vars['adminstatus'] == "start") { $where .= " AND bgpPeerAdminStatus = 'start'"; } if ($vars['state'] == "down") { $where .= " AND bgpPeerState = 'down'"; } $sql = 'SELECT * FROM `bgpPeers` AS B LEFT JOIN `bgpPeers-state` AS S ON B.bgpPeer_id = S.bgpPeer_id WHERE `device_id` = ? ' . $where . ' ORDER BY `bgpPeerRemoteAs`, `bgpPeerRemoteAddr`'; foreach (dbFetchRows($sql, array($device['device_id'])) as $peer) { $peer['bgpLocalAs'] = $device['bgpLocalAs']; humanize_bgp($peer); $has_macaccounting = dbFetchCell("SELECT COUNT(*) FROM mac_accounting AS M\n LEFT JOIN `ip_mac` AS I ON M.mac = I.mac_address\n WHERE I.ip_address = ?", array($peer['bgpPeerRemoteAddr'])); unset($peerhost, $peername); $ip_version = strstr($peer['bgpPeerRemoteAddr'], ':') ? 'ipv6' : 'ipv4'; $peerhost = dbFetchRow('SELECT * FROM ' . $ip_version . '_addresses AS A LEFT JOIN ports AS I ON A.port_id = I.port_id LEFT JOIN devices AS D ON I.device_id = D.device_id WHERE A.' . $ip_version . '_address = ?', array($peer['bgpPeerRemoteAddr'])); if ($peerhost['device_id']) { $peername = generate_device_link($peerhost, short_hostname($peerhost['hostname']), array('tab' => 'routing', 'proto' => 'bgp')); $peer['remote_id'] = $peerhost['device_id']; } else { $peername = $peer['reverse_dns']; } unset($sep); foreach (dbFetchRows("SELECT * FROM `bgpPeers_cbgp` WHERE `device_id` = ? AND bgpPeerRemoteAddr = ?", array($device['device_id'], $peer['bgpPeerRemoteAddr'])) as $afisafi) {
/** * Params: * * pagination, pageno, pagesize * device, type, adminstatus, state */ function get_bgp_array($vars) { $array = array(); // With pagination? (display page numbers in header) $array['pagination'] = isset($vars['pagination']) && $vars['pagination']; pagination($vars, 0, TRUE); // Get default pagesize/pageno $array['pageno'] = $vars['pageno']; $array['pagesize'] = $vars['pagesize']; $start = $array['pagesize'] * $array['pageno'] - $array['pagesize']; $pagesize = $array['pagesize']; // Require cached IDs from html/includes/cache-data.inc.php $cache_bgp =& $GLOBALS['cache']['bgp']; // Begin query generate $param = array(); $where = ' WHERE 1 '; foreach ($vars as $var => $value) { if ($value != '') { switch ($var) { case 'device': case 'device_id': $where .= generate_query_values($value, 'B.device_id'); break; case 'peer': case 'peer_id': $where .= generate_query_values($value, 'B.peer_device_id'); break; case 'local_ip': $where .= generate_query_values($value, 'B.bgpPeerLocalAddr'); break; case 'peer_ip': $where .= generate_query_values($value, 'B.bgpPeerRemoteAddr'); break; case 'local_as': $where .= generate_query_values($value, 'D.bgpLocalAs'); break; case 'peer_as': $where .= generate_query_values($value, 'B.bgpPeerRemoteAs'); break; case 'type': if ($value == 'external' || $value == 'ebgp') { $where .= generate_query_values($cache_bgp['external'], 'B.bgpPeer_id'); } else { if ($value == 'internal' || $value == 'ibgp') { $where .= generate_query_values($cache_bgp['internal'], 'B.bgpPeer_id'); } } break; case 'adminstatus': if ($value == 'stop') { $where .= generate_query_values($cache_bgp['start'], 'B.bgpPeer_id', '!='); // NOT IN } else { if ($value == 'start') { $where .= generate_query_values($cache_bgp['start'], 'B.bgpPeer_id'); } } break; case 'state': if ($value == 'down') { $where .= generate_query_values($cache_bgp['up'], 'B.bgpPeer_id', '!='); // NOT IN } else { if ($value == 'up') { $where .= generate_query_values($cache_bgp['up'], 'B.bgpPeer_id'); } } break; } } } // Show peers only for permitted devices $query_permitted = generate_query_values($cache_bgp['permitted'], 'B.bgpPeer_id'); $query = 'FROM `bgpPeers` AS B'; $query_count = 'SELECT COUNT(*) ' . $query . $where . $query_permitted; // Use only bgpPeer_id and device_id in query! $query .= ' LEFT JOIN `bgpPeers-state` AS S ON B.`bgpPeer_id` = S.`bgpPeer_id`'; $query .= ' LEFT JOIN `devices` AS D ON B.`device_id` = D.`device_id`'; $query .= $where . $query_permitted; $query = 'SELECT D.`hostname`, D.`bgpLocalAs`, B.*, S.* ' . $query; $query .= ' ORDER BY D.`hostname`, B.`bgpPeerRemoteAs`, B.`bgpPeerRemoteAddr`'; $query .= " LIMIT {$start},{$pagesize}"; $peer_devices = array(); // Query BGP foreach (dbFetchRows($query, $param) as $entry) { humanize_bgp($entry); // Collect peer devices for AFI/SAFI $peer_devices[$entry['device_id']] = 1; $array['entries'][] = $entry; } // Query AFI/SAFI if (count($peer_devices)) { $query_afi = 'SELECT * FROM `bgpPeers_cbgp` WHERE 1' . generate_query_values(array_keys($peer_devices), 'device_id'); foreach (dbFetchRows($query_afi) as $entry) { $array['afisafi'][$entry['device_id']][$entry['bgpPeerRemoteAddr']][$entry['afi'] . '.' . $entry['safi']] = array('afi' => $entry['afi'], 'safi' => $entry['safi']); } } // Query BGP peers count if ($array['pagination']) { $array['count'] = dbFetchCell($query_count, $param); $array['pagination_html'] = pagination($vars, $array['count']); } else { $array['count'] = count($array['entries']); } return $array; }
function get_status_array($status) { // Mike: I know that there are duplicated variables, but later will remove global global $config, $cache; $max_interval = filter_var($status['max']['interval'], FILTER_VALIDATE_INT, array('options' => array('default' => 24, 'min_range' => 1))); $max_count = filter_var($status['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)); $query_port_permitted = generate_query_permitted(array('port'), array('port_table' => 'I', 'hide_ignored' => TRUE)); // Show Device Status if ($status['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($device, short_hostname($device['hostname'])), 'time' => deviceUptime($device, 'short-3')); } } // Uptime if ($status['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($device, short_hostname($device['hostname'])), 'time' => deviceUptime($device, 'short-3'), 'location' => $device['location']); } } } // Ports Down if ($status['ports'] || $status['neighbours']) { $status['neighbours'] = $status['neighbours'] && !$status['ports']; // Disable 'neighbours' if 'ports' already enabled $query = 'SELECT * FROM `ports` AS I '; if ($status['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 ($status['neighbours']) { $query .= ' AND L.`active` = 1 '; } $query .= $query_port_permitted; $query .= ' AND I.`ifLastChange` >= DATE_SUB(NOW(), INTERVAL ' . $max_interval . ' HOUR) '; if ($status['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($port, short_hostname($port['hostname'])), 'entity_link' => generate_port_link($port, short_ifname($port['port_label'], 13)), 'time' => formatUptime($config['time']['now'] - strtotime($port['ifLastChange'])), 'location' => $device['location']); } } // Ports Errors (only deltas) if ($status['errors']) { foreach ($cache['ports']['errored'] as $port_id) { if (in_array($port_id, $cache['ports']['ignored'])) { continue; } // Skip ignored ports $port = get_port_by_id($port_id); $device = device_by_id_cache($port['device_id']); humanize_port($port); if ($port['ifInErrors_delta']) { $port['string'] .= 'Rx: ' . format_number($port['ifInErrors_delta']); } if ($port['ifInErrors_delta'] && $port['ifOutErrors_delta']) { $port['string'] .= ', '; } if ($port['ifOutErrors_delta']) { $port['string'] .= 'Tx: ' . format_number($port['ifOutErrors_delta']); } $boxes[] = array('sev' => 75, 'class' => 'Port', 'event' => 'Errors', 'device_link' => generate_device_link($device, short_hostname($device['hostname'])), 'entity_link' => generate_port_link($port, short_ifname($port['port_label'], 13)), 'time' => $port['string'], 'location' => $device['location']); } } // Services if ($status['services']) { $query = 'SELECT * FROM `services` AS S '; $query .= 'LEFT JOIN `devices` AS D ON S.`device_id` = D.`device_id` '; $query .= "WHERE S.`service_status` = 'down' AND S.`service_ignore` = 0"; $query .= $query_device_permitted; $query .= 'ORDER BY D.`hostname` ASC'; $entries = dbFetchRows($query); foreach ($entries as $service) { $boxes[] = array('sev' => 50, 'class' => 'Service', 'event' => 'Down', 'device_link' => generate_device_link($service, short_hostname($service['hostname'])), 'entity_link' => $service['service_type'], 'time' => formatUptime($config['time']['now'] - strtotime($service['service_changed']), 'short'), 'location' => $device['location']); } } // BGP if ($status['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'] = strstr($peer['bgpPeerRemoteAddr'], ':') ? TRUE : FALSE; $boxes[] = array('sev' => 75, 'class' => 'BGP Peer', 'event' => 'Down', 'device_link' => generate_device_link($peer, short_hostname($peer['hostname'])), 'entity_link' => $peer_ip, 'wide' => $peer['wide'], 'time' => formatUptime($peer['bgpPeerFsmEstablishedTime'], 'short-3'), 'location' => $device['location']); } } } // Return boxes array return $boxes; }
/** * Params: * * pagination, pageno, pagesize * device, type, adminstatus, state */ function get_bgp_array($vars) { $array = array(); // With pagination? (display page numbers in header) $array['pagination'] = isset($vars['pagination']) && $vars['pagination']; pagination($vars, 0, TRUE); // Get default pagesize/pageno $array['pageno'] = $vars['pageno']; $array['pagesize'] = $vars['pagesize']; $start = $array['pagesize'] * $array['pageno'] - $array['pagesize']; $pagesize = $array['pagesize']; // Require cached IDs from html/includes/cache-data.inc.php $cache_bgp =& $GLOBALS['cache']['bgp']; // Begin query generate $param = array(); $where = ' WHERE 1 '; foreach ($vars as $var => $value) { if ($value != '') { switch ($var) { case 'device': case 'device_id': $where .= generate_query_values($value, 'B.device_id'); break; case 'type': if ($value == 'external' || $value == 'ebgp') { $where .= generate_query_values($cache_bgp['external'], 'B.bgpPeer_id'); } else { if ($value == 'internal' || $value == 'ibgp') { $where .= generate_query_values($cache_bgp['internal'], 'B.bgpPeer_id'); } } break; case 'adminstatus': if ($value == 'stop') { $where .= generate_query_values($cache_bgp['start'], 'B.bgpPeer_id', '!='); // NOT IN } else { if ($value == 'start') { $where .= generate_query_values($cache_bgp['start'], 'B.bgpPeer_id'); } } break; case 'state': if ($value == 'down') { $where .= generate_query_values($cache_bgp['up'], 'B.bgpPeer_id', '!='); // NOT IN } else { if ($value == 'up') { $where .= generate_query_values($cache_bgp['up'], 'B.bgpPeer_id'); } } break; } } } // Cache IP array $cache_ip = dbFetchColumn("SELECT `ipv4_address` FROM `ipv4_addresses` WHERE `ipv4_address` NOT IN (?, ?)" . $GLOBALS['cache']['where']['ports_permitted'], array('127.0.0.1', '0.0.0.0')); $cache_ip = array_merge($cache_ip, dbFetchColumn("SELECT `ipv6_address` FROM `ipv6_addresses` WHERE `ipv6_compressed` NOT IN (?)" . $GLOBALS['cache']['where']['ports_permitted'], array('::1'))); //r($cache_ip); // Show peers only for permitted devices $query_permitted = generate_query_values($cache_bgp['permitted'], 'B.bgpPeer_id'); $query = 'FROM `bgpPeers` AS B'; $query_count = 'SELECT COUNT(*) ' . $query . $where . $query_permitted; // Use only bgpPeer_id and device_id in query! $query .= ' LEFT JOIN `bgpPeers-state` AS S ON B.`bgpPeer_id` = S.`bgpPeer_id`'; $query .= ' LEFT JOIN `devices` AS D ON B.`device_id` = D.`device_id`'; $query .= $where . $query_permitted; $query = 'SELECT D.`hostname`, D.`bgpLocalAs`, B.*, S.* ' . $query; $query .= ' ORDER BY D.`hostname`, B.`bgpPeerRemoteAs`, B.`bgpPeerRemoteAddr`'; $query .= " LIMIT {$start},{$pagesize}"; // Query BGP foreach (dbFetchRows($query, $param) as $entry) { humanize_bgp($entry); $peer_addr = $entry['bgpPeerRemoteAddr']; $peer_devices[$entry['device_id']] = 1; // Collect devices for AFIs query if (!isset($cache_bgp['ips'][$peer_addr])) { $cache_bgp['ips'][$peer_addr] = array(); if (in_array($peer_addr, $cache_ip)) { $peer_addr_type = get_ip_version($peer_addr); if ($peer_addr_type) { $peer_addr_type = 'ipv' . $peer_addr_type; $query_ip = 'SELECT `device_id`, `port_id`, `ifOperStatus`, `ifAdminStatus` FROM `' . $peer_addr_type . '_addresses` JOIN `ports` USING (`port_id`) WHERE `' . $peer_addr_type . '_address` = ?;'; $ip_array = dbFetchRows($query_ip, array($peer_addr)); if (count($ip_array) > 1) { // We have multiple ports for same IPs, complicated logic foreach ($ip_array as $ip) { $device_tmp = device_by_id_cache($ip['device_id']); // Crazy logic, exclude down/disabled ports/devices if (!$device_tmp['bgpLocalAs'] || $device_tmp['status'] == 0 || $ip['ifAdminStatus'] != 'up') { continue; } $cache_bgp['ips'][$peer_addr]['device_id'] = $ip['device_id']; $cache_bgp['ips'][$peer_addr]['port_id'] = $ip['port_id']; } } else { $device_tmp = device_by_id_cache($ip_array[0]['device_id']); if ($device_tmp['bgpLocalAs']) { // We found device in DB by IP, but this device really have BGP? $cache_bgp['ips'][$peer_addr]['device_id'] = $ip_array[0]['device_id']; $cache_bgp['ips'][$peer_addr]['port_id'] = $ip_array[0]['port_id']; } } } //r($cache_bgp['ips'][$peer_addr]); } } $entry['peer_port_id'] = $cache_bgp['ips'][$peer_addr]['port_id']; //$entry['peer_port'] = get_port_by_id_cache($entry['peer_port_id']); $entry['peer_device_id'] = $cache_bgp['ips'][$peer_addr]['device_id']; //$entry['peer_device'] = device_by_id_cache($entry['peer_device_id']); $array['entries'][] = $entry; } // Query AFI/SAFI $query_afi = 'SELECT * FROM `bgpPeers_cbgp` WHERE 1' . generate_query_values(array_keys($peer_devices), 'device_id'); //.generate_query_values(array_keys($cache_bgp['ips']), 'bgpPeerRemoteAddr'); foreach (dbFetchRows($query_afi) as $entry) { $array['afisafi'][$entry['device_id']][$entry['bgpPeerRemoteAddr']][] = $entry['afi'] . '.' . $entry['safi']; } // Query BGP peers count if ($array['pagination']) { $array['count'] = dbFetchCell($query_count, $param); $array['pagination_html'] = pagination($vars, $array['count']); } else { $array['count'] = count($array['entries']); } return $array; }