« blib/lib/App/Netdisco/Worker/Plugin/Show.pm
↑ Summary
blib/lib/App/Netdisco/Worker/Plugin/Stats.pm »
| 1 | package App::Netdisco::Worker::Plugin::Snapshot; | ||
| 2 | |||
| 3 | use Dancer ':syntax'; | ||
| 4 | use Dancer::Plugin::DBIC; | ||
| 5 | |||
| 6 | use App::Netdisco::Worker::Plugin; | ||
| 7 | use aliased 'App::Netdisco::Worker::Status'; | ||
| 8 | |||
| 9 | use App::Netdisco::Util::Snapshot 'make_snmpwalk_browsable'; | ||
| 10 | use App::Netdisco::Transport::SNMP; | ||
| 11 | use MIME::Base64 'encode_base64'; | ||
| 12 | |||
| 13 | register_worker({ phase => 'check' }, sub { | ||
| 14 | my ($job, $workerconf) = @_; | ||
| 15 | my $device = $job->device; | ||
| 16 | |||
| 17 | return Status->error('Missing device (-d).') | ||
| 18 | unless defined $device; | ||
| 19 | |||
| 20 | return Status->error(sprintf 'Unknown device: %s', ($device || '')) | ||
| 21 | unless $device and $device->in_storage; | ||
| 22 | |||
| 23 | return Status->done('Bulkwalk is able to run'); | ||
| 24 | }); | ||
| 25 | |||
| 26 | register_worker({ phase => 'main', driver => 'snmp' }, sub { | ||
| 27 | my ($job, $workerconf) = @_; | ||
| 28 | my ($device, $extra) = map {$job->$_} qw/device extra/; | ||
| 29 | |||
| 30 | set(net_snmp_options => { | ||
| 31 | %{ setting('net_snmp_options') }, | ||
| 32 | 'UseLongNames' => 1, # Return full OID tags | ||
| 33 | 'UseSprintValue' => 0, | ||
| 34 | 'UseEnums' => 0, # Don't use enumerated vals | ||
| 35 | 'UseNumeric' => 1, # Return dotted decimal OID | ||
| 36 | }); | ||
| 37 | |||
| 38 | my $snmp = App::Netdisco::Transport::SNMP->reader_for($device); | ||
| 39 | my $sess = $snmp->session(); | ||
| 40 | my $from = SNMP::Varbind->new([ $extra || '.1' ]); | ||
| 41 | |||
| 42 | my $vars = []; | ||
| 43 | my $errornum = 0; | ||
| 44 | my %store = (); | ||
| 45 | |||
| 46 | debug sprintf 'bulkwalking %s from %s', $device->ip, ($extra || '.1'); | ||
| 47 | ($vars) = $sess->bulkwalk( 0, $snmp->{BulkRepeaters}, $from ); | ||
| 48 | |||
| 49 | if ( $sess->{ErrorNum} ) { | ||
| 50 | return Status->error( | ||
| 51 | sprintf 'snmp fatal error - %s', $sess->{ErrorStr}); | ||
| 52 | } | ||
| 53 | |||
| 54 | while (not $errornum) { | ||
| 55 | my $var = shift @$vars or last; | ||
| 56 | my $idx = $var->[0]; | ||
| 57 | $idx .= '.'. $var->[1] if $var->[1]; # ignore .0 | ||
| 58 | my $val = $var->[2]; | ||
| 59 | |||
| 60 | # Check if last element, V2 devices may report ENDOFMIBVIEW even if | ||
| 61 | # instance or object doesn't exist. | ||
| 62 | last if $val eq 'ENDOFMIBVIEW'; | ||
| 63 | |||
| 64 | if ($val eq 'NOSUCHOBJECT') { | ||
| 65 | return Status->error('snmp fatal error - NOSUCHOBJECT'); | ||
| 66 | } | ||
| 67 | if ( $val eq 'NOSUCHINSTANCE' ) { | ||
| 68 | return Status->error('snmp fatal error - NOSUCHINSTANCE'); | ||
| 69 | } | ||
| 70 | |||
| 71 | # Check to see if we've already seen this IID (looping) | ||
| 72 | if (defined $store{$idx} and $store{$idx}) { | ||
| 73 | return Status->error(sprintf 'snmp fatal error - looping at %s', $idx); | ||
| 74 | } | ||
| 75 | |||
| 76 | $store{$idx} = { | ||
| 77 | oid => $idx, | ||
| 78 | oid_parts => [], # intentional, is inflated via make_snmpwalk_browsable() | ||
| 79 | value => to_json([encode_base64($val, '')]), | ||
| 80 | }; | ||
| 81 | } | ||
| 82 | debug sprintf 'walked %d rows', scalar keys %store; | ||
| 83 | |||
| 84 | schema('netdisco')->txn_do(sub { | ||
| 85 | my $gone = $device->oids->delete; | ||
| 86 | debug sprintf 'removed %d old oids', $gone; | ||
| 87 | $device->oids->populate([values %store]); | ||
| 88 | }); | ||
| 89 | |||
| 90 | # loadmibs is only optional for getting snapshots | ||
| 91 | if (schema('netdisco')->resultset('SNMPObject')->count) { | ||
| 92 | debug 'you have run loadmibs. promoting oids to browser data...'; | ||
| 93 | make_snmpwalk_browsable($device); | ||
| 94 | } | ||
| 95 | |||
| 96 | return Status->done( | ||
| 97 | sprintf 'completed bulkwalk of %s entries from %s for %s', (scalar keys %store), ($extra || '.1'), $device->ip); | ||
| 98 | }); | ||
| 99 | |||
| 100 | true; |