#72 Search on Vendor / OUI

This commit is contained in:
Eric A. Miller
2014-02-16 23:09:33 -05:00
parent 6b13100b82
commit b49216c9cb
7 changed files with 265 additions and 1 deletions

View File

@@ -4,6 +4,7 @@
* [#75] Device module inventory report / search
* [#70] SSID Search (port)
* [#72] Search on Vendor / OUI
[ENHANCEMENTS]

View File

@@ -0,0 +1,104 @@
package App::Netdisco::Web::Plugin::Report::NodeVendor;
use Dancer ':syntax';
use Dancer::Plugin::DBIC;
use Dancer::Plugin::Auth::Extensible;
use App::Netdisco::Web::Plugin;
register_report(
{ category => 'Node',
tag => 'nodevendor',
label => 'Node Vendor Inventory',
provides_csv => 1,
}
);
hook 'before' => sub {
return
unless (
request->path eq uri_for('/report/nodevendor')->path
or index( request->path,
uri_for('/ajax/content/report/nodevendor')->path ) == 0
);
params->{'limit'} ||= 1024;
params->{'order'} ||= 'MAC';
};
hook 'before_template' => sub {
my $tokens = shift;
return
unless (
request->path eq uri_for('/report/nodevendor')->path
or index( request->path,
uri_for('/ajax/content/report/nodevendor')->path ) == 0
);
# used in the search sidebar template to set selected items
foreach my $opt (qw/vendor/) {
my $p = (
ref [] eq ref param($opt)
? param($opt)
: ( param($opt) ? [ param($opt) ] : [] )
);
$tokens->{"${opt}_lkp"} = { map { $_ => 1 } @$p };
}
};
get '/ajax/content/report/nodevendor' => require_login sub {
my $vendor = param('vendor');
my $rs = schema('netdisco')->resultset('Node');
if ( defined $vendor ) {
my $match = $vendor eq 'blank' ? undef : $vendor;
my $order = {
MAC => 'me.mac',
Device => 'me.switch',
Vendor => 'oui.company'
};
$rs = $rs->search( { 'oui.abbrev' => $match } )
->prefetch( [qw/ oui device /] );
unless ( param('archived') ) {
$rs = $rs->search( { -bool => 'me.active' } );
}
$rs = $rs->order_by( $order->{ param('order') } )
->limit( param('limit') )->hri;
}
else {
$rs = $rs->search(
{ -bool => 'me.active' },
{ join => 'oui',
select => [ 'oui.abbrev', { count => 'me.mac' } ],
as => [qw/ vendor count /],
group_by => [qw/ oui.abbrev /]
}
)->order_by( { -desc => 'count' } )->hri;
}
return unless $rs->has_rows;
if ( request->is_ajax ) {
template 'ajax/report/nodevendor.tt',
{ results => $rs, opt => $vendor },
{ layout => undef };
}
else {
header( 'Content-Type' => 'text/comma-separated-values' );
template 'ajax/report/nodevendor_csv.tt',
{ results => $rs, opt => $vendor },
{ layout => undef };
}
};
1;

View File

@@ -8,7 +8,7 @@ get '/report/*' => require_login sub {
my ($tag) = splat;
# used in the report search sidebar to populate select inputs
my ( $domain_list, $class_list, $ssid_list );
my ( $domain_list, $class_list, $ssid_list, $vendor_list );
if ( $tag eq 'netbios' ) {
$domain_list = [ schema('netdisco')->resultset('NodeNbt')
@@ -22,6 +22,18 @@ get '/report/*' => require_login sub {
$ssid_list = [ schema('netdisco')->resultset('DevicePortSsid')
->get_distinct_col('ssid') ];
}
elsif ( $tag eq 'nodevendor' ) {
$vendor_list = [
schema('netdisco')->resultset('Node')->search(
{},
{ join => 'oui',
columns => ['oui.abbrev'],
order_by => 'oui.abbrev',
group_by => 'oui.abbrev',
}
)->get_column('abbrev')->all
];
}
# trick the ajax into working as if this were a tabbed page
params->{tab} = $tag;
@@ -33,6 +45,7 @@ get '/report/*' => require_login sub {
domain_list => $domain_list,
class_list => $class_list,
ssid_list => $ssid_list,
vendor_list => $vendor_list,
};
};

View File

@@ -51,6 +51,7 @@ web_plugins:
- Report::ModuleInventory
- Report::Netbios
- Report::NodeMultiIPs
- Report::NodeVendor
- Report::PhonesDiscovered
- Report::SsidInventory
- Report::VlanInventory

View File

@@ -0,0 +1,59 @@
[% USE Number.Format %]
[% IF opt %]
<table class="table table-bordered table-condensed table-striped nd_floatinghead">
<thead>
<tr>
<th>MAC</th>
<th>Vendor</th>
<th>Device (Port)</th>
</tr>
</thead>
</tbody>
[% WHILE (row = results.next) %]
<tr>
<td>
<a class="nd_linkcell"
href="[% search_node %]&q=[% row.mac | uri %]">
[% row.mac.upper | html_entity %][% '&nbsp;&nbsp;<i class="icon-book"></i> ' IF NOT row.active %]</a>
</td>
<td>
<a class="nd_linkcell"
[% IF row.oui.abbrev %]
href="[% uri_for('/report/nodevendor') %]?vendor=[% row.oui.abbrev | uri %]">
[% row.oui.abbrev | html_entity %] ( [% row.oui.company | html_entity %] )</a>
[% ELSE %]
href="[% uri_for('/report/nodevendor') %]?vendor=blank">
(Unknown Vendor)</a>
[% END %]
</td>
<td>
<a class="nd_linkcell"
href="[% device_ports %]&q=[% row.switch | uri %]&f=[% row.port | uri %]&c_nodes=on&n_ssid=on">
[% row.device.dns || row.device.name || row.switch | html_entity %] ( [% row.port | html_entity %] )</a>
</td>
</tr>
[% END %]
</tbody>
</table>
[% ELSE %]
<table class="table table-bordered table-condensed table-striped nd_floatinghead">
<thead>
<tr>
<th class="nd_center-cell">Vendor</th>
<th class="nd_center-cell">Count</th>
</tr>
</thead>
</tbody>
[% WHILE (row = results.next) %]
<tr>
<td>
<a class="nd_linkcell"
href="[% uri_for('/report/nodevendor') %]?vendor=[% row.vendor || 'blank' | uri %]">
[% row.vendor || '(Unknown Vendor)' | html_entity %]</a>
</td>
<td class="nd_center-cell">[% row.count | format_number %]</td>
</tr>
[% END %]
</tbody>
</table>
[% END %]

View File

@@ -0,0 +1,26 @@
[% USE CSV -%]
[% IF opt %]
[% CSV.dump(['MAC' 'Vendor' 'Company' 'Device' 'Port']) %]
[% WHILE (row = results.next) %]
[% mylist = [] %]
[% device = row.device.dns || row.device.name || row.switch %]
[% FOREACH col IN [ row.mac.upper row.oui.abbrev row.oui.company device row.port ] %]
[% mylist.push(col) %]
[% END %]
[% CSV.dump(mylist) %]
[% END %]
[% ELSE %]
[% CSV.dump(['Vendor' 'Count']) %]
[% WHILE (row = results.next) %]
[% mylist = [] %]
[% vendor = row.vendor || '(Unknown Vendor)' %]
[% FOREACH col IN [ vendor row.count ] %]
[% mylist.push(col) %]
[% END %]
[% CSV.dump(mylist) %]
[% END %]
[% END %]

View File

@@ -0,0 +1,60 @@
<p class="nd_sidebar-title"><em>Node Vendor Search</em></p>
<fieldset>
<legend class="nd_sidebar-legend">
<label><em><strong>Vendor</strong></em></label>
</legend>
<div class="clearfix">
<select class="nd_side-select nd_colored-input" size="[% vendor_list.size > 8 ? 8 : vendor_list.size %]"
multiple="on" name="vendor"
rel="tooltip" data-placement="left" data-offset="5" data-title="Vendor"/>
[% FOREACH opt IN vendor_list %]
[% UNLESS opt == '' %]
<option[% ' selected="selected"' IF vendor_lkp.exists(opt) %]>[% opt | html_entity %]</option>
[% ELSE %]
<option value="blank"[% ' selected="selected"' IF vendor_lkp.exists('blank') %]>[% '(Unknown Vendor)' | html_entity %]</option>
[% END %]
[% END %]
</select>
</div>
</fieldset>
<fieldset>
<legend class="nd_sidebar-legend">
<label><em><strong>Options</strong></em></label>
</legend>
<div class="clearfix">
<ul class="unstyled">
<li>
<em class="muted">Limit:</em><br/>
<select id="nd_mac-format" class="nd_side-select" name="limit">
[% FOREACH size IN [ '128', '256', '1024', '2048', '4096', '8192' ] %]
<option[% ' selected="selected"' IF params.limit == size %]>[% size %]</option>
[% END %]
</select>
</li>
<li>
<em class="muted">Order By:</em><br/>
<select id="nd_mac-format" class="nd_side-select" name="order">
[% FOREACH item IN [ 'Device', 'MAC', 'Vendor' ] %]
<option[% ' selected="selected"' IF params.order == item %]>[% item %]</option>
[% END %]
</select>
</li>
</ul>
<div class="clearfix input-prepend"
rel="tooltip" data-placement="left" data-offset="5" data-title="Include Archived Data">
<label class="add-on">
<input type="checkbox" id="archived"
name="archived"[% ' checked="checked"' IF params.archived %]/>
</label>
<label class="nd_checkboxlabel" for="archived">
<span class="nd_searchcheckbox uneditable-input">Archived Data</span>
</label>
</div>
</div>
</fieldset>
<button id="[% report.tag %]_submit" type="submit" class="btn btn-info">
<i class="icon-search icon-large pull-left nd_navbar-icon"></i> Search Vendors</button>