implement (initial) device port listing

This commit is contained in:
Oliver Gorwits
2012-01-11 22:12:30 +00:00
parent d704a04760
commit 4e34274ac9
6 changed files with 172 additions and 10 deletions

View File

@@ -64,7 +64,17 @@ __PACKAGE__->set_primary_key("port", "ip");
# Created by DBIx::Class::Schema::Loader v0.07015 @ 2012-01-07 14:20:02
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:lcbweb0loNwHoWUuxTN/hA
__PACKAGE__->belongs_to( device => 'Netdisco::DB::Result::Device', 'ip' );
__PACKAGE__->belongs_to( device => 'Netdisco::DB::Result::Device', 'ip',
{
'+select' => [
\"replace(age(timestamp 'epoch' + uptime / 100 * interval '1 second', timestamp '1970-01-01 00:00:00-00')::text, 'mon', 'month')",
\"to_char(last_discover, 'YYYY-MM-DD HH24:MI')",
\"to_char(last_macsuck, 'YYYY-MM-DD HH24:MI')",
\"to_char(last_arpnip, 'YYYY-MM-DD HH24:MI')",
],
'+as' => [qw/ uptime last_discover last_macsuck last_arpnip /],
},
);
__PACKAGE__->has_many( port_vlans_tagged => 'Netdisco::DB::Result::DevicePortVlan',
sub {
my $args = shift;
@@ -91,4 +101,25 @@ sub native_vlan {
return eval { $row->native_port_vlan->vlan || undef };
};
sub is_free {
my ($row, $num, $unit) = @_;
return unless $num =~ m/^\d+$/
and $unit =~ m/(?:days|weeks|months|years)/;
return 0 unless
($row->up_admin and $row->up_admin eq 'up')
and ($row->up and $row->up eq 'down');
my $quan = {
days => (60 * 60 * 24),
weeks => (60 * 60 * 24 * 7),
months => (60 * 60 * 24 * 31),
years => (60 * 60 * 24 * 365),
};
my $total = $num * $quan->{$unit};
my $diff_sec = $row->lastchange / 100;
return ($diff_sec >= $total ? 1 : 0);
}
1;

View File

@@ -4,6 +4,24 @@ use base 'DBIx::Class::ResultSet';
use strict;
use warnings FATAL => 'all';
sub by_ip {
my ($set, $ip) = @_;
return $set unless $ip;
return $set->search(
{
'me.ip' => $ip,
},
{
'+select' => [
\"to_char(last_discover - (uptime - lastchange) / 100 * interval '1 second', 'YYYY-MM-DD HH24:MI:SS')",
],
'+as' => [qw/ lastchange_stamp /],
join => 'device',
}
);
}
sub by_mac {
my ($set, $mac) = @_;
return $set unless $mac;

View File

@@ -10,6 +10,7 @@ use HTML::Entities (); # to ensure dependency is met
use NetAddr::IP::Lite ':lower';
use Net::MAC ();
use List::MoreUtils ();
use netdisco (); # for sort_port
hook 'before' => sub {
if (! session('user') && request->path !~ m{^/login}) {
@@ -33,15 +34,15 @@ hook 'before' => sub {
{ name => 'c_port', label => 'Port', default => 'on' },
{ name => 'c_descr', label => 'Description', default => '' },
{ name => 'c_type', label => 'Type', default => '' },
{ name => 'c_duplex', label => 'Duplex', default => 'on' },
{ name => 'c_duplex', label => 'Duplex', default => '' },
{ name => 'c_lastchange', label => 'Last Change', default => '' },
{ name => 'c_name', label => 'Name', default => 'on' },
{ name => 'c_speed', label => 'Speed', default => 'on' },
{ name => 'c_speed', label => 'Speed', default => '' },
{ name => 'c_mac', label => 'Port MAC', default => '' },
{ name => 'c_mtu', label => 'MTU', default => '' },
{ name => 'c_vlan', label => 'Native VLAN', default => 'on' },
{ name => 'c_vmember', label => 'VLAN Membership', default => 'on' },
{ name => 'c_connected', label => 'Connected Devices', default => '' },
{ name => 'c_connected', label => 'Connected Devices', default => 'on' },
{ name => 'c_stp', label => 'Spanning Tree', default => '' },
{ name => 'c_up', label => 'Status', default => '' },
]);
@@ -88,6 +89,22 @@ ajax '/ajax/content/device/:thing' => sub {
};
# device ports with a description (er, name) matching
ajax '/ajax/content/device/ports' => sub {
my $ip = param('ip');
return unless $ip;
my $set = schema('netdisco')->resultset('DevicePort')->by_ip($ip);
return unless $set->count;
my $results = [ sort { &netdisco::sort_port($a->port, $b->port) } $set->all ];
content_type('text/html');
template 'ajax/device/ports.tt', {
results => $results,
}, { layout => undef };
};
# device details table
ajax '/ajax/content/device/details' => sub {
my $ip = param('ip');
return unless $ip;

View File

@@ -78,6 +78,7 @@ form .clearfix.success input {
/* somewhere between span1 and span2 is desirable */
.nd_days_select {
width: 56px;
margin-left: -2px !important;
}
/* search/filter button placement */
@@ -137,3 +138,7 @@ form .clearfix.success input {
.nd_port_query {
margin-left: -2px !important;
}
.center_cell {
text-align: center;
}

View File

@@ -0,0 +1,91 @@
<table class="bordered-table condensed-table zebra-striped">
<thead>
<tr>
<th></th>
[% FOREACH item IN vars.port_columns %]
[% NEXT UNLESS params.${item.name} %]
<th[% ' class="center_cell"' IF NOT loop.first %]>[% item.label %]</th>
[% END %]
</tr>
</thead>
</tbody>
[% FOREACH row in results %]
[% NEXT IF params.free AND NOT row.is_free(params.age_num, params.age_unit) %]
<tr>
<td>
[% IF row.up_admin == 'down' %]
<span class="nd_legendlabel label">s</span>
[% ELSIF row.stp == 'blocking' %]
<span class="nd_legendlabel label notice">b</span>
[% ELSIF row.is_free(params.age_num, params.age_unit) %]
<span class="nd_legendlabel label success">f</span>
[% ELSIF row.up_admin == 'up' AND row.up == 'down' %]
<span class="nd_legendlabel label warning">d</span>
[% END %]
</td>
[% IF params.c_port %]
<td>
[% row.port | html_entity %]
</td>
[% END %]
[% IF params.c_descr %]
<td class="center_cell">[% row.descr | html_entity %]</td>
[% END %]
[% IF params.c_type %]
<td class="center_cell">[% row.type | html_entity %]</td>
[% END %]
[% IF params.c_duplex %]
<td class="center_cell">
[% IF row.up == 'up' AND row.duplex %]
[% row.duplex | html_entity %] / [% row.duplex_admin | html_entity %]
[% END %]
</td>
[% END %]
[% IF params.c_lastchange %]
<td class="center_cell">[% row.get_column('lastchange_stamp') | html_entity %]</td>
[% END %]
[% IF params.c_name %]
<td class="center_cell">[% row.name | html_entity %]</td>
[% END %]
[% IF params.c_speed %]
<td class="center_cell">[% row.speed | html_entity %]</td>
[% END %]
[% IF params.c_mac %]
<td class="center_cell">[% row.mac | html_entity %]</td>
[% END %]
[% IF params.c_mtu %]
<td class="center_cell">[% row.mtu | html_entity %]</td>
[% END %]
[% IF params.c_vlan %]
<td class="center_cell">
<a class="nd_linkcell" href="/search?tab=vlan&q=[% row.vlan | uri %]">[% row.vlan | html_entity %]</a>
</td>
[% END %]
[% IF params.c_vmember %]
<td>
[% SET count = 1 %]
[% FOREACH vlan IN row.tagged_vlans %]
<a href="/search?tab=vlan&q=[% vlan.vlan | uri %]">[% vlan.vlan | html_entity %]</a>
[% SET count = count + 1 %]
[% IF count > 25 %]
(more...)
[% LAST %]
[% ELSE %]
[% ', ' IF NOT loop.last %]
[% END %]
[% END %]
</td>
[% END %]
[% IF params.c_connected %]
<td>[% row.connected | html_entity %]</td>
[% END %]
[% IF params.c_stp %]
<td class="center_cell">[% row.stp | html_entity %]</td>
[% END %]
[% IF params.c_up %]
<td class="center_cell">[% row.up | html_entity %]</td>
[% END %]
</tr>
[% END %]
</tbody>
</table>

View File

@@ -3,27 +3,27 @@
<input name="tab" value="[% tab.id %]" type="hidden"/>
<input name="ip" value="[% params.ip %]" type="hidden"/>
<div class="clearfix">
<input class="nd_port_query span3" placeholder="Port Name or VLAN"
<input class="nd_port_query span3" placeholder="Port, Name or VLAN"
name="q" value="[% params.port || params.vlan %]" type="text"
rel="twipsy" data-placement="right" data-offset="5" title="Filter by Port Name or VLAN"/>
rel="twipsy" data-placement="right" data-offset="5" title="Filter by Port, Name or VLAN"/>
</div>
<div class="clearfix">
<a href="#" class="nd_collapse_legend"><label>Legend</label></a>
<ul class="inputs-list unstyled">
<li>
<span class="label warning">d</span>&nbsp; Admin Disabled
<span class="label">s</span>&nbsp; Admin Disabled
</li>
<li>
<span class="label">l</span>&nbsp; Link Down
<span class="label warning">d</span>&nbsp; Link Down
</li>
<li>
<span class="label">f</span>&nbsp; Port Free (Down)
<span class="label success">f</span>&nbsp; Port Free (Down)
</li>
<li>
<span class="label notice">b</span>&nbsp; Blocking
</li>
<li>
<span class="label success">p</span>&nbsp; IP Phone
<span class="label">p</span>&nbsp; IP Phone
</li>
<li>
<span class="label important">n</span>&nbsp; Neighbor Inacessible