Section 7.3. Setting a MIB Value


7.3. Setting a MIB Value

With snmpget and snmpwalk, we have retrieved management information only from devices. The next logical step is to change the value of a MIB object via SNMP. This operation is known as snmpset, or set. In this section, we'll read the value of an object, use snmpset to change its value, and read the value again to prove that it's been changed.

There's obviously some danger here: what happens if you change a variable that's critical to the state of the system you're monitoring? In this chapter, we'll deal only with some simple objects, such as the administrative contact, that won't damage anything if they're changed incorrectly. Therefore, if you keep the OIDs correct, you shouldn't worry about hurting any of your devices. All the objects we set in this chapter have an ACCESS of read-write. It's a good idea to get a feel for which objects are writable by reading the MIB in which the object is definedeither one of the RFCs, or a MIB file provided by your vendor.

Let's get started. Run the following OpenView command (or use one of the other programs we've discussed) to find out the sysContact for your chosen device:

 $ /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 

The -c public switch passes the community string public to the snmpget command.

Keep in mind that your devices shouldn't use the same (default) community strings that are used in this book. In addition, using the same string for the read-only (snmpget) and read-write (snmpset) communities is a poor idea.


Now let's run the OpenView snmpset command. This command takes the value specified in quotes on the command line and uses it to set the object indicated by the given OID. Use the same OID (system.sysContact.0). Since the new value for sysContact contains words and possibly numbers, we must also specify the variable type octetstring.[*] Run the OpenView snmpset command with the following parameters:

[*] If you read RFC 1213 (MIB-II), you will note that sysLocation has a SYNTAX of DisplayString. This is really a textual convention of type OCTET STRING with a size of 0.255 octets.

 $ /opt/OV/bin/snmpset -c private orarouter1 .1.3.6.1.2.1.1.4.0 \ octetstring "Meg A. Byte  555-1212" system.sysContact.0 : DISPLAY STRING- (ascii):  Meg A. Byte  555-1212 

The result shows that snmpset successfully changed the router's contact person to Meg A. Byte 555-1212. If you don't see this result, the set was not successful. Table 7-2 shows some of the common error messages you might receive and steps you can take to correct the problems. To confirm the value the device has stored in sysContact, we can repeat the snmpget command.

If we use OpenView's GUI, things start to get a bit easier to see, set, and confirm. Use the GUI to get the value of sysContact. Once you have confirmed that a value is there, type a description in the SNMP Set Value text box. Since there is only one instance for sysContact, you have to insert a 0 (zero) for the MIB Instance. After you have completed all the required input items, click on the Set button located to the right of the SNMP Set Value text box. You should see a pop-up window that reads "Set has completed successfully." To verify that the set actually occurred, click on Start Query. (It should be apparent to you by now that using a GUI such as OpenView's MIB Browser program makes getting and setting MIB objects much easier.)

To show how this can be done programmatically, we will write another small Perl script, named snmpset.pl:

 #!/usr/local/bin/perl #filename: /opt/local/perl_scripts/snmpset.pl use SNMP_util; $MIB1 = ".1.3.6.1.2.1.1.6.0"; $HOST = "oraswitch2"; $LOC  = "@ARGV"; ($value) = &snmpset("private\@$HOST","$MIB1",'string',"$LOC"); if ($value) { print "Results :$MIB1: :$value:\n"; } else { warn "No response from host :$HOST:\n"; } 

Let's run this script:

 $ /opt/local/perl_scripts/snmpset.pl A bld JM-10119 floor 7 Results :.1.3.6.1.2.1.1.6.0: :A bld JM-10119 floor 7: 

Using the snmpget.pl script, we can verify that the set took place:

 $ /opt/local/perl_scripts/snmpget.pl .1.3.6.1.2.1.1.6.0 public@oraswitch2 Results :.1.3.6.1.2.1.1.1.0: :A bld JM-10119 floor 7: 

Now we'll use the Net-SNMP snmpset utility to change the system contact:

 $ snmpset -v1 -c private oraswitch2 sysContact.0 s myself SNMPv2-MIB::system.sysContact.0 = myself $ snmpget -v1 -c public oraswitch2 sysContact.0 SNMPv2-MIB::system.sysContact.0 = myself 

There's nothing really confusing here. We supplied a community string, a hostname, and an object ID, followed by a datatype (s for String) and the new value of sysContact. Just to convince ourselves that the set actually took place, we followed it with an snmpget. The only additional thing you need to know is the mechanism for specifying datatypes. Net-SNMP uses the single-character abbreviations shown in Table 7-1.

Table 7-1. Net-SNMP datatype abbreviations

Abbreviation

Meaning

a

IP address

b

Bits

d

Decimal string

D

Double

F

Float

i

Integer

I

Signed int64

o

Object ID

s

String

t

Time ticks

u

Unsigned integer

U

Unsigned int64

x

Hexadecimal string





Essential SNMP
Essential SNMP, Second Edition
ISBN: 0596008406
EAN: 2147483647
Year: 2003
Pages: 165

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net