implement ip/host search for node, and vendor column

This commit is contained in:
Oliver Gorwits
2012-01-08 15:24:18 +00:00
parent 0008ebeffd
commit daea7243be
6 changed files with 130 additions and 55 deletions

View File

@@ -39,15 +39,25 @@ __PACKAGE__->set_primary_key("mac", "ip");
# Created by DBIx::Class::Schema::Loader v0.07015 @ 2012-01-07 14:20:02
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:9+CuvuVWH88WxAf6IBij8g
__PACKAGE__->has_many( nodeips => 'Netdisco::DB::Result::NodeIp',
__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)" }
};
}
);
__PACKAGE__->has_many( node_ips => 'Netdisco::DB::Result::NodeIp',
{ 'foreign.mac' => 'self.mac' } );
__PACKAGE__->has_many( nodes => 'Netdisco::DB::Result::Node',
{ 'foreign.mac' => 'self.mac' } );
sub tidy_nodeips {
sub ip_aliases {
my ($row, $archive) = @_;
return $row->nodeips(
return $row->node_ips(
{
ip => { '!=' => $row->ip },
($archive ? () : (active => 1)),
@@ -64,7 +74,7 @@ sub tidy_nodeips {
);
}
sub tidy_nodes {
sub node_sightings {
my ($row, $archive) = @_;
return $row->nodes(

View File

@@ -1,24 +1,46 @@
package Netdisco::DB::ResultSet::NodeIp;
use base 'DBIx::Class::ResultSet';
my $search_attr = {
order_by => {'-desc' => 'time_last'},
columns => [qw/ mac ip dns active oui.company /],
'+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 => 'oui'
};
sub by_ip {
my ($set, $ip, $archive) = @_;
my ($set, $archive, $ip) = @_;
return $set unless $ip;
my $op = '=';
if ('NetAddr::IP::Lite' eq ref $ip) {
$op = '<<=' if $ip->num > 1;
$ip = $ip->cidr;
}
return $set->search(
{
ip => $ip,
ip => { $op => $ip },
($archive ? () : (active => 1)),
},
$search_attr,
);
}
sub by_name {
my ($set, $archive, $name) = @_;
return $set unless $name;
return $set->search(
{
order_by => {'-desc' => 'time_last'},
columns => [qw/ mac ip dns active /],
'+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 /],
dns => { '-ilike' => $name },
($archive ? () : (active => 1)),
},
$search_attr,
);
}

View File

@@ -3,7 +3,11 @@ package Netdisco::Web;
use Dancer ':syntax';
use Dancer::Plugin::Ajax;
use Dancer::Plugin::DBIC;
use Digest::MD5 ();
use Socket6 ();
use NetAddr::IP::Lite ':lower';
use Regexp::Common 'net';
hook 'before' => sub {
if (! session('user') && request->path !~ m{^/login}) {
@@ -11,6 +15,16 @@ hook 'before' => sub {
#var(requested_path => request->path);
#request->path_info('/');
}
# set up default search options for each type
if (not param('tab') or param('tab') ne 'node') {
params->{'stamps'} = 'checked';
}
# set up query string defaults for templates
var('query_defaults' => { map { ($_ => "tab=$_") } qw/node/ });
var('query_defaults')->{node} .= "\&$_=". (param($_) || '')
for qw/stamps vendor archived partial/;
};
get '/' => sub {
@@ -26,15 +40,35 @@ ajax '/ajax/content/search/:thing' => sub {
ajax '/ajax/content/search/node' => sub {
my $node = param('q');
return unless $node;
my $set;
my $set = schema('netdisco')->resultset('NodeIp')
->by_ip($node, param('archived'));
return unless $set->count;
# if mac
# search on mac
# try to make ip
# search on ip
# text search for node dns
content_type('text/html');
template 'ajax/node.tt', {
results => $set,
}, { layout => undef };
if ($node =~ m/^$RE{net}{MAC}$/) {
}
else {
if (my $ip = NetAddr::IP::Lite->new($node)) {
# by_ip() will extract cidr notation if necessary
$set = schema('netdisco')->resultset('NodeIp')
->by_ip(param('archived'), $ip);
return unless $set->count;
}
else {
$node = "\%$node\%" if param('partial');
$set = schema('netdisco')->resultset('NodeIp')
->by_name(param('archived'), $node);
return unless $set->count;
}
content_type('text/html');
template 'ajax/node.tt', {
results => $set,
}, { layout => undef };
}
};
# devices carrying vlan xxx
@@ -48,27 +82,10 @@ ajax '/ajax/content/search/vlan' => sub {
content_type('text/html');
template 'ajax/vlan.tt', {
results => $set,
columns => [
{ key => 'dns', label => 'Device' },
{ key => 'vlan.description', label => 'Description' },
{ key => 'model', label => 'Model' },
{ key => 'os', label => 'OS' },
{ key => 'vendor', label => 'Vendor' },
],
hyperlink => sub {
my $row = shift;
return '/device?q='. $row->ip .'&vlan='. $vlan;
},
}, { layout => undef };
};
get '/search' => sub {
# set up default search options for each type
if (not param('tab') or param('tab') ne 'node') {
params->{'stamps'} = 'checked';
params->{'vendor'} = 'checked';
}
my $q = param('q');
if ($q and not param('tab')) {
# pick most likely tab for initial results