From 2856271cd2529177e2a52ce86c1ed79f573b0fb3 Mon Sep 17 00:00:00 2001 From: "Eric A. Miller" Date: Tue, 24 Sep 2013 23:51:33 -0400 Subject: [PATCH] add vlan inventory report --- .../lib/App/Netdisco/DB/Result/DeviceVlan.pm | 11 +++++ .../Web/Plugin/Report/VlanInventory.pm | 44 +++++++++++++++++++ Netdisco/share/config.yml | 1 + .../share/views/ajax/report/vlaninventory.tt | 24 ++++++++++ .../views/ajax/report/vlaninventory_csv.tt | 12 +++++ 5 files changed, 92 insertions(+) create mode 100644 Netdisco/lib/App/Netdisco/Web/Plugin/Report/VlanInventory.pm create mode 100644 Netdisco/share/views/ajax/report/vlaninventory.tt create mode 100644 Netdisco/share/views/ajax/report/vlaninventory_csv.tt diff --git a/Netdisco/lib/App/Netdisco/DB/Result/DeviceVlan.pm b/Netdisco/lib/App/Netdisco/DB/Result/DeviceVlan.pm index d3e57f5a..06d328c5 100644 --- a/Netdisco/lib/App/Netdisco/DB/Result/DeviceVlan.pm +++ b/Netdisco/lib/App/Netdisco/DB/Result/DeviceVlan.pm @@ -69,6 +69,17 @@ __PACKAGE__->has_many( port_vlans_native => 'App::Netdisco::DB::Result::Virtual: { cascade_copy => 0, cascade_update => 0, cascade_delete => 0 } ); +=head2 ports + +Link relationship to support C. + +=cut + +__PACKAGE__->has_many( ports => 'App::Netdisco::DB::Result::DevicePortVlan', + { 'foreign.ip' => 'self.ip', 'foreign.vlan' => 'self.vlan' }, + { cascade_copy => 0, cascade_update => 0, cascade_delete => 0 } +); + =head2 tagging_ports Returns the set of Device Ports on which this VLAN is configured to be tagged. diff --git a/Netdisco/lib/App/Netdisco/Web/Plugin/Report/VlanInventory.pm b/Netdisco/lib/App/Netdisco/Web/Plugin/Report/VlanInventory.pm new file mode 100644 index 00000000..fcd6bc2c --- /dev/null +++ b/Netdisco/lib/App/Netdisco/Web/Plugin/Report/VlanInventory.pm @@ -0,0 +1,44 @@ +package App::Netdisco::Web::Plugin::Report::VlanInventory; + +use Dancer ':syntax'; +use Dancer::Plugin::DBIC; +use Dancer::Plugin::Auth::Extensible; + +use App::Netdisco::Web::Plugin; + +register_report( + { category => 'VLAN', + tag => 'vlaninventory', + label => 'VLAN Inventory', + provides_csv => 1, + } +); + +get '/ajax/content/report/vlaninventory' => require_login sub { + my $set = schema('netdisco')->resultset('DeviceVlan')->search( + { 'vlan.description' => { '!=', 'NULL' } }, + { join => { 'ports' => 'vlan' }, + select => [ + 'vlan.vlan', + 'vlan.description', + { count => { distinct => 'ports.ip' } }, + { count => 'ports.vlan' } + ], + as => [qw/ vlan description dcount pcount /], + group_by => [qw/ vlan.vlan vlan.description /], + } + ); + return unless $set->count; + + if ( request->is_ajax ) { + template 'ajax/report/vlaninventory.tt', { results => $set, }, + { layout => undef }; + } + else { + header( 'Content-Type' => 'text/comma-separated-values' ); + template 'ajax/report/vlaninventory_csv.tt', { results => $set, }, + { layout => undef }; + } +}; + +true; diff --git a/Netdisco/share/config.yml b/Netdisco/share/config.yml index 19284c17..bd4bc9a8 100644 --- a/Netdisco/share/config.yml +++ b/Netdisco/share/config.yml @@ -38,6 +38,7 @@ web_plugins: - Report::ApRadioChannelPower - Report::HalfDuplex - Report::DuplexMismatch + - Report::VlanInventory - AdminTask::PseudoDevice - AdminTask::Topology - AdminTask::JobQueue diff --git a/Netdisco/share/views/ajax/report/vlaninventory.tt b/Netdisco/share/views/ajax/report/vlaninventory.tt new file mode 100644 index 00000000..84b95ed2 --- /dev/null +++ b/Netdisco/share/views/ajax/report/vlaninventory.tt @@ -0,0 +1,24 @@ +[% USE Number.Format %] + + + + + + + + + + + [% WHILE (row = results.next) %] + + + + + + + [% END %] + +
VLAN IDVLAN NameDevice CountPort Count
+ [% row.vlan | html_entity %][% row.description %][% row.get_column('dcount') | format_number %][% row.get_column('pcount') | format_number %]
+ diff --git a/Netdisco/share/views/ajax/report/vlaninventory_csv.tt b/Netdisco/share/views/ajax/report/vlaninventory_csv.tt new file mode 100644 index 00000000..1bfc51c3 --- /dev/null +++ b/Netdisco/share/views/ajax/report/vlaninventory_csv.tt @@ -0,0 +1,12 @@ +[% USE CSV -%] +[% CSV.dump([ 'VLAN ID' 'VLAN Name' 'Device Count' 'Port Count' ]) %] + +[% WHILE (row = results.next) %] + [% mylist = [] %] + [% mylist.push(row.vlan) %] + [% mylist.push(row.description) %] + [% mylist.push(row.get_column('dcount')) %] + [% mylist.push(row.get_column('pcount')) %] + [% CSV.dump(mylist) %] + +[% END %]