& SELF:err &>
% if (scalar @$ports) {
<& SELF:show_ports &>
% } elsif (scalar @$jacks) {
<& SELF:show_jacks &>
% } elsif ($building) {
<& SELF:show_rooms &>
% } elsif ($cmd eq 'list') {
<& SELF:list_buildings &>
% } elsif (! $err){
%}
<%args>
$cmd => ''
$hub => ''
$port => ''
$building => ''
@rooms => ()
%args>
<%shared>
my ($db_ports,$arg_port,$port_num,$hub_ip,$hub_num,$hub_dns,$arg_building,$jacks,$ports,$odd);
my (%db_ports_cache,%hub_ip_cache);
$jacks = $ports = [];
my $err = '';
my $domain = $netdisco::CONFIG{domain};
%shared>
<%doc>
Pinnacles Database on Oracle 8i
View 1 - netdisco_vw1
Any port, connected or not, no building name
HUB, PORT, BLDG, JACK, ROOM
View 2 - netdisco_vw2
Connected Ports only. Building Name
HUB, PORT, BLDG, BUILDING, JACK, ROOM
View 3 - netdisc_vw3
Same as one but w/ email only
HUB VARCHAR2(6)
PORT NOT NULL NUMBER
BLDG VARCHAR2(6)
ROOM VARCHAR2(6)
JACK VARCHAR2(6)
EMAIL VARCHAR2(30)
%doc>
<%init>
$arg_port = $port; $arg_building = $building;
# Check to see this feature is turned on
my $port_info = (defined $netdisco::CONFIG{port_info} and $netdisco::CONFIG{port_info} =~ /^(1|t|y)/i) ? 1 : 0;
unless ($port_info){
$m->redirect('./');
}
# Lookup Pinnacle data for given Port
if ($hub and $port){
$hub_dns = $hub;
$hub_dns .= $domain unless $hub =~ /\./;
$hub_ip = sql_scalar('device',['ip'],{'dns'=>$hub_dns});
if ($hub_dns =~ /hub(\d+)/i){
$hub_num = $1;
} else {
$err .= "Only switches with name HUBnnn are allowed.
\n";
}
unless (defined $hub_ip) {
$err .= "Hub $hub not found.
\n";
}
unless ($err) {
# Translate port name into Pinnacle port number
$port_num = $m->comp('SELF:map_port',hub_ip => $hub_ip, port => $port);
$err .= "Port $port not resolved to number.
\n" unless ($port_num =~ /^\d+$/);
}
unless ($err) {
local $netdisco::DB = 'Oracle';
# Try fetching from connected port first :
$ports = sql_rows('netdisco_vw2',['*'],{'HUB'=>$hub_num,'PORT'=>$port_num}) || [];
# Next try the unconnected ports:
unless (scalar @$ports) {
$ports = sql_rows('netdisco_vw1',['*'],{'HUB'=>$hub_num,'PORT'=>$port_num}) || [];
foreach my $row (@$ports){
$row->{BUILDING} = 'Unconnected';
$row->{BLDG} = '';
$row->{ROOM} = '-';
$row->{JACK} = '-';
}
}
# Try to grab Email Addresses
foreach my $prt (@$ports){
my $email = sql_scalar('netdisco_vw3',['EMAIL'],
{'HUB'=>$hub_num,'PORT'=>$port_num});
$prt->{EMAIL} = $email;
}
unless (scalar @$ports){
$err .= "$hub / $port ($port_num) not found in Pinnacles database.
\n";
}
}
}
# Search for jack -> switch mappings
if ($building and scalar(@rooms)){
if (grep(/^all$/,@rooms)){
local $netdisco::DB = 'Oracle';
$jacks = sql_rows('netdisco_vw2',['*'],{'BLDG'=>$building});
} else {
local $netdisco::DB = 'Oracle';
$jacks = sql_rows('netdisco_vw2',['*'],{'BLDG'=>$building,'ROOM'=>[\@rooms]});
}
# Try to grab Email Addresses
foreach my $jack (@$jacks){
local $netdisco::DB = 'Oracle';
my $jjack = $jack->{JACK};
my $jroom = $jack->{ROOM};
my $jbldg = $jack->{BLDG};
my $jhub = $jack->{HUB};
my $jport = $jack->{PORT};
my $email = undef;
# looks like some entries are missing hub/port info
# using jack/room/bldg instead of hub/port
unless (defined $jhub and defined $jport){
$email = sql_scalar('netdisco_vw3',['EMAIL'],{'JACK'=>$jjack,'ROOM'=>$jroom,'BLDG'=>$jbldg});
} else {
# this is faster, hence default.
$email = sql_scalar('netdisco_vw3',['EMAIL'],{'HUB'=>$jhub,'PORT'=>$jport});
}
$jack->{EMAIL} = $email;
}
unless (scalar @$jacks){
$err .= "No Jacks found for building $building.
\n";
}
}
%init>
%#
%# show_jacks() - Display results
%#
<%method show_jacks>
Jack List
% unless (scalar @$jacks) {
% return; }
Jack |
Room |
Switch / Port
(Pinnacle Port)
|
Email |
% foreach my $row (sort {$a->{ROOM} cmp $b->{ROOM}} @$jacks){
% my $hub = $row->{HUB};
% $hub_ip = $m->comp('SELF:hub_ip',hub_no => $hub);
% my $port = $m->comp('SELF:map_port_rev', port => $row->{PORT}, hub_ip => $hub_ip);
% my $email = $row->{EMAIL} || '';
<%$row->{JACK}%> |
<%$row->{ROOM}%> |
Hub<%$hub%> / <%$port%>
% if ($row->{PORT} ne $port) {
(<%$row->{PORT} %>) \
% }
|
<%$email ? "" : ''%><%$email |h%><%$email ? '' : ''%> |
%}
<% scalar @$jacks %> jacks found.
%method>
%#
%# show_ports() - Display results from port_info
%#
<%method show_ports>
Port Info
Data imported live from Pinnacles database.
%method>
%#
%# show_rooms() - Fetch rooms in a building from pinnacle
%#
<%method show_rooms>
Jack Search - Select Rooms
<%perl>
my $rooms;
{
local $netdisco::DB = 'Oracle';
$rooms = sql_rows('netdisco_vw2',['distinct ROOM','BLDG','BUILDING'],{'BLDG'=>$arg_building}) || [];
}
unless (scalar @$rooms){
$m->out("No rooms found for building No. $arg_building.
\n");
return;
}
%perl>
%method>
%#
%# list_buildings() - Populates drop-downs with options from Pinnacles
%#
<%method list_buildings>
Jack Search - Building List
<%perl>
my $buildings;
my $time1 = time;
{
local $netdisco::DB = 'Oracle';
$buildings = sql_rows('netdisco_vw2',['DISTINCT BLDG,BUILDING']) || ();
}
%perl>
%my $time2 = time;
Data took <%$time2 - $time1 %> seconds to load.
%method>
%#
%# map_port(hub_ip,port) - Translates device port name into Pinnacle port number
%#
<%method map_port>
<%args>
$port
$hub_ip
%args>
<%perl>
# Cisco 1900, Bay, HP 4000, ... - Just a number
if ($port =~ /^\d+$/){
return $port;
}
# Load Ports from database :
my $db_ports = $m->comp('SELF:db_ports',hub_ip => $hub_ip);
# New Method, let sort_port do the math and we're golden?
my @ports_sorted = map {$_->{port} } sort {sort_port} @$db_ports;
for (my $i=0; $i<@ports_sorted; $i++){
#$m->out($i+1 . " $ports_sorted[$i]
\n");
return $i+1 if $ports_sorted[$i] eq $port;
}
return undef;
%perl>
%method>
%#
%# map_port_rev(hub_ip,port) - Reverse mapping from a pinnacle port number to a netdisco port
%# Takes the list of ports, runs it through sort_port() and then
%# returns the index of the given port.
%#
<%method map_port_rev>
<%args>
$hub_ip
$port
%args>
<%perl>
my $db_ports = $m->comp('SELF:db_ports',hub_ip => $hub_ip);
my @port_list = map { $_->{port} } sort {sort_port} @$db_ports;
return undef if $port >= scalar @port_list;
return $port_list[$port-1];
%perl>
%method>
%#
%# db_ports() - Returns info from device_ports, caches data.
%#
<%method db_ports>
<%args>
$hub_ip
%args>
<%perl>
return $db_ports_cache{$hub_ip} if defined $db_ports_cache{$hub_ip};
my $ports = sql_rows('device_port',['ip','port','type'],{'ip'=>$hub_ip});
my @ports_no_vlans = ();
foreach my $p (@$ports){
my $type = $p->{type} || '';
next if ($type eq '53' or $type =~ /vlan/i);
push(@ports_no_vlans,$p);
}
$db_ports_cache{$hub_ip} = \@ports_no_vlans;
return $ports;
%perl>
%method>
%#
%# hub_ip() - Returns IP address when given hub # from pinnacles. Somewhat of a guess.
%#
<%method hub_ip>
<%args>
$hub_no
%args>
<%perl>
return $hub_ip_cache{$hub_no} if defined $hub_ip_cache{$hub_no};
$hub_ip_cache{$hub_no} = sql_scalar('device',['ip'],{'dns'=>"hub$hub_no\.%"});
return $hub_ip_cache{$hub_no};
%perl>
%method>
%#
%# err()
%#
<%method err>
% return unless $err;
<%$err%>
%method>
%#
%# title()
%#
<%method title>
- Port Info \
%method>
%# $Id: port_info.html,v 1.7 2007/12/14 02:18:24 maxbaker Exp $
%# vim:syntax=mason