line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package SNMP::Simple; |
2
|
1
|
|
|
1
|
|
5933
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
33
|
|
3
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
32
|
|
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
5
|
use Carp; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
88
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 NAME |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
SNMP::Simple - shortcuts for when using SNMP |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
=cut |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our $VERSION = 0.02; |
14
|
|
|
|
|
|
|
|
15
|
1
|
|
|
1
|
|
1563
|
use SNMP; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
$SNMP::use_enums = 1; # can be overridden with new(UseEnums=>0) |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
=head1 SYNOPSIS |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
use SNMP::Simple; |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
$name = $s->get('sysName'); # same as sysName.0 |
24
|
|
|
|
|
|
|
$location = $s->get('sysLocation'); |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
@array = $s->get_list('hrPrinterStatus'); |
27
|
|
|
|
|
|
|
$arrayref = $s->get_list('hrPrinterStatus'); |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
@list_of_lists = $s->get_table( |
30
|
|
|
|
|
|
|
qw( |
31
|
|
|
|
|
|
|
prtConsoleOnTime |
32
|
|
|
|
|
|
|
prtConsoleColor |
33
|
|
|
|
|
|
|
prtConsoleDescription |
34
|
|
|
|
|
|
|
) |
35
|
|
|
|
|
|
|
); |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
@list_of_hashes = $s->get_named_table( |
38
|
|
|
|
|
|
|
name => 'prtInputDescription', |
39
|
|
|
|
|
|
|
media => 'prtInputMediaName', |
40
|
|
|
|
|
|
|
status => 'prtInputStatus', |
41
|
|
|
|
|
|
|
level => 'prtInputCurrentLevel', |
42
|
|
|
|
|
|
|
max => 'prtInputMaxCapacity', |
43
|
|
|
|
|
|
|
); |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=head1 DESCRIPTION |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
This module provides shortcuts when performing repetitive information-retrieval |
48
|
|
|
|
|
|
|
tasks with L. |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
Instead of this: |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
use SNMP; |
53
|
|
|
|
|
|
|
$vars = new SNMP::VarList( ['prtConsoleOnTime'], ['prtConsoleColor'], |
54
|
|
|
|
|
|
|
['prtConsoleDescription'], ); |
55
|
|
|
|
|
|
|
my ( $light_status, $light_color, $light_desc ) = $s->getnext($vars); |
56
|
|
|
|
|
|
|
die $s->{ErrorStr} if $s->{ErrorStr}; |
57
|
|
|
|
|
|
|
while ( !$s->{ErrorStr} and $$vars[0]->tag eq "prtConsoleOnTime" ) { |
58
|
|
|
|
|
|
|
push @{ $data{lights} }, |
59
|
|
|
|
|
|
|
{ |
60
|
|
|
|
|
|
|
status => ( $light_status ? 0 : 1 ), |
61
|
|
|
|
|
|
|
color => SNMP::mapEnum( $$vars[1]->tag, $light_color ), |
62
|
|
|
|
|
|
|
description => $light_desc, |
63
|
|
|
|
|
|
|
}; |
64
|
|
|
|
|
|
|
( $light_status, $light_color, $light_desc ) = $s->getnext($vars); |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
...you can do this: |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
use SNMP::Simple; |
70
|
|
|
|
|
|
|
$data{lights} = $s->get_named_table( |
71
|
|
|
|
|
|
|
status => 'prtConsoleOnTime', |
72
|
|
|
|
|
|
|
color => 'prtConsoleColor', |
73
|
|
|
|
|
|
|
name => 'prtConsoleDescription', |
74
|
|
|
|
|
|
|
); |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=head2 SNMP Beginners, read me first! |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
Please, please, B do not use this module as a starting point for |
79
|
|
|
|
|
|
|
working with SNMP and Perl. Look elsewhere for starting resources: |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=over 4 |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=item * The L module |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=item * The Net-SNMP web site (L) and tutorial (L) |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=item * Appendix E of Perl for System Administration (L) by David N. Blank-Edelman |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=back |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=head2 SNMP Advanced and Intermediate users, read me first! |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
I'll admit this is a complete slaughtering of SNMP, but my goals were precise. |
94
|
|
|
|
|
|
|
If you think SNMP::Simple could be refined in any way, feel free to send me |
95
|
|
|
|
|
|
|
suggestions/fixes/patches. |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
=cut |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
=head1 METHODS |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=head2 new( @args ) |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
Creates a new SNMP::Simple object. Arguments given are passed directly to |
104
|
|
|
|
|
|
|
Cnew>. See L for details. |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
Example: |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
use SNMP::Simple |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
my $s = SNMP::Simple->new( |
111
|
|
|
|
|
|
|
DestHost => 'host.example.com', |
112
|
|
|
|
|
|
|
Community => 'public', |
113
|
|
|
|
|
|
|
Version => 1, |
114
|
|
|
|
|
|
|
) or die "couldn't create session"; |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
... |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=cut |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
sub new { |
121
|
|
|
|
|
|
|
my ( $class, @args ) = @_; |
122
|
|
|
|
|
|
|
my $session = SNMP::Session->new(@args) |
123
|
|
|
|
|
|
|
or croak "Couldn't create session"; |
124
|
|
|
|
|
|
|
bless \$session, $class; |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=head2 get( $oid ) |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
Gets the named variable and returns its value. If no value is returned, |
130
|
|
|
|
|
|
|
C will try to retrieve a list named C<$name> and return its first vlaue. |
131
|
|
|
|
|
|
|
Thus, for convenience, |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
$s->get('sysDescr') |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
..should be the same as: |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
$s->get('sysDescr.0') |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
Numbered OIDs are fine, too, with or without a leading dot: |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
$s->get('1.3.6.1.2.1.1.1.0') |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
C is automatically used on the result. |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=cut |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
sub get { |
148
|
|
|
|
|
|
|
my ( $self, $name ) = @_; |
149
|
|
|
|
|
|
|
my $result = $$self->get($name) || ( $self->get_list($name) )[0]; |
150
|
|
|
|
|
|
|
my $enum = SNMP::mapEnum( $name, $result ); |
151
|
|
|
|
|
|
|
return defined $enum ? $enum : $result; |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=head2 get_list( $oid ) |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
Returns leaves of the given OID. |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
If called in array context, returns an array. If called in scalar context, |
159
|
|
|
|
|
|
|
returns an array reference. |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=cut |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
sub get_list { |
164
|
|
|
|
|
|
|
my ( $self, $oid ) = @_; |
165
|
|
|
|
|
|
|
my @table = $self->get_table($oid); |
166
|
|
|
|
|
|
|
my @output = map { $_->[0] } @table; |
167
|
|
|
|
|
|
|
return wantarray ? @output : \@output; |
168
|
|
|
|
|
|
|
} |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=head2 get_table( @oids ) |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
Given a list of OIDs, this will return a list of lists of all of the values of |
173
|
|
|
|
|
|
|
the table. |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
For example, to get a list of all known network interfaces on a machine and |
176
|
|
|
|
|
|
|
their status: |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
$s->get_table('ifDescr', 'ifOperStatus') |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
Would return something like the following: |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
[ 'lo', 'up' ], |
183
|
|
|
|
|
|
|
[ 'eth0', 'down' ], |
184
|
|
|
|
|
|
|
[ 'eth1', 'up' ], |
185
|
|
|
|
|
|
|
[ 'sit0', 'down' ] |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
If called in array context, returns an array (of arrays). If called in scalar |
188
|
|
|
|
|
|
|
context, returns an array reference. |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
=cut |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
sub get_table { |
193
|
|
|
|
|
|
|
my ( $self, @oids ) = @_; |
194
|
|
|
|
|
|
|
my @output = (); |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
# build our varlist, the fun VarList way |
197
|
|
|
|
|
|
|
my $vars = new SNMP::VarList( map { [$_] } @oids ); |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
# get our initial results, assume that we should be able to get at least |
200
|
|
|
|
|
|
|
# *one* row back |
201
|
|
|
|
|
|
|
my @results = $$self->getnext($vars); |
202
|
|
|
|
|
|
|
croak $$self->{ErrorStr} if $$self->{ErrorStr}; |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
# dNb's recipe for iteration: make sure that there's no error and that the |
205
|
|
|
|
|
|
|
# OID name of the first cell is actually what we want |
206
|
|
|
|
|
|
|
while ( !$$self->{ErrorStr} and $$vars[0]->tag eq $oids[0] ) { |
207
|
|
|
|
|
|
|
push @output, [@results]; |
208
|
|
|
|
|
|
|
@results = $$self->getnext($vars); |
209
|
|
|
|
|
|
|
} |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
return wantarray ? @output : \@output; |
212
|
|
|
|
|
|
|
} |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
=head2 get_named_table( %oids_by_alias ) |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
Like L<"get_table">, but lets you rename ugly OID names on the fly. To get |
217
|
|
|
|
|
|
|
a list of all known network interfaces on a machine and their status: |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
$s->get_table( name => 'ifDescr', status => 'ifOperStatus' ) |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
Would return something like the following: |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
{ |
224
|
|
|
|
|
|
|
status => 'up', |
225
|
|
|
|
|
|
|
name => 'lo' |
226
|
|
|
|
|
|
|
}, |
227
|
|
|
|
|
|
|
{ |
228
|
|
|
|
|
|
|
status => 'down', |
229
|
|
|
|
|
|
|
name => 'eth0' |
230
|
|
|
|
|
|
|
}, |
231
|
|
|
|
|
|
|
{ |
232
|
|
|
|
|
|
|
status => 'up', |
233
|
|
|
|
|
|
|
name => 'eth1' |
234
|
|
|
|
|
|
|
}, |
235
|
|
|
|
|
|
|
{ |
236
|
|
|
|
|
|
|
status => 'down', |
237
|
|
|
|
|
|
|
name => 'sit0' |
238
|
|
|
|
|
|
|
} |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
If called in array context, returns an array (of hashes). If called in scalar |
241
|
|
|
|
|
|
|
context, returns an array reference. |
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
=cut |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
sub get_named_table { |
246
|
|
|
|
|
|
|
my $self = shift; |
247
|
|
|
|
|
|
|
my %oid_to_name = reverse @_; |
248
|
|
|
|
|
|
|
my @oids = keys %oid_to_name; |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
# remap table so it's a list of hashes instead of a list of lists |
251
|
|
|
|
|
|
|
my @table = $self->get_table( keys %oid_to_name ); |
252
|
|
|
|
|
|
|
my @output; |
253
|
|
|
|
|
|
|
foreach my $row (@table) { |
254
|
|
|
|
|
|
|
my %data = (); |
255
|
|
|
|
|
|
|
for ( my $i = 0; $i < @oids; $i++ ) { |
256
|
|
|
|
|
|
|
$data{ $oid_to_name{ $oids[$i] } } = $row->[$i]; |
257
|
|
|
|
|
|
|
} |
258
|
|
|
|
|
|
|
push @output, \%data; |
259
|
|
|
|
|
|
|
} |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
return wantarray ? @output : \@output; |
262
|
|
|
|
|
|
|
} |
263
|
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
=head1 EXAMPLES |
265
|
|
|
|
|
|
|
|
266
|
|
|
|
|
|
|
A sample script F is included with this distribution. |
267
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
=head1 SEE ALSO |
269
|
|
|
|
|
|
|
|
270
|
|
|
|
|
|
|
L |
271
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
=head1 AUTHOR |
273
|
|
|
|
|
|
|
|
274
|
|
|
|
|
|
|
Ian Langworth, C<< >> |
275
|
|
|
|
|
|
|
|
276
|
|
|
|
|
|
|
=head1 BUGS |
277
|
|
|
|
|
|
|
|
278
|
|
|
|
|
|
|
=over 4 |
279
|
|
|
|
|
|
|
|
280
|
|
|
|
|
|
|
=item * There are no real tests. |
281
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
=item * I haven't tested this with v3. |
283
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
=back |
285
|
|
|
|
|
|
|
|
286
|
|
|
|
|
|
|
Please report any bugs or feature requests to |
287
|
|
|
|
|
|
|
C, or through the web interface at |
288
|
|
|
|
|
|
|
L. I will be notified, and then you'll automatically be |
289
|
|
|
|
|
|
|
notified of progress on your bug as I make changes. |
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
=head1 COPYRIGHT & LICENSE |
292
|
|
|
|
|
|
|
|
293
|
|
|
|
|
|
|
Copyright 2005 Ian Langworth, All Rights Reserved. |
294
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it under |
296
|
|
|
|
|
|
|
the same terms as Perl itself. |
297
|
|
|
|
|
|
|
|
298
|
|
|
|
|
|
|
=cut |
299
|
|
|
|
|
|
|
|
300
|
|
|
|
|
|
|
1; |