#912 use new all_oids format with additional status,enum,descr

This commit is contained in:
Oliver Gorwits
2022-08-07 22:53:40 +01:00
parent 83996046ae
commit 1e2d9f36bf
9 changed files with 80 additions and 20 deletions

View File

@@ -51,6 +51,7 @@ Module::Build->new(
'Guard' => '1.022',
'HTML::Parser' => '3.70',
'HTTP::Tiny' => '0.029',
'IO::Uncompress::Gunzip' => '0',
'IO::Socket::INET6' => '2.72',
'IO::Socket::SSL' => '2.048',
'JSON' => '2.90',

View File

@@ -11,7 +11,7 @@ __PACKAGE__->load_namespaces(
);
our # try to hide from kwalitee
$VERSION = 73; # schema version used for upgrades, keep as integer
$VERSION = 74; # schema version used for upgrades, keep as integer
use Path::Class;
use File::ShareDir 'dist_dir';

View File

@@ -23,6 +23,12 @@ __PACKAGE__->add_columns(
{ data_type => "text[]", is_nullable => 1, default_value => \"'{}'::text[]" },
"num_children",
{ data_type => "integer", is_nullable => 0, default_value => \'0' },
"status",
{ data_type => "text", is_nullable => 1 },
"enum",
{ data_type => "text[]", is_nullable => 1, default_value => \"'{}'::text[]" },
"descr",
{ data_type => "text", is_nullable => 1 },
);
__PACKAGE__->set_primary_key("oid");

View File

@@ -12,7 +12,7 @@ __PACKAGE__->table("filtered_snmp_object");
__PACKAGE__->result_source_instance->is_virtual(1);
__PACKAGE__->result_source_instance->view_definition(<<ENDSQL
SELECT so.oid, so.oid_parts, so.mib, so.leaf, so.type, so.access, so.index, so.num_children,
SELECT so.oid, so.oid_parts, so.mib, so.leaf, so.type, so.access, so.index, so.status, so.enum, so.descr, so.num_children,
count(db.oid) AS browser
FROM snmp_object so
@@ -25,7 +25,7 @@ __PACKAGE__->result_source_instance->view_definition(<<ENDSQL
WHERE array_length(so.oid_parts,1) = ?
AND so.oid LIKE ?::text || '.%'
GROUP BY so.oid, so.oid_parts, so.mib, so.leaf, so.type, so.access, so.index, so.num_children
GROUP BY so.oid, so.oid_parts, so.mib, so.leaf, so.type, so.access, so.index, so.status, so.enum, so.descr, so.num_children
ENDSQL
);
@@ -38,6 +38,9 @@ __PACKAGE__->add_columns(
'type' => { data_type => 'text' },
'access' => { data_type => 'text' },
'index' => { data_type => 'text[]' },
'status' => { data_type => 'text' },
'enum' => { data_type => 'text[]' },
'descr' => { data_type => 'text' },
'num_children' => { data_type => 'integer' },
'browser' => { data_type => 'integer' },
);

View File

@@ -40,6 +40,7 @@ ajax '/ajax/data/device/:ip/snmptree/:base' => require_login sub {
to_json $items;
};
# TODO add form option for limiting to this device, so leave :ip
ajax '/ajax/data/device/:ip/typeahead' => require_login sub {
my $device = try { schema('netdisco')->resultset('Device')
->find( param('ip') ) }
@@ -48,8 +49,8 @@ ajax '/ajax/data/device/:ip/typeahead' => require_login sub {
my $term = param('term') or return to_json [];
$term = '%'. $term .'%';
my @found = schema('netdisco')->resultset('DeviceBrowser')
->search({ leaf => { -ilike => $term }, ip => $device->ip },
my @found = schema('netdisco')->resultset('SNMPObject')
->search({ leaf => { -ilike => $term } },
{ rows => 25, columns => 'leaf' })
->get_column('leaf')->all;
return to_json [] unless scalar @found;
@@ -58,6 +59,7 @@ ajax '/ajax/data/device/:ip/typeahead' => require_login sub {
to_json [ sort @found ];
};
# TODO add form option for limiting to this device, so leave :ip
ajax '/ajax/data/device/:ip/snmpnodesearch' => require_login sub {
my $device = try { schema('netdisco')->resultset('Device')
->find( param('ip') ) }
@@ -72,8 +74,8 @@ ajax '/ajax/data/device/:ip/snmpnodesearch' => require_login sub {
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 },
$found = schema('netdisco')->resultset('SNMPObject')
->search({ -or => [ oid => { $op => $to_match }, leaf => { $op => $to_match } ] },
{ rows => 1, order_by => 'oid_parts' })->first;
return to_json [] unless $found;
@@ -140,7 +142,7 @@ sub _get_snmp_data {
(scalar @{$meta{$_}->{index}}
? (icon => 'icon-th'.($meta{$_}->{browser} ? ' text-info' : ' muted')) : ()),
(($meta{$_}->{num_children} == 0 and ($meta{$_}->{type} or $meta{$_}->{oid_parts}->[-1] == 0))
(($meta{$_}->{num_children} == 0 and ($meta{$_}->{access} =~ m/^(?:read|write)/ or $meta{$_}->{oid_parts}->[-1] == 0))
? (icon => 'icon-leaf'.($meta{$_}->{browser} ? ' text-info' : ' muted')) : ()),
# jstree will async call to expand these, and while it's possible

View File

@@ -8,6 +8,8 @@ use Dancer::Plugin::DBIC 'schema';
use File::Spec::Functions qw(catdir catfile);
use File::Slurper qw(read_lines write_text);
use IO::Uncompress::Gunzip qw(gunzip $GunzipError);
use File::Temp;
# use DDP;
register_worker({ phase => 'main' }, sub {
@@ -16,13 +18,17 @@ register_worker({ phase => 'main' }, sub {
debug "loadmibs - loading netdisco-mibs object cache";
my $home = (setting('mibhome') || catdir(($ENV{NETDISCO_HOME} || $ENV{HOME}), 'netdisco-mibs'));
my @report = read_lines(catfile($home, qw(EXTRAS reports all_oids)), 'latin-1');
my $infile = catfile($home, qw(EXTRAS reports all_oids));
my $outfh = File::Temp->new();
my $outfile = $outfh->filename;
gunzip $infile => $outfile or die "gunzip failed: $GunzipError\n";
my @report = read_lines($outfile, 'latin-1');
my @browser = ();
my %children = ();
foreach my $line (@report) {
my ($oid, $qual_leaf, $type, $access, $index) = split m/,/, $line;
my ($oid, $qual_leaf, $type, $access, $index, $status, $enum, $descr) = split m/,/, $line, 8;
next unless defined $oid and defined $qual_leaf;
my ($mib, $leaf) = split m/::/, $qual_leaf;
@@ -38,6 +44,9 @@ register_worker({ phase => 'main' }, sub {
type => $type,
access => $access,
index => [($index ? (split m/:/, $index) : ())],
status => $status,
enum => [($enum ? (split m/:/, $enum ) : ())],
descr => $descr,
};
}

View File

@@ -0,0 +1,7 @@
BEGIN;
ALTER TABLE snmp_object ADD COLUMN "status" text;
ALTER TABLE snmp_object ADD COLUMN "enum" text[] DEFAULT '{}' NOT NULL;
ALTER TABLE snmp_object ADD COLUMN "descr" text;
COMMIT;

View File

@@ -33,7 +33,7 @@
<td></td>
</tr>
<tr>
<th scope="row">Munge</th>
<th scope="row">Unpacker</th>
<td></td>
</tr>
<tr>
@@ -44,6 +44,18 @@
<th scope="row">Index</th>
<td></td>
</tr>
<tr>
<th scope="row">Status</th>
<td></td>
</tr>
<tr>
<th scope="row">Enum Syntax</th>
<td></td>
</tr>
<tr>
<th scope="row">Description</th>
<td></td>
</tr>
<tr>
<th scope="row">Value</th>
<td></td>

View File

@@ -2,28 +2,28 @@
<tbody>
<tr>
<th scope="row" class="span1">OID</th>
<td>[% node.snmp_object.oid %]</td>
<td>[% node.snmp_object.oid | html_entity %]</td>
</tr>
<tr>
<th scope="row">Module</th>
<td>[% node.snmp_object.mib %]</td>
<td>[% node.snmp_object.mib | html_entity %]</td>
</tr>
<tr>
<th scope="row">Leaf</th>
<td>[% node.snmp_object.leaf %]</td>
<td>[% node.snmp_object.leaf | html_entity %]</td>
</tr>
<tr>
<th scope="row">Type</th>
<td>[% node.snmp_object.type %]</td>
<td>[% node.snmp_object.type | html_entity %]</td>
</tr>
<tr>
<th scope="row">Munge</th>
<th scope="row">Unpacker</th>
<td>
[% IF node.value %]
<select name="munger" id="munger">
[% UNLESS munge %]<option value="" selected disabled hidden>None applied</option>[% END %]
[% FOREACH m IN mungers %]
<option [% 'selected' IF m == munge %] value="[% m %]">[% m %]</option>
<option [% 'selected' IF m == munge %] value="[% m | uri %]">[% m | html_entity %]</option>
[% END %]
</select>
[% END %]
@@ -31,7 +31,7 @@
</tr>
<tr>
<th scope="row">Access</th>
<td>[% node.snmp_object.access %]</td>
<td>[% node.snmp_object.access | html_entity %]</td>
</tr>
<tr>
<th scope="row">Index</th>
@@ -39,15 +39,35 @@
[% IF node.snmp_object.index.size > 0 %]
<table class="table table-condensed table-bordered">
[% FOREACH idx IN node.snmp_object.index %]
<tr><td>[% idx %]</td></tr>
<tr><td>[% idx | html_entity %]</td></tr>
[% END %]
</table>
[% END %]
</td>
</tr>
<tr>
<th scope="row">Status</th>
<td>[% node.snmp_object.status | html_entity %]</td>
</tr>
<tr>
<th scope="row">Enum Syntax</th>
<td>
[% IF node.snmp_object.enum.size > 0 %]
<table class="table table-condensed table-bordered">
[% FOREACH idx IN node.snmp_object.enum %]
<tr><td>[% idx | html_entity %]</td></tr>
[% END %]
</table>
[% END %]
</td>
</tr>
<tr>
<th scope="row">Description</th>
<td>[% node.snmp_object.descr | html_entity %]</td>
</tr>
<tr>
<th scope="row">Value</th>
<td>[% IF node.value %]<pre id="snmp_node_value">[% node.value %]</pre>[% END %]</td>
<td>[% IF node.value %]<pre id="snmp_node_value">[% node.value | html_entity %]</pre>[% END %]</td>
</tr>
</tbody>
</table>