'Port Labels' ]; foreach ($options as $option => $label) { if (isset($vars[$option]) && $vars[$option]) { $navbar['options'][$option]['class'] = 'active'; $navbar['options'][$option]['url'] = generate_url($vars, [ $option => NULL ]); } else { $navbar['options'][$option]['url'] = generate_url($vars, [ $option => 'YES' ]); } $navbar['options'][$option]['text'] = $label; $navbar['options'][$option]['icon'] = $config['icon']['cef']; } // 'Devices' navbar menu $navbar['options']['devices']['text'] = 'Devices'; $navbar['options']['devices']['class'] = 'dropdown-scrollable'; $navbar['options']['devices']['icon'] = $config['icon']['network']; foreach(generate_form_values('device') AS $device_id => $device) { $navbar['options']['devices']['suboptions'][$device_id]['text'] = $device['name']; $navbar['options']['devices']['suboptions'][$device_id]['url'] = generate_url($vars, ['group' => NULL, 'device_id' => $device_id]); if($vars['device_id'] == $device_id) { $navbar['options']['devices']['text'] .= ' ('.$device['name'].')'; $navbar['options']['devices']['suboptions'][$device_id]['class'] = 'active'; } } // 'Groups' navbar menu $navbar['options']['groups']['text'] = 'Groups'; $navbar['options']['groups']['class'] = 'dropdown-scrollable'; $navbar['options']['groups']['icon'] = $config['icon']['group']; $groups = get_groups_by_type('device'); foreach($groups['device'] AS $group_id => $group) { $navbar['options']['groups']['suboptions'][$group_id]['text'] = $group['group_name']; $navbar['options']['groups']['suboptions'][$group_id]['url'] = generate_url($vars, ['group_id' => $group_id, 'device_id' => NULL]); if($vars['group_id'] == $group_id) { $navbar['options']['groups']['text'] .= ' ('.$group['group_name'].')'; $navbar['options']['groups']['class'] = 'active'; } } print_navbar($navbar); unset($navbar); function get_neighbour_map($vars) { global $cache; $where = [ generate_query_permitted('device') ]; if (isset($vars['group_id'])) { $device_id_list = get_group_entities($vars['group_id'], 'device'); if (count($device_id_list) > 0) { $where[] = '('.generate_query_values_ng($device_id_list, 'device_id') . ' OR '. generate_query_values_ng($device_id_list, 'remote_device_id') . ')'; } } elseif (isset($vars['device_id']) && $vars['device_id']) { //$where[] = "(`device_id` = '".dbEscape($vars['device_id'])."' OR `remote_device_id` = '".dbEscape($vars['device_id'])."')"; $where[] = '('.generate_query_values_ng($vars['device_id'], 'device_id') . ' OR '. generate_query_values_ng($vars['device_id'], 'remote_device_id') . ')'; } //r($where); $where = implode(" AND ", $where); $query = "SELECT * FROM `neighbours` WHERE `active` = '1' " . $where; //r($query); $neighbours = dbFetchRows($query); $nodes = []; $edges = []; // Build device_id and port_id lists for cache foreach ($neighbours as $neighbour) { $device_list[$neighbour['device_id']] = $neighbour['device_id']; if(is_numeric($neighbour['remote_device_id'])) { $device_list[$neighbour['remote_device_id']] = $neighbour['remote_device_id']; } $port_list[$neighbour['port_id']] = $neighbour['port_id']; if(is_numeric($neighbour['remote_port_id'])) { $port_list[$neighbour['remote_port_id']] = $neighbour['remote_port_id']; } } // Pre-populate cache with device and port info cache_entities_by_id('device', $device_list); cache_entities_by_id('port', $port_list); //r(count($cache['device'])); //r(count($cache['port'])); foreach ($neighbours as $neighbour) { // Is this a link to a known device? Do we have it already? if (is_numeric($neighbour['remote_port_id'])) { $remote_port = get_port_by_id_cache($neighbour['remote_port_id']); // Do we have this source device already? if (!isset($devices[$neighbour['device_id']])) { $devices[$neighbour['device_id']] = device_by_id_cache($neighbour['device_id']); } // Suppress links to unknown devices //$neighbour['remote_device_id'] = get_device_id_by_port_id($neighbour['remote_port_id']); if (!is_numeric($neighbour['remote_device_id'])) { continue; } // Suppress links to self if ($neighbour['remote_device_id'] == $neighbour['device_id']) { continue; } // Suppress duplicate links from other protocols and from other end of link if (isset($link_exists[$neighbour['remote_port_id'] . '-' . $neighbour['port_id']]) || isset($link_exists[$neighbour['port_id'] . '-' . $neighbour['remote_port_id']])) { continue; } if (!isset($devices[$neighbour['remote_device_id']])) { $devices[$neighbour['remote_device_id']] = device_by_id_cache($neighbour['remote_device_id']); } $port = get_port_by_id_cache($neighbour['port_id']); if ($port['ifInOctets_perc'] > $port['ifOutOctets_perc']) { $port['percent'] = $port['ifInOctets_perc']; } else { $port['percent'] = $port['ifOutOctets_perc']; } // Labels as in/out traffic //$in_label = format_number($port['ifInOctets_rate'] * 8); //$out_label = format_number($port['ifOutOctets_rate'] * 8); // labels as port_label_short $out_label = $port['port_label_short']; $in_label = $remote_port['port_label_short']; $in_colour = percent_colour($port['ifInOctets_perc'], 160); $out_colour = percent_colour($port['ifOutOctets_perc'], 160); // Out is from perspective of local node $gradient = $out_colour . ' ' . $out_colour . ' ' . $in_colour . ' ' . $in_colour; $edges[$neighbour['neighbour_id']] = ['source' => 'd'.$neighbour['device_id'], 'target' => 'd'.$neighbour['remote_device_id'], 'percent' => $port['percent'], 'percentin' => $port['ifInOctets_perc'], 'percentout' => $port['ifOutOctets_perc'], 'gradient' => $gradient, 'colourin' => $in_colour, 'colourout' => $out_colour, 'labelin' => $in_label, 'labelout' => $out_label, 'popupurl' => 'ajax/entity_popup.php?entity_type=port&entity_id=' . $port['port_id'], //'label' => $port['percent'].'%' ]; if(!isset($vars['port_labels'])) { unset($edges[$neighbour['neighbour_id']]['labelin']); unset($edges[$neighbour['neighbour_id']]['labelout']); } if (is_numeric($neighbour['remote_port_id'])) { $link_exists[$port['port_id'] . '-' . $neighbour['remote_port_id']] = TRUE; } } else { $external_id = string_to_id($neighbour['remote_hostname']); $externals[$external_id] = ['id' => $external_id, 'label' => $neighbour['remote_hostname']]; } } foreach($devices AS $device) { $id = 'd' . $device['device_id']; $parent_id = 'l'.string_to_id($device['location']); $nodes[$id] = [ 'id' => $id, 'label' => short_hostname($device['hostname']), 'popupurl' => 'ajax/entity_popup.php?entity_type=device&entity_id=' . $device['device_id'], 'parent' => $parent_id]; // Add parent nodes if (!isset($parents[$parent_id])) { $nodes[$parent_id] = [ 'id' => $parent_id, 'label' => $device['location']]; } } return [ 'nodes' => $nodes, 'edges' => $edges, 'external_nodes' => $externals ]; } $map = get_neighbour_map($vars); ?>