« blib/lib/App/Netdisco/Worker/Plugin/CheckSiteLocal.pm
↑ Summary
blib/lib/App/Netdisco/Worker/Plugin/Delete.pm »
| 1 | package App::Netdisco::Worker::Plugin::Contact; | ||
| 2 | |||
| 3 | use Dancer ':syntax'; | ||
| 4 | use App::Netdisco::Worker::Plugin; | ||
| 5 | use aliased 'App::Netdisco::Worker::Status'; | ||
| 6 | |||
| 7 | use App::Netdisco::Transport::SNMP; | ||
| 8 | |||
| 9 | register_worker({ phase => 'check' }, sub { | ||
| 10 | return Status->error('Missing device (-d).') | ||
| 11 | unless defined shift->device; | ||
| 12 | return Status->done('Contact is able to run'); | ||
| 13 | }); | ||
| 14 | |||
| 15 | register_worker({ phase => 'main', driver => 'snmp' }, sub { | ||
| 16 | my ($job, $workerconf) = @_; | ||
| 17 | my ($device, $data) = map {$job->$_} qw/device extra/; | ||
| 18 | |||
| 19 | # update pseudo devices directly in database | ||
| 20 | unless ($device->is_pseudo()) { | ||
| 21 | # snmp connect using rw community | ||
| 22 | my $snmp = App::Netdisco::Transport::SNMP->writer_for($device) | ||
| 23 | or return Status->defer("failed to connect to $device to update contact"); | ||
| 24 | |||
| 25 | my $rv = $snmp->set_contact($data); | ||
| 26 | |||
| 27 | if (!defined $rv) { | ||
| 28 | return Status->error( | ||
| 29 | "failed to set contact on $device: ". ($snmp->error || '')); | ||
| 30 | } | ||
| 31 | |||
| 32 | # confirm the set happened | ||
| 33 | $snmp->clear_cache; | ||
| 34 | my $new_data = ($snmp->contact || ''); | ||
| 35 | if ($new_data ne $data) { | ||
| 36 | return Status->error("verify of contact failed on $device: $new_data"); | ||
| 37 | } | ||
| 38 | } | ||
| 39 | |||
| 40 | # update netdisco DB | ||
| 41 | $device->update({contact => $data}); | ||
| 42 | |||
| 43 | return Status->done("Updated contact on $device to [$data]"); | ||
| 44 | }); | ||
| 45 | |||
| 46 | true; |