Debian Codegraph @

perl/SNMP/examples/bulkwalk.pl

raw
1 use SNMP;
2
3 # Hard-coded hostname and community. This is icky, but I didn't want to
4 # muddle the example with parsing command line arguments. Deal with it. -r
5 #
6 my $hostname='localhost';
7 my $port='161';
8 my $community='public';
9
10 $SNMP::debugging = 0;
11 $SNMP::dump_packet = 0;
12
13 $sess = new SNMP::Session( 'DestHost' => $hostname,
14 'Community' => $community,
15 'RemotePort' => $port,
16 'Timeout' => 300000,
17 'Retries' => 3,
18 'Version' => '2c',
19 'UseLongNames' => 1, # Return full OID tags
20 'UseNumeric' => 1, # Return dotted decimal OID
21 'UseEnums' => 0, # Don't use enumerated vals
22 'UseSprintValue' => 0); # Don't pretty-print values
23
24 die "Cannot create session: ${SNMP::ErrorStr}\n" unless defined $sess;
25
26 # Set up a list of two non-repeaters and some repeated variables.
27 #
28 # IMPORTANT NOTE:
29 #
30 # The 'get' performed for non-repeaters is a "GETNEXT" (the non-repeater
31 # requests are not fulfilled with SNMP GET's). This means that you must
32 # ask for the lexicographically preceding variable for non-repeaters.
33 #
34 # For most branches (i.e. 'sysUpTime'), this "just works" -- be sure you
35 # don't ask for an instance, and the response will be as expected. However,
36 # if you want a specific variable instance (i.e. 'ifSpeed.5'), you must
37 # ask for the _preceding_ variable ('ifSpeed.4' in this example).
38 #
39 # See section 4.2.3 of RFC 1905 for more details on GETBULK PDU handling.
40 #
41
42 my $vars = new SNMP::VarList( ['sysUpTime'], # Nonrepeater variable
43 ['ifNumber'], # Nonrepeater variable
44 ['ifSpeed'], # Repeated variable
45 ['ifDescr'] ); # Repeated variable.
46
47 # Do the bulkwalk of the two non-repeaters, and the repeaters. Ask for no
48 # more than 8 values per response packet. If the caller already knows how
49 # many instances will be returned for the repeaters, it can ask only for
50 # that many repeaters.
51 #
52 @resp = $sess->bulkwalk(2, 8, $vars);
53 die "Cannot do bulkwalk: $sess->{ErrorStr} ($sess->{ErrorNum})\n"
54 if $sess->{ErrorNum};
55
56 # Print out the returned response for each variable.
57 for $vbarr ( @resp ) {
58 # Determine which OID this request queried. This is kept in the VarList
59 # reference passed to bulkwalk().
60 $oid = $$vars[$i++]->tag();
61
62 # Count the number of responses to this query. The count will be 1 for
63 # non-repeaters, 1 or more for repeaters.
64 $num = scalar @$vbarr;
65 print "$num responses for oid $oid: \n";
66
67 # Display the returned list of varbinds using the SNMP::Varbind methods.
68 for $v (@$vbarr) {
69 printf("\t%s = %s (%s)\n", $v->name, $v->val, $v->type);
70 }
71 print "\n";
72 }
73
74 #
75 # Now do the same bulkwalk again, but in asynchronous mode. Set up a Perl
76 # callback to receive the reference to the array of arrays of Varbind's for
77 # the return value, and pass along the $vars VarList to it. This allows us
78 # to print the oid tags (the callback code is almost the same as above).
79 #
80 # First, define the Perl callback to be called when the bulkwalk completes.
81 # The call to SNMP::finish() will cause the SNMP::MainLoop() to return once
82 # the callback has completed, so that processing can continue.
83 #
84 sub callback {
85 my ($vars, $values) = @_;
86
87 for $vbarr ( @$values ) {
88 # Determine which OID this request queried. This is kept in the
89 # '$vars' VarList reference passed to the Perl callback by the
90 # asynchronous callback.
91 $oid = (shift @$vars)->tag();
92
93 # Count the number of responses to this query. The count will be 1 for
94 # non-repeaters, 1 or more for repeaters.
95 $num = scalar @$vbarr;
96 print "$num responses for oid $oid: \n";
97
98 # Display the returned list of varbinds using the SNMP::Varbind methods.
99 for $v (@$vbarr) {
100 printf("\t%s = %s (%s)\n", $v->name, $v->val, $v->type);
101 }
102 print "\n";
103 }
104
105 SNMP::finish();
106 }
107
108 # The actual bulkwalk request is done here. Note that the $vars VarList
109 # reference will be passed to the Perl callback when the bulkwalk completes.
110 #
111 my $reqid = $sess->bulkwalk(2, 8, $vars, [ \&callback, $vars ]);
112 die "Cannot do async bulkwalk: $sess->{ErrorStr} ($sess->{ErrorNum})\n"
113 if $sess->{ErrorNum};
114
115 # Now drop into the SNMP event loop and await completion of the bulkwalk.
116 # The call to SNMP::finish() in &callback will make the SNMP::MainLoop()
117 # return to the caller.
118 #
119 SNMP::MainLoop();
120
121 exit 0;