-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathLive.pm
259 lines (182 loc) · 5.9 KB
/
Live.pm
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package GLPI::Agent::SNMP::Live;
use strict;
use warnings;
use parent 'GLPI::Agent::SNMP';
use English qw(-no_match_vars);
use Net::SNMP;
use Net::SNMP qw/SNMP_PORT/;
sub new {
my ($class, %params) = @_;
die "no hostname parameters\n" 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} parameter\n" unless $version;
my $self;
# shared options
my %options = (
-retries => 0,
-version => $version,
-hostname => $params{hostname},
-port => $params{port} || SNMP_PORT,
-domain => $params{domain} || 'udp/ipv4',
);
$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);
if (!$self->{session}) {
die "no response from host $params{hostname}\n"
if $error =~ /^No response from remote host/;
die "authentication error on host $params{hostname}\n"
if $error =~ /^Received usmStats(WrongDigests|UnknownUserNames)/;
die "Crypt::Rijndael perl module needs to be installed\n"
if $error =~ /Required module Crypt\/Rijndael\.pm not found/;
die $error . "\n";
}
if ($version ne 'snmpv3') {
my $oid = '.1.3.6.1.2.1.1.1.0';
my $response = $self->{session}->get_request(
-varbindlist => [$oid]
);
die "no response from host $params{hostname}\n"
if !$response;
die "missing response from host $params{hostname}\n"
unless defined $response->{$oid};
die "no response from host $params{hostname}\n"
if $response->{$oid} =~ /No response from remote host/;
}
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."\n" 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') {
$self->{context} = "";
} else {
$self->{session} = $self->{oldsession};
delete $self->{oldsession};
}
}
sub get {
my ($self, $oid) = @_;
return unless $oid;
my $session = $self->{session};
my %options = (-varbindlist => [$oid]);
$options{'-contextname'} = $self->{context} if defined($self->{context});
my $response = $session->get_request(%options);
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 %options = (-baseoid => $oid);
$options{'-contextname'} = $self->{context} if defined($self->{context});
$options{'-maxrepetitions'} = 1 if $session->version() != 0;
my $response = $session->get_table(%options);
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;
}
sub peer_address {
my ($self) = @_;
# transport() API is not documented in Net::SNMP
my $transport = $self->{session}->transport()
or return;
return $transport->peer_address();
}
1;
__END__
=head1 NAME
GLPI::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 port
=item domain
Can be one of:
=over
=item 'udp/ipv4' (default)
=item 'udp/ipv6'
=item 'tcp/ipv4'
=item 'tcp/ipv6'
=back
=item community
=item username
=item authpassword
=item authprotocol
=item privpassword
=item privprotocol
=back