blib/lib/App/Netdisco/Worker/Plugin/LoadMIBsFromOIDs.pm untested

filter: Statementstmt 0.0% 0 / 109 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 10 Podpod n/a totaltotal 0.0% 0 / 174 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package App::Netdisco::Worker::Plugin::LoadMIBsFromOIDs;
2  
3 use Dancer ':syntax';
4 use App::Netdisco::Worker::Plugin;
5 use aliased 'App::Netdisco::Worker::Status';
6  
7 use Dancer::Plugin::DBIC 'schema';
8  
9 use Storable 'thaw';
10 use MIME::Base64 qw/encode_base64 decode_base64/;
11 use File::Spec::Functions qw(splitdir catfile catdir);
12 use File::Slurper qw(read_lines write_text);
13  
14 use App::Netdisco::Util::Device 'get_device';
15 use App::Netdisco::Util::Snapshot 'make_snmpwalk_browsable';
16 # use DDP;
17  
18 register_worker({ phase => 'main' }, sub {
19 my ($job, $workerconf) = @_;
20  
21 my $vendor = $job->extra;
22 return Status->error(sprintf
23 'loadmibsfromoids: "%s" is not a report name; pass the part of an'
24 . ' EXTRAS/reports file name before "_oids", such as juniper', $vendor)
25 if defined $vendor and length $vendor and $vendor !~ m/\A[\w.-]+\z/;
26 debug sprintf 'loadmibsfromoids - loading netdisco-mibs object cache%s',
27 ($vendor ? (sprintf ' for vendor "%s"', $vendor) : '');
28  
29 my $home = (setting('mibhome') || catdir(($ENV{NETDISCO_HOME} || $ENV{HOME}), 'netdisco-mibs'));
30 my $reports = catdir( $home, 'EXTRAS', 'reports' );
31 my @report = ();
32  
33 # always load these, whether loading one or all vendors
34 push @report, read_lines( catfile( $reports, $_ ), 'latin-1' )
35 for qw(rfc_oids net-snmp_oids cisco_oids);
36  
37 if ($vendor) {
38 push @report, read_lines( catfile( $reports, "${vendor}_oids" ), 'latin-1' );
39 }
40 else {
41 my %seen_report = (map {($_ => 1)} qw(rfc_oids net-snmp_oids cisco_oids));
42 my @maps = grep { ! m/\./ }
43 map { (splitdir($_))[-1] }
44 grep { -f }
45 glob (catfile( $reports, '*_oids' ));
46 my @to_read = grep { not $seen_report{$_}++ } @maps;
47 push @report, read_lines( catfile( $reports, $_ ), 'latin-1' )
48 for @to_read;
49 }
50  
51 my @browser = ();
52 my %children = ();
53 my %seenoid = ();
54  
55 foreach my $line (@report) {
56 my ($oid, $qual_leaf, $type, $access, $index, $status, $enum, $descr) = split m/,/, $line, 8;
57 next unless defined $oid and defined $qual_leaf;
58 next if ++$seenoid{$oid} > 1;
59  
60 my ($mib, $leaf) = split m/::/, $qual_leaf;
61 my @oid_parts = grep {length} (split m/\./, $oid);
62 ++$children{ join '.', '', @oid_parts[0 .. (@oid_parts - 2)] }
63 if scalar @oid_parts > 1;
64  
65 push @browser, {
66 oid => $oid,
67 oid_parts => [ @oid_parts ],
68 mib => $mib,
69 leaf => $leaf,
70 type => $type,
71 access => $access,
72 index => [($index ? (split m/:/, $index) : ())],
73 status => $status,
74 enum => [($enum ? (split m/:/, $enum ) : ())],
75 descr => $descr,
76 };
77 }
78  
79 foreach my $row (@browser) {
80 $row->{num_children} = $children{ $row->{oid} } || 0;
81 }
82  
83 debug sprintf "loadmibsfromoids - loaded %d objects from netdisco-mibs",
84 scalar @browser;
85  
86 if (not scalar @browser) {
87 my $refusal = sprintf
88 'loadmibsfromoids - refusing to empty snmp_object: read %d lines from %s but '
89 . 'parsed 0 objects', scalar @report, $reports;
90  
91 # error() is what survives --quiet on the console. add_status() records the
92 # refusal now so the blocks below still run and cannot displace it.
93 error $refusal;
94 $job->add_status( Status->error($refusal) );
95 }
96 else {
97 schema('netdisco')->txn_do(sub {
98 my $gone = schema('netdisco')->resultset('SNMPObject')->delete;
99 debug sprintf 'loadmibsfromoids - removed %d oids', $gone;
100 schema('netdisco')->resultset('SNMPObject')->populate(\@browser);
101 debug sprintf 'loadmibsfromoids - added %d new oids', scalar @browser;
102 });
103 }
104  
105 # promote snapshots prior to loadmibs to be browsable
106 schema('netdisco')->txn_do(sub {
107 my @devices = schema('netdisco')
108 ->resultset('DeviceBrowser')
109 ->search({ -bool => \q{ array_length(oid_parts, 1) IS NULL } })
110 ->distinct('ip')->get_column('ip')->all;
111  
112 foreach my $ip (@devices) {
113 my $dev = get_device($ip);
114 next unless $dev->in_storage;
115 debug sprintf 'loadmibsfromoids - promoting snapshot for %s to be browsable', $dev->ip;
116 make_snmpwalk_browsable($dev);
117 }
118 });
119  
120 # legacy snapshot upgrade
121 schema('netdisco')->txn_do(sub {
122 my $legacy_rs = schema('netdisco')
123 ->resultset('DeviceBrowser')
124 ->search({ -bool => \q{ jsonb_typeof(value) != 'array' } });
125  
126 if ($legacy_rs->count) {
127 my @rows = $legacy_rs->hri->all;
128 my $gone = $legacy_rs->delete;
129
130 # the legacy looks like encode_base64( nfreeze( [$data] ) )
131 foreach my $row (@rows) {
132 my $value = (@{ thaw( decode_base64( from_json($row->{value}) ) ) })[0];
133 $value = (ref {} eq ref $value)
134 ? { map {($_ => (defined $value->{$_} ? encode_base64($value->{$_}, '') : undef))}
135 keys %$value }
136 : (defined $value ? encode_base64($value, '') : undef);
137 $row->{value} = to_json([$value]);
138 }
139  
140 schema('netdisco')->resultset('DeviceBrowser')->populate(\@rows);
141 debug sprintf 'loadmibsfromoids - updated %d legacy snapshot rows', scalar @rows;
142 }
143 });
144  
145 return Status->done('Loaded MIBs');
146 });
147  
148 true;
149  
150 __DATA__