sanity check ip inventory subnet, and fix SQL::Abstract glitching on the CidrIps query

This commit is contained in:
Oliver Gorwits
2014-01-01 15:54:08 +00:00
parent cb9abc2355
commit dcc8b2c644
2 changed files with 21 additions and 13 deletions

View File

@@ -11,16 +11,18 @@ __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
SELECT host(network (prefix) + 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 prefix,
generate_series(1, (broadcast(prefix) - network(prefix) - 1)) AS int
FROM (
SELECT ?::inet AS prefix
) AS addr
) AS sub
ENDSQL
__PACKAGE__->add_columns(
@@ -42,4 +44,4 @@ __PACKAGE__->add_columns(
},
);
1;
1;

View File

@@ -5,6 +5,7 @@ use Dancer::Plugin::DBIC;
use Dancer::Plugin::Auth::Extensible;
use App::Netdisco::Web::Plugin;
use NetAddr::IP::Lite ':lower';
register_report(
{ category => 'IP',
@@ -37,6 +38,9 @@ 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';
$subnet = NetAddr::IP::Lite->new($subnet);
$subnet = NetAddr::IP::Lite->new('0.0.0.0/32')
if (! $subnet) or ($subnet->addr eq '0.0.0.0');
my $age = param('age_on') || '0';
my $agenot = param('age_invert') || '0';
@@ -91,9 +95,11 @@ get '/ajax/content/report/ipinventory' => require_login sub {
my $rs_union = $rs1->union( [ $rs2, $rs3 ] );
if ( $never ) {
$subnet = NetAddr::IP::Lite->new('0.0.0.0/32') if ($subnet->bits ne 32);
my $rs4 = schema('netdisco')->resultset('Virtual::CidrIps')->search(
undef,
{ bind => [ $subnet ],
{ bind => [ $subnet->cidr ],
columns => [qw( ip time_first time_last dns active)],
'+select' => [ \'false AS node', \'age(time_last) AS age' ],
'+as' => [ 'node', 'age' ],
@@ -104,7 +110,7 @@ get '/ajax/content/report/ipinventory' => require_login sub {
}
my $rs_sub = $rs_union->search(
{ ip => { '<<' => $subnet } },
{ ip => { '<<' => $subnet->cidr } },
{ order_by => [qw( ip time_last )],
select => [
\'DISTINCT ON (ip) ip',