4.7   Traps

Traps are used to notify remote managers that a significant event has taken place at the agent. Many different kinds of events qualify as "significant." For example, a manager may want to know if a certain internal agent variable reaches an arbitrary threshold; this can be accomplished by defining that condition as a trap. In another example, a communication link has gone down or come up in order to decide on an appropriate course of action, such as rerouting the packets; this is accomplished with the standard MIB-II traps described below.

The user-defined agent routine snmpIoTrapSend( ) can be used to convey standard traps (defined by SNMP or MIB-II) from the agent to one or more managers. For more information on this routine, see the entry for snmpIoTrapSend( )in F. SNMP Reference.

Traps that are generated by user-defined applications should invoke the function snmpdTrapSend( ) directly. For more information, see the manual page for snmpdTrapSend( ) in F. SNMP Reference.

SNMP traps usually consist of, among other parameters, a trap value, in integer which indicates the event which triggered the trap. Table 4-1 displays the available standard SNMP and MIB-II traps.

Table 4-1:  Standard Traps


Trap message type
 
Defined in
 
Trap value
 
coldStart
 
SNMP
 
0
 
warmStart
 
SNMP
 
1
 
linkDown
 
MIB-II
 
2
 
linkUp
 
MIB-II
 
3
 

Example 4-1:  Trap Example

The code below provides a simple example of a trap. Invoking the function trapExmpl( ) causes the trap to be sent to the boot host.

/* This is the routine to bind values in the trap */  
  
int trapBindVals 
( 
SNMP_PKT_T *pkt, 
void *   vals 
) 
{ 
static OIDC_T   compl_1 [] = { 1, 3, 6, 1, 4, 1, 731, 1, 0 }; 
static OIDC_T   compl_2 [] = { 1, 3, 6, 1, 4, 1, 731, 2, 0 }; 
  
static OCTET_T  name [] = "Wind River SNMP"; 
  
/* Do the bindings done with index 0 and 1 respectively */ 
  
SNMP_Bind_Integer (pkt, 0, sizeof compl_1 / sizeof compl_1 [0], compl_1, 1996); 
  
SNMP_Bind_String (pkt, 1, sizeof compl_2 / sizeof compl_2 [0], compl_2, VT_STRING,  
                  strlen (name), name, 1); 
  
return (0); 
} 
 
 
void trapExmpl() 
{ 
    void *                    pDestAddr; 
    struct sockaddr_in        destAddr; 
    u_long                    ipAddr; 
  
    IMPORT    int             snmpSocket; 
    IMPORT    char            sysBootHost []; 
  
    ipAddr = 0;     
  
     destAddr.sin_family = AF_INET; 
     destAddr.sin_port = htons (TRAP_PORT); 
     destAddr.sin_addr.s_addr = hostGetByName (sysBootHost); 
     pDestAddr =  & destAddr; 
  
     /* We send a trap with 2 varbinds as specified in the 3'rd last 
      * param using the rtn trapBindVals to do the binding. A null 
      * cookie is passed to the binding rtn in this particular case. 
      * You may make use of it if needed. 
      */ 
  
     snmpdTrapSend (&snmpSocket, 1, & pDestAddr, NULL, SNMP_VERSION_1,  
                 "trap community", snmpTrapMyOid, MYOIDLEN, &ipAddr,  
                ENTERPRISE_SPECIFIC, 0, 2, trapBindVals, 0); 
}

The output trap is as follows

------------  SNMP Header  ------------ 
SNMP: Version = 0 
SNMP: Community = trap community 
SNMP: PDU = Trap 
SNMP: Enterprise = 1.2.3.4.5.6 
 
SNMP: Agent address = 0.0.0.0 
SNMP: Trap type = enterpriseSpecific (6) 
SNMP: Specific code = 0 
SNMP: Time stamp = 4368 
 
SNMP: Object = 1.3.6.1.4.1.731.1.0 
 
SNMP: Value = 1996 
 
SNMP: Object = 1.3.6.1.4.1.731.2.0 
 
SNMP: Value = "Wind River SNMP"