tighen up mac node search to work for switchports, and vendor display

This commit is contained in:
Oliver Gorwits
2012-01-13 20:38:20 +00:00
parent 2b4c6d26e9
commit f1d3bb9fd9
5 changed files with 54 additions and 12 deletions

View File

@@ -139,6 +139,16 @@ sub native_vlan {
return eval { $row->native_port_vlan->vlan || undef };
};
__PACKAGE__->belongs_to( oui => 'Netdisco::DB::Result::Oui',
sub {
my $args = shift;
return {
"$args->{foreign_alias}.oui" =>
{ '=' => \"substring(cast($args->{self_alias}.mac as varchar) for 8)" }
};
}
);
sub is_free {
my ($row, $num, $unit) = @_;
return unless $num =~ m/^\d+$/

View File

@@ -54,5 +54,6 @@ __PACKAGE__->belongs_to( device_port => 'Netdisco::DB::Result::DevicePort',
{ 'foreign.ip' => 'self.switch', 'foreign.port' => 'self.port' }, { join_type => 'LEFT' } );
__PACKAGE__->has_many( ips => 'Netdisco::DB::Result::NodeIp',
{ 'foreign.mac' => 'self.mac', 'foreign.active' => 'self.active' } );
__PACKAGE__->belongs_to( oui => 'Netdisco::DB::Result::Oui', 'oui' );
1;

View File

@@ -32,7 +32,6 @@ sub by_mac {
},
{
order_by => {'-desc' => 'me.creation'},
columns => [qw/ ip port device.dns /],
'+select' => [
\"to_char(me.creation, 'YYYY-MM-DD HH24:MI')",
],

View File

@@ -25,7 +25,7 @@ hook 'before' => sub {
# make hash lookups of query lists
foreach my $opt (qw/model vendor os_ver/) {
my $p = (ref [] eq ref param($opt) ? param($opt) : (param($opt) ? param($opt) : []));
my $p = (ref [] eq ref param($opt) ? param($opt) : (param($opt) ? [param($opt)] : []));
var("${opt}_lkp" => { map { $_ => 1 } @$p });
}
@@ -198,16 +198,19 @@ ajax '/ajax/content/search/node' => sub {
my $mac = Net::MAC->new(mac => $node, 'die' => 0, verbose => 0);
if (eval { $mac->as_IEEE }) {
my $ips = schema('netdisco')->resultset('NodeIp')
->by_mac(param('archived'), $mac->as_IEEE);
return unless $ips->count;
my $sightings = schema('netdisco')->resultset('Node')
->by_mac(param('archived'), $mac->as_IEEE);
my $ips = schema('netdisco')->resultset('NodeIp')
->by_mac(param('archived'), $mac->as_IEEE);
my $ports = schema('netdisco')->resultset('DevicePort')
->by_mac($mac->as_IEEE);
return unless $sightings->count
or $ips->count
or $ports->count;
template 'ajax/search/node_by_mac.tt', {
ips => $ips,
sightings => $sightings,
@@ -300,7 +303,8 @@ get '/search' => sub {
else {
my $s = schema('netdisco');
if ($q =~ m{^[a-f0-9.:/]+$}i) {
if ($s->resultset('Device')->find($q)) {
if (NetAddr::IP::Lite->new($q) and
$s->resultset('Device')->find($q)) {
params->{'tab'} = 'device';
}
else {

View File

@@ -41,16 +41,29 @@
</td>
[% IF params.stamps %]
<td>[% row.time_first %]</td>
<td>[% row.time_last %]</td>
<td>[% rw.time_last %]</td>
[% END %]
</tr>
[% SET first_row = 0 %]
[% END %]
[% WHILE (node = sightings.next) %]
<tr>
<td>&nbsp;</td>
<td>
[% IF first_row %]
<a class="nd_linkcell"
href="/search?[% vars.query_defaults.node %]&q=[% node.mac | uri %]">[% node.mac %]</a>
[% ELSE %]
&nbsp;
[% END %]
</td>
[% IF params.vendor %]
<td>&nbsp;</td>
<td>
[% IF first_row %]
[% node.oui.company %]
[% ELSE %]
&nbsp;
[% END %]
</td>
[% END %]
<td>Switch Port</td>
<td><a href="/device?tab=ports&ip=[% node.switch | url %]&q=[% node.port | url %]">[% node.switch %] [ [% node.port %] ]</a>
@@ -62,12 +75,26 @@
<td>[% node.time_last %]</td>
[% END %]
</tr>
[% SET first_row = 0 %]
[% END %]
[% WHILE (port = ports.next) %]
<tr>
<td>&nbsp;</td>
<td>
[% IF first_row %]
<a class="nd_linkcell"
href="/search?[% vars.query_defaults.node %]&q=[% port.mac | uri %]">[% port.mac %]</a>
[% ELSE %]
&nbsp;
[% END %]
</td>
[% IF params.vendor %]
<td>&nbsp;</td>
<td>
[% IF first_row %]
[% port.oui.company %]
[% ELSE %]
&nbsp;
[% END %]
</td>
[% END %]
<td>Switch Port</td>
<td><a href="/device?tab=ports&ip=[% port.ip | url %]&q=[% port.port | url %]">[% port.ip %] [ [% port.port %] ]</a>
@@ -78,6 +105,7 @@
<td>[% port.creation %]</td>
[% END %]
</tr>
[% SET first_row = 0 %]
[% END %]
</tbody>
</table>