1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
|
package FusionInventory::Agent::SNMP::Live;
use strict;
use warnings;
use base 'FusionInventory::Agent::SNMP';
use Encode qw(encode);
use English qw(-no_match_vars);
use Net::SNMP;
sub new {
my ($class, %params) = @_;
die "no hostname parameters" unless $params{hostname};
my $version =
! $params{version} ? 'snmpv1' :
$params{version} eq '1' ? 'snmpv1' :
$params{version} eq '2c' ? 'snmpv2c' :
$params{version} eq '3' ? 'snmpv3' :
undef ;
die "invalid SNMP version $params{version}" unless $version;
my $self;
# shared options
my %options = (
-retries => 0,
-version => $version,
-hostname => $params{hostname},
);
$options{'-timeout'} = $params{timeout} if $params{timeout};
# version-specific options
if ($version eq 'snmpv3') {
# only username is mandatory
$options{'-username'} = $params{username};
$options{'-authprotocol'} = $params{authprotocol}
if $params{authprotocol};
$options{'-authpassword'} = $params{authpassword}
if $params{authpassword};
$options{'-privprotocol'} = $params{privprotocol}
if $params{privprotocol};
$options{'-privpassword'} = $params{privpassword}
if $params{privpassword};
} else { # snmpv2c && snmpv1 #
$options{'-community'} = $params{community};
$self->{community} = $params{community};
}
($self->{session}, my $error) = Net::SNMP->session(%options);
die $error unless $self->{session};
bless $self, $class;
return $self;
}
sub switch_vlan_context {
my ($self, $vlan_id) = @_;
my $version_id = $self->{session}->version();
my $version =
$version_id == 0 ? 'snmpv1' :
$version_id == 1 ? 'snmpv2c' :
$version_id == 3 ? 'snmpv3' :
undef ;
my $error;
if ($version eq 'snmpv3') {
$self->{context} = 'vlan-' . $vlan_id;
} else {
# save original session
$self->{oldsession} = $self->{session} unless $self->{oldsession};
($self->{session}, $error) = Net::SNMP->session(
-timeout => $self->{session}->timeout(),
-retries => 0,
-version => $version,
-hostname => $self->{session}->hostname(),
-community => $self->{community} . '@' . $vlan_id
);
}
die $error unless $self->{session};
}
sub reset_original_context {
my ($self) = @_;
my $version_id = $self->{session}->version();
my $version =
$version_id == 0 ? 'snmpv1' :
$version_id == 1 ? 'snmpv2c' :
$version_id == 3 ? 'snmpv3' :
undef ;
if ($version eq 'snmpv3') {
delete $self->{context};
} else {
$self->{session} = $self->{oldsession};
delete $self->{oldsession};
}
}
sub get {
my ($self, $oid) = @_;
return unless $oid;
my $session = $self->{session};
my $response = $session->get_request(
-varbindlist => [$oid],
($self->{context} ? (-contextname => $self->{context}) : ())
);
return unless $response;
return if $response->{$oid} =~ /noSuchInstance/;
return if $response->{$oid} =~ /noSuchObject/;
return if $response->{$oid} =~ /No response from remote host/;
my $value = $response->{$oid};
return $value;
}
sub walk {
my ($self, $oid) = @_;
return unless $oid;
my $session = $self->{session};
my $response = $session->get_table(
-baseoid => $oid,
-maxrepetitions => 1,
($self->{context} ? (-contextname => $self->{context}) : ())
);
return unless $response;
my $values;
my $offset = length($oid) + 1;
foreach my $oid (keys %{$response}) {
my $value = $response->{$oid};
$values->{substr($oid, $offset)} = $value;
}
return $values;
}
1;
__END__
=head1 NAME
FusionInventory::Agent::SNMP::Live - Live SNMP client
=head1 DESCRIPTION
This is the object used by the agent to perform SNMP queries on live host.
=head1 METHODS
=head2 new(%params)
The constructor. The following parameters are allowed, as keys of the %params
hash:
=over
=item version (mandatory)
Can be one of:
=over
=item '1'
=item '2c'
=item '3'
=back
=item timeout
The transport layer timeout
=item hostname (mandatory)
=item community
=item username
=item authpassword
=item authprotocol
=item privpassword
=item privprotocol
=back
|