99)) - display 99 neighbours from all device * print_neighbours(array('pagesize' => 10, 'pageno' => 3, 'pagination' => TRUE)) - display 10 neighbours from page 3 with pagination header * print_neighbours(array('pagesize' => 10, 'device' = 4)) - display 10 neighbours for device_id 4 * * @param array $vars * @return none * */ function print_neighbours($vars) { // Get neighbours array $neighbours = get_neighbours_array($vars); if (!$neighbours['count']) { // There have been no entries returned. Print the warning. print_warning('

No neighbours found!

'); } else { // Entries have been returned. Print the table. $list = array('device' => FALSE); if ($vars['page'] !== 'device') { $list['device'] = TRUE; } if (in_array($vars['graph'], array('bits', 'upkts', 'nupkts', 'pktsize', 'percent', 'errors', 'etherlike', 'fdb_count'))) { $graph_types = array($vars['graph']); } else { $graph_types = array('bits', 'upkts', 'errors'); } $string = generate_box_open($vars['header']); $string .= '' . PHP_EOL; $cols = array( array(NULL, 'class="state-marker"'), 'device_a' => 'Local Device', 'port_a' => 'Local Port', 'NONE' => NULL, 'device_b' => 'Remote Device', 'port_b' => 'Remote Port', 'protocol' => 'Protocol', 'last_change' => 'Last changed', ); if ($_SESSION['userlevel'] > 7) { $cols[] = '';//'Autodiscovery' } if (!$list['device']) { unset($cols[0], $cols['device_a']); } $string .= get_table_header($cols, $vars); $string .= ' ' . PHP_EOL; $protocol_classmap = [ 'cdp' => 'success', 'lldp' => 'warning', 'amap' => 'primary', 'mndp' => 'error', 'fdp' => 'delayed', 'edp' => 'suppressed' ]; foreach ($neighbours['entries'] as $entry) { $string .= ' ' . PHP_EOL; if ($list['device']) { $string .= ' '; $string .= ' ' . PHP_EOL; } $port = get_port_by_id_cache($entry['port_id']); $string .= ' ' . PHP_EOL; $string .= ' ' . PHP_EOL; //$string .= ""; // r($entry['remote_port_id']); r($entry['remote_port']); if (isset($entry['remote_port_id']) && is_numeric($entry['remote_port_id'])) { $remote_port = get_port_by_id_cache($entry['remote_port_id']); $remote_device = device_by_id_cache($remote_port['device_id']); $remote_info = strlen($remote_device['hardware']) ? '
' . escape_html($remote_device['hardware']) : ''; if (strlen($remote_device['version'])) { $remote_info .= '
' . $GLOBALS['config']['os'][$remote_device['os']]['text'] . ' ' . escape_html($remote_device['version']) . ''; } if (empty($remote_info)) { $remote_info = '
' . escape_html(truncate($entry['remote_platform'], '100')); if (strlen($entry['remote_version'])) { $remote_info .= '
' . escape_html(truncate($entry['remote_version'], 150)) . ''; } } $string .= ' ' . PHP_EOL; $string .= ' ' . PHP_EOL; } else { $remote_ip = strlen($entry['remote_address']) ? ' ('.generate_popup_link('ip', $entry['remote_address']).')' : ''; $remote_version = strlen($entry['remote_version']) ? '
'.escape_html(truncate($entry['remote_version'], 150)).'' : ''; $string .= ' '; $string .= ' ' . PHP_EOL; } if (isset($protocol_classmap[$entry['protocol']])) { $entry['protocol_class'] = 'label-'.$protocol_classmap[$entry['protocol']]; } $string .= ' ' . PHP_EOL; $string .= ' ' . PHP_EOL; if ($_SESSION['userlevel'] > 7) { $string .= ' ' . PHP_EOL; } $string .= ' ' . PHP_EOL; } $string .= ' ' . PHP_EOL; $string .= '
' . generate_device_link($entry, NULL, array('tab' => 'ports', 'view' => 'neighbours')) . ''.generate_port_link($port) . '
' . escape_html($port['ifAlias']) . '
' . generate_device_link($remote_device) .'' . $remote_info . '' . generate_port_link($remote_port) . '
' . escape_html($remote_port['ifAlias']) . '
' . escape_html($entry['remote_hostname']) . $remote_ip . '
'; $string .= escape_html(truncate($entry['remote_platform'], '100')) . $remote_version . PHP_EOL; $string .= '
' . escape_html($entry['remote_port']) . '' . strtoupper($entry['protocol']) . '' . format_uptime(get_time() - $entry['last_change_unixtime'], 'shorter') . ' ago' . generate_popup_link('autodiscovery', $entry['autodiscovery_id']) . '
'; $string .= generate_box_close(); // Print pagination header if ($neighbours['pagination_html']) { $string = $neighbours['pagination_html'] . $string . $neighbours['pagination_html']; } // Print echo $string; } } /** * Params: * * pagination, pageno, pagesize * device, port */ function get_neighbours_array($vars) { $array = array(); // With pagination? (display page numbers in header) $array['pagination'] = (isset($vars['pagination']) && $vars['pagination']); pagination($vars, 0, TRUE); // Get default pagesize/pageno $array['pageno'] = $vars['pageno']; $array['pagesize'] = $vars['pagesize']; $start = $array['pagesize'] * $array['pageno'] - $array['pagesize']; $pagesize = $array['pagesize']; // Active by default if (!isset($vars['active'])) { $vars['active'] = '1'; } elseif ($vars['active'] === 'any') { unset($vars['active']); } // Begin query generate $param = array(); $where = ' WHERE 1 '; foreach ($vars as $var => $value) { if ($value != '') { switch ($var) { case 'device': case 'device_a': $where .= generate_query_values($value, 'device_id'); break; case 'port': case 'port_a': $where .= generate_query_values($value, 'port_id'); break; case 'device_b': $where .= generate_query_values($value, 'remote_hostname'); break; case 'port_b': $where .= generate_query_values($value, 'remote_port'); break; case 'protocol': $where .= generate_query_values($value, 'protocol'); break; case 'platform': $where .= generate_query_values($value, 'remote_platform'); break; case 'version': $where .= generate_query_values($value, 'remote_version'); break; case 'active': $value = $value && $value != 'no' ? '1' : '0'; $where .= generate_query_values($value, 'active'); break; case 'remote_port_id': if ($value === 'NULL' || $value == 0) { $where .= ' AND isnull(`remote_port_id`)'; } else { $where .= ' AND !isnull(`remote_port_id`)'; } break; } } } // Show neighbours only for permitted devices and ports //$query_permitted = $GLOBALS['cache']['where']['ports_permitted']; $query_permitted = generate_query_permitted(array('ports', 'devices')); if ($vars['sort'] === 'port_a') { $query = 'SELECT `neighbours`.*, UNIX_TIMESTAMP(`last_change`) AS `last_change_unixtime`, `ports`.`port_label` '; $query .= 'FROM `neighbours` LEFT JOIN `ports` USING(`port_id`,`device_id`) '; } else { $query = 'SELECT `neighbours`.*, UNIX_TIMESTAMP(`last_change`) AS `last_change_unixtime` '; $query .= 'FROM `neighbours` '; } $query .= $where . ' ' . $query_permitted; // Query neighbours $array['entries'] = dbFetchRows($query, $param); //r($array['entries']); foreach ($array['entries'] as &$entry) { $device = &$GLOBALS['cache']['devices']['id'][$entry['device_id']]; if (!$entry['active']) { $entry['row_class'] = 'disabled'; } elseif ((isset($device['status']) && !$device['status'])) { $entry['row_class'] = 'error'; } elseif (isset($device['disabled']) && $device['disabled']) { $entry['row_class'] = 'ignore'; } $entry['hostname'] = $device['hostname']; //$entry['row_class'] = $device['row_class']; } // Sorting // FIXME. Sorting can be as function, but in must before print_table_header and after get table from db switch ($vars['sort_order']) { case 'desc': $sort_order = SORT_DESC; $sort_neg = SORT_ASC; break; case 'reset': unset($vars['sort'], $vars['sort_order']); // no break here default: $sort_order = SORT_ASC; $sort_neg = SORT_DESC; } switch ($vars['sort']) { case 'device_a': $array['entries'] = array_sort_by($array['entries'], 'hostname', $sort_order, SORT_STRING); break; case 'port_a': $array['entries'] = array_sort_by($array['entries'], 'port_label', $sort_order, SORT_STRING); break; case 'device_b': $array['entries'] = array_sort_by($array['entries'], 'remote_hostname', $sort_order, SORT_STRING); break; case 'port_b': $array['entries'] = array_sort_by($array['entries'], 'remote_port', $sort_order, SORT_STRING); break; case 'protocol': $array['entries'] = array_sort_by($array['entries'], 'protocol', $sort_order, SORT_STRING); break; case 'last_change': $array['entries'] = array_sort_by($array['entries'], 'last_change_unixtime', $sort_order, SORT_NUMERIC); break; default: // Not sorted } // Query neighbours count $array['count'] = safe_count($array['entries']); if ($array['pagination']) { $array['pagination_html'] = pagination($vars, $array['count']); $array['entries'] = array_chunk($array['entries'], $vars['pagesize']); $array['entries'] = $array['entries'][$vars['pageno'] - 1]; } // Query for last timestamp //$array['updated'] = dbFetchCell($query_updated, $param); return $array; } // EOF