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