« blib/lib/App/Netdisco/Web/Plugin/Report/InventoryByModelByOS.pm
↑ Summary
blib/lib/App/Netdisco/Web/Plugin/Report/ModuleInventory.pm »
| 1 | package App::Netdisco::Web::Plugin::Report::IpInventory; | ||
| 2 | |||
| 3 | use Dancer ':syntax'; | ||
| 4 | use Dancer::Plugin::DBIC; | ||
| 5 | use Dancer::Plugin::Auth::Extensible; | ||
| 6 | |||
| 7 | use App::Netdisco::Web::Plugin; | ||
| 8 | use App::Netdisco::Util::L3Domain qw/results_span_domains label_rows_by_domain/; | ||
| 9 | use NetAddr::IP::Lite ':lower'; | ||
| 10 | use POSIX qw/strftime/; | ||
| 11 | |||
| 12 | register_report( | ||
| 13 | { category => 'IP', | ||
| 14 | tag => 'ipinventory', | ||
| 15 | label => 'IP Inventory', | ||
| 16 | provides_csv => 1, | ||
| 17 | api_endpoint => 1, | ||
| 18 | api_parameters => [ | ||
| 19 | subnet => { | ||
| 20 | description => 'IP Prefix to search', | ||
| 21 | required => 1, | ||
| 22 | }, | ||
| 23 | daterange => { | ||
| 24 | description => 'Date range to search', | ||
| 25 | default => ('1970-01-01 to '. strftime('%Y-%m-%d', gmtime)), | ||
| 26 | }, | ||
| 27 | age_invert => { | ||
| 28 | description => 'Results should NOT be within daterange', | ||
| 29 | type => 'boolean', | ||
| 30 | default => 'false', | ||
| 31 | }, | ||
| 32 | limit => { | ||
| 33 | description => 'Maximum number of historical records', | ||
| 34 | enum => [qw/32 64 128 256 512 1024 2048 4096 8192/], | ||
| 35 | default => '2048', | ||
| 36 | }, | ||
| 37 | never => { | ||
| 38 | description => 'Include in the report IPs never seen', | ||
| 39 | type => 'boolean', | ||
| 40 | default => 'false', | ||
| 41 | }, | ||
| 42 | ], | ||
| 43 | } | ||
| 44 | ); | ||
| 45 | |||
| 46 | get '/ajax/content/report/ipinventory' => require_login sub { | ||
| 47 | |||
| 48 | # Default to something simple with no results to prevent | ||
| 49 | # "Search failed!" error | ||
| 50 | (my $subnet = (param('subnet') || '0.0.0.0/32')) =~ s/\s//g; | ||
| 51 | $subnet = NetAddr::IP::Lite->new($subnet); | ||
| 52 | $subnet = NetAddr::IP::Lite->new('0.0.0.0/32') | ||
| 53 | if (! $subnet) or ($subnet->addr eq '0.0.0.0'); | ||
| 54 | |||
| 55 | my $agenot = param('age_invert') || '0'; | ||
| 56 | |||
| 57 | my $daterange = param('daterange') | ||
| 58 | || ('1970-01-01 to '. strftime('%Y-%m-%d', gmtime)); | ||
| 59 | my ( $start, $end ) = $daterange =~ /(\d+-\d+-\d+)/gmx; | ||
| 60 | |||
| 61 | my $limit = param('limit') || 256; | ||
| 62 | my $never = param('never') || '0'; | ||
| 63 | |||
| 64 | # We need a reasonable limit to prevent a potential DoS, especially if | ||
| 65 | # 'never' is true. TODO: Need better input validation, both JS and | ||
| 66 | # server-side to provide user feedback | ||
| 67 | $limit = 8192 if $limit > 8192; | ||
| 68 | |||
| 69 | my @results = schema(vars->{'tenant'})->resultset('DeviceIp')->ip_inventory($subnet, { | ||
| 70 | never => $never, | ||
| 71 | age_invert => $agenot, | ||
| 72 | limit => $limit, | ||
| 73 | (($start and $end) ? (start => "$start 00:00:00", end => "$end 23:59:59") : ()), | ||
| 74 | })->all; | ||
| 75 | return unless scalar @results; | ||
| 76 | my $multi_domain = results_span_domains(\@results); | ||
| 77 | |||
| 78 | if ( request->is_ajax ) { | ||
| 79 | my $json = to_json( label_rows_by_domain(\@results, $multi_domain) ); | ||
| 80 | template 'ajax/report/ipinventory.tt', | ||
| 81 | { results => $json, multi_domain => $multi_domain }, { layout => 'noop' }; | ||
| 82 | } | ||
| 83 | else { | ||
| 84 | header( 'Content-Type' => 'text/comma-separated-values' ); | ||
| 85 | template 'ajax/report/ipinventory_csv.tt', | ||
| 86 | { results => \@results, multi_domain => $multi_domain }, { layout => 'noop' }; | ||
| 87 | } | ||
| 88 | }; | ||
| 89 | |||
| 90 | 1; |