#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

@@ -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,
};
}