New report for Port VLAN Mismatches (M. Bernstein)

This commit is contained in:
Oliver Gorwits
2017-04-25 12:26:09 +01:00
parent fd4678a68b
commit 3ec84161ae
5 changed files with 138 additions and 0 deletions

View File

@@ -1,3 +1,9 @@
2.035005 -
[NEW FEATURES]
* New report for Port VLAN Mismatches (M. Bernstein)
2.035004 - 2017-04-25
[BUG FIXES]

View File

@@ -0,0 +1,46 @@
package App::Netdisco::DB::Result::Virtual::PortVLANMismatch;
use strict;
use warnings;
use base 'DBIx::Class::Core';
__PACKAGE__->table_class('DBIx::Class::ResultSource::View');
__PACKAGE__->table('port_vlan_mismatch');
__PACKAGE__->result_source_instance->is_virtual(1);
__PACKAGE__->result_source_instance->view_definition(<<'ENDSQL');
WITH all_vlans AS
(SELECT ip, port,
array_to_string(array_agg( CASE WHEN native THEN 'n:' || vlan::text
ELSE vlan::text END
ORDER BY vlan ASC ), ', ') AS vlist
FROM device_port_vlan GROUP BY ip, port)
SELECT CASE WHEN length(ld.dns) > 0 THEN ld.dns ELSE host(ld.ip) END AS left_device,
lp.port AS left_port,
(SELECT vlist FROM all_vlans WHERE ip=lp.ip AND port=lp.port) AS left_vlans,
CASE WHEN length(rd.dns) > 0 THEN rd.dns ELSE host(rd.ip) END AS right_device,
rp.port AS right_port,
(SELECT vlist FROM all_vlans WHERE ip=rp.ip AND port=rp.port) AS right_vlans
FROM device ld
JOIN device_port lp USING (ip)
JOIN device_port rp ON lp.remote_ip=rp.ip AND lp.remote_port=rp.port
JOIN device rd ON rp.ip=rd.ip
WHERE ld.ip < rd.ip AND
(SELECT vlist FROM all_vlans WHERE ip=lp.ip AND port=lp.port)
!=
(SELECT vlist FROM all_vlans WHERE ip=rp.ip AND port=rp.port)
ORDER BY left_device, left_port
ENDSQL
__PACKAGE__->add_columns(
'left_device' => { data_type => 'text' },
'left_port' => { data_type => 'text' },
'left_vlans' => { data_type => 'text' },
'right_device' => { data_type => 'text' },
'right_port' => { data_type => 'text' },
'right_vlans' => { data_type => 'text' },
);
1;

View File

@@ -0,0 +1,33 @@
package App::Netdisco::Web::Plugin::Report::PortVLANMismatch;
use Dancer ':syntax';
use Dancer::Plugin::DBIC;
use Dancer::Plugin::Auth::Extensible;
use App::Netdisco::Web::Plugin;
register_report(
{ category => 'Port',
tag => 'portvlanmismatch',
label => 'Port VLAN Mismatches',
provides_csv => 1,
}
);
get '/ajax/content/report/portvlanmismatch' => require_login sub {
return unless schema('netdisco')->resultset('Device')->count;
my @results = schema('netdisco')->resultset('Virtual::PortVLANMismatch')->hri->all;
if (request->is_ajax) {
my $json = to_json (\@results);
template 'ajax/report/portvlanmismatch.tt', { results => $json },
{ layout => undef };
}
else {
header( 'Content-Type' => 'text/comma-separated-values' );
template 'ajax/report/portvlanmismatch.tt', { results => \@results, },
{ layout => undef };
}
};
1;

View File

@@ -36,6 +36,7 @@ trust_x_remote_user: false
path: '/'
web_plugins:
- Inventory
- Report::PortVLANMismatch
- Report::PortAdminDown
- Report::PortBlocking
- Report::PortMultiNodes

View File

@@ -0,0 +1,52 @@
<table id="data-table" class="table table-striped table-bordered" width="100%" cellspacing="0">
<thead>
<tr>
<th>Left Device</th>
<th>Left Port</th>
<th>Left VLANs</th>
<th>Right Device</th>
<th>Right Port</th>
<th>Right VLANs</th>
</tr>
</thead>
</table>
<script type="text/javascript">
$(document).ready(function() {
var table = $('#data-table').dataTable({
"deferRender": true,
"data": [% results %],
"columns": [
{
"data": 'left_device',
"render": function(data, type, row, meta) {
return '<a href="[% device_ports %]&q=' + encodeURIComponent(data) + '">' + he.encode(data) + '</a>'; }
}, {
"data": 'left_port',
"type": 'portsort',
"render": function(data, type, row, meta) {
return type === 'display' ?
'<a href="[% device_ports %]&q=' + encodeURIComponent(row.left_device) + '&f=' + encodeURIComponent(data) + '">' + he.encode(data) + '</a>' :
data; }
}, {
"data": 'left_vlans'
}, {
"data": 'right_device',
"render": function(data, type, row, meta) {
return '<a href="[% device_ports %]&q=' + encodeURIComponent(data) + '">' + he.encode(data) + '</a>'; }
}, {
"data": 'right_port',
"type": 'portsort',
"render": function(data, type, row, meta) {
return type === 'display' ?
'<a href="[% device_ports %]&q=' + encodeURIComponent(row.right_device) + '&f=' + encodeURIComponent(data) + '">' + he.encode(data) + '</a>' :
data; }
}, {
"data": 'right_vlans'
}
],
[% INCLUDE 'ajax/datatabledefaults.tt' -%]
});
});
</script>