node search by mac implementation

This commit is contained in:
Oliver Gorwits
2012-01-08 17:55:53 +00:00
parent af7a74c316
commit 51c88eefdc
5 changed files with 130 additions and 13 deletions

View File

@@ -0,0 +1,26 @@
package Netdisco::DB::ResultSet::Node;
use base 'DBIx::Class::ResultSet';
sub by_mac {
my ($set, $archive, $mac) = @_;
return $set unless $mac;
return $set->search(
{
'me.mac' => $mac,
($archive ? () : (active => 1)),
},
{
order_by => {'-desc' => 'time_last'},
columns => [qw/ mac switch port oui active device.dns /],
'+select' => [
\"to_char(time_first, 'YYYY-MM-DD HH24:MI')",
\"to_char(time_last, 'YYYY-MM-DD HH24:MI')",
],
'+as' => [qw/ time_first time_last /],
join => 'device',
},
);
}
1;

View File

@@ -57,4 +57,20 @@ sub by_name {
); );
} }
sub by_mac {
my ($set, $archive, $mac) = @_;
return $set unless $mac;
return $set->search(
{
mac => $mac,
($archive ? () : (active => 1)),
},
{
%$search_attr,
( $set->has_dns_col ? ('+columns' => 'dns') : () ),
}
);
}
1; 1;

View File

@@ -3,11 +3,12 @@ package Netdisco::Web;
use Dancer ':syntax'; use Dancer ':syntax';
use Dancer::Plugin::Ajax; use Dancer::Plugin::Ajax;
use Dancer::Plugin::DBIC; use Dancer::Plugin::DBIC;
use Dancer::Exception ':all';
use Digest::MD5 (); use Digest::MD5 ();
use Socket6 (); use Socket6 (); # to ensure dependency is met
use NetAddr::IP::Lite ':lower'; use NetAddr::IP::Lite ':lower';
use Regexp::Common 'net'; use Net::MAC ();
hook 'before' => sub { hook 'before' => sub {
if (! session('user') && request->path !~ m{^/login}) { if (! session('user') && request->path !~ m{^/login}) {
@@ -40,17 +41,25 @@ ajax '/ajax/content/search/:thing' => sub {
ajax '/ajax/content/search/node' => sub { ajax '/ajax/content/search/node' => sub {
my $node = param('q'); my $node = param('q');
return unless $node; return unless $node;
content_type('text/html');
try {
my $mac = Net::MAC->new(mac => $node);
my $ips = schema('netdisco')->resultset('NodeIp')
->by_mac(param('archived'), $mac->as_IEEE);
return unless $ips->count;
my $ports = schema('netdisco')->resultset('Node')
->by_mac(param('archived'), $mac->as_IEEE);
template 'ajax/node_by_mac.tt', {
ips => $ips,
ports => $ports,
}, { layout => undef };
}
catch {
my $set; my $set;
# if mac
# search on mac
# try to make ip
# search on ip
# text search for node dns
if ($node =~ m/^$RE{net}{MAC}$/) {
}
else {
if (my $ip = NetAddr::IP::Lite->new($node)) { if (my $ip = NetAddr::IP::Lite->new($node)) {
# by_ip() will extract cidr notation if necessary # by_ip() will extract cidr notation if necessary
$set = schema('netdisco')->resultset('NodeIp') $set = schema('netdisco')->resultset('NodeIp')
@@ -64,11 +73,10 @@ ajax '/ajax/content/search/node' => sub {
return unless $set->count; return unless $set->count;
} }
content_type('text/html'); template 'ajax/node_by_ip.tt', {
template 'ajax/node.tt', {
results => $set, results => $set,
}, { layout => undef }; }, { layout => undef };
} };
}; };
# devices carrying vlan xxx # devices carrying vlan xxx

View File

@@ -0,0 +1,67 @@
<table class="bordered-table condensed-table zebra-striped">
<thead>
<tr>
<th>MAC</th>
[% IF params.vendor %]
<th>Vendor</th>
[% END %]
<th>Match</th>
<th>Device or Node</th>
[% IF params.stamps %]
<th>First Seen</th>
<th>Last Seen</th>
[% END %]
</tr>
</thead>
</tbody>
[% SET first_row = 1 %]
[% WHILE (row = ips.next) %]
<tr>
<td>
[% IF first_row %]
<a class="nd_linkcell"
href="/search?[% vars.query_defaults.node %]&q=[% row.mac | uri %]">[% row.mac %]</a>
[% ELSE %]
&nbsp;
[% END %]
</a>
[% IF params.vendor %]
<td>
[% IF first_row %]
[% row.oui.company %]
[% ELSE %]
&nbsp;
[% END %]
</td>
[% END %]
<td>MAC &rarr; IP</td>
<td><a href="/search?[% vars.query_defaults.node %]&q=[% row.ip | uri %]">[% row.ip %]</a>
[% ' (' _ row.dns.remove(settings.domain_suffix) _ ')' IF row.dns %]
[% ' <span class="label warning">a</span>' IF NOT row.active %]
</td>
[% IF params.stamps %]
<td>[% row.time_first %]</td>
<td>[% row.time_last %]</td>
[% END %]
</tr>
[% SET first_row = 0 %]
[% END %]
[% WHILE (node = ports.next) %]
<tr>
<td>&nbsp;</td>
[% IF params.vendor %]
<td>&nbsp;</td>
[% END %]
<td>Switch Port</td>
<td><a href="/device?q=[% node.switch | url %]&port=[% node.port | url %]">[% node.switch %] [ [% node.port %] ]</a>
[% ' (' _ node.device.dns.remove(settings.domain_suffix) _ ')' IF node.device.dns %]
[% ' <span class="label warning">a</span>' IF NOT node.active %]
</td>
[% IF params.stamps %]
<td>[% node.time_first %]</td>
<td>[% node.time_last %]</td>
[% END %]
</tr>
[% END %]
</tbody>
</table>