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 = [ 'device' => FALSE ]; if ($vars['page'] !== 'device') { $list['device'] = TRUE; } if (in_array($vars['graph'], ['bits', 'upkts', 'nupkts', 'pktsize', 'percent', 'errors', 'etherlike', 'fdb_count'])) { $graph_types = [$vars['graph']]; } else { $graph_types = ['bits', 'upkts', 'errors']; } $string = generate_box_open($vars['header']); $string .= '' . PHP_EOL; $header = [ 'state-marker' => '', [ 'device_a' => 'Local Device' ], [ 'port_a' => 'Local Port' ], '', [ 'device_b' => 'Remote Device' ], [ 'port_b' => 'Remote Port' ], [ 'protocol' => 'Protocol' ], [ 'last_change' => 'Last changed', 'class' => 'text-nowrap' ], ]; if ($_SESSION['userlevel'] > 7) { $header[] = ''; // 'Autodiscovery' } if (!$list['device']) { //r($header); unset($header[0], $header['state-marker']); } $string .= generate_table_header($header, $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); //r($entry['remote_port']); if ((is_intnum($entry['remote_device_id']) && $entry['remote_device_id']) || is_intnum($entry['remote_port_id'])) { $remote_port = $entry['remote_port_id'] ? get_port_by_id_cache($entry['remote_port_id']) : []; if ($entry['remote_device_id']) { $remote_device = device_by_id_cache($entry['remote_device_id']); } else { $remote_device = device_by_id_cache($remote_port['device_id']); } $remote_info = !safe_empty($remote_device['hardware']) ? '
' . escape_html($remote_device['hardware']) : ''; if (!safe_empty($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 (!safe_empty($entry['remote_version'])) { $remote_info .= '
' . escape_html(truncate($entry['remote_version'], 150)) . ''; } } $string .= ' ' . PHP_EOL; if ($remote_port) { $string .= ' ' . PHP_EOL; } else { $string .= ' ' . PHP_EOL; } } else { $remote_ip = !safe_empty($entry['remote_address']) ? ' (' . generate_popup_link('ip', $entry['remote_address']) . ')' : ''; $remote_version = !safe_empty($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, [ '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_port']) . '' . 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 = []; // 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 = []; $where = ' WHERE 1 '; $where_array = []; foreach ($vars as $var => $value) { if (!safe_empty($value)) { switch ($var) { case 'device': case 'device_id': case 'device_a': $where_array[] = generate_query_values($value, 'device_id'); break; case 'port': case 'port_id': case 'port_a': $where_array[] = generate_query_values($value, 'port_id'); break; case 'remote': case 'device_b': $where_array[] = generate_query_values($value, 'remote_hostname'); break; case 'port_b': $where_array[] = generate_query_values($value, 'remote_port'); break; case 'protocol': $where_array[] = generate_query_values($value, 'protocol'); break; case 'platform': $where_array[] = generate_query_values($value, 'remote_platform'); break; case 'version': $where_array[] = generate_query_values($value, 'remote_version'); break; case 'active': $value = !get_var_false($value, 'no') ? 1 : 0; $where_array[] = generate_query_values($value, 'active'); break; case 'known': if ($value === 'NULL' || $value == 0) { $where_array[] = 'ISNULL(`remote_port_id`)'; } else { $where_array[] = '!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_ng([ '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 .= generate_where_clause($where_array, $query_permitted); // Query neighbours $array['entries'] = dbFetchRows($query, $param); //r($array['entries']); foreach ($array['entries'] as &$entry) { $device = device_by_id_cache($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