implement separate snmp_connect and snmp_connect_rw methods

This commit is contained in:
Oliver Gorwits
2013-04-04 10:37:46 +01:00
parent 62c8e19063
commit 6c5b6bbbee
3 changed files with 31 additions and 8 deletions

View File

@@ -22,7 +22,7 @@ sub _set_device_generic {
$data ||= ''; $data ||= '';
# snmp connect using rw community # snmp connect using rw community
my $info = snmp_connect($ip) my $info = snmp_connect_rw($ip)
or return error("Failed to connect to device [$ip] to update $slot"); or return error("Failed to connect to device [$ip] to update $slot");
my $method = 'set_'. $slot; my $method = 'set_'. $slot;

View File

@@ -56,7 +56,7 @@ sub _set_port_generic {
or return error("Unknown port name [$pn] on device [$ip]"); or return error("Unknown port name [$pn] on device [$ip]");
# snmp connect using rw community # snmp connect using rw community
my $info = snmp_connect($ip) my $info = snmp_connect_rw($ip)
or return error("Failed to connect to device [$ip] to control port"); or return error("Failed to connect to device [$ip] to control port");
my $iid = get_iid($info, $port) my $iid = get_iid($info, $port)
@@ -104,7 +104,7 @@ sub set_power {
(my $data = $job->subaction) =~ s/-\w+//; (my $data = $job->subaction) =~ s/-\w+//;
# snmp connect using rw community # snmp connect using rw community
my $info = snmp_connect($ip) my $info = snmp_connect_rw($ip)
or return error("Failed to connect to device [$ip] to control port"); or return error("Failed to connect to device [$ip] to control port");
my $powerid = get_powerid($info, $port) my $powerid = get_powerid($info, $port)

View File

@@ -10,7 +10,7 @@ use Path::Class 'dir';
use base 'Exporter'; use base 'Exporter';
our @EXPORT = (); our @EXPORT = ();
our @EXPORT_OK = qw/ our @EXPORT_OK = qw/
snmp_connect snmp_connect snmp_connect_rw
/; /;
our %EXPORT_TAGS = (all => \@EXPORT_OK); our %EXPORT_TAGS = (all => \@EXPORT_OK);
@@ -33,16 +33,39 @@ Given an IP address, returns an L<SNMP::Info> instance configured for and
connected to that device. The IP can be any on the device, and the management connected to that device. The IP can be any on the device, and the management
interface will be connected to. 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.
Returns C<undef> if the connection fails. Returns C<undef> if the connection fails.
=cut =cut
sub snmp_connect { sub snmp_connect { _snmp_connect_generic(@_, 'community') }
=head2 snmp_connect_rw( $ip )
Same as C<snmp_connect> but uses the read-write community string(s) from the
application configuration file.
Returns C<undef> if the connection fails.
=cut
sub snmp_connect_rw { _snmp_connect_generic(@_, 'community_rw') }
sub _snmp_connect_generic {
my $ip = shift; my $ip = shift;
# get device details from db # get device details from db
my $device = get_device($ip) my $device = get_device($ip);
or return ();
# get the community string(s)
my $comm_type = pop;
my @communities = @{ setting($comm_type) || []};
unshift @communities, $device->snmp_comm
if length $device->snmp_comm
and length $comm_type and $comm_type eq 'community';
# TODO: only supporing v2c at the moment # TODO: only supporing v2c at the moment
my %snmp_args = ( my %snmp_args = (
@@ -58,7 +81,7 @@ sub snmp_connect {
my $info = undef; my $info = undef;
my $last_comm = 0; my $last_comm = 0;
COMMUNITY: foreach my $c ($device->snmp_comm, @{ setting('community_rw') || []}) { COMMUNITY: foreach my $c (@communities) {
next unless defined $c and length $c; next unless defined $c and length $c;
try { try {
$info = SNMP::Info->new(%snmp_args, Community => $c); $info = SNMP::Info->new(%snmp_args, Community => $c);