function generate_port_link($port, $text = NULL, $type = NULL, $escape = FALSE) { global $config; humanize_port($port); //if (!isset($port['html_class'])) { $port['html_class'] = ifclass($port['ifOperStatus'], $port['ifAdminStatus']); } //if (!isset($text)) { $text = rewrite_ifname($port['label'], !$escape); } // Negative escape flag for exclude double escape // Fixme -- does this function even need alternative $text? I think not. It's a hangover from before label. if (!isset($text)) { $text = $port['label']; } if (port_permitted($port['port_id'], $port['device_id'])) { $url = generate_port_url($port); if ($escape) { $text = escape_html($text); } return '<a href="' . $url . '" class="entity-popup ' . $port['html_class'] . '" data-eid="' . $port['port_id'] . '" data-etype="port">' . $text . '</a>'; } else { return rewrite_ifname($text); } }
function get_port_by_ifIndex($device_id, $ifIndex) { $port = dbFetchRow("SELECT * FROM `ports` WHERE `device_id` = ? AND `ifIndex` = ? LIMIT 1", array($device_id, $ifIndex)); if (is_array($port)) { humanize_port($port); return $port; } return FALSE; }
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; }
function get_status_array($status) { // Mike: I know that there are duplicated variables, but later will remove global global $config; global $cache; $param = array(); if ($_SESSION['userlevel'] >= 5) { $query_perms = ''; $query_user = ''; } else { $query_perms = 'LEFT JOIN devices_perms AS P ON D.device_id = P.device_id '; $query_user = '******'; $param[] = $_SESSION['user_id']; } // Don't show ignored and disabled devices $query_device = ' AND D.ignore = 0 '; if (!$config['web_show_disabled']) { $query_device .= 'AND D.disabled = 0 '; } // Show Device Status if ($status['devices']) { $query = 'SELECT * FROM `devices` AS D '; $query .= $query_perms; $query .= 'WHERE D.status = 0' . $query_device . $query_user; $query .= 'ORDER BY D.hostname ASC'; $entries = dbFetchRows($query, $param); foreach ($entries as $device) { $boxes[] = array('sev' => 100, 'class' => 'Device', 'event' => 'Down', 'device_link' => generate_device_link($device, shorthost($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 '; $query .= $query_perms; $query .= 'WHERE D.status = 1 AND D.uptime > 0 AND D.uptime < ' . $config['uptime_warning'] . $query_device . $query_user; $query .= 'ORDER BY D.hostname ASC'; $entries = dbFetchRows($query, $param); foreach ($entries as $device) { $boxes[] = array('sev' => 10, 'class' => 'Device', 'event' => 'Rebooted', 'device_link' => generate_device_link($device, shorthost($device['hostname'])), 'time' => deviceUptime($device, 'short-3'), 'location' => $device['location']); } } } // Ports Down if ($status['ports'] || $status['links']) { // warning about deprecated option: $config['warn']['ifdown'] if (isset($config['warn']['ifdown']) && !$config['warn']['ifdown']) { echo ' <div class="alert"> <button type="button" class="close" data-dismiss="alert">×</button> <p><i class="oicon-bell"></i> <strong>Config option obsolete</strong></p> <p>Please note that config option <strong>$config[\'warn\'][\'ifdown\']</strong> is now obsolete.<br />Use options: <strong>$config[\'frontpage\'][\'device_status\'][\'ports\']</strong> and <strong>$config[\'frontpage\'][\'device_status\'][\'errors\']</strong></p> <p>To remove this message, delete <strong>$config[\'warn\'][\'ifdown\']</strong> from configuration file.</p> </div>'; } $query = 'SELECT * FROM `ports` AS I '; if ($status['links'] && !$status['ports']) { $query .= 'INNER JOIN links as L ON I.port_id = L.local_port_id '; } $query .= 'LEFT JOIN `devices` AS D ON I.device_id = D.device_id '; $query .= $query_perms; $query .= "WHERE I.ifOperStatus = 'down' AND I.ifAdminStatus = 'up' AND I.ignore = 0 AND I.deleted = 0 "; if ($status['links'] && !$status['ports']) { $query .= ' AND L.active = 1 '; } $query .= $query_device . $query_user; $query .= ' AND I.ifLastChange >= DATE_SUB(NOW(), INTERVAL 24 HOUR) '; $query .= 'ORDER BY I.ifLastChange DESC, D.hostname ASC, I.ifDescr * 1 ASC '; $entries = dbFetchRows($query, $param); //$count = count($entries); $i = 1; foreach ($entries as $port) { if ($i > 200) { // Limit to 200 ports on overview page $string .= ' <tr><td></td><td><span class="badge badge-info">Port</span></td>'; $string .= '<td><span class="label label-important">Port Down</span></td>'; $string .= '<td colspan=3>Too many ports down. See <strong><a href="' . generate_url(array('page' => 'ports'), array('state' => 'down')) . '">All DOWN ports</a></strong>.</td></tr>' . PHP_EOL; break; } humanize_port($port); $boxes[] = array('sev' => 50, 'class' => 'Port', 'event' => 'Down', 'device_link' => generate_device_link($port, shorthost($port['hostname'])), 'entity_link' => generate_port_link($port, truncate(makeshortif($port['label']), 13, '')), 'time' => formatUptime($config['time']['now'] - strtotime($port['ifLastChange'])), 'location' => $device['location']); // We don't do anything with this here at the moment. There is no comment on it, what is it for? // if ($status['links'] && !$status['ports']) { $string .= ' ('.strtoupper($port['protocol']).': ' .$port['remote_hostname'].' / ' .$port['remote_port'] .')'; } } } // Ports Errors (only deltas) if ($status['errors']) { foreach ($cache['ports_errored'] as $port_id) { $port = get_port_by_id($port_id); if (port_permitted($port)) { $device = device_by_id_cache($port['device_id']); humanize_port($port); if ($port['ifInErrors_delta']) { $port['string'] .= 'Rx: ' . $port['ifInErrors_delta']; } if ($port['ifInErrors_delta'] && $port['ifOutErrors_delta']) { $port['string'] .= ', '; } if ($port['ifOutErrors_delta']) { $port['string'] .= 'Tx: ' . $port['ifOutErrors_delta']; } $boxes[] = array('sev' => 75, 'class' => 'Port', 'event' => 'Errors', 'device_link' => generate_device_link($device, shorthost($device['hostname'])), 'entity_link' => generate_port_link($port, truncate(makeshortif($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 .= $query_perms; $query .= "WHERE S.service_status = 'down' AND S.service_ignore = 0" . $query_device . $query_user; $query .= 'ORDER BY D.hostname ASC'; $entries = dbFetchRows($query, $param); foreach ($entries as $service) { $boxes[] = array('sev' => 50, 'class' => 'Service', 'event' => 'Down', 'device_link' => generate_device_link($service, shorthost($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']) { // 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 .= $query_perms; $query .= "WHERE (bgpPeerAdminStatus = 'start' OR bgpPeerAdminStatus = 'running') AND bgpPeerState != 'established' " . $query_device . $query_user; $query .= 'ORDER BY D.hostname ASC'; $entries = dbFetchRows($query, $param); foreach ($entries as $peer) { $peer_ip = strstr($peer['bgpPeerRemoteAddr'], ':') ? Net_IPv6::compress($peer['bgpPeerRemoteAddr']) : $peer['bgpPeerRemoteAddr']; if (strstr($peer['bgpPeerRemoteAddr'], ':')) { $peer['wide'] = TRUE; } $boxes[] = array('sev' => 75, 'class' => 'BGP Peer', 'event' => 'Down', 'device_link' => generate_device_link($peer, shorthost($peer['hostname'])), 'entity_link' => $peer_ip, 'wide' => $peer['wide'], 'time' => formatUptime($peer['bgpPeerFsmEstablishedTime'], 'shorter'), 'location' => $device['location']); } } } $string .= ' </tbody>' . PHP_EOL; $string .= '</table>'; // Final print all statuses return $boxes; }
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')); $query_port_permitted = generate_query_permitted(array('port'), array('port_table' => 'I')); // 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 '; $query .= 'WHERE D.`status` = 1 AND D.`uptime` > 0 AND D.`uptime` < ' . $config['uptime_warning']; $query .= $query_device_permitted; $query .= 'ORDER BY D.`hostname` ASC'; $entries = dbFetchRows($query); 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['links']) { // warning about deprecated option: $config['warn']['ifdown'] if (isset($config['warn']['ifdown']) && !$config['warn']['ifdown']) { print_warning("<strong>Config option obsolete</strong>\n Please note that config option <strong>\$config['warn']['ifdown']</strong> is now obsolete.\n Use options: <strong>\$config['frontpage']['device_status']['ports']</strong> and <strong>\$config['frontpage']['device_status']['errors']</strong>\n To remove this message, delete <strong>\$config['warn']['ifdown']</strong> from configuration file."); } $query = 'SELECT * FROM `ports` AS I '; if ($status['links'] && !$status['ports']) { $query .= 'INNER JOIN `links` as L ON I.`port_id` = L.`local_port_id` '; } $query .= 'LEFT JOIN `devices` AS D ON I.`device_id` = D.`device_id` '; $query .= "WHERE I.`ifOperStatus` = 'down' AND I.`ifAdminStatus` = 'up' "; if ($status['links'] && !$status['ports']) { $query .= ' AND L.`active` = 1 '; } $query .= $query_port_permitted; $query .= ' AND I.`ifLastChange` >= DATE_SUB(NOW(), INTERVAL ' . $max_interval . ' HOUR) '; $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['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) { $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['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 `devices` AS D '; $query .= 'LEFT JOIN `bgpPeers` AS B ON B.`device_id` = D.`device_id` '; $query .= 'LEFT JOIN `bgpPeers-state` AS BS ON B.`bgpPeer_id` = BS.`bgpPeer_id` '; $query .= "WHERE (`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) { $peer_ip = strstr($peer['bgpPeerRemoteAddr'], ':') ? Net_IPv6::compress($peer['bgpPeerRemoteAddr']) : $peer['bgpPeerRemoteAddr']; $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; }
<?php $i = 0; foreach (explode(",", $vars['id']) as $ifid) { $port = dbFetchRow("SELECT * FROM `ports` AS I, devices as D WHERE I.port_id = ? AND I.device_id = D.device_id", array($ifid)); $rrdfile = get_port_rrdfilename($port, $port); if (is_file($rrdfile)) { humanize_port($port); $rrd_list[$i]['filename'] = $rrdfile; $rrd_list[$i]['descr'] = $port['hostname'] . " " . $port['ifDescr']; $rrd_list[$i]['descr_in'] = $port['hostname']; $rrd_list[$i]['descr_out'] = short_ifname($port['label']); $i++; } } $units = 'b'; $total_units = 'B'; $colours_in = 'greens'; $multiplier = "8"; $colours_out = 'blues'; $ds_in = "INOCTETS"; $ds_out = "OUTOCTETS"; include "includes/graphs/generic_multi_bits_separated.inc.php";
/** * Display ARP/NDP table addresses. * * Display pages with ARP/NDP tables addresses from devices. * * @param array $vars * @return none * */ function print_arptable($vars) { // With pagination? (display page numbers in header) $pagination = isset($vars['pagination']) && $vars['pagination']; $pageno = isset($vars['pageno']) && !empty($vars['pageno']) ? $vars['pageno'] : 1; $pagesize = isset($vars['pagesize']) && !empty($vars['pagesize']) ? $vars['pagesize'] : 10; $start = $pagesize * $pageno - $pagesize; $param = array(); $where = ' WHERE 1 '; foreach ($vars as $var => $value) { if ($value != '') { switch ($var) { case 'device': case 'device_id': $where .= ' AND I.`device_id` = ?'; $param[] = $value; break; case 'port': case 'port_id': $where .= ' AND I.`port_id` = ?'; $param[] = $value; break; case 'ip_version': $where .= ' AND `ip_version` = ?'; $param[] = $value; break; case 'address': if (isset($vars['searchby']) && $vars['searchby'] == 'ip') { $where .= ' AND `ip_address` LIKE ?'; $value = trim($value); ///FIXME. Need another conversion ("2001:b08:b08" -> "2001:0b08:0b08") -- mike if (Net_IPv6::checkIPv6($value)) { $value = Net_IPv6::uncompress($value, true); } $param[] = '%' . $value . '%'; } else { $where .= ' AND `mac_address` LIKE ?'; $param[] = '%' . str_replace(array(':', ' ', '-', '.', '0x'), '', mres($value)) . '%'; } break; } } } // Show ARP tables only for permitted ports $query_permitted = generate_query_permitted(array('port'), array('port_table' => 'I')); $query = 'FROM `ip_mac` AS M '; $query .= 'LEFT JOIN `ports` AS I ON I.`port_id` = M.`port_id` '; $query .= $where . $query_permitted; $query_count = 'SELECT COUNT(`mac_id`) ' . $query; $query = 'SELECT * ' . $query; $query .= ' ORDER BY M.`mac_address`'; $query .= " LIMIT {$start},{$pagesize}"; // Query ARP/NDP table addresses $entries = dbFetchRows($query, $param); // Query ARP/NDP table address count if ($pagination) { $count = dbFetchCell($query_count, $param); } $list = array('device' => FALSE, 'port' => FALSE); if (!isset($vars['device']) || empty($vars['device']) || $vars['page'] == 'search') { $list['device'] = TRUE; } if (!isset($vars['port']) || empty($vars['port']) || $vars['page'] == 'search') { $list['port'] = TRUE; } $string = '<table class="table table-bordered table-striped table-hover table-condensed">' . PHP_EOL; if (!$short) { $string .= ' <thead>' . PHP_EOL; $string .= ' <tr>' . PHP_EOL; $string .= ' <th>MAC Address</th>' . PHP_EOL; $string .= ' <th>IP Address</th>' . PHP_EOL; if ($list['device']) { $string .= ' <th>Device</th>' . PHP_EOL; } if ($list['port']) { $string .= ' <th>Interface</th>' . PHP_EOL; } $string .= ' <th>Remote Device</th>' . PHP_EOL; $string .= ' <th>Remote Interface</th>' . PHP_EOL; $string .= ' </tr>' . PHP_EOL; $string .= ' </thead>' . PHP_EOL; } $string .= ' <tbody>' . PHP_EOL; foreach ($entries as $entry) { humanize_port($entry); $ip_version = $entry['ip_version']; $ip_address = $ip_version == 6 ? Net_IPv6::compress($entry['ip_address']) : $entry['ip_address']; $arp_host = dbFetchRow('SELECT * FROM `ipv' . $ip_version . '_addresses` AS A LEFT JOIN `ports` AS I ON A.`port_id` = I.`port_id` LEFT JOIN `devices` AS D ON D.`device_id` = I.`device_id` WHERE A.`ipv' . $ip_version . '_address` = ?', array($ip_address)); $arp_name = $arp_host ? generate_device_link($arp_host) : ''; $arp_if = $arp_host ? generate_port_link($arp_host) : ''; if ($arp_host['device_id'] == $entry['device_id']) { $arp_name = 'Self Device'; } if ($arp_host['port_id'] == $entry['port_id']) { $arp_if = 'Self Port'; } $string .= ' <tr>' . PHP_EOL; $string .= ' <td width="160">' . formatMac($entry['mac_address']) . '</td>' . PHP_EOL; $string .= ' <td width="140">' . $ip_address . '</td>' . PHP_EOL; if ($list['device']) { $dev = device_by_id_cache($entry['device_id']); $string .= ' <td class="entity" nowrap>' . generate_device_link($dev) . '</td>' . PHP_EOL; } if ($list['port']) { if ($entry['ifInErrors_delta'] > 0 || $entry['ifOutErrors_delta'] > 0) { $port_error = generate_port_link($entry, '<span class="label label-important">Errors</span>', 'port_errors'); } $string .= ' <td class="entity">' . generate_port_link($entry, short_ifname($entry['label'])) . ' ' . $port_error . '</td>' . PHP_EOL; } $string .= ' <td class="entity" width="200">' . $arp_name . '</td>' . PHP_EOL; $string .= ' <td class="entity">' . $arp_if . '</td>' . PHP_EOL; $string .= ' </tr>' . PHP_EOL; } $string .= ' </tbody>' . PHP_EOL; $string .= '</table>'; // Print pagination header if ($pagination) { $string = pagination($vars, $count) . $string . pagination($vars, $count); } // Print ARP/NDP table echo $string; }
* @author Adam Armstrong <*****@*****.**> * @copyright (C) 2006-2013 Adam Armstrong, (C) 2013-2016 Observium Limited * */ ?> <div class="row"> <div class="col-md-12"> <?php unset($search, $vlans, $vlan_names, $port_names); // Select ports only present in FDB tables foreach (dbFetchRows('SELECT `port_id`, `device_id`, `ifDescr`, `ifName`, `ifAlias` FROM `vlans_fdb` AS F LEFT JOIN `ports` as P USING (`port_id`, `device_id`) WHERE `device_id` = ? AND `port_id` != 0 GROUP BY `port_id`;', array($device['device_id'])) as $data) { humanize_port($data); $form_items['ports'][$data['port_id']] = $data['port_label']; } natcasesort($form_items['ports']); foreach (dbFetchRows('SELECT `vlan_vlan`, `vlan_name` FROM `vlans_fdb` AS F LEFT JOIN `vlans` as V ON V.`vlan_vlan` = F.`vlan_id` AND V.`device_id` = F.`device_id` WHERE F.`device_id` = ? GROUP BY `vlan_vlan`', array($device['device_id'])) as $data) { $form_items['vlans'][$data['vlan_vlan']] = 'Vlan ' . $data['vlan_vlan']; $form_items['vlan_name'][$data['vlan_name']] = $data['vlan_name']; } ksort($form_items['vlans']); natcasesort($form_items['vlan_name']); $form = array('type' => 'rows', 'space' => '5px', 'submit_by_key' => TRUE, 'url' => 'search/search=fdb/'); $form['row'][0]['port'] = array('type' => 'multiselect', 'name' => 'Ports', 'width' => '100%', 'value' => $vars['port'], 'values' => $form_items['ports']);
/** * Display IPv4/IPv6 addresses. * * Display pages with IP addresses from device Interfaces. * * @param array $vars * @return none * */ function print_addresses($vars) { // With pagination? (display page numbers in header) $pagination = isset($vars['pagination']) && $vars['pagination']; pagination($vars, 0, TRUE); // Get default pagesize/pageno $pageno = $vars['pageno']; $pagesize = $vars['pagesize']; $start = $pagesize * $pageno - $pagesize; if (in_array($vars['search'], array('6', 'v6', 'ipv6')) || in_array($vars['view'], array('6', 'v6', 'ipv6'))) { $address_type = 'ipv6'; } else { $address_type = 'ipv4'; } $ip_array = array(); $param = array(); $where = ' WHERE 1 '; $param_netscaler = array(); $where_netscaler = " WHERE `vsvr_ip` != '0.0.0.0' AND `vsvr_ip` != '' "; foreach ($vars as $var => $value) { if ($value != '') { switch ($var) { case 'device': case 'device_id': $where .= generate_query_values($value, 'I.device_id'); $where_netscaler .= generate_query_values($value, 'N.device_id'); break; case 'interface': $where .= generate_query_values($value, 'I.ifDescr', 'LIKE%'); break; case 'network': list($net, $mask) = explode('/', $value); if (is_numeric(stripos($net, ':abcdef'))) { $address_type = 'ipv6'; } $where .= generate_query_values($value, 'N.ip_network', 'LIKE%'); break; case 'address': list($addr, $mask) = explode('/', $value); if (is_numeric(stripos($addr, ':abcdef'))) { $address_type = 'ipv6'; } switch ($address_type) { case 'ipv6': $ip_valid = Net_IPv6::checkIPv6($addr); break; case 'ipv4': $ip_valid = Net_IPv4::validateIP($addr); break; } if ($ip_valid) { // If address valid -> seek occurrence in network if (!$mask) { $mask = $address_type === 'ipv4' ? '32' : '128'; } $where_netscaler .= generate_query_values($addr, 'N.vsvr_ip'); } else { // If address not valid -> seek LIKE $where .= generate_query_values($addr, 'A.ip_address', '%LIKE%'); $where_netscaler .= generate_query_values($addr, 'N.vsvr_ip', '%LIKE%'); } break; } } } $query_device_permitted = generate_query_permitted(array('device'), array('device_table' => 'D')); $query_port_permitted = generate_query_permitted(array('port'), array('port_table' => 'I')); // Also search netscaler Vserver IPs $query_netscaler = 'FROM `netscaler_vservers` AS N '; $query_netscaler .= 'LEFT JOIN `devices` AS D ON N.`device_id` = D.`device_id` '; $query_netscaler .= $where_netscaler . $query_device_permitted; //$query_netscaler_count = 'SELECT COUNT(`vsvr_id`) ' . $query_netscaler; $query_netscaler = 'SELECT * ' . $query_netscaler; $query_netscaler .= ' ORDER BY N.`vsvr_ip`'; // Override by address type if ($address_type == 'ipv6') { $query_netscaler = str_replace(array('vsvr_ip', '0.0.0.0'), array('vsvr_ipv6', '0:0:0:0:0:0:0:0'), $query_netscaler); //$query_netscaler_count = str_replace(array('vsvr_ip', '0.0.0.0'), array('vsvr_ipv6', '0:0:0:0:0:0:0:0'), $query_netscaler_count); } $entries = dbFetchRows($query_netscaler, $param_netscaler); // Rewrite netscaler addresses foreach ($entries as $entry) { $ip_address = $address_type == 'ipv4' ? $entry['vsvr_ip'] : $entry['vsvr_' . $address_type]; $ip_network = $address_type == 'ipv4' ? $entry['vsvr_ip'] . '/32' : $entry['vsvr_' . $address_type] . '/128'; $ip_array[] = array('type' => 'netscaler_vsvr', 'device_id' => $entry['device_id'], 'hostname' => $entry['hostname'], 'vsvr_id' => $entry['vsvr_id'], 'vsvr_label' => $entry['vsvr_label'], 'ifAlias' => 'Netscaler: ' . $entry['vsvr_type'] . '/' . $entry['vsvr_entitytype'], $address_type . '_address' => $ip_address, $address_type . '_network' => $ip_network); } //print_message($query_netscaler_count); $query = 'FROM `ip_addresses` AS A '; $query .= 'LEFT JOIN `ports` AS I ON I.`port_id` = A.`port_id` '; $query .= 'LEFT JOIN `devices` AS D ON I.`device_id` = D.`device_id` '; $query .= 'LEFT JOIN `ip_networks` AS N ON N.`ip_network_id` = A.`ip_network_id` '; $query .= $where . $query_port_permitted; //$query_count = 'SELECT COUNT(`ip_address_id`) ' . $query; $query = 'SELECT * ' . $query; $query .= ' ORDER BY A.`ip_address`'; if ($ip_valid) { $pagination = FALSE; } // Override by address type $query = str_replace(array('ip_address', 'ip_network'), array($address_type . '_address', $address_type . '_network'), $query); //$query_count = str_replace(array('ip_address', 'ip_network'), array($address_type.'_address', $address_type.'_network'), $query_count); // Query addresses $entries = dbFetchRows($query, $param); $ip_array = array_merge($ip_array, $entries); $ip_array = array_sort($ip_array, $address_type . '_address'); // Query address count //if ($pagination) { $count = dbFetchCell($query_count, $param); } if ($pagination) { $count = count($ip_array); $ip_array = array_slice($ip_array, $start, $pagesize); } $list = array('device' => FALSE); if (!isset($vars['device']) || empty($vars['device']) || $vars['page'] == 'search') { $list['device'] = TRUE; } $string = generate_box_open($vars['header']); $string .= '<table class="' . OBS_CLASS_TABLE_STRIPED . '">' . PHP_EOL; if (!$short) { $string .= ' <thead>' . PHP_EOL; $string .= ' <tr>' . PHP_EOL; if ($list['device']) { $string .= ' <th>Device</th>' . PHP_EOL; } $string .= ' <th>Interface</th>' . PHP_EOL; $string .= ' <th>Address</th>' . PHP_EOL; $string .= ' <th>Description</th>' . PHP_EOL; $string .= ' </tr>' . PHP_EOL; $string .= ' </thead>' . PHP_EOL; } $string .= ' <tbody>' . PHP_EOL; foreach ($ip_array as $entry) { $address_show = TRUE; if ($ip_valid) { // If address not in specified network, don't show entry. if ($address_type === 'ipv4') { $address_show = Net_IPv4::ipInNetwork($entry[$address_type . '_address'], $addr . '/' . $mask); } else { $address_show = Net_IPv6::isInNetmask($entry[$address_type . '_address'], $addr, $mask); } } if ($address_show) { list($prefix, $length) = explode('/', $entry[$address_type . '_network']); if (port_permitted($entry['port_id']) || $entry['type'] == 'netscaler_vsvr') { if ($entry['type'] == 'netscaler_vsvr') { $entity_link = generate_entity_link($entry['type'], $entry); } else { humanize_port($entry); if ($entry['ifInErrors_delta'] > 0 || $entry['ifOutErrors_delta'] > 0) { $port_error = generate_port_link($entry, '<span class="label label-important">Errors</span>', 'port_errors'); } $entity_link = generate_port_link($entry, $entry['port_label_short']) . ' ' . $port_error; } $device_link = generate_device_link($entry); $string .= ' <tr>' . PHP_EOL; if ($list['device']) { $string .= ' <td class="entity" style="white-space: nowrap">' . $device_link . '</td>' . PHP_EOL; } $string .= ' <td class="entity">' . $entity_link . '</td>' . PHP_EOL; if ($address_type === 'ipv6') { $entry[$address_type . '_address'] = Net_IPv6::compress($entry[$address_type . '_address']); } $string .= ' <td>' . generate_popup_link('ip', $entry[$address_type . '_address'] . '/' . $length) . '</td>' . PHP_EOL; $string .= ' <td>' . $entry['ifAlias'] . '</td>' . PHP_EOL; $string .= ' </tr>' . PHP_EOL; } } } $string .= ' </tbody>' . PHP_EOL; $string .= '</table>'; $string .= generate_box_close(); // Print pagination header if ($pagination) { $string = pagination($vars, $count) . $string . pagination($vars, $count); } // Print addresses echo $string; }
function get_port_by_id($port_id) { if (is_numeric($port_id)) { $port = dbFetchRow("SELECT * FROM `ports` LEFT JOIN `ports-state` ON `ports`.`port_id` = `ports-state`.`port_id` WHERE `ports`.`port_id` = ?", array($port_id)); } if (is_array($port)) { humanize_port($port); return $port; } else { return FALSE; } }
function discover_new_device($hostname, $source = 'xdp', $protocol = NULL, $device = NULL, $port = NULL) { global $config, $debug; # FIXME remodel function a bit like the one above? refactor so they share some parts? if ($config['autodiscovery'][$source]) { echo "Discovering new host {$hostname}\n"; if (!empty($config['mydomain']) && isDomainResolves($hostname . "." . $config['mydomain'])) { if ($debug) { echo "appending " . $config['mydomain'] . "!\n"; } $dst_host = $hostname . "." . $config['mydomain']; } else { $dst_host = $hostname; } $ip = gethostbyname($dst_host); if ($debug) { echo "resolving {$dst_host} to {$ip}\n"; } if (match_network($config['autodiscovery']['ip_nets'], $ip)) { if ($debug) { echo "found {$ip} inside configured nets, adding!\n"; } $remote_device_id = add_device($dst_host); if ($remote_device_id) { $remote_device = device_by_id_cache($remote_device_id, 1); if (!$protocol) { $protocol = strtoupper($source); } if ($port) { humanize_port($port); log_event("Device autodiscovered through {$protocol} on " . $device['hostname'] . " (port " . $port['label'] . ")", $remote_device_id, 'interface', $port['port_id']); } else { log_event("Device autodiscovered through {$protocol} on " . $device['hostname'], $remote_device_id); } array_push($GLOBALS['devices'], $remote_device); return $remote_device_id; } } } else { if ($debug) { echo "{$source} autodiscovery disabled"; } return FALSE; } }
/** * Display FDB table. * * @param array $vars * @return none * */ function print_fdbtable($vars) { // With pagination? (display page numbers in header) $pagination = isset($vars['pagination']) && $vars['pagination']; pagination($vars, 0, TRUE); // Get default pagesize/pageno $pageno = $vars['pageno']; $pagesize = $vars['pagesize']; $start = $pagesize * $pageno - $pagesize; $param = array(); $where = ' WHERE 1 '; foreach ($vars as $var => $value) { if ($value != '') { switch ($var) { case 'device': case 'device_id': $where .= generate_query_values($value, 'I.device_id'); break; case 'port': case 'port_id': $where .= generate_query_values($value, 'I.port_id'); break; case 'interface': case 'port_name': $where .= generate_query_values($value, 'I.ifDescr', 'LIKE%'); break; case 'vlan_id': $where .= generate_query_values($value, 'F.vlan_id'); break; case 'vlan_name': $where .= generate_query_values($value, 'V.vlan_name'); break; case 'address': $where .= generate_query_values(str_replace(array(':', ' ', '-', '.', '0x'), '', $value), 'F.mac_address', '%LIKE%'); break; } } } if (isset($vars['sort'])) { switch ($vars['sort']) { case "vlan_id": $sort = " ORDER BY `V`.`vlan_vlan`"; break; case "vlan_name": $sort = " ORDER BY `V`.`vlan_name`"; break; case "port": $sort = " ORDER BY `I`.`port_label`"; break; case "mac": default: $sort = " ORDER BY `mac_address`"; } } // Show FDB tables only for permitted ports $query_permitted = generate_query_permitted(array('port'), array('port_table' => 'I')); $query = 'FROM `vlans_fdb` AS F '; $query .= 'LEFT JOIN `vlans` as V ON V.`vlan_vlan` = F.`vlan_id` AND V.`device_id` = F.`device_id` '; $query .= 'LEFT JOIN `ports` AS I ON I.`port_id` = F.`port_id` '; $query .= $where . $query_permitted; $query_count = 'SELECT COUNT(*) ' . $query; $query = 'SELECT * ' . $query; $query .= $sort; $query .= " LIMIT {$start},{$pagesize}"; // Query addresses $entries = dbFetchRows($query, $param); // Query address count if ($pagination) { $count = dbFetchCell($query_count, $param); } $list = array('device' => FALSE, 'port' => FALSE); if (!isset($vars['device']) || empty($vars['device']) || $vars['page'] == 'search') { $list['device'] = TRUE; } if (!isset($vars['port']) || empty($vars['port']) || $vars['page'] == 'search') { $list['port'] = TRUE; } $string = generate_box_open(); $string .= '<table class="table table-striped table-hover table-condensed">' . PHP_EOL; $cols = array('device' => 'Device', 'mac' => array('MAC Address', 'style="width: 160px;"'), 'status' => array('Status', 'style="width: 100px;"'), 'port' => 'Port', 'vlan_id' => 'VLAN ID', 'vlan_name' => 'VLAN NAME'); if (!$list['device']) { unset($cols['device']); } if (!$list['port']) { unset($cols['port']); } if (!$short) { $string .= get_table_header($cols, $vars); // Currently sorting is not available } foreach ($entries as $entry) { humanize_port($entry); $string .= ' <tr>' . PHP_EOL; if ($list['device']) { $dev = device_by_id_cache($entry['device_id']); $string .= ' <td class="entity" style="white-space: nowrap;">' . generate_device_link($dev) . '</td>' . PHP_EOL; } $string .= ' <td>' . generate_popup_link('mac', format_mac($entry['mac_address'])) . '</td>' . PHP_EOL; $string .= ' <td>' . $entry['fdb_status'] . '</td>' . PHP_EOL; if ($list['port']) { $string .= ' <td class="entity">' . generate_port_link($entry, $entry['port_label_short']) . ' ' . $port_error . '</td>' . PHP_EOL; } $string .= ' <td>Vlan' . $entry['vlan_vlan'] . '</td>' . PHP_EOL; $string .= ' <td>' . $entry['vlan_name'] . '</td>' . PHP_EOL; $string .= ' </tr>' . PHP_EOL; } $string .= ' </tbody>' . PHP_EOL; $string .= '</table>'; $string .= generate_box_close(); // Print pagination header if ($pagination) { $string = pagination($vars, $count) . $string . pagination($vars, $count); } // Print FDB table echo $string; }
function generate_port_row($port, $vars = array()) { global $config, $cache; $device = device_by_id_cache($port['device_id']); humanize_port($port); if (!isset($vars['view'])) { $vars['view'] = "basic"; } // Populate $port_adsl if the port has ADSL-MIB data if (!isset($cache['ports_option']['ports_adsl']) || in_array($port['port_id'], $cache['ports_option']['ports_adsl'])) { $port_adsl = dbFetchRow("SELECT * FROM `ports_adsl` WHERE `port_id` = ?", array($port['port_id'])); } // Populate $port['tags'] with various tags to identify port statuses and features // Port Errors if ($port['ifInErrors_delta'] > 0 || $port['ifOutErrors_delta'] > 0) { $port['tags'] .= generate_port_link($port, '<span class="label label-important">Errors</span>', 'port_errors'); } // Port Deleted if ($port['deleted'] == '1') { $port['tags'] .= '<a href="' . generate_url(array('page' => 'deleted-ports')) . '"><span class="label label-important">Deleted</span></a>'; } // Port CBQoS if (isset($cache['ports_option']['ports_cbqos'])) { if (in_array($port['port_id'], $cache['ports_option']['ports_cbqos'])) { $port['tags'] .= '<a href="' . generate_port_url($port, array('view' => 'cbqos')) . '"><span class="label label-info">CBQoS</span></a>'; } } else { if (dbFetchCell("SELECT COUNT(*) FROM `ports_cbqos` WHERE `port_id` = ?", array($port['port_id']))) { $port['tags'] .= '<a href="' . generate_port_url($port, array('view' => 'cbqos')) . '"><span class="label label-info">CBQoS</span></a>'; } } // Port MAC Accounting if (isset($cache['ports_option']['mac_accounting'])) { if (in_array($port['port_id'], $cache['ports_option']['mac_accounting'])) { $port['tags'] .= '<a href="' . generate_port_url($port, array('view' => 'macaccounting')) . '"><span class="label label-info">MAC</span></a>'; } } else { if (dbFetchCell("SELECT COUNT(*) FROM `mac_accounting` WHERE `port_id` = ?", array($port['port_id']))) { $port['tags'] .= '<a href="' . generate_port_url($port, array('view' => 'macaccounting')) . '"><span class="label label-info">MAC</span></a>'; } } // Populated formatted versions of port rates. $port['bps_in'] = formatRates($port['ifInOctets_rate'] * 8); $port['bps_out'] = formatRates($port['ifOutOctets_rate'] * 8); $port['pps_in'] = format_si($port['ifInUcastPkts_rate']) . "pps"; $port['pps_out'] = format_si($port['ifOutUcastPkts_rate']) . "pps"; $string = ''; if ($vars['view'] == "basic" || $vars['view'] == "graphs") { $table_cols = '8'; $string .= '<tr class="' . $port['row_class'] . '"> <td class="state-marker"></td> <td style="width: 1px;"></td>'; if ($vars['page'] != "device" && $vars['popup'] != TRUE) { $table_cols++; // Increment table columns by one to make sure graph line draws correctly $string .= ' <td style="width: 200px;"><span class="entity">' . generate_device_link($device, short_hostname($device['hostname'], "20")) . '</span><br /> <span class="em">' . escape_html(truncate($port['location'], 32, "")) . '</span></td>'; } $string .= ' <td><span class="entity">' . generate_port_link($port, rewrite_ifname($port['port_label'])) . ' ' . $port['tags'] . '</span><br /> <span class="em">' . escape_html(truncate($port['ifAlias'], 50, '')) . '</span></td>' . '<td style="width: 110px;"> <i class="icon-circle-arrow-down" style="' . $port['bps_in_style'] . '"></i> <span class="small" style="' . $port['bps_in_style'] . '">' . formatRates($port['in_rate']) . '</span><br />' . '<i class="icon-circle-arrow-up" style="' . $port['bps_out_style'] . '"></i> <span class="small" style="' . $port['bps_out_style'] . '">' . formatRates($port['out_rate']) . '</span><br /></td>' . '<td style="width: 90px;"> <i class="icon-circle-arrow-down" style="' . $port['bps_in_style'] . '"></i> <span class="small" style="' . $port['bps_in_style'] . '">' . $port['ifInOctets_perc'] . '%</span><br />' . '<i class="icon-circle-arrow-up" style="' . $port['bps_out_style'] . '"></i> <span class="small" style="' . $port['bps_out_style'] . '">' . $port['ifOutOctets_perc'] . '%</span><br /></td>' . '<td style="width: 110px;"><i class="icon-circle-arrow-down" style="' . $port['pps_in_style'] . '"></i> <span class="small" style="' . $port['pps_in_style'] . '">' . format_bi($port['ifInUcastPkts_rate']) . 'pps</span><br />' . '<i class="icon-circle-arrow-up" style="' . $port['pps_out_style'] . '"></i> <span class="small" style="' . $port['pps_out_style'] . '">' . format_bi($port['ifOutUcastPkts_rate']) . 'pps</span></td>' . '<td style="width: 110px;"><small>' . $port['human_speed'] . '<br />' . $port['ifMtu'] . '</small></td> <td ><small>' . $port['human_type'] . '<br />' . $port['human_mac'] . '</small></td> </tr>'; } else { if ($vars['view'] == "details" || $vars['view'] == "detail") { $table_cols = '9'; $string .= '<tr class="' . $port['row_class'] . '"'; if ($vars['tab'] != "port") { $string .= ' onclick="openLink(\'' . generate_port_url($port) . '\')" style="cursor: pointer;"'; } $string .= '>'; $string .= ' <td class="state-marker"></td> <td style="width: 1px;"></td>'; if ($vars['page'] != "device" && $vars['popup'] != TRUE) { $table_cols++; // Increment table columns by one to make sure graph line draws correctly $string .= ' <td width="200"><span class="entity">' . generate_device_link($device, short_hostname($device['hostname'], "20")) . '</span><br /> <span class="em">' . escape_html(truncate($port['location'], 32, "")) . '</span></td>'; } $string .= ' <td style="min-width: 250px;">'; $string .= ' <span class="entity-title"> ' . generate_port_link($port) . ' ' . $port['tags'] . ' </span><br /><span class="small">' . escape_html($port['ifAlias']) . '</span>'; if ($port['ifAlias']) { $string .= '<br />'; } unset($break); if (!isset($cache['ports_option']['ipv4_addresses']) || in_array($port['port_id'], $cache['ports_option']['ipv4_addresses'])) { foreach (dbFetchRows("SELECT * FROM `ipv4_addresses` WHERE `port_id` = ?", array($port['port_id'])) as $ip) { $string .= $break . generate_popup_link('ip', $ip['ipv4_address'] . '/' . $ip['ipv4_prefixlen'], NULL, 'small'); $break = "<br />"; } } if (!isset($cache['ports_option']['ipv6_addresses']) || in_array($port['port_id'], $cache['ports_option']['ipv6_addresses'])) { foreach (dbFetchRows("SELECT * FROM `ipv6_addresses` WHERE `port_id` = ?", array($port['port_id'])) as $ip6) { $string .= $break . generate_popup_link('ip', $ip6['ipv6_address'] . '/' . $ip6['ipv6_prefixlen'], NULL, 'small'); $break = "<br />"; } } //$string .= '</span>'; $string .= '</td>'; // Print port graph thumbnails $string .= '<td style="width: 147px;">'; $port['graph_type'] = "port_bits"; $graph_array = array(); $graph_array['to'] = $config['time']['now']; $graph_array['id'] = $port['port_id']; $graph_array['type'] = $port['graph_type']; $graph_array['width'] = 100; $graph_array['height'] = 20; $graph_array['bg'] = 'ffffff00'; $graph_array['from'] = $config['time']['day']; $string .= generate_port_link($port, generate_graph_tag($graph_array)); $port['graph_type'] = "port_upkts"; $graph_array['type'] = $port['graph_type']; $string .= generate_port_link($port, generate_graph_tag($graph_array)); $port['graph_type'] = "port_errors"; $graph_array['type'] = $port['graph_type']; $string .= generate_port_link($port, generate_graph_tag($graph_array)); $string .= '</td>'; $string .= '<td style="width: 100px; white-space: nowrap;">'; if ($port['ifOperStatus'] == "up" || $port['ifOperStatus'] == "monitoring") { // Colours generated by humanize_port $string .= '<i class="icon-circle-arrow-down" style="' . $port['bps_in_style'] . '"></i> <span class="small" style="' . $port['bps_in_style'] . '">' . formatRates($port['in_rate']) . '</span><br /> <i class="icon-circle-arrow-up" style="' . $port['bps_out_style'] . '"></i> <span class="small" style="' . $port['bps_out_style'] . '">' . formatRates($port['out_rate']) . '</span><br /> <i class="icon-circle-arrow-down" style="' . $port['pps_in_style'] . '"></i> <span class="small" style="' . $port['pps_in_style'] . '">' . format_bi($port['ifInUcastPkts_rate']) . 'pps</span><br /> <i class="icon-circle-arrow-up" style="' . $port['pps_out_style'] . '"></i> <span class="small" style="' . $port['pps_out_style'] . '">' . format_bi($port['ifOutUcastPkts_rate']) . 'pps</span>'; } $string .= '</td><td style="width: 110px;">'; if ($port['ifType'] && $port['ifType'] != "") { $string .= '<span class="small">' . $port['human_type'] . '</span>'; } else { $string .= '-'; } $string .= '<br />'; if ($port['ifSpeed']) { $string .= '<span class="small">' . humanspeed($port['ifSpeed']) . '</span>'; } if ($port['ifDuplex'] && $port['ifDuplex'] != "unknown") { $string .= '<span class="small"> (' . str_replace("Duplex", "", $port['ifDuplex']) . ')</span>'; } $string .= '<br />'; if ($port['ifMtu'] && $port['ifMtu'] != "") { $string .= '<span class="small">MTU ' . $port['ifMtu'] . '</span>'; } else { $string .= '<span class="small">Unknown MTU</span>'; } // if ($ifHardType && $ifHardType != "") { $string .= '<span class="small">" . $ifHardType . "</span>"); } else { $string .= '-'; } //$string .= '<br />'; // Set VLAN data if the port has ifTrunk populated if ($port['ifTrunk']) { if ($port['ifVlan']) { // Native VLAN if (!isset($cache['ports_vlan'])) { $native_state = dbFetchCell('SELECT `state` FROM `ports_vlans` WHERE `device_id` = ? AND `port_id` = ?', array($device['device_id'], $port['port_id'])); $native_name = dbFetchCell('SELECT `vlan_name` FROM vlans WHERE `device_id` = ? AND `vlan_vlan` = ?;', array($device['device_id'], $port['ifVlan'])); } else { $native_state = $cache['ports_vlan'][$port['port_id']][$port['ifVlan']]['state']; $native_name = $cache['ports_vlan'][$port['port_id']][$port['ifVlan']]['vlan_name']; } switch ($native_state) { case 'blocking': $class = 'text-danger'; break; case 'forwarding': $class = 'text-success'; break; default: $class = 'muted'; } if (empty($native_name)) { $native_name = 'VLAN' . str_pad($port['ifVlan'], 4, '0', STR_PAD_LEFT); } $native_tooltip = 'NATIVE: <strong class=' . $class . '>' . $port['ifVlan'] . ' [' . $native_name . ']</strong><br />'; } if (!isset($cache['ports_vlan'])) { $vlans = dbFetchRows('SELECT * FROM `ports_vlans` AS PV LEFT JOIN vlans AS V ON PV.`vlan` = V.`vlan_vlan` AND PV.`device_id` = V.`device_id` WHERE PV.`port_id` = ? AND PV.`device_id` = ? ORDER BY PV.`vlan`;', array($port['port_id'], $device['device_id'])); } else { $vlans = $cache['ports_vlan'][$port['port_id']]; } $vlans_count = count($vlans); $rel = $vlans_count || $native_tooltip ? 'tooltip' : ''; // Hide tooltip for empty $string .= '<p class="small"><a class="label label-info" data-rel="' . $rel . '" data-tooltip="<div class=\'small\' style=\'max-width: 320px; text-align: justify;\'>' . $native_tooltip; if ($vlans_count) { $string .= 'ALLOWED: '; $vlans_aggr = array(); foreach ($vlans as $vlan) { if ($vlans_count > 20) { // Aggregate VLANs $vlans_aggr[] = $vlan['vlan']; } else { // List VLANs switch ($vlan['state']) { case 'blocking': $class = 'text-danger'; break; case 'forwarding': $class = 'text-success'; break; default: $class = 'muted'; } if (empty($vlan['vlan_name'])) { 'VLAN' . str_pad($vlan['vlan'], 4, '0', STR_PAD_LEFT); } $string .= '<strong class=' . $class . '>' . $vlan['vlan'] . ' [' . $vlan['vlan_name'] . ']</strong><br />'; } } if ($vlans_count > 20) { // End aggregate VLANs $string .= '<strong>' . range_to_list($vlans_aggr, ', ') . '</strong>'; } } $string .= '</div>">' . $port['ifTrunk'] . '</a></p>'; } else { if ($port['ifVlan']) { if (!isset($cache['ports_vlan'])) { $native_state = dbFetchCell('SELECT `state` FROM `ports_vlans` WHERE `device_id` = ? AND `port_id` = ?', array($device['device_id'], $port['port_id'])); $native_name = dbFetchCell('SELECT `vlan_name` FROM vlans WHERE `device_id` = ? AND `vlan_vlan` = ?;', array($device['device_id'], $port['ifVlan'])); } else { $native_state = $cache['ports_vlan'][$port['port_id']][$port['ifVlan']]['state']; $native_name = $cache['ports_vlan'][$port['port_id']][$port['ifVlan']]['vlan_name']; } switch ($native_state) { case 'blocking': $class = 'label-error'; break; case 'forwarding': $class = 'label-success'; break; default: $class = ''; } $rel = $native_name ? 'tooltip' : ''; // Hide tooltip for empty $string .= '<br /><span data-rel="' . $rel . '" class="label ' . $class . '" data-tooltip="<strong class=\'small\'>' . $port['ifVlan'] . ' [' . $native_name . ']</strong>">VLAN ' . $port['ifVlan'] . '</span>'; } else { if ($port['ifVrf']) { $vrf_name = dbFetchCell("SELECT `vrf_name` FROM `vrfs` WHERE `vrf_id` = ?", array($port['ifVrf'])); $string .= '<br /><span class="label label-success" data-rel="tooltip" data-tooltip="VRF">' . $vrf_name . '</span>'; } } } $string .= '</td>'; // If the port is ADSL, print ADSL port data. if ($port_adsl['adslLineCoding']) { $string .= '<td style="width: 200px;"><span class="small">'; $string .= '<span class="label">' . $port_adsl['adslLineCoding'] . '</span> <span class="label">' . rewrite_adslLineType($port_adsl['adslLineType']) . '</span>'; $string .= '<br />'; $string .= 'SYN <i class="icon-circle-arrow-down green"></i> ' . formatRates($port_adsl['adslAtucChanCurrTxRate']) . ' <i class="icon-circle-arrow-up blue"></i> ' . formatRates($port_adsl['adslAturChanCurrTxRate']); $string .= '<br />'; //$string .= 'Max:'.formatRates($port_adsl['adslAtucCurrAttainableRate']) . '/'. formatRates($port_adsl['adslAturCurrAttainableRate']); //$string .= '<br />'; $string .= 'ATN <i class="icon-circle-arrow-down green"></i> ' . $port_adsl['adslAtucCurrAtn'] . 'dBm <i class="icon-circle-arrow-up blue"></i> ' . $port_adsl['adslAturCurrAtn'] . 'dBm'; $string .= '<br />'; $string .= 'SNR <i class="icon-circle-arrow-down green"></i> ' . $port_adsl['adslAtucCurrSnrMgn'] . 'dB <i class="icon-circle-arrow-up blue"></i> ' . $port_adsl['adslAturCurrSnrMgn'] . 'dB'; $string .= '</span>'; } else { // Otherwise print normal port data $string .= '<td style="width: 150px;"><span class="small">'; if ($port['ifPhysAddress'] && $port['ifPhysAddress'] != "") { $string .= $port['human_mac']; } else { $string .= '-'; } $string .= '<br />' . $port['ifLastChange'] . '</span>'; } $string .= '</td>'; $string .= '<td style="min-width: 200px" class="small">'; if (strpos($port['port_label'], "oopback") === FALSE && !$graph_type) { unset($br); // Populate links array for ports with direct links if (!isset($cache['ports_option']['neighbours']) || in_array($port['port_id'], $cache['ports_option']['neighbours'])) { foreach (dbFetchRows('SELECT * FROM `neighbours` WHERE `port_id` = ?', array($port['port_id'])) as $neighbour) { // print_r($link); if ($neighbour['remote_port_id']) { $int_links[$neighbour['remote_port_id']] = $neighbour['remote_port_id']; $int_links_phys[$neighbour['remote_port_id']] = 1; } else { $int_links_unknown[] = $neighbour; } } } else { } // Populate links array for devices which share an IPv4 subnet if (!isset($cache['ports_option']['ipv4_addresses']) || in_array($port['port_id'], $cache['ports_option']['ipv4_addresses'])) { foreach (dbFetchColumn('SELECT DISTINCT(`ipv4_network_id`) FROM `ipv4_addresses` LEFT JOIN `ipv4_networks` USING(`ipv4_network_id`) WHERE `port_id` = ? AND `ipv4_network` NOT IN (?);', array($port['port_id'], $config['ignore_common_subnet'])) as $network_id) { $sql = 'SELECT N.*, P.`port_id`, P.`device_id` FROM `ipv4_addresses` AS A, `ipv4_networks` AS N, `ports` AS P WHERE A.`port_id` = P.`port_id` AND P.`device_id` != ? AND A.`ipv4_network_id` = ? AND N.`ipv4_network_id` = A.`ipv4_network_id` AND P.`ifAdminStatus` = "up"'; $params = array($device['device_id'], $network_id); foreach (dbFetchRows($sql, $params) as $new) { if ($cache['devices']['id'][$new['device_id']]['disabled'] && !$config['web_show_disabled']) { continue; } $int_links[$new['port_id']] = $new['port_id']; $int_links_v4[$new['port_id']][] = $new['ipv4_network']; } } } // Populate links array for devices which share an IPv6 subnet if (!isset($cache['ports_option']['ipv6_addresses']) || in_array($port['port_id'], $cache['ports_option']['ipv6_addresses'])) { foreach (dbFetchColumn("SELECT DISTINCT(`ipv6_network_id`) FROM `ipv6_addresses`\n LEFT JOIN `ipv6_networks` USING(`ipv6_network_id`)\n WHERE `port_id` = ? AND `ipv6_network` NOT IN (?);", array($port['port_id'], $config['ignore_common_subnet'])) as $network_id) { $sql = "SELECT N.*, P.`port_id`, P.`device_id` FROM `ipv6_addresses` AS A, `ipv6_networks` AS N, `ports` AS P\n WHERE A.`port_id` = P.`port_id` AND P.device_id != ?\n AND A.`ipv6_network_id` = ? AND N.`ipv6_network_id` = A.`ipv6_network_id`\n AND P.`ifAdminStatus` = 'up' AND A.`ipv6_origin` != 'linklayer' AND A.`ipv6_origin` != 'wellknown'"; $params = array($device['device_id'], $network_id); foreach (dbFetchRows($sql, $params) as $new) { if ($cache['devices']['id'][$new['device_id']]['disabled'] && !$config['web_show_disabled']) { continue; } $int_links[$new['port_id']] = $new['port_id']; $int_links_v6[$new['port_id']][] = $new['ipv6_network']; } } } // Output contents of links array foreach ($int_links as $int_link) { $link_if = get_port_by_id_cache($int_link); if (!device_permitted($link_if['device_id'])) { continue; } // Skip not permitted links $link_dev = device_by_id_cache($link_if['device_id']); $string .= $br; if ($int_links_phys[$int_link]) { $string .= '<a data-alt="Directly connected" class="oicon-connect"></a> '; } else { $string .= '<a data-alt="Same subnet" class="oicon-network-hub"></a> '; } $string .= '<b>' . generate_port_link($link_if, $link_if['port_label_short']) . ' on ' . generate_device_link($link_dev, short_hostname($link_dev['hostname'])) . '</b>'; ## FIXME -- do something fancy here. if ($int_links_v6[$int_link]) { $string .= ' ' . overlib_link('', '<span class="label label-success">IPv6</span>', implode("<br />", $int_links_v6[$int_link]), NULL); } if ($int_links_v4[$int_link]) { $string .= ' ' . overlib_link('', '<span class="label label-info">IPv4</span>', implode("<br />", $int_links_v4[$int_link]), NULL); } $br = "<br />"; } // Output content of unknown links array (where ports don't exist in our database, or they weren't matched) foreach ($int_links_unknown as $int_link) { // FIXME -- Expose platform and version here. $string .= '<a data-alt="Directly connected" class="oicon-plug-connect"></a> '; $string .= '<b><i>' . short_ifname($int_link['remote_port']) . '</i></b> on '; $string .= '<i><b>' . generate_tooltip_link(NULL, $int_link['remote_hostname'], '<div class="small" style="max-width: 500px;"><b>' . $int_link['remote_platform'] . '</b><br />' . $int_link['remote_version'] . '</div>') . '</b></i>'; $string .= '<br />'; } } if (!isset($cache['ports_option']['pseudowires']) || in_array($port['port_id'], $cache['ports_option']['pseudowires'])) { foreach (dbFetchRows("SELECT * FROM `pseudowires` WHERE `port_id` = ?", array($port['port_id'])) as $pseudowire) { //`port_id`,`peer_device_id`,`peer_ldp_id`,`pwID`,`pwIndex` # $pw_peer_dev = dbFetchRow("SELECT * FROM `devices` WHERE `device_id` = ?", array($pseudowire['peer_device_id'])); $pw_peer_int = dbFetchRow("SELECT * FROM `ports` AS I, `pseudowires` AS P WHERE I.`device_id` = ? AND P.`pwID` = ? AND P.`port_id` = I.`port_id`", array($pseudowire['peer_device_id'], $pseudowire['pwID'])); # $pw_peer_int = get_port_by_id_cache($pseudowire['peer_device_id']); $pw_peer_dev = device_by_id_cache($pseudowire['peer_device_id']); if (is_array($pw_peer_int)) { humanize_port($pw_peer_int); $string .= $br . '<i class="oicon-arrow-switch"></i> <strong>' . generate_port_link($pw_peer_int, $pw_peer_int['port_label_short']) . ' on ' . generate_device_link($pw_peer_dev, short_hostname($pw_peer_dev['hostname'])) . '</strong>'; } else { $string .= $br . '<i class="oicon-arrow-switch"></i> <strong> VC ' . $pseudowire['pwID'] . ' on ' . $pseudowire['peer_addr'] . '</strong>'; } $string .= ' <span class="label">' . $pseudowire['pwPsnType'] . '</span>'; $string .= ' <span class="label">' . $pseudowire['pwType'] . '</span>'; $br = "<br />"; } } if (!isset($cache['ports_option']['ports_pagp']) || in_array($port['ifIndex'], $cache['ports_option']['ports_pagp'])) { foreach (dbFetchRows("SELECT * FROM `ports` WHERE `pagpGroupIfIndex` = ? AND `device_id` = ?", array($port['ifIndex'], $device['device_id'])) as $member) { humanize_port($member); $pagp[$device['device_id']][$port['ifIndex']][$member['ifIndex']] = TRUE; $string .= $br . '<i class="oicon-arrow-join"></i> <strong>' . generate_port_link($member) . ' [PAgP]</strong>'; $br = "<br />"; } } if ($port['pagpGroupIfIndex'] && $port['pagpGroupIfIndex'] != $port['ifIndex']) { $pagp[$device['device_id']][$port['pagpGroupIfIndex']][$port['ifIndex']] = TRUE; $parent = dbFetchRow("SELECT * FROM `ports` WHERE `ifIndex` = ? and `device_id` = ?", array($port['pagpGroupIfIndex'], $device['device_id'])); humanize_port($parent); $string .= $br . '<i class="oicon-arrow-split"></i> <strong>' . generate_port_link($parent) . ' [PAgP]</strong>'; $br = "<br />"; } if (!isset($cache['ports_option']['ports_stack_low']) || in_array($port['ifIndex'], $cache['ports_option']['ports_stack_low'])) { foreach (dbFetchRows("SELECT * FROM `ports_stack` WHERE `port_id_low` = ? and `device_id` = ?", array($port['ifIndex'], $device['device_id'])) as $higher_if) { if ($higher_if['port_id_high']) { if ($pagp[$device['device_id']][$higher_if['port_id_high']][$port['ifIndex']]) { continue; } // Skip if same PAgP port $this_port = get_port_by_index_cache($device['device_id'], $higher_if['port_id_high']); if (is_array($this_port)) { $string .= $br . '<i class="oicon-arrow-split"></i> <strong>' . generate_port_link($this_port) . '</strong>'; $br = "<br />"; } } } } if (!isset($cache['ports_option']['ports_stack_high']) || in_array($port['ifIndex'], $cache['ports_option']['ports_stack_high'])) { foreach (dbFetchRows("SELECT * FROM `ports_stack` WHERE `port_id_high` = ? and `device_id` = ?", array($port['ifIndex'], $device['device_id'])) as $lower_if) { if ($lower_if['port_id_low']) { if ($pagp[$device['device_id']][$port['ifIndex']][$lower_if['port_id_low']]) { continue; } // Skip if same PAgP ports $this_port = get_port_by_index_cache($device['device_id'], $lower_if['port_id_low']); if (is_array($this_port)) { $string .= $br . '<i class="oicon-arrow-join"></i> <strong>' . generate_port_link($this_port) . '</strong>'; $br = "<br />"; } } } } unset($int_links, $int_links_v6, $int_links_v4, $int_links_phys, $br); $string .= '</td></tr>'; } } // End Detailed View // If we're showing graphs, generate the graph and print the img tags if ($vars['graph'] == "etherlike") { $graph_file = get_port_rrdfilename($port, "dot3", TRUE); } else { $graph_file = get_port_rrdfilename($port, NULL, TRUE); } if ($vars['graph'] && is_file($graph_file)) { $string .= '<tr><td colspan="' . $table_cols . '">'; $graph_array['to'] = $config['time']['now']; $graph_array['id'] = $port['port_id']; $graph_array['type'] = 'port_' . $vars['graph']; $string .= generate_graph_row($graph_array); $string .= '</td></tr>'; } return $string; }
function discover_new_device($hostname, $source = 'xdp', $protocol = NULL, $device = NULL, $snmp_port = 161) { global $config; $source = strtolower($source); if ($config['autodiscovery'][$source]) { if (!$protocol) { $protocol = strtoupper($source); } print_message("发现新主机 {$hostname} 通过 {$protocol}"); // By first detect hostname is IP or domain name (IPv4/6 == 4/6, hostname == FALSE) $ip_version = get_ip_version($hostname); if ($ip_version) { // Hostname is IPv4/IPv6 $use_ip = TRUE; $ip = $hostname; } else { $use_ip = FALSE; if (!empty($config['mydomain']) && isDomainResolves($hostname . '.' . $config['mydomain'])) { $hostname .= '.' . $config['mydomain']; } $ip = gethostbyname6($hostname); if ($ip) { $ip_version = get_ip_version($ip); print_debug("主机 {$hostname} 解析为 {$ip}"); } else { // No DNS records print_debug("主机 {$hostname} 无法解析, 自动发现失败."); return FALSE; } } if (match_network($ip, $config['autodiscovery']['ip_nets'])) { print_debug("主机 {$hostname} ({$ip}) 内部网络创建配置, 尝试增加:"); if (isPingable($ip)) { // Check if device duplicated by IP $ip = $ip_version == 4 ? $ip : Net_IPv6::uncompress($ip, TRUE); $db = dbFetchRow('SELECT D.`hostname` FROM ipv' . $ip_version . '_addresses AS A LEFT JOIN `ports` AS P ON A.`port_id` = P.`port_id` LEFT JOIN `devices` AS D ON D.`device_id` = P.`device_id` WHERE D.`disabled` = 0 AND A.`ipv' . $ip_version . '_address` = ?', array($ip)); if ($db) { print_debug('已经有设备 ' . $db['hostname'] . " 包含 {$ip}"); return FALSE; } // Detect snmp transport $snmp_transport = $ip_version == 4 ? 'udp' : 'udp6'; $new_device = detect_device_snmpauth($ip, $snmp_port, $snmp_transport); if ($new_device) { if ($use_ip) { // Detect FQDN hostname // by sysName $snmphost = snmp_get($new_device, "sysName.0", "-Oqv", "SNMPv2-MIB", mib_dirs()); if ($snmphost) { $snmp_ip = gethostbyname6($snmphost); } if ($snmp_ip == $ip) { $hostname = $snmphost; } else { // by PTR $ptr = gethostbyaddr6($ip); if ($ptr) { $ptr_ip = gethostbyname6($ptr); } if ($ptr && $ptr_ip == $ip) { $hostname = $ptr; } else { print_debug("设备 IP {$ip} 没有 FQDN 名称"); return FALSE; } } print_debug("设备 IP {$ip} 发现 FQDN 名称: {$hostname}"); } $new_device['hostname'] = $hostname; if (!check_device_duplicated($new_device)) { $snmp_v3 = array(); if ($new_device['snmp_version'] === 'v3') { $snmp_v3['snmp_authlevel'] = $new_device['snmp_authlevel']; $snmp_v3['snmp_authname'] = $new_device['snmp_authname']; $snmp_v3['snmp_authpass'] = $new_device['snmp_authpass']; $snmp_v3['snmp_authalgo'] = $new_device['snmp_authalgo']; $snmp_v3['snmp_cryptopass'] = $new_device['snmp_cryptopass']; $snmp_v3['snmp_cryptoalgo'] = $new_device['snmp_cryptoalgo']; } $remote_device_id = createHost($new_device['hostname'], $new_device['snmp_community'], $new_device['snmp_version'], $new_device['snmp_port'], $new_device['snmp_transport'], $snmp_v3); if ($remote_device_id) { $remote_device = device_by_id_cache($remote_device_id, 1); if ($port) { humanize_port($port); log_event("设备自动发现通过 {$protocol} 在 " . $device['hostname'] . " (port " . $port['label'] . ")", $remote_device_id, 'port', $port['port_id']); } else { log_event("设备自动发现通过 {$protocol} 在 " . $device['hostname'], $remote_device_id, $protocol); } //array_push($GLOBALS['devices'], $remote_device); // createHost() already puth this return $remote_device_id; } } } } } else { print_debug("IP {$ip} ({$hostname}) 不允许内部 \$config['autodiscovery']['ip_nets'] 位于 config.php"); } print_debug('自动发现主机 ' . $hostname . ' 错误.'); } else { print_debug('自动发现协议 ' . $protocol . ' 禁用.'); } return FALSE; }
/** * Display Interface MACs addresses. * * Display pages with MAC addresses from device Interfaces. * * @param array $vars * @return none * */ function print_mac_addresses($vars) { // With pagination? (display page numbers in header) $pagination = isset($vars['pagination']) && $vars['pagination']; pagination($vars, 0, TRUE); // Get default pagesize/pageno $pageno = $vars['pageno']; $pagesize = $vars['pagesize']; $start = $pagesize * $pageno - $pagesize; $param = array(); $where = ' WHERE 1 '; foreach ($vars as $var => $value) { if ($value != '') { switch ($var) { case 'device': case 'device_id': $where .= generate_query_values($value, 'device_id'); break; case 'interface': $where .= ' AND `ifDescr` LIKE ?'; $param[] = $value; break; case 'address': $where .= ' AND `ifPhysAddress` LIKE ?'; # FIXME hm? mres in a dbFacile parameter? $param[] = '%' . str_replace(array(':', ' ', '-', '.', '0x'), '', mres($value)) . '%'; break; } } } $where .= ' AND `ifPhysAddress` IS NOT NULL'; //Exclude empty MACs // Show MACs only for permitted ports $query_permitted = generate_query_permitted(array('port')); $query = 'FROM `ports` '; $query .= $where . $query_permitted; $query_count = 'SELECT COUNT(*) ' . $query; $query = 'SELECT * ' . $query; $query .= ' ORDER BY `ifPhysAddress`'; $query .= " LIMIT {$start},{$pagesize}"; // Query addresses $entries = dbFetchRows($query, $param); // Query address count if ($pagination) { $count = dbFetchCell($query_count, $param); } $list = array('device' => FALSE); if (!isset($vars['device']) || empty($vars['device']) || $vars['page'] == 'search') { $list['device'] = TRUE; } $string = '<table class="table table-bordered table-striped table-hover table-condensed">' . PHP_EOL; if (!$short) { $string .= ' <thead>' . PHP_EOL; $string .= ' <tr>' . PHP_EOL; if ($list['device']) { $string .= ' <th>Device</th>' . PHP_EOL; } $string .= ' <th>Interface</th>' . PHP_EOL; $string .= ' <th>MAC Address</th>' . PHP_EOL; $string .= ' <th>Description</th>' . PHP_EOL; $string .= ' </tr>' . PHP_EOL; $string .= ' </thead>' . PHP_EOL; } $string .= ' <tbody>' . PHP_EOL; foreach ($entries as $entry) { if (port_permitted($entry['port_id'])) { humanize_port($entry); $string .= ' <tr>' . PHP_EOL; if ($list['device']) { $dev = device_by_id_cache($entry['device_id']); $string .= ' <td class="entity" style="white-space: nowrap;">' . generate_device_link($dev) . '</td>' . PHP_EOL; } if ($entry['ifInErrors_delta'] > 0 || $entry['ifOutErrors_delta'] > 0) { $port_error = generate_port_link($entry, '<span class="label label-important">Errors</span>', 'port_errors'); } $string .= ' <td class="entity">' . generate_port_link($entry, short_ifname($entry['label'])) . ' ' . $port_error . '</td>' . PHP_EOL; $string .= ' <td style="width: 160px;">' . $entry['human_mac'] . '</td>' . PHP_EOL; $string .= ' <td>' . $entry['ifAlias'] . '</td>' . PHP_EOL; $string .= ' </tr>' . PHP_EOL; } } $string .= ' </tbody>' . PHP_EOL; $string .= '</table>'; // Print pagination header if ($pagination) { $string = pagination($vars, $count) . $string . pagination($vars, $count); } // Print MAC addresses echo $string; }
echo " <b style='color: #a10000;'>v6</b>"; } if ($int_links_v4[$int_link]) { echo " <b style='color: #00a100'>v4</b>"; } $br = "<br />"; } } # unset($int_links, $int_links_v6, $int_links_v4, $int_links_phys, $br); } if ($port_details) { foreach (dbFetchRows("SELECT * FROM `pseudowires` WHERE `port_id` = ?", array($port['port_id'])) as $pseudowire) { #`port_id`,`peer_device_id`,`peer_ldp_id`,`cpwVcID`,`cpwOid` $pw_peer_dev = dbFetchRow("SELECT * FROM `devices` WHERE `device_id` = ?", array($pseudowire['peer_device_id'])); $pw_peer_int = dbFetchRow("SELECT * FROM `ports` AS I, pseudowires AS P WHERE I.device_id = ? AND P.cpwVcID = ? AND P.port_id = I.port_id", array($pseudowire['peer_device_id'], $pseudowire['cpwVcID'])); humanize_port($pw_peer_int); echo "{$br}<img src='images/16/arrow_switch.png' align=absmiddle><b> " . generate_port_link($pw_peer_int, makeshortif($pw_peer_int['label'])) . " on " . generate_device_link($pw_peer_dev, shorthost($pw_peer_dev['hostname'])) . "</b>"; $br = "<br />"; } foreach (dbFetchRows("SELECT * FROM `ports` WHERE `pagpGroupIfIndex` = ? and `device_id` = ?", array($port['ifIndex'], $device['device_id'])) as $member) { echo "{$br}<img src='images/16/brick_link.png' align=absmiddle> <strong>" . generate_port_link($member) . " (PAgP)</strong>"; $br = "<br />"; } if ($port['pagpGroupIfIndex'] && $port['pagpGroupIfIndex'] != $port['ifIndex']) { $parent = dbFetchRow("SELECT * FROM `ports` WHERE `ifIndex` = ? and `device_id` = ?", array($port['pagpGroupIfIndex'], $device['device_id'])); echo "{$br}<img src='images/16/bricks.png' align=absmiddle> <strong>" . generate_port_link($parent) . " (PAgP)</strong>"; $br = "<br />"; } foreach (dbFetchRows("SELECT * FROM `ports_stack` WHERE `port_id_low` = ? and `device_id` = ?", array($port['ifIndex'], $device['device_id'])) as $higher_if) { if ($higher_if['port_id_high']) { $this_port = get_port_by_index_cache($device['device_id'], $higher_if['port_id_high']);
function discover_new_device($hostname, $source = 'xdp', $protocol = NULL, $device = NULL, $snmp_port = 161) { global $config; $source = strtolower($source); // Check if source is enabled for autodiscovery if ($config['autodiscovery'][$source]) { $flags = OBS_DNS_ALL; if (!$protocol) { $protocol = strtoupper($source); } print_cli_data("Try discovering host", "{$hostname} through {$protocol}", 3); // By first detect hostname is IP or domain name (IPv4/6 == 4/6, hostname == FALSE) $ip_version = get_ip_version($hostname); if ($ip_version) { // Hostname is IPv4/IPv6 $use_ip = TRUE; $ip = $hostname; } else { $use_ip = FALSE; // Add "mydomain" configuration if this resolves, converts switch1 -> switch1.mydomain.com if (!empty($config['mydomain']) && isDomainResolves($hostname . '.' . $config['mydomain'], $flags)) { $hostname .= '.' . $config['mydomain']; } // Determine v4 vs v6 $ip = gethostbyname6($hostname, $flags); if ($ip) { $ip_version = get_ip_version($ip); print_debug("Host {$hostname} resolved as {$ip}"); } else { // No DNS records print_debug("Host {$hostname} not resolved, autodiscovery fails."); return FALSE; } } if ($ip_version == 6) { $flags = $flags ^ OBS_DNS_A; // Exclude IPv4 } if (isset($config['autodiscovery']['ping_skip']) && $config['autodiscovery']['ping_skip']) { $flags = $flags | OBS_PING_SKIP; // Add skip pings flag } if (match_network($ip, $config['autodiscovery']['ip_nets'])) { print_debug("Host {$hostname} ({$ip}) founded inside configured nets, trying to add:"); // By first check if pingable $pingable = isPingable($ip, $flags); if (!$pingable && (isset($config['autodiscovery']['ping_skip']) && $config['autodiscovery']['ping_skip'])) { $flags = $flags | OBS_PING_SKIP; // Add skip pings flag if allowed in config $pingable = TRUE; } if ($pingable) { // Check if device duplicated by IP $ip = $ip_version == 4 ? $ip : Net_IPv6::uncompress($ip, TRUE); $db = dbFetchRow('SELECT D.`hostname` FROM ipv' . $ip_version . '_addresses AS A LEFT JOIN `ports` AS P ON A.`port_id` = P.`port_id` LEFT JOIN `devices` AS D ON D.`device_id` = P.`device_id` WHERE D.`disabled` = 0 AND A.`ipv' . $ip_version . '_address` = ?', array($ip)); if ($db) { print_debug('Already have device ' . $db['hostname'] . " with IP {$ip}"); return FALSE; } // Detect snmp transport, net-snmp needs udp6 for ipv6 $snmp_transport = $ip_version == 4 ? 'udp' : 'udp6'; $new_device = detect_device_snmpauth($ip, $snmp_port, $snmp_transport); if ($new_device) { if ($use_ip) { // Detect FQDN hostname // by sysName $snmphost = snmp_get($new_device, 'sysName.0', '-Oqv', 'SNMPv2-MIB'); if ($snmphost) { $snmp_ip = gethostbyname6($snmphost, $flags); } if ($snmp_ip == $ip) { $hostname = $snmphost; } else { // by PTR $ptr = gethostbyaddr6($ip); if ($ptr) { $ptr_ip = gethostbyname6($ptr, $flags); } if ($ptr && $ptr_ip == $ip) { $hostname = $ptr; } else { if ($config['autodiscovery']['require_hostname']) { print_debug("Device IP {$ip} does not seem to have FQDN."); return FALSE; } else { $hostname = $ip_version == 4 ? $ip : Net_IPv6::compress($hostname, TRUE); // Always use compressed IPv6 name } } } print_debug("Device IP {$ip} linked to FQDN name: {$hostname}"); } $new_device['hostname'] = $hostname; if (!check_device_duplicated($new_device)) { $snmp_v3 = array(); if ($new_device['snmp_version'] === 'v3') { $snmp_v3['snmp_authlevel'] = $new_device['snmp_authlevel']; $snmp_v3['snmp_authname'] = $new_device['snmp_authname']; $snmp_v3['snmp_authpass'] = $new_device['snmp_authpass']; $snmp_v3['snmp_authalgo'] = $new_device['snmp_authalgo']; $snmp_v3['snmp_cryptopass'] = $new_device['snmp_cryptopass']; $snmp_v3['snmp_cryptoalgo'] = $new_device['snmp_cryptoalgo']; } $remote_device_id = createHost($new_device['hostname'], $new_device['snmp_community'], $new_device['snmp_version'], $new_device['snmp_port'], $new_device['snmp_transport'], $snmp_v3); if ($remote_device_id) { if (is_flag_set(OBS_PING_SKIP, $flags)) { set_entity_attrib('device', $remote_device_id, 'ping_skip', 1); } $remote_device = device_by_id_cache($remote_device_id, 1); if ($port) { humanize_port($port); log_event("Device autodiscovered through {$protocol} on " . $device['hostname'] . " (port " . $port['port_label'] . ")", $remote_device_id, 'port', $port['port_id']); } else { log_event("Device autodiscovered through {$protocol} on " . $device['hostname'], $remote_device_id, $protocol); } //array_push($GLOBALS['devices'], $remote_device); // createHost() already puth this return $remote_device_id; } } } } } else { print_debug("IP {$ip} ({$hostname}) not permitted inside \$config['autodiscovery']['ip_nets'] in config.php"); } print_debug('Autodiscovery for host ' . $hostname . ' failed.'); } else { print_debug('Autodiscovery for protocol ' . $protocol . ' disabled.'); } return FALSE; }
} else { $sql = mysql_query("SELECT * FROM `devices` AS D, devices_perms AS P WHERE D.device_id = P.device_id AND P.user_id = '" . $_SESSION['user_id'] . "' AND D.status = '0' AND D.ignore = '0'"); } while ($device = mysql_fetch_assoc($sql)) { generate_front_box("#ffaaaa", "<center><strong>" . generate_device_link($device, shorthost($device['hostname'])) . "</strong><br />\n <span style='font-size: 14px; font-weight: bold; margin: 5px; color: #c00;'>Device Down</span> <br />\n <span class=body-date-1>" . truncate($device['location'], 20) . "</span>\n </center>"); } if ($_SESSION['userlevel'] == '10') { $sql = mysql_query("SELECT * FROM `ports` AS I, `devices` AS D WHERE I.device_id = D.device_id AND ifOperStatus = 'down' AND ifAdminStatus = 'up' AND D.ignore = '0' AND I.ignore = '0'"); } else { $sql = mysql_query("SELECT * FROM `ports` AS I, `devices` AS D, devices_perms AS P WHERE D.device_id = P.device_id AND P.user_id = '" . $_SESSION['user_id'] . "' AND I.device_id = D.device_id AND ifOperStatus = 'down' AND ifAdminStatus = 'up' AND D.ignore = '0' AND I.ignore = '0'"); } // These things need to become more generic, and more manageable across different frontpages... rewrite inc :> if ($config['frontpage']['device_status']['ports']) { while ($interface = mysql_fetch_assoc($sql)) { if (!$interface['deleted']) { humanize_port($interface); generate_front_box("#ffdd99", "<center><strong>" . generate_device_link($interface, shorthost($interface['hostname'])) . "</strong><br />\n <span style='font-size: 14px; font-weight: bold; margin: 5px; color: #c00;'>Port Down</span><br />\n<!-- <img src='graph.php?type=bits&if=" . $interface['port_id'] . "&from=" . $config['time']['day'] . "&to=" . $config['time']['now'] . "&width=100&height=32' /> -->\n <strong>" . generate_port_link($interface, truncate(makeshortif($interface['label']), 13, '')) . "</strong> <br />\n " . ($interface['ifAlias'] ? '<span class="body-date-1">' . truncate($interface['ifAlias'], 20, '') . '</span>' : '') . "\n </center>"); } } } /* FIXME service permissions? seem nonexisting now.. */ $sql = mysql_query("SELECT * FROM `services` AS S, `devices` AS D WHERE S.device_id = D.device_id AND service_status = 'down' AND D.ignore = '0' AND S.service_ignore = '0'"); while ($service = mysql_fetch_assoc($sql)) { generate_front_box("#ffaaaa", "<center><strong>" . generate_device_link($service, shorthost($service['hostname'])) . "</strong><br />\n <span style='font-size: 14px; font-weight: bold; margin: 5px; color: #c00;'>Service Down</span>\n <strong>" . $service['service_type'] . "</strong><br />\n <span class=body-date-1>" . truncate($interface['ifAlias'], 20) . "</span>\n </center>"); } if (isset($config['enable_bgp']) && $config['enable_bgp']) { if ($_SESSION['userlevel'] == '10') { $sql = mysql_query("SELECT * FROM `devices` AS D, bgpPeers AS B WHERE bgpPeerAdminStatus = 'start' AND bgpPeerState != 'established' AND B.device_id = D.device_id AND D.ignore = 0"); } else { $sql = mysql_query("SELECT * FROM `devices` AS D, bgpPeers AS B, devices_perms AS P WHERE D.device_id = P.device_id AND P.user_id = '" . $_SESSION['user_id'] . "' AND bgpPeerAdminStatus = 'start' AND bgpPeerState != 'established' AND B.device_id = D.device_id AND D.ignore = 0"); }
<br /> ' . highlight_search(htmlentities($result['location'], 0, 'UTF-8')) . ' | ' . $num_ports . ' ports</small> </strong> </dd> </dl> </a> </li>' . PHP_EOL; } } /// SEARCH PORTS $query_permitted_port = generate_query_permitted(array('port')); $results = dbFetchRows("SELECT * FROM `ports`\n LEFT JOIN `devices` ON `ports`.`device_id` = `devices`.`device_id`\n WHERE (`ifAlias` LIKE '%{$queryString}%' OR `ifDescr` LIKE '%{$queryString}%') {$query_permitted_port}\n ORDER BY `ifDescr` LIMIT 8;"); if (count($results)) { $found = 1; echo '<li class="nav-header">Ports found: ' . count($results) . '</li>'; foreach ($results as $result) { humanize_port($result); echo '<li class="divider" style="margin: 0px;"></li>'; echo '<li>'; echo '<a href="' . generate_port_url($result) . '">'; $name = rewrite_ifname($result['ifDescr']); if (strlen($name) > 35) { $name = substr($name, 0, 35) . "..."; } $description = $result['ifAlias']; if (strlen($description) > 80) { $description = substr($description, 0, 80) . "..."; } $type = rewrite_iftype($result['ifType']); if ($description) { $type .= ' | '; }
function discover_new_device($hostname, $source = 'xdp', $protocol = NULL, $device = NULL, $port = 161) { global $config; $source = strtolower($source); if ($config['autodiscovery'][$source]) { if (!$protocol) { $protocol = strtoupper($source); } print_message("Discovering new host {$hostname} through {$protocol}"); // By first detect hostname is IP or domain name (IPv4/6 == 4/6, hostname == FALSE) $ip_version = get_ip_version($hostname); if ($ip_version) { // Hostname is IPv4/IPv6 $use_ip = TRUE; } else { $use_ip = FALSE; if (!empty($config['mydomain']) && isDomainResolves($hostname . '.' . $config['mydomain'])) { $hostname .= '.' . $config['mydomain']; } $ip = gethostbyname6($hostname); if ($ip) { $ip_version = get_ip_version($ip); print_debug("Host {$hostname} resolved as {$ip}"); } else { // No DNS records print_debug("Host {$hostname} not resolved, autodiscovery fails."); return FALSE; } } if (match_network($ip, $config['autodiscovery']['ip_nets'])) { print_debug("Host {$hostname} ({$ip}) founded inside configured nets, try to adding:"); if (isPingable($ip)) { // Check if device duplicated by IP $ip = $ip_version == 4 ? $hostname : Net_IPv6::uncompress($hostname, TRUE); $db = dbFetchRow('SELECT D.`hostname` FROM ipv' . $ip_version . '_addresses AS A LEFT JOIN `ports` AS P ON A.`port_id` = P.`port_id` LEFT JOIN `devices` AS D ON D.`device_id` = P.`device_id` WHERE D.`disabled` = 0 AND A.`ipv' . $ip_version . '_address` = ?', array($ip)); if ($db) { print_debug('Already have device ' . $db['hostname'] . " with {$ip}"); return FALSE; } // Detect snmp transport $transport = $ip_version == 4 ? 'udp' : 'udp6'; $new_device = detect_device_snmpauth($ip, $port, $transport); if ($new_device) { if ($use_ip) { // Detect FQDN hostname // by sysName $snmphost = snmp_get($new_device, "sysName.0", "-Oqv", "SNMPv2-MIB", mib_dirs()); if ($snmphost) { $snmp_ip = gethostbyname6($snmphost); } if ($snmp_ip == $ip) { $hostname = $snmphost; } else { // by PTR $ptr = gethostbyaddr6($ip); if ($ptr) { $ptr_ip = gethostbyname6($ptr); } if ($ptr && $ptr_ip == $ip) { $hostname = $ptr; } else { print_debug("Device IP {$ip} not have FQDN name"); return FALSE; } } print_debug("Device IP {$ip} founded FQDN name: {$hostname}"); } $new_device['hostname'] = $hostname; if (!check_device_duplicated($new_device)) { $v3 = array(); if ($new_device['snmpver'] === 'v3') { $v3['authlevel'] = $new_device['authlevel']; $v3['authname'] = $new_device['authname']; $v3['authpass'] = $new_device['authpass']; $v3['authalgo'] = $new_device['authalgo']; $v3['cryptopass'] = $new_device['cryptopass']; $v3['cryptoalgo'] = $new_device['cryptoalgo']; } $remote_device_id = createHost($new_device['hostname'], $new_device['community'], $new_device['snmpver'], $new_device['port'], $new_device['transport'], $v3); if ($remote_device_id) { $remote_device = device_by_id_cache($remote_device_id, 1); if ($port) { humanize_port($port); log_event("Device autodiscovered through {$protocol} on " . $device['hostname'] . " (port " . $port['label'] . ")", $remote_device_id, 'port', $port['port_id']); } else { log_event("Device autodiscovered through {$protocol} on " . $device['hostname'], $remote_device_id, $protocol); } //array_push($GLOBALS['devices'], $remote_device); // createHost() already puth this return $remote_device_id; } } } } } else { print_debug("IP {$ip} ({$hostname}) not permitted inside \$config['autodiscovery']['ip_nets'] in config.php"); } print_debug('Autodiscovery for host ' . $hostname . ' fails.'); } else { print_debug('Autodiscovery for protocol ' . $protocol . ' disabled.'); } return FALSE; }
function generate_ap_link($args, $text = NULL, $type = NULL, $escape = FALSE) { global $config; humanize_port($args); if (!$text) { $text = rewrite_ifname($args['port_label'], !$escape); } // Negative escape flag for exclude double escape if ($type) { $args['graph_type'] = $type; } if (!isset($args['graph_type'])) { $args['graph_type'] = 'port_bits'; } if (!isset($args['hostname'])) { $args = array_merge($args, device_by_id_cache($args['device_id'])); } $content = "<div class=entity-title>" . $args['text'] . " - " . rewrite_ifname($args['port_label'], !$escape) . "</div>"; if ($args['ifAlias']) { $content .= $args['ifAlias'] . "<br />"; } $content .= "<div style=\\'width: 850px\\'>"; $graph_array['type'] = $args['graph_type']; $graph_array['legend'] = "yes"; $graph_array['height'] = "100"; $graph_array['width'] = "340"; $graph_array['to'] = $config['time']['now']; $graph_array['from'] = $config['time']['day']; $graph_array['id'] = $args['accesspoint_id']; $content .= generate_graph_tag($graph_array); $graph_array['from'] = $config['time']['week']; $content .= generate_graph_tag($graph_array); $graph_array['from'] = $config['time']['month']; $content .= generate_graph_tag($graph_array); $graph_array['from'] = $config['time']['year']; $content .= generate_graph_tag($graph_array); $content .= "</div>"; $url = generate_ap_url($args); if (port_permitted($args['interface_id'], $args['device_id'])) { return overlib_link($url, $text, $content, $class, $escape); } else { return rewrite_ifname($text); } }
/** * Display FDB table. * * @param array $vars * @return none * */ function print_fdbtable($vars) { // With pagination? (display page numbers in header) $pagination = isset($vars['pagination']) && $vars['pagination']; $pageno = isset($vars['pageno']) && !empty($vars['pageno']) ? $vars['pageno'] : 1; $pagesize = isset($vars['pagesize']) && !empty($vars['pagesize']) ? $vars['pagesize'] : 10; $start = $pagesize * $pageno - $pagesize; $param = array(); $where = ' WHERE 1 '; foreach ($vars as $var => $value) { if ($value != '') { $cond = array(); switch ($var) { case 'device': case 'device_id': $where .= ' AND I.`device_id` = ?'; $param[] = $value; break; case 'port': case 'port_id': $where .= ' AND I.`port_id` = ?'; $param[] = $value; break; case 'interface': $where .= ' AND I.`ifDescr` LIKE ?'; $param[] = $value; break; case 'vlan_id': if (!is_array($value)) { $value = array($value); } foreach ($value as $v) { $cond[] = '?'; $param[] = $v; } $where .= " AND F.`vlan_id` IN ("; $where .= implode(', ', $cond); $where .= ')'; break; case 'vlan_name': if (!is_array($value)) { $value = array($value); } foreach ($value as $v) { $cond[] = '?'; $param[] = $v; } $where .= " AND V.`vlan_name` IN ("; $where .= implode(', ', $cond); $where .= ')'; break; case 'address': $where .= ' AND F.`mac_address` LIKE ?'; $param[] = '%' . str_replace(array(':', ' ', '-', '.', '0x'), '', mres($value)) . '%'; break; } } } // Show FDB tables only for permitted ports $query_permitted = generate_query_permitted(array('port'), array('port_table' => 'I')); $query = 'FROM `vlans_fdb` AS F '; $query .= 'LEFT JOIN `vlans` as V ON V.`vlan_vlan` = F.`vlan_id` AND V.`device_id` = F.`device_id` '; $query .= 'LEFT JOIN `ports` AS I ON I.`port_id` = F.`port_id` '; $query .= $where . $query_permitted; $query_count = 'SELECT COUNT(*) ' . $query; $query = 'SELECT * ' . $query; $query .= ' ORDER BY F.`mac_address`'; $query .= " LIMIT {$start},{$pagesize}"; // Query addresses $entries = dbFetchRows($query, $param); // Query address count if ($pagination) { $count = dbFetchCell($query_count, $param); } $list = array('device' => FALSE); if (!isset($vars['device']) || empty($vars['device']) || $vars['page'] == 'search') { $list['device'] = TRUE; } $string = '<table class="table table-bordered table-striped table-hover table-condensed">' . PHP_EOL; if (!$short) { $string .= ' <thead>' . PHP_EOL; $string .= ' <tr>' . PHP_EOL; $string .= ' <th>MAC Address</th>' . PHP_EOL; if ($list['device']) { $string .= ' <th>Device</th>' . PHP_EOL; } $string .= ' <th>Interface</th>' . PHP_EOL; $string .= ' <th>VLAN ID</th>' . PHP_EOL; $string .= ' <th>VLAN Name</th>' . PHP_EOL; $string .= ' </tr>' . PHP_EOL; $string .= ' </thead>' . PHP_EOL; } $string .= ' <tbody>' . PHP_EOL; foreach ($entries as $entry) { humanize_port($entry); $string .= ' <tr>' . PHP_EOL; $string .= ' <td width="160">' . formatMac($entry['mac_address']) . '</td>' . PHP_EOL; if ($list['device']) { $dev = device_by_id_cache($entry['device_id']); $string .= ' <td class="entity" nowrap>' . generate_device_link($dev) . '</td>' . PHP_EOL; } if ($entry['ifInErrors_delta'] > 0 || $entry['ifOutErrors_delta'] > 0) { $port_error = generate_port_link($entry, '<span class="label label-important">Errors</span>', 'port_errors'); } $string .= ' <td class="entity">' . generate_port_link($entry, short_ifname($entry['label'])) . ' ' . $port_error . '</td>' . PHP_EOL; $string .= ' <td>Vlan' . $entry['vlan_vlan'] . '</td>' . PHP_EOL; $string .= ' <td>' . $entry['vlan_name'] . '</td>' . PHP_EOL; $string .= ' </tr>' . PHP_EOL; } $string .= ' </tbody>' . PHP_EOL; $string .= '</table>'; // Print pagination header if ($pagination) { $string = pagination($vars, $count) . $string . pagination($vars, $count); } // Print FDB table echo $string; }
$dst_query = dbFetchRow("SELECT D.`device_id`, `hostname` FROM `devices` AS D, `ports` AS I WHERE I.`port_id` = ? AND D.`device_id` = I.`device_id`" . $cache['where']['devices_permitted'], array($remote_port_id)); $dst = $dst_query['hostname']; $dst_host = $dst_query['device_id']; } else { unset($dst_host); $dst = $link['remote_hostname']; } if ($anon) { $dst = md5($dst); $src = md5($src); } $sif = dbFetchRow("SELECT * FROM `ports` WHERE `port_id` = ?" . $cache['where']['ports_permitted'], array($link['local_port_id'])); humanize_port($sif); if ($remote_port_id) { $dif = dbFetchRow("SELECT * FROM `ports` WHERE `port_id` = ?" . $cache['where']['ports_permitted'], array($link['remote_port_id'])); humanize_port($dif); } else { $dif['label'] = $link['remote_port']; $dif['port_id'] = $link['remote_hostname'] . '/' . $link['remote_port']; } if (!is_numeric($device['device_id'])) { if (!$ifdone[$dst][$dif['port_id']] && !$ifdone[$src][$sif['port_id']]) { $map .= "\"{$src}\" -> \"" . $dst . "\" [weight=500000, arrowsize=0, len=0, {$info}];\n"; } $ifdone[$src][$sif['port_id']] = 1; } else { $map .= "\"" . $sif['port_id'] . "\" [label=\"" . $sif['label'] . "\", fontsize=12, fillcolor=lightblue, URL=\"" . generate_url(array('page' => 'device', 'device' => $device['device_id'], 'tab' => 'port', 'port' => $local_port_id)) . "\"]\n"; if (!$ifdone[$src][$sif['port_id']]) { $map .= "\"{$src}\" -> \"" . $sif['port_id'] . "\" [weight=500000, arrowsize=0, len=0];\n"; $ifdone[$src][$sif['port_id']] = 1; }
/** * Display ARP/NDP table addresses. * * Display pages with ARP/NDP tables addresses from devices. * * @param array $vars * @return none * */ function print_arptable($vars) { // With pagination? (display page numbers in header) $pagination = isset($vars['pagination']) && $vars['pagination']; pagination($vars, 0, TRUE); // Get default pagesize/pageno $pageno = $vars['pageno']; $pagesize = $vars['pagesize']; $start = $pagesize * $pageno - $pagesize; $param = array(); $where = ' WHERE 1 '; foreach ($vars as $var => $value) { if ($value != '') { switch ($var) { case 'device': case 'device_id': $where .= generate_query_values($value, 'device_id'); break; case 'port': case 'port_id': $where .= generate_query_values($value, 'I.port_id'); break; case 'ip_version': $where .= generate_query_values($value, 'ip_version'); break; case 'address': if (isset($vars['searchby']) && $vars['searchby'] == 'ip') { $value = trim($value); if (strpos($value, ':') !== FALSE) { if (Net_IPv6::checkIPv6($value)) { $value = Net_IPv6::uncompress($value, TRUE); } else { // FIXME. Need another conversion ("2001:b08:b08" -> "2001:0b08:0b08") -- mike } } $where .= generate_query_values($value, 'ip_address', '%LIKE%'); } else { // MAC Addresses $value = str_replace(array(':', ' ', '-', '.', '0x'), '', $value); $where .= generate_query_values($value, 'mac_address', '%LIKE%'); } break; } } } if (isset($vars['sort'])) { switch ($vars['sort']) { case "port": $sort = " ORDER BY `I`.`port_label`"; break; case "ip_version": $sort = " ORDER BY `ip_version`"; break; case "ip": case "address": $sort = " ORDER BY `ip_address`"; break; case "mac": default: $sort = " ORDER BY `mac_address`"; } } // Show ARP tables only for permitted ports $query_permitted = generate_query_permitted(array('port'), array('port_table' => 'I')); $query = 'FROM `ip_mac` AS M '; $query .= 'LEFT JOIN `ports` AS I ON I.`port_id` = M.`port_id` '; $query .= $where . $query_permitted; $query_count = 'SELECT COUNT(`mac_id`) ' . $query; $query = 'SELECT * ' . $query; $query .= $sort; $query .= " LIMIT {$start},{$pagesize}"; // Query ARP/NDP table addresses $entries = dbFetchRows($query, $param); // Query ARP/NDP table address count if ($pagination) { $count = dbFetchCell($query_count, $param); } $list = array('device' => FALSE, 'port' => FALSE); if (!isset($vars['device']) || empty($vars['device']) || $vars['page'] == 'search') { $list['device'] = TRUE; } if (!isset($vars['port']) || empty($vars['port']) || $vars['page'] == 'search') { $list['port'] = TRUE; } $string = generate_box_open(); $string .= '<table class="table table-striped table-hover table-condensed">' . PHP_EOL; $cols = array('mac' => 'MAC Address', 'ip' => 'IP Address', 'device' => 'Device', 'port' => 'Port', '!remote_device' => 'Remote Device', '!remote_port' => 'Remote Port'); if (!$list['device']) { unset($cols['device']); } if (!$list['port']) { unset($cols['port']); } if (!$short) { $string .= get_table_header($cols, $vars); // Currently sorting is not available } foreach ($entries as $entry) { humanize_port($entry); $ip_version = $entry['ip_version']; $ip_address = $ip_version == 6 ? Net_IPv6::compress($entry['ip_address']) : $entry['ip_address']; $arp_host = dbFetchRow('SELECT * FROM `ipv' . $ip_version . '_addresses` AS A LEFT JOIN `ports` AS I ON A.`port_id` = I.`port_id` LEFT JOIN `devices` AS D ON D.`device_id` = I.`device_id` WHERE A.`ipv' . $ip_version . '_address` = ?', array($ip_address)); $arp_name = $arp_host ? generate_device_link($arp_host) : ''; $arp_if = $arp_host ? generate_port_link($arp_host) : ''; if ($arp_host['device_id'] == $entry['device_id']) { $arp_name = 'Self Device'; } if ($arp_host['port_id'] == $entry['port_id']) { $arp_if = 'Self Port'; } $string .= ' <tr>' . PHP_EOL; $string .= ' <td style="width: 160px;" class="entity">' . generate_popup_link('mac', format_mac($entry['mac_address'])) . '</td>' . PHP_EOL; $string .= ' <td style="width: 140px;">' . generate_popup_link('ip', $ip_address) . '</td>' . PHP_EOL; if ($list['device']) { $dev = device_by_id_cache($entry['device_id']); $string .= ' <td class="entity" style="white-space: nowrap;">' . generate_device_link($dev) . '</td>' . PHP_EOL; } if ($list['port']) { if ($entry['ifInErrors_delta'] > 0 || $entry['ifOutErrors_delta'] > 0) { $port_error = generate_port_link($entry, '<span class="label label-important">Errors</span>', 'port_errors'); } $string .= ' <td class="entity">' . generate_port_link($entry, $entry['port_label_short']) . ' ' . $port_error . '</td>' . PHP_EOL; } $string .= ' <td class="entity" style="width: 200px;">' . $arp_name . '</td>' . PHP_EOL; $string .= ' <td class="entity">' . $arp_if . '</td>' . PHP_EOL; $string .= ' </tr>' . PHP_EOL; } $string .= ' </tbody>' . PHP_EOL; $string .= '</table>'; $string .= generate_box_close(); // Print pagination header if ($pagination) { $string = pagination($vars, $count) . $string . pagination($vars, $count); } // Print ARP/NDP table echo $string; }
echo ' <span class="label">' . $pseudowire['pw_type'] . '</span>'; $br = "<br />"; } } if (!isset($ports_has_ext['ports_pagp']) || in_array($port['ifIndex'], $ports_has_ext['ports_pagp'])) { foreach (dbFetchRows("SELECT * FROM `ports` WHERE `pagpGroupIfIndex` = ? AND `device_id` = ?", array($port['ifIndex'], $device['device_id'])) as $member) { humanize_port($member); $pagp[$device['device_id']][$port['ifIndex']][$member['ifIndex']] = TRUE; echo $br . '<i class="oicon-arrow-join"></i> <strong>' . generate_port_link($member) . ' [PAgP]</strong>'; $br = "<br />"; } } if ($port['pagpGroupIfIndex'] && $port['pagpGroupIfIndex'] != $port['ifIndex']) { $pagp[$device['device_id']][$port['pagpGroupIfIndex']][$port['ifIndex']] = TRUE; $parent = dbFetchRow("SELECT * FROM `ports` WHERE `ifIndex` = ? and `device_id` = ?", array($port['pagpGroupIfIndex'], $device['device_id'])); humanize_port($parent); echo $br . '<i class="oicon-arrow-split"></i> <strong>' . generate_port_link($parent) . ' [PAgP]</strong>'; $br = "<br />"; } if (!isset($ports_has_ext['ports_stack_low']) || in_array($port['ifIndex'], $ports_has_ext['ports_stack_low'])) { foreach (dbFetchRows("SELECT * FROM `ports_stack` WHERE `port_id_low` = ? and `device_id` = ?", array($port['ifIndex'], $device['device_id'])) as $higher_if) { if ($higher_if['port_id_high']) { if ($pagp[$device['device_id']][$higher_if['port_id_high']][$port['ifIndex']]) { continue; } // Skip if same PAgP port $this_port = get_port_by_index_cache($device['device_id'], $higher_if['port_id_high']); if (is_array($this_port)) { echo $br . '<i class="oicon-arrow-split"></i> <strong>' . generate_port_link($this_port) . '</strong>'; $br = "<br />"; }
/** * Display FDB table. * * @param array $vars * @return none * */ function print_fdbtable($vars) { // With pagination? (display page numbers in header) $pagination = isset($vars['pagination']) && $vars['pagination']; pagination($vars, 0, TRUE); // Get default pagesize/pageno $pageno = $vars['pageno']; $pagesize = $vars['pagesize']; $start = $pagesize * $pageno - $pagesize; $param = array(); $where = ' WHERE 1 '; foreach ($vars as $var => $value) { if ($value != '') { switch ($var) { case 'device': case 'device_id': $where .= generate_query_values($value, 'I.device_id'); break; case 'port': case 'port_id': $where .= generate_query_values($value, 'I.port_id'); break; case 'interface': case 'port_name': $where .= generate_query_values($value, 'I.ifDescr', 'LIKE%'); break; case 'vlan_id': $where .= generate_query_values($value, 'F.vlan_id'); break; case 'vlan_name': $where .= generate_query_values($value, 'V.vlan_name'); break; case 'address': $where .= generate_query_values(str_replace(array(':', ' ', '-', '.', '0x'), '', $value), 'F.mac_address', '%LIKE%'); break; } } } // Show FDB tables only for permitted ports $query_permitted = generate_query_permitted(array('port'), array('port_table' => 'I')); $query = 'FROM `vlans_fdb` AS F '; $query .= 'LEFT JOIN `vlans` as V ON V.`vlan_vlan` = F.`vlan_id` AND V.`device_id` = F.`device_id` '; $query .= 'LEFT JOIN `ports` AS I ON I.`port_id` = F.`port_id` '; $query .= $where . $query_permitted; $query_count = 'SELECT COUNT(*) ' . $query; $query = 'SELECT * ' . $query; $query .= ' ORDER BY F.`mac_address`'; $query .= " LIMIT {$start},{$pagesize}"; // Query addresses $entries = dbFetchRows($query, $param); // Query address count if ($pagination) { $count = dbFetchCell($query_count, $param); } $list = array('device' => FALSE); if (!isset($vars['device']) || empty($vars['device']) || $vars['page'] == 'search') { $list['device'] = TRUE; } $string = '<table class="table table-bordered table-striped table-hover table-condensed">' . PHP_EOL; if (!$short) { $string .= ' <thead>' . PHP_EOL; $string .= ' <tr>' . PHP_EOL; $string .= ' <th>MAC地址</th>' . PHP_EOL; if ($list['device']) { $string .= ' <th>设备</th>' . PHP_EOL; } $string .= ' <th>接口</th>' . PHP_EOL; $string .= ' <th>VLAN ID</th>' . PHP_EOL; $string .= ' <th>VLAN名称</th>' . PHP_EOL; $string .= ' </tr>' . PHP_EOL; $string .= ' </thead>' . PHP_EOL; } $string .= ' <tbody>' . PHP_EOL; foreach ($entries as $entry) { humanize_port($entry); $string .= ' <tr>' . PHP_EOL; $string .= ' <td style="width: 160px;">' . generate_popup_link('mac', format_mac($entry['mac_address'])) . '</td>' . PHP_EOL; if ($list['device']) { $dev = device_by_id_cache($entry['device_id']); $string .= ' <td class="entity" style="white-space: nowrap;">' . generate_device_link($dev) . '</td>' . PHP_EOL; } if ($entry['ifInErrors_delta'] > 0 || $entry['ifOutErrors_delta'] > 0) { $port_error = generate_port_link($entry, '<span class="label label-important">错误</span>', 'port_errors'); } $string .= ' <td class="entity">' . generate_port_link($entry, short_ifname($entry['label'])) . ' ' . $port_error . '</td>' . PHP_EOL; $string .= ' <td>Vlan' . $entry['vlan_vlan'] . '</td>' . PHP_EOL; $string .= ' <td>' . $entry['vlan_name'] . '</td>' . PHP_EOL; $string .= ' </tr>' . PHP_EOL; } $string .= ' </tbody>' . PHP_EOL; $string .= '</table>'; // Print pagination header if ($pagination) { $string = pagination($vars, $count) . $string . pagination($vars, $count); } // Print FDB table echo $string; }