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

filter: Statementstmt 0.0% 0 / 80 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 11 Podpod n/a totaltotal 0.0% 0 / 120 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::LoadMIBs;
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(catfile catdir);
12 use File::Slurper qw(read_lines write_text);
13 use Archive::Extract;
14  
15 use App::Netdisco::Util::Device 'get_device';
16 use App::Netdisco::Util::Snapshot 'make_snmpwalk_browsable';
17  
18 register_worker({ phase => 'main' }, sub {
19 my ($job, $workerconf) = @_;
20  
21 my $mibhome = (setting('mibhome') || catdir(($ENV{NETDISCO_HOME} || $ENV{HOME}), 'netdisco-mibs'));
22 my $objects = catfile( $mibhome, 'EXTRAS', 'sql', 'netdisco-mibs-snmp-object.sql.gz' );
23  
24 return Status->error(sprintf 'cannot find file: "%s"', $objects)
25 unless (-f $objects and -s $objects);
26  
27 my $ae = Archive::Extract->new( archive => $objects );
28 return Status->error(sprintf 'is not a gzip file: "%s"', $objects)
29 unless $ae->is_gz;
30  
31 my $obj_count_pre = schema(vars->{'tenant'})->resultset('SNMPObject')->count();
32 debug sprintf 'loadmibs - current object cache size: %s', $obj_count_pre;
33 debug 'loadmibs - loading netdisco-mibs object cache';
34 my $gzip = $ae->bin_gzip;
35 system(qq{$gzip -d -c '$objects' | psql -X -v ON_ERROR_STOP=0 -v ON_ERROR_ROLLBACK=on -q});
36 my $obj_count_post = schema(vars->{'tenant'})->resultset('SNMPObject')->count();
37 debug sprintf 'loadmibs - new object cache size: %s', $obj_count_post;
38  
39 debug 'loadmibs - checking for snapshots made prior to loadmibs to make browsable';
40 schema('netdisco')->txn_do(sub {
41 my @devices = schema('netdisco')
42 ->resultset('DeviceBrowser')
43 ->search({ -bool => \q{ array_length(oid_parts, 1) IS NULL } })
44 ->distinct('ip')->get_column('ip')->all;
45  
46 foreach my $ip (@devices) {
47 my $dev = get_device($ip);
48 next unless $dev->in_storage;
49 debug sprintf 'loadmibs - promoting snapshot for %s to be browsable', $dev->ip;
50 make_snmpwalk_browsable($dev);
51 }
52 });
53  
54 debug 'loadmibs - checking for legacy format snapshots to upgrade';
55 schema('netdisco')->txn_do(sub {
56 my $legacy_rs = schema('netdisco')
57 ->resultset('DeviceBrowser')
58 ->search({ -bool => \q{ jsonb_typeof(value) != 'array' } });
59  
60 if ($legacy_rs->count) {
61 my @rows = $legacy_rs->hri->all;
62 my $gone = $legacy_rs->delete;
63
64 # the legacy looks like encode_base64( nfreeze( [$data] ) )
65 foreach my $row (@rows) {
66 my $value = (@{ thaw( decode_base64( from_json($row->{value}) ) ) })[0];
67 $value = (ref {} eq ref $value)
68 ? { map {($_ => (defined $value->{$_} ? encode_base64($value->{$_}, '') : undef))}
69 keys %$value }
70 : (defined $value ? encode_base64($value, '') : undef);
71 $row->{value} = to_json([$value]);
72 }
73  
74 schema('netdisco')->resultset('DeviceBrowser')->populate(\@rows);
75 debug sprintf 'loadmibs - updated %d legacy snapshot rows', scalar @rows;
76 }
77 });
78  
79 return Status->done('Loaded MIBs');
80 });
81  
82 true;
83  
84 __DATA__