diff --git a/bin/netdisco-do b/bin/netdisco-do index 90cf20c6..0d5c7ce5 100755 --- a/bin/netdisco-do +++ b/bin/netdisco-do @@ -138,7 +138,7 @@ unless ($action) { $extra = $class; undef $class; } - my $i = App::Netdisco::Core::Transport::SNMP->instance->reader_for($device, $class); + my $i = App::Netdisco::Core::Transport::SNMP->reader_for($device, $class); Data::Printer::p($i->$extra); return ('done', sprintf "Showed %s response from %s.", $extra, $device->ip); } diff --git a/lib/App/Netdisco/Backend/Worker/Interactive/DeviceActions.pm b/lib/App/Netdisco/Backend/Worker/Interactive/DeviceActions.pm index 9560099c..99d54f67 100644 --- a/lib/App/Netdisco/Backend/Worker/Interactive/DeviceActions.pm +++ b/lib/App/Netdisco/Backend/Worker/Interactive/DeviceActions.pm @@ -22,7 +22,7 @@ sub _set_device_generic { $data ||= ''; # snmp connect using rw community - my $info = App::Netdisco::Core::Transport::SNMP->instance->writer_for($ip) + my $info = App::Netdisco::Core::Transport::SNMP->writer_for($ip) or return job_defer("Failed to connect to device [$ip] to update $slot"); my $method = 'set_'. $slot; diff --git a/lib/App/Netdisco/Backend/Worker/Interactive/PortActions.pm b/lib/App/Netdisco/Backend/Worker/Interactive/PortActions.pm index 0052bc7f..85a13b0c 100644 --- a/lib/App/Netdisco/Backend/Worker/Interactive/PortActions.pm +++ b/lib/App/Netdisco/Backend/Worker/Interactive/PortActions.pm @@ -74,7 +74,7 @@ sub _set_port_generic { if ($device->vendor ne 'netdisco') { # snmp connect using rw community - my $info = App::Netdisco::Core::Transport::SNMP->instance->writer_for($ip) + my $info = App::Netdisco::Core::Transport::SNMP->writer_for($ip) or return job_defer("Failed to connect to device [$ip] to control port"); my $iid = get_iid($info, $port) @@ -127,7 +127,7 @@ sub power { $data = 'false' if $data =~ m/^(off|no|down)$/; # snmp connect using rw community - my $info = App::Netdisco::Core::Transport::SNMP->instance->writer_for($ip) + my $info = App::Netdisco::Core::Transport::SNMP->writer_for($ip) or return job_defer("Failed to connect to device [$ip] to control power"); my $powerid = get_powerid($info, $port) diff --git a/lib/App/Netdisco/Backend/Worker/Poller/Common.pm b/lib/App/Netdisco/Backend/Worker/Poller/Common.pm index 77bce4e9..7440edce 100644 --- a/lib/App/Netdisco/Backend/Worker/Poller/Common.pm +++ b/lib/App/Netdisco/Backend/Worker/Poller/Common.pm @@ -63,7 +63,7 @@ sub _single_body { return job_defer("$job_type deferred: $host is not ${job_type}able"); } - my $snmp = App::Netdisco::Core::Transport::SNMP->instance->reader_for($device); + my $snmp = App::Netdisco::Core::Transport::SNMP->reader_for($device); if (!defined $snmp) { return job_defer("$job_type failed: could not SNMP connect to $host"); } diff --git a/lib/App/Netdisco/Backend/Worker/Poller/Device.pm b/lib/App/Netdisco/Backend/Worker/Poller/Device.pm index a8a8b022..964056c8 100644 --- a/lib/App/Netdisco/Backend/Worker/Poller/Device.pm +++ b/lib/App/Netdisco/Backend/Worker/Poller/Device.pm @@ -59,7 +59,7 @@ sub discover { return job_defer("discover deferred: $host is not discoverable"); } - my $snmp = App::Netdisco::Core::Transport::SNMP->instance->reader_for($device); + my $snmp = App::Netdisco::Core::Transport::SNMP->reader_for($device); if (!defined $snmp) { return job_defer("discover failed: could not SNMP connect to $host"); } diff --git a/lib/App/Netdisco/Core/Transport/SNMP.pm b/lib/App/Netdisco/Core/Transport/SNMP.pm index 03d38c72..a59aa168 100644 --- a/lib/App/Netdisco/Core/Transport/SNMP.pm +++ b/lib/App/Netdisco/Core/Transport/SNMP.pm @@ -19,9 +19,9 @@ App::Netdisco::Core::Transport::SNMP =head1 DESCRIPTION Singleton for SNMP connections. Returns cached L instance for a -given device IP, or else undef. Prefix calls to this class with: +given device IP, or else undef. All methods are class methods, for example: - App::Netdisco::Core::Transport::SNMP->instance() + App::Netdisco::Core::Transport::SNMP->reader_for( ... ); =cut @@ -41,8 +41,8 @@ connected to that device. The IP can be any on the device, and the management interface will be connected to. If the device is known to Netdisco and there is a cached SNMP community -string, this will be tried first, and then other community string(s) from the -application configuration will be tried. +string, that community will be tried first, and then other community strings +from the application configuration will be tried. If C<$useclass> is provided, it will be used as the L device class instead of the class in the Netdisco database. @@ -52,18 +52,18 @@ Returns C if the connection fails. =cut sub reader_for { - my ($self, $ip, $useclass) = @_; + my ($class, $ip, $useclass) = @_; my $device = get_device($ip) or return undef; - return $self->readers->{$device->ip} - if exists $self->readers->{$device->ip}; + my $readers = $class->instance->readers or return undef; + return $readers->{$device->ip} if exists $readers->{$device->ip}; debug sprintf 'snmp reader cache warm: [%s]', $device->ip; - return ($self->readers->{$device->ip} + return ($readers->{$device->ip} = _snmp_connect_generic('read', $device, $useclass)); } -=head2 writer_for( $ip, $useclass? ) +=head1 writer_for( $ip, $useclass? ) -Same as C but uses the read-write community string(s) from the +Same as C but uses the read-write community strings from the application configuration file. Returns C if the connection fails. @@ -71,16 +71,15 @@ Returns C if the connection fails. =cut sub writer_for { - my ($self, $ip, $useclass) = @_; + my ($class, $ip, $useclass) = @_; my $device = get_device($ip) or return undef; - return $self->writers->{$device->ip} - if exists $self->writers->{$device->ip}; + my $writers = $class->instance->writers or return undef; + return $writers->{$device->ip} if exists $writers->{$device->ip}; debug sprintf 'snmp writer cache warm: [%s]', $device->ip; - return ($self->writers->{$device->ip} + return ($writers->{$device->ip} = _snmp_connect_generic('write', $device, $useclass)); } - sub _snmp_connect_generic { my ($mode, $device, $useclass) = @_; $mode ||= 'read';