Generating and Handling Traps
SNMP-managed
devices are usually shipped with pre-defined traps that can be enabled
or disabled during configuration. Some agents are extensible and allow
you to define additional traps. The Net-SNMP package includes the snmptrap command for manually generating traps. Figure Six shows an example of using snmptrap.
Figure Six: Using snmptrap to generate a trap # snmptrap -v2c dalton anothername '' .1.3.6.1.6.3.1.1.5.3 \
ifIndex i 2 ifAdminStatus i 1 ifOperStatus i 1 |
The -v2c option indicates that an SNMP Version 2c trap is to be sent. The next two arguments (dalton and anothername)
are the destination for the trap message (the SNMP manager) and the
trap community name. The next argument is the device uptime, which is
required for all traps. In this example, we specify a null string,
which defaults to the current uptime. The final argument in the first
line is the OID for the trap itself. Traps are defined in a MIB just
like regular data nodes and therefore have OIDs. This OID corresponds
to the trap (or node) linkDown, and means a network interface has changed state (from up to down or from down to up).
The remaining arguments (starting with ifIndex) are determined by the specific trap being sent. This particular trap (linkDown)
requires three additional arguments, where each is, in turn, composed
of three values. The three arguments are the interface number, the
administrative status and the operational status. The three values that
specify each of these arguments are a keyword (ifIndex, ifAdminStatus, and ifOperStatus), the type of data (i for integer), and the actual (integer) value (2, 1, and 1).
In
this case, the trap specifies interface number 2. The two status values
of 1 indicate that the interface is up, so this trap means that the
interface is back online after being down.
Net-SNMP also includes the snmptrapd
daemon that runs on the manager host to handle incoming traps (although
you might not need this daemon if your management software handles
traps). Start the daemon manually by running the snmptrapd -s command, which by default sends trap messages to the syslogd daemon, using the Local0 facility (for more on syslogd, see the September 2000 Guru Guidance column, online at http://www.linuxmagazine.com/2000-09/guru_01.html). Figure Seven shows the syslog message generated by the trap command in Figure Six.
Figure Seven: The syslog log message for a trap Feb 25 11:44:21 beulah snmptrapd[8675]: beulah.local [192.0.9.84]:
Trap system.sysUpTime.0 = Timeticks: (144235905) 16 days, 16:39:19.05,
.iso.org.dod.internet.snmpV2.snmpModules.snmpMIB.
snmpMIBObjects.snmpTrap.snmpTrapOID.0 = OID: 1.1.5.3,
interfaces.ifTable.ifEntry.ifIndex = 2,
interfaces.ifTable.ifEntry.ifAdminStatus = up(1),
interfaces.ifTable.ifEntry.ifOperStatus = up(1) |
To start snmptrapd at boot time, you'll need to add the startup command to the /etc/ init.d/snmp boot script. Its configuration file is /usr/share/snmp/snmptrapd.conf.
If
you want the daemon to perform certain tasks upon receiving various
traps, add entries to the configuration file that look like this:
traphandle OID|default program
[arguments]
The second field is either the OID of the trap that should be handled or the word default, which handles traps when there is no matching traphandle
definition. The rest of the line contains the name of a program and its
arguments. This program (or action) is run whenever the specified trap
is received.