Book HomeEssential SNMPSearch this book

Chapter 8. Polling and Setting

Contents:

Retrieving a Single MIB Value
Retrieving Multiple MIB Values
Setting a MIB Value
Error Responses

We've put a lot of work into getting things set up so that we can use SNMP effectively. But now that we've installed a fancy node manager and configured agents on all our devices, what can we do? How can we interact with the devices that are out there?

The three basic SNMP operations are snmpget, snmpset, and snmpwalk. They are fairly self-explanatory: snmpget reads a value from a managed device, snmpset sets a value on a device, and snmpwalk reads a portion of the MIB tree from a device. For example, you can use snmpget to query a router and find out its administrative contact (i.e., the person to call if the router appears to be broken), snmpset to change this contact information, and snmpwalk to traverse a MIB to get an idea of which objects the router has implemented or to retrieve status information on all the router's interfaces.

This chapter shows you how to use these operations in day-to-day network management. First, we will use Perl to demonstrate how you can set,get, and walkobjects in a script (the nice thing about using Perl is that you can easily extend the simple scripts in this chapter to fit your needs and environment). We will then use HP OpenView and Net-SNMP to perform the same operations, but from the command line. Finally, as an alternative to the command line, we will demonstrate OpenView's graphical MIB Browser, which has a nice interface for getting, setting and walking MIB data.

8.1. Retrieving a Single MIB Value

Let's start by querying a router for the name of its administrative contact. This operation, called polling, is accomplished with the SNMP get command. The following Perl script, snmpget.pl, uses an SNMP Perl module to retrieve the information we want (Chapter 5, "Network-Management Software" contains the URL for this module):

#!/usr/local/bin/perl
#filename: /opt/local/perl_scripts/snmpget.pl
use BER;
use SNMP_util;
use SNMP_Session;
$MIB1 = ".1.3.6.1.2.1.1.4.0";
$HOST = "orarouter1";
($value) = &snmpget("public\@$HOST","$MIB1");
if ($value) { print "Results :$MIB1: :$value:\n"; }
else { warn "No response from host :$HOST:\n"; } 
This script is obviously very primitive, but it is also easy to understand, even if you're not an experienced Perl user. It's importance isn't in what it does, which is very little, but as a template you can use to insert SNMP operations into other programs. (If you are not used to writing quick Perl programs, or are unfamiliar with the language, a good starting point is the official Perl web site, http://www.perl.com.) The script starts with three use statements, which are similar to #include statements in C. The use statements load Perl modules containing functions and definitions for working with SNMP. The three modules we use are:

BER
Describes how to encode management data into bit patterns for transmission. Basic Encoding Rules (BER) is an ISO standard.

SNMP_util
Defines a set of functions that use the SNMP_Session module to make it much more programmer-friendly. SNMP_util itself uses BER and SNMP_Session, but in this first script we chose to reference these other modules explicitly. In future programs, we'll just use SNMP_util.

SNMP_Session
Provides Perl with core SNMP functionality.

The next two lines specify the data we want to get. We have hardcoded the object ID of a particular piece of data defined by the MIB and the hostname from which we want to retrieve this MIB data. In a more flexible program, you might want to get these values from the command line, or build a user interface to help users specify exactly what they are interested in retrieving. For the time being, however, this will get us started. It is easy enough to replace orarouter1 with the hostname or IP address of the device you want to poll. The OID we are requesting is stored in the variable $MIB1. The value .1.3.6.1.2.1.1.4.0 requests the device's administrative contact. Again, you can replace this with any OID of your choice. We used the numeric form of this object, but you can also use the textual form for the OID, which is .org.dod.internet.mgmt.mib-2.system.sysContact.0. You can abbreviate this further to sysContact because SNMP_util defines some parts of the OID string for us (for example, SNMP_util defines sysContact as 1.3.6.1.2.1.1.4.0), but it's often safer to be explicit and use the entire OID. Don't forget to include the .0, which states that we want the first (0) and only instance of iso.org.dod.internetmgmt.mib-2.system.sysContact.0, at the end of your OID.

The next line polls the device. The snmpget function retrieves the data from the device specified by the variable $HOST. Notice the two arguments to the function. The first is the device we want to poll, preceded by the community name public. (If you need to use another community name -- you did change the community names when you configured the device, didn't you? -- you'll have to modify this line and insert your community name in place of it.) The second argument to snmpget is the OID in which we are interested. If you type the code in yourself, do not forget the parentheses around $value. If you omit the parentheses, $value will be set to the number of items in the array snmpget returns.

Once we have polled the device, we print either the output or an error message. I put a colon before and after any output that I print; this makes it easy to see if there are any hidden characters in the output. The decimal integer "16" is very different from "16\n", which is the decimal integer 16 followed by a newline character.

Now let's run the program:

$ /opt/local/perl_scripts/snmpget.pl
Results :.1.3.6.1.2.1.1.4.0: :ORA IT Group:
snmpget.pl prints the OID we requested, followed by the actual value of that object, which is ORA IT Group. Don't worry if the return value for sysContact is wrong or blank. (The trick of putting colons before and after the output will make it clear if sysContact is blank or empty.) This probably means that no one has configured an administrative contact, or that it was configured incorrectly. We'll show you how to fix that when we discuss the set operation. If you get an error, skip to the end of this chapter to see a list of some errors and their appropriate fixes.

We will now modify snmpget.pl to poll any host and any OID we want. This is accomplished by passing the host and OID as command-line arguments to the Perl script:

#!/usr/local/bin/perl
#filename: /opt/local/perl_scripts/snmpget.pl
use SNMP_util;
$MIB1 = shift;
$HOST = shift;
($MIB1) && ($HOST) || die "Usage: $0 MIB_OID HOSTNAME";
($value) = &snmpget("$HOST","$MIB1");
if ($value) { print "Results :$MIB1: :$value:\n"; }
else { warn "No response from host :$HOST:\n"; }
Now that this program is a little more flexible, it is possible to look up different kinds of information on different hosts. We even left out the community string, which allows us to poll hosts with different community names. Here's how to run the new version of snmpget.pl:

$ /opt/local/perl_scripts/snmpget.pl .1.3.6.1.2.1.1.1.0 public@orarouter1
Results :.1.3.6.1.2.1.1.1.0: :Cisco Internetwork Operating System Software
IOS (tm) 3000 Software (IGS-I-L), Version 11.0(16), RELEASE SOFTWARE (fc1)
Copyright (c) 1986-1997 by cisco Systems, Inc.
Compiled Tue 24-Jun-97 12:20 by jaturner:
In this example, we asked the router to describe itself by looking up the OID .1.3.6.1.2.1.1.1.0 (system.sysDesc.0). The result tells us that orarouter1 is a Cisco router running Version 11.0(16) of the IOS operating system, along with some other useful information.

8.1.1. Using HP OpenView to Retrieve Values

Let's start by looking up our router's administrative contact (system.sysContact.0) and see if we get the same result as we did with our previous Perl script. The arguments to OpenView's snmpget [30] are the community name, the hostname of the device we want to poll, and the OID of the data we are requesting; we gave the OID in numeric form, but again, we could have given it as a text string:

[30]Most OpenView executable files are located in /opt/OV/bin.

$ /opt/OV/bin/snmpget -c public orarouter1 .1.3.6.1.2.1.1.4.0
system.sysContact.0 : DISPLAY STRING- (ascii):  ORA IT Group
Although this looks a little different from the output of the Perl script, it tells us the same thing. snmpget prints the OID we requested on the command line, making it easy to verify that we polled the right object. Again, note that the trailing .0 is important. The output also tells us the object's datatype: DISPLAY STRING- (ascii). Back in Chapter 2, "A Closer Look at SNMP", we discussed the datatypes that SNMP uses; some of the common types are INTEGER, OCTET STRING, Counter, and IpAddress. Finally, the output gives us the information we asked for: the router is administered by the ORA IT Group, which is the value returned from the SNMP get request.

Now let's do the same thing using OpenView's GUI interface. From the Network Node Manager's display, select "Misc Figure 8.1.1 SNMP MIB Browser."[31] If you don't have NNM running, you can start the MIB Browser from the command line: /opt/OV/bin/xnmbrowser. Figure 8-1 shows the GUI. Its input fields are similar to the variables we have been setting in our Perl scripts: Name or IP Address, Community Name, MIB Object ID, MIB Instance, SNMP Set Value, and MIB Values.

[31]If you find that the SNMP MIB Browser menu item is grayed out and cannot be clicked on, click on an SNMP object on your NNM map. You should then be able to click on the menu item to start your GUI.

Let's use this browser to run an snmpget. Start by inserting a Name or IP Address and Community Name in the input boxes provided. To enter the object you want to retrieve, use the MIB Object ID field and the text box below it. MIB Object ID shows us that we are currently in the subtree .iso.org.dod.internet. The text area shows the objects at the next level of the tree: directory, mgmt, etc. (To see the numeric OIDs for these objects, click on their names and then on the "Describe" button.) Then browse down through the MIB by double-clicking mgmt, then mib-2, system, and finally sysContact. Click on sysContact and then on "Start Query." The result that appears in the "MIB Values" field (as shown in Figure 8-2) should look very similar to the value that was returned in the command-line example.

Figure 8-1

Figure 8-1. OpenView xnmbrowser default

Figure 8-2

Figure 8-2. OpenView xnmbrowser response

Let's go back to the command line and poll for sysDesc again:

$ /opt/OV/bin/snmpget orarouter1 .1.3.6.1.2.1.1.1.0
system.sysDescr.0 : DISPLAY STRING- (ascii):  Cisco Internetwork Operating 
System Software IOS (tm) 3000 Software (IGS-I-L), Version 11.0(16), RELEASE 
SOFTWARE (fc1)Copyright (c) 1986-1997 by cisco Systems, Inc. Compiled Tue 
24-Jun-97 12:20 by jaturner
Looks the same, right? Notice that we left out the community string. We can do this because the default get community string is public, which is the correct community string for the target host, orarouter1. You can change your default community strings in OpenView's global settings. Let's see if we can get an object with a different datatype:

$ /opt/OV/bin/snmpget  orarouter1 .1.3.6.1.2.1.1.3.0
system.sysUpTime.0 : Timeticks: (159857288) 18 days, 12:02:52.88
This command returns the system uptime, which is of type TimeTicks. TimeTicks (RFC 1155) represents a nonnegative integer, which counts the time in hundredths of a second since some epoch. Ignoring the number in parentheses, this shows me that my router has been up and operational for 18 days, 12 hours, 02 minutes, and so on. The big number in parentheses is the exact amount of time the machine has been up, in hundredths of seconds. If you do the math, you will see this adds up to 18.501 days, or 18 days, 12 hours, and a little bit: exactly what we expect.

8.1.2. Using Net-SNMP

The Net-SNMP tools provide an excellent command-line interface to SNMP operations. These tools are also commonly known as UCD-SNMP -- you'll still find this older name in many references, and even in the code itself.

Chapter 7, "Configuring SNMP Agents" discussed how to compile, install, and configure the Net-SNMP agent. If you've done that, you've already compiled and installed the SNMP tools. They're shipped in the same package as the SNMP agent, and no real configuration is necessary for them. There is a configuration program, called snmpconf, which can be used to generate an snmp.conf file that provides default values for some of the options to the commands.[32] Unless you're using SNMPv3, though, it isn't really necessary. It might be handy to set up a default community string but, in practice, this is of only limited use: you probably have different community strings on different devices, anyway. If you decide to use snmpconf to create the tool configuration file, make sure that you place snmp.conf in the .snmp subdirectory of your home directory or (if you want the options to apply to all users) in /usr/local/share/snmp.

[32]This is the same command used to create snmpd.conf, which configures the Net-SNMP agent. The snmp.conf configuration file is similar in form to snmpd.conf.

We'll assume that you won't do any configuration and will simply use the tools "out of the box." Here's a simple poll that asks a router for its location:

$ snmpget orarouter1 public .1.3.6.1.2.1.1.6.0
system.sysLocation.0 = Sebastopol CA
It's fairly simple: we provided the hostname of the router we wanted to poll, a community string, and the OID of the object we wanted to retrieve. Instead of using the numeric OID, you can use the lengthy human-readable form. To save typing, snmpget assumes everything up to the object name and instance ID. Therefore, the following command is exactly equivalent to the previous one:

$ snmpget orarouter1 public sysLocation.0
system.sysLocation.0 = Sebastopol CA
We'll take a look at the snmpwalk and snmpset commands that come with the Net-SNMP package later in this chapter, but the package contains many tools and is well worth a more detailed explanation. One tool that's particularly useful is snmptranslate, which converts between the numeric and textual names of MIB objects and can do things such as look up the definition of an object in a MIB file. The software distribution comes with a number of standard MIBs; you can place additional MIB files in /usr/local/share/snmp/mibs. Appendix C, "Net-SNMP Tools" gives an overview of the Net-SNMP package.



Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.