Squashed commit of the following:

commit 49e585ef4bcbafd1964f0d0b8b9eda4b2624c86e
Author: Oliver Gorwits <oliver@cpan.org>
Date:   Mon Dec 30 23:20:05 2013 +0000

    default is to have age filter enabled

commit c3635b3b096ea73906a553db5b588eaee29b2145
Author: Oliver Gorwits <oliver@cpan.org>
Date:   Mon Dec 30 23:15:08 2013 +0000

    disable Seen IPs checkbox when v6ish cidr is seen

commit f1550f2dcff96a9b2d77cbc3a8582429f3b078df
Author: Oliver Gorwits <oliver@cpan.org>
Date:   Mon Dec 30 22:56:12 2013 +0000

    make daterange green when age filter activated

commit 838578f2b9935c6f2abba7679f2ba87f30f651a3
Author: Oliver Gorwits <oliver@cpan.org>
Date:   Mon Dec 30 22:47:29 2013 +0000

    use class to control input colouring

commit 291d230e6a0216b4f5ad52b8eb04f3949ffe85e3
Author: Oliver Gorwits <oliver@cpan.org>
Date:   Mon Dec 30 22:43:45 2013 +0000

    move cidr input and remove sidebar title

    this is only so that the tooltip on the cide input isn't obscured
    by the table heading. I've no idea how to avoid that happening :-/

commit d0df5c7af82bea3fc674585962ca67f4cd82e553
Author: Oliver Gorwits <oliver@cpan.org>
Date:   Mon Dec 30 22:11:34 2013 +0000

    layout fixes for ipinventory sidebar

commit faeb614147e673d4d08822cafb65ca003c400edd
Author: Oliver Gorwits <oliver@cpan.org>
Date:   Mon Dec 30 21:27:01 2013 +0000

    prevent autoload for ipinventory data; prevent submit if CIDR is empty

commit b993611ff9bf1bd1eaed410beab09c754c786b1a
Author: Oliver Gorwits <oliver@cpan.org>
Date:   Mon Dec 30 21:08:26 2013 +0000

    revert to 0.0.0.0/32 default search

commit d9e34ec19a2ca0d5893215a0508478985c96076e
Author: Oliver Gorwits <oliver@cpan.org>
Date:   Mon Dec 30 20:59:22 2013 +0000

    set age sort to desc - show oldest unused

commit 4a4231c69c8483c65d211119d772ad40a3a95b69
Author: Oliver Gorwits <oliver@cpan.org>
Date:   Mon Dec 30 20:55:00 2013 +0000

    fix parameter names; make 0.0.0.0/0 default

commit 5fb6e42426ae8bf96d81b2f2ad25d118c900646d
Author: Eric A. Miller <emiller@cpan.org>
Date:   Mon Dec 30 15:16:13 2013 -0500

    Add IP Inventory report with options sidebar

commit 4b84ca3714990199303cd5e099310e4c1ff1458f
Author: Eric A. Miller <emiller@cpan.org>
Date:   Mon Dec 30 14:34:29 2013 -0500

    Add 'IP' as a report category

commit 849ae4e0a6f51102cb90236629f4e1b43fc9bc2e
Author: Eric A. Miller <emiller@cpan.org>
Date:   Mon Dec 30 14:33:10 2013 -0500

    Add CidrIps virtual view ResultSource. Enumerates all IP addresses within an IPv4 subnet.

commit 30abbb957bd40270a14ca6289e76f70bf721696e
Author: Eric A. Miller <emiller@cpan.org>
Date:   Mon Dec 30 14:29:27 2013 -0500

    Add Dan Grossman's date range picker component for Twitter Bootstrap.  Component relies on Moment.js which is also added.

commit 19830639bdbcd409a49228f18d29f6dff5e933e8
Author: Eric A. Miller <emiller@cpan.org>
Date:   Mon Dec 30 14:23:11 2013 -0500

    DBIx::Class::Helpers is currently a requirement for DBIx::Class::ResultSource::View's which use Helper::Row::SubClass.  Use a base ResultSet class so that we can make Helper::ResultSet::SetOperations and Helper::ResultSet::Shortcut available to all of our classes.  SetOperations is specifically needed for UNION operations, Shortcut's are candy.  Note: Trying to add entire Helper::ResultSet seemed to cause problems.
This commit is contained in:
Eric A. Miller
2013-12-31 13:53:49 -05:00
parent d782ef17b4
commit a2bba4484d
25 changed files with 1578 additions and 11 deletions

View File

@@ -6,7 +6,9 @@ use warnings;
use base 'DBIx::Class::Schema';
__PACKAGE__->load_namespaces;
__PACKAGE__->load_namespaces(
default_resultset_class => 'ResultSet',
);
our $VERSION = 31; # schema version used for upgrades, keep as integer

View File

@@ -0,0 +1,45 @@
package App::Netdisco::DB::Result::Virtual::CidrIps;
use strict;
use warnings;
use utf8;
use base 'DBIx::Class::Core';
__PACKAGE__->table_class('DBIx::Class::ResultSource::View');
__PACKAGE__->table('cidr_ips');
__PACKAGE__->result_source_instance->is_virtual(1);
__PACKAGE__->result_source_instance->view_definition(<<'ENDSQL');
SELECT host(network (cidr) + sub.int)::inet AS ip, NULL::text AS dns,
NULL::timestamp AS time_first, NULL::timestamp AS time_last, false::boolean AS active
FROM
( SELECT cidr, generate_series(1, broadcast(cidr) - (network(cidr)) - 1) AS int
FROM (
SELECT CASE WHEN family(cidr) = 4 THEN cidr
ELSE '0.0.0.0/32'::inet
END AS cidr
FROM ( SELECT ?::inet AS cidr) AS input) AS addr
) AS sub
ENDSQL
__PACKAGE__->add_columns(
"ip",
{ data_type => "inet", is_nullable => 0 },
"dns",
{ data_type => "text", is_nullable => 1 },
"active",
{ data_type => "boolean", is_nullable => 1 },
"time_first",
{
data_type => "timestamp",
is_nullable => 1,
},
"time_last",
{
data_type => "timestamp",
is_nullable => 1,
},
);
1;

View File

@@ -0,0 +1,11 @@
package App::Netdisco::DB::ResultSet;
use strict;
use warnings;
use base 'DBIx::Class::ResultSet';
__PACKAGE__->load_components(
qw{Helper::ResultSet::SetOperations Helper::ResultSet::Shortcut});
1;

View File

@@ -1,5 +1,5 @@
package App::Netdisco::DB::ResultSet::Admin;
use base 'DBIx::Class::ResultSet';
use base 'App::Netdisco::DB::ResultSet';
use strict;
use warnings FATAL => 'all';

View File

@@ -1,5 +1,5 @@
package App::Netdisco::DB::ResultSet::Device;
use base 'DBIx::Class::ResultSet';
use base 'App::Netdisco::DB::ResultSet';
use strict;
use warnings FATAL => 'all';

View File

@@ -1,5 +1,5 @@
package App::Netdisco::DB::ResultSet::DevicePort;
use base 'DBIx::Class::ResultSet';
use base 'App::Netdisco::DB::ResultSet';
use strict;
use warnings FATAL => 'all';

View File

@@ -1,5 +1,5 @@
package App::Netdisco::DB::ResultSet::Node;
use base 'DBIx::Class::ResultSet';
use base 'App::Netdisco::DB::ResultSet';
use strict;
use warnings FATAL => 'all';

View File

@@ -1,5 +1,5 @@
package App::Netdisco::DB::ResultSet::NodeIp;
use base 'DBIx::Class::ResultSet';
use base 'App::Netdisco::DB::ResultSet';
use strict;
use warnings FATAL => 'all';

View File

@@ -1,5 +1,5 @@
package App::Netdisco::DB::ResultSet::NodeWireless;
use base 'DBIx::Class::ResultSet';
use base 'App::Netdisco::DB::ResultSet';
use strict;
use warnings FATAL => 'all';

View File

@@ -1,5 +1,5 @@
package App::Netdisco::DB::ResultSet::Subnet;
use base 'DBIx::Class::ResultSet';
use base 'App::Netdisco::DB::ResultSet';
use strict;
use warnings FATAL => 'all';

View File

@@ -137,6 +137,10 @@ Port
=item *
IP
=item *
Node
=item *

View File

@@ -16,7 +16,7 @@ set(
'_admin_tasks' => {},
'_reports_menu' => {},
'_reports' => {},
'_report_order' => [qw/Device Port Node VLAN Network Wireless/],
'_report_order' => [qw/Device Port IP Node VLAN Network Wireless/],
);
# this is what Dancer::Template::TemplateToolkit does by default

View File

@@ -0,0 +1,169 @@
package App::Netdisco::Web::Plugin::Report::IpInventory;
use Dancer ':syntax';
use Dancer::Plugin::DBIC;
use Dancer::Plugin::Auth::Extensible;
use App::Netdisco::Web::Plugin;
register_report(
{ category => 'IP',
tag => 'ipinventory',
label => 'IP Inventory',
provides_csv => 1,
}
);
# Following two Perl Core 5.10+
use Time::Piece;
use Time::Seconds;
hook 'before' => sub {
return
unless ( request->path eq uri_for('/report/ipinventory')->path
or index( request->path, uri_for('/ajax/content/report/ipinventory')->path )
== 0 );
my $start = Time::Piece->new - ONE_DAY * 29;
params->{'limit'} ||= 256;
params->{'daterange'}
||= $start->ymd . " to " . Time::Piece->new->ymd;
};
get '/ajax/content/report/ipinventory' => require_login sub {
# Default to something simple with no results to prevent
# "Search failed!" error
my $subnet = param('subnet') || '0.0.0.0/32';
my $age = param('age_on') || '0';
my $agenot = param('age_invert') || '0';
my ( $start, $end ) = param('daterange') =~ /(\d+-\d+-\d+)/gmx;
my $limit = param('limit') || 256;
my $order = param('order') || 'IP';
my $never = param('never') || '0';
# We need a reasonable limit to prevent a potential DoS, especially if
# 'never' is true. TODO: Need better input validation, both JS and
# server-side to provide user feedback
$limit = 8192 if $limit > 8192;
$order = $order eq 'IP' ? \'ip ASC' : \'age DESC';
my $rs1 = schema('netdisco')->resultset('DeviceIp')->search(
undef,
{ join => 'device',
select => [
'alias AS ip',
'creation AS time_first',
'device.last_discover AS time_last',
'dns',
\'true AS active',
\'false AS node',
\'age(device.last_discover) AS age'
],
as => [qw( ip time_first time_last dns active node age)],
}
)->hri;
my $rs2 = schema('netdisco')->resultset('NodeIp')->search(
undef,
{ columns => [qw( ip time_first time_last dns active)],
'+select' => [ \'true AS node', \'age(time_last) AS age' ],
'+as' => [ 'node', 'age' ],
}
)->hri;
my $rs3 = schema('netdisco')->resultset('NodeNbt')->search(
undef,
{ columns => [qw( ip time_first time_last )],
'+select' => [
'nbname AS dns', 'active',
\'true AS node', \'age(time_last) AS age'
],
'+as' => [ 'dns', 'active', 'node', 'age' ],
}
)->hri;
my $rs_union = $rs1->union( [ $rs2, $rs3 ] );
if ( $never ) {
my $rs4 = schema('netdisco')->resultset('Virtual::CidrIps')->search(
undef,
{ bind => [ $subnet ],
columns => [qw( ip time_first time_last dns active)],
'+select' => [ \'false AS node', \'age(time_last) AS age' ],
'+as' => [ 'node', 'age' ],
}
)->hri;
$rs_union = $rs_union->union( [$rs4] );
}
my $rs_sub = $rs_union->search(
{ ip => { '<<' => $subnet } },
{ order_by => [qw( ip time_last )],
select => [
\'DISTINCT ON (ip) ip',
'dns',
\'date_trunc(\'second\', time_last) AS time_last',
\'date_trunc(\'second\', time_first) AS time_first',
'active',
'node',
'age'
],
as => [
'ip', 'dns', 'time_last', 'time_first',
'active', 'node', 'age'
],
}
)->as_query;
my $rs;
if ( $age && $start && $end ) {
$start = $start . ' 00:00:00';
$end = $end . ' 23:59:59';
if ( $agenot ) {
$rs = $rs_union->search(
{ -or => [
time_first => [ { '<', $start }, undef ],
time_last => { '>', $end },
]
},
{ from => { me => $rs_sub }, }
);
}
else {
$rs = $rs_union->search(
{ -and => [
time_first => { '>=', $start },
time_last => { '<=', $end },
]
},
{ from => { me => $rs_sub }, }
);
}
}
else {
$rs = $rs_union->search( undef, { from => { me => $rs_sub }, } );
}
my @results = $rs->order_by($order)->limit($limit)->all;
return unless scalar @results;
if ( request->is_ajax ) {
template 'ajax/report/ipinventory.tt', { results => \@results, },
{ layout => undef };
}
else {
header( 'Content-Type' => 'text/comma-separated-values' );
template 'ajax/report/ipinventory_csv.tt', { results => \@results, },
{ layout => undef };
}
};
1;

View File

@@ -55,4 +55,15 @@ ajax '/ajax/data/port/typeahead' => require_login sub {
to_json \@$results;
};
ajax '/ajax/data/subnet/typeahead' => require_login sub {
my $q = param('query') || param('term');
$q = "$q\%" if $q !~ m/\%/;
my $nets = schema('netdisco')->resultset('Subnet')->search(
{ 'me.net::text' => { '-ilike' => $q }},
{ columns => ['net'] } );
content_type 'application/json';
to_json [map {$_->net} $nets->all];
};
true;