Files
netdisco/lib/App/Netdisco/Web/Plugin/Device/SNMP.pm
Oliver Gorwits dc1f76c1aa Feature to gather SNMP Walk, use as Pseudo Device, and Browse Objects
* fix anomalous name

* add gather worker

* fix encoding of binary storage

* store results back to job

* now parsing mbis report to translate

* fix the broken report parser

* rename gather to snapshot

* implement walk code copied from SNMP::Info

* can now bulkwalk and parse mibs report and store resolved walk in cache

* add func/glob aliasing broken

* better aliasing

* implement aliasing from globals and funcs

* fix regexp for matching netdisco-mibs report

* fake cache entry for all ND2 methods called, add comments

* also save to logs/snapshots/IP

* add doc for netdisco-do

* add is_pseudo column to device table

* support for loading cache for pseudo devices

* check for hrSystemUptime as well as sysUpTime for snmp connect

* display pseudo devices with yellow pill for name

* color all cells for layers for pseudo

* no need to b64 encode binary data in scalars as we b64 whole thing after

* tweaked uptime check

* store snapshot to database instead of Job

* expose snapshots in device details tab

* small ux improvements on snap download

* fixes for errors in subnet mask searching

* hide snapshot management for pseudo devices

* update to use new netdisco-mibs object cache

* update for new format oids file

* start of work on loading walk into db for browsing

* store values and meta

* add auto increment col and oid index to browser

* start web plugin for browser

* add virtual search for oid children

* have all oid in separte table (60 seconds load on my laptop)

* rename table and add relation

* store oid as int array

* fix sql for children

* make jstree start working

* working very slow tree expand

* fix to work when first displaying tree

* store both oid and oid_parts

* simplify SQL to speed up (more complicated perl)

* fix sql bug, add better index, prettify tree

* render the snmp node detail

* add node template, make scrollable, pretty print data values (insecure)

* store munge hint

* some dubious code to munge the data

* make sure to filter by IP on device_browser

* make safer the rendering of value data (but need to come back to key ordering)

* fix sorting on object values

* limit the opening of child nodes to keep response good and unclutter

* factor out the munge and make safer

* reject unknown mungers

* show the munger and option (not working) to change

* additional js for munge select

* complete custom munge

* change so that saving to database is only at CLI and on request

* hide snmp tab if no browser rows in the db

* add helpful message when no browser rows for the device

* stub handler for search and add recurse control

* working search

* minor ui fixes

* implement typeahead for leaf search

* limit rows in typeahead

* make sure device_browser is visited in delete and renumber

* add requirements for this branch

* update manifest

* make sure node search and typeahead are restricted to current device only
2021-11-06 07:47:29 +00:00

176 lines
6.0 KiB
Perl

package App::Netdisco::Web::Plugin::Device::SNMP;
use strict;
use warnings;
use Dancer qw(:syntax);
use Dancer::Plugin::Ajax;
use Dancer::Plugin::DBIC;
use Dancer::Plugin::Swagger;
use Dancer::Plugin::Auth::Extensible;
use App::Netdisco::Web::Plugin;
use App::Netdisco::Util::SNMP qw(%ALL_MUNGERS decode_and_munge);
use Module::Load ();
use Try::Tiny;
register_device_tab({ tag => 'snmp', label => 'SNMP',
render_if => sub { schema('netdisco')->resultset('DeviceBrowser')->count() } });
get '/ajax/content/device/snmp' => require_login sub {
my $device = try { schema('netdisco')->resultset('Device')
->find( param('ip') ) }
or send_error('Bad Device', 404);
template 'ajax/device/snmp.tt', { device => $device->ip },
{ layout => 'noop' };
};
ajax '/ajax/data/device/:ip/snmptree/:base' => require_login sub {
my $device = try { schema('netdisco')->resultset('Device')
->find( param('ip') ) }
or send_error('Bad Device', 404);
my $recurse = ((param('recurse') and param('recurse') eq 'on') ? 0 : 1);
my $base = param('base');
$base =~ m/^\.1\.3\.6\.1(\.\d+)*$/ or send_error('Bad OID Base', 404);
my $items = _get_snmp_data($device->ip, $base, $recurse);
content_type 'application/json';
to_json $items;
};
ajax '/ajax/data/device/:ip/typeahead' => require_login sub {
my $device = try { schema('netdisco')->resultset('Device')
->find( param('ip') ) }
or send_error('Bad Device', 404);
my $term = param('term') or return to_json [];
$term = '%'. $term .'%';
my @found = schema('netdisco')->resultset('DeviceBrowser')
->search({ leaf => { -ilike => $term }, ip => $device->ip },
{ rows => 25, columns => 'leaf' })
->get_column('leaf')->all;
return to_json [] unless scalar @found;
content_type 'application/json';
to_json [ sort @found ];
};
ajax '/ajax/data/device/:ip/snmpnodesearch' => require_login sub {
my $device = try { schema('netdisco')->resultset('Device')
->find( param('ip') ) }
or send_error('Bad Device', 404);
my $to_match = param('str');
my $partial = param('partial');
my $excludeself = param('excludeself');
return to_json [] unless $to_match or length($to_match);
$to_match = $to_match . '%' if $partial;
my $found = undef;
my $op = ($partial ? '-ilike' : '=');
$found = schema('netdisco')->resultset('DeviceBrowser')
->search({ -or => [ oid => { $op => $to_match }, leaf => { $op => $to_match } ], ip => $device->ip },
{ rows => 1, order_by => 'oid_parts' })->first;
return to_json [] unless $found;
$found = $found->oid;
$found =~ s/^\.1\.3\.6\.1\.?//;
my @results = ('.1.3.6.1');
foreach my $part (split m/\./, $found) {
my $last = $results[-1];
push @results, "${last}.${part}";
}
content_type 'application/json';
to_json \@results;
};
ajax '/ajax/content/device/:ip/snmpnode/:oid' => require_login sub {
my $device = try { schema('netdisco')->resultset('Device')
->find( param('ip') ) }
or send_error('Bad Device', 404);
my $oid = param('oid');
$oid =~ m/^\.1\.3\.6\.1(\.\d+)*$/ or send_error('Bad OID', 404);
my $object = schema('netdisco')->resultset('DeviceBrowser')
->with_snmp_object($device->ip)->find({ 'snmp_object.oid' => $oid })
or send_error('Bad OID', 404);
my $munge = (param('munge') and exists $ALL_MUNGERS{param('munge')})
? param('munge') : $object->munge;
my %data = (
$object->get_columns,
snmp_object => { $object->snmp_object->get_columns },
value => decode_and_munge( $munge, $object->value ),
);
template 'ajax/device/snmpnode.tt',
{ node => \%data, munge => $munge, mungers => [sort keys %ALL_MUNGERS] },
{ layout => 'noop' };
};
sub _get_snmp_data {
my ($ip, $base, $recurse) = @_;
my @parts = grep {length} split m/\./, $base;
++$recurse;
my %kids = map { ($base .'.'. $_->{part}) => $_ }
schema('netdisco')->resultset('Virtual::OidChildren')
->search({}, { bind => [
(scalar @parts + 1),
(scalar @parts + 2),
$base,
(scalar @parts + 1),
(scalar @parts + 1),
$ip,
$base,
] })->hri->all;
return [{
text => 'No SNMP data for this device.',
children => \0,
state => { disabled => \1 },
icon => 'icon-search',
}] unless scalar keys %kids;
my %meta = map { ('.'. join '.', @{$_->{oid_parts}}) => $_ }
schema('netdisco')->resultset('Virtual::FilteredSNMPObject')
->search({}, { bind => [
$base,
(scalar @parts + 1),
[[ map {$_->{part}} values %kids ]],
(scalar @parts + 1),
] })->hri->all;
my @items = map {{
id => $_,
text => ($meta{$_}->{leaf} .' ('. $kids{$_}->{part} .')'),
# for nodes with only one child, recurse to prefetch...
children => ( ($recurse < 2 and $kids{$_}->{children} == 1)
? _get_snmp_data($ip, ("${base}.". $kids{$_}->{part}), $recurse)
: ($kids{$_}->{children} ? \1 : \0)),
# and set the display to open to show the single child
state => { opened => ( ($recurse < 2 and $kids{$_}->{children} == 1)
? \1
: \0 ) },
($kids{$_}->{children} ? () : (icon => 'icon-leaf')),
(scalar @{$meta{$_}->{index}} ? (icon => 'icon-th') : ()),
}} sort {$kids{$a}->{part} <=> $kids{$b}->{part}} keys %kids;
return \@items;
}
true;