allow setting some speudo device info (#572)

* allow port name to be changed on pseudo devices, was once added in commit 1f020e07ee

* also allow this for location and contact
This commit is contained in:
nick n
2019-05-02 14:19:46 +02:00
committed by Oliver Gorwits
parent 3d5525ca77
commit 3ec136aea3
3 changed files with 50 additions and 41 deletions

View File

@@ -16,22 +16,25 @@ register_worker({ phase => 'main', driver => 'snmp' }, sub {
my ($job, $workerconf) = @_; my ($job, $workerconf) = @_;
my ($device, $data) = map {$job->$_} qw/device extra/; my ($device, $data) = map {$job->$_} qw/device extra/;
# snmp connect using rw community # update pseudo devices directly in database
my $snmp = App::Netdisco::Transport::SNMP->writer_for($device) unless ($device->is_pseudo()) {
or return Status->defer("failed to connect to $device to update contact"); # snmp connect using rw community
my $snmp = App::Netdisco::Transport::SNMP->writer_for($device)
or return Status->defer("failed to connect to $device to update contact");
my $rv = $snmp->set_contact($data); my $rv = $snmp->set_contact($data);
if (!defined $rv) { if (!defined $rv) {
return Status->error( return Status->error(
"failed to set contact on $device: ". ($snmp->error || '')); "failed to set contact on $device: ". ($snmp->error || ''));
} }
# confirm the set happened # confirm the set happened
$snmp->clear_cache; $snmp->clear_cache;
my $new_data = ($snmp->contact || ''); my $new_data = ($snmp->contact || '');
if ($new_data ne $data) { if ($new_data ne $data) {
return Status->error("verify of contact failed on $device: $new_data"); return Status->error("verify of contact failed on $device: $new_data");
}
} }
# update netdisco DB # update netdisco DB

View File

@@ -16,22 +16,25 @@ register_worker({ phase => 'main', driver => 'snmp' }, sub {
my ($job, $workerconf) = @_; my ($job, $workerconf) = @_;
my ($device, $data) = map {$job->$_} qw/device extra/; my ($device, $data) = map {$job->$_} qw/device extra/;
# snmp connect using rw community # update pseudo devices directly in database
my $snmp = App::Netdisco::Transport::SNMP->writer_for($device) unless ($device->is_pseudo()) {
or return Status->defer("failed to connect to $device to update location"); # snmp connect using rw community
my $snmp = App::Netdisco::Transport::SNMP->writer_for($device)
or return Status->defer("failed to connect to $device to update location");
my $rv = $snmp->set_location($data); my $rv = $snmp->set_location($data);
if (!defined $rv) { if (!defined $rv) {
return Status->error( return Status->error(
"failed to set location on $device: ". ($snmp->error || '')); "failed to set location on $device: ". ($snmp->error || ''));
} }
# confirm the set happened # confirm the set happened
$snmp->clear_cache; $snmp->clear_cache;
my $new_data = ($snmp->location || ''); my $new_data = ($snmp->location || '');
if ($new_data ne $data) { if ($new_data ne $data) {
return Status->error("verify of location failed on $device: $new_data"); return Status->error("verify of location failed on $device: $new_data");
}
} }
# update netdisco DB # update netdisco DB

View File

@@ -24,25 +24,28 @@ register_worker({ phase => 'main', driver => 'snmp' }, sub {
my ($job, $workerconf) = @_; my ($job, $workerconf) = @_;
my ($device, $pn, $data) = map {$job->$_} qw/device port extra/; my ($device, $pn, $data) = map {$job->$_} qw/device port extra/;
# snmp connect using rw community # update pseudo devices directly in database
my $snmp = App::Netdisco::Transport::SNMP->writer_for($device) unless ($device->is_pseudo()) {
or return Status->defer("failed to connect to $device to update alias"); # snmp connect using rw community
my $snmp = App::Netdisco::Transport::SNMP->writer_for($device)
or return Status->defer("failed to connect to $device to update alias");
my $iid = get_iid($snmp, vars->{'port'}) my $iid = get_iid($snmp, vars->{'port'})
or return Status->error("Failed to get port ID for [$pn] from $device"); or return Status->error("Failed to get port ID for [$pn] from $device");
my $rv = $snmp->set_i_alias($data, $iid); my $rv = $snmp->set_i_alias($data, $iid);
if (!defined $rv) { if (!defined $rv) {
return Status->error(sprintf 'Failed to set [%s] alias to [%s] on $device: %s', return Status->error(sprintf 'Failed to set [%s] alias to [%s] on $device: %s',
$pn, $data, ($snmp->error || '')); $pn, $data, ($snmp->error || ''));
} }
# confirm the set happened # confirm the set happened
$snmp->clear_cache; $snmp->clear_cache;
my $state = ($snmp->i_alias($iid) || ''); my $state = ($snmp->i_alias($iid) || '');
if (ref {} ne ref $state or $state->{$iid} ne $data) { if (ref {} ne ref $state or $state->{$iid} ne $data) {
return Status->error("Verify of [$pn] alias failed on $device"); return Status->error("Verify of [$pn] alias failed on $device");
}
} }
# update netdisco DB # update netdisco DB