Merge branch 'master' of ssh://git.code.sf.net/p/netdisco/netdisco-ng
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
45
Netdisco/lib/App/Netdisco/DB/Result/Virtual/CidrIps.pm
Normal file
45
Netdisco/lib/App/Netdisco/DB/Result/Virtual/CidrIps.pm
Normal 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;
|
||||
11
Netdisco/lib/App/Netdisco/DB/ResultSet.pm
Normal file
11
Netdisco/lib/App/Netdisco/DB/ResultSet.pm
Normal 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;
|
||||
@@ -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';
|
||||
|
||||
@@ -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';
|
||||
|
||||
@@ -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';
|
||||
|
||||
@@ -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';
|
||||
|
||||
@@ -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';
|
||||
|
||||
@@ -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';
|
||||
|
||||
@@ -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';
|
||||
|
||||
@@ -137,6 +137,10 @@ Port
|
||||
|
||||
=item *
|
||||
|
||||
IP
|
||||
|
||||
=item *
|
||||
|
||||
Node
|
||||
|
||||
=item *
|
||||
|
||||
@@ -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
|
||||
|
||||
169
Netdisco/lib/App/Netdisco/Web/Plugin/Report/IpInventory.pm
Normal file
169
Netdisco/lib/App/Netdisco/Web/Plugin/Report/IpInventory.pm
Normal 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;
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user