Showing posts with label SNMP Trap. Show all posts
Showing posts with label SNMP Trap. Show all posts

8.11.10

Understanding Simple Network Management Protocol (SNMP) Traps

http://www.cisco.com/en/US/tech/tk648/tk362/technologies_tech_note09186a0080094aa5.shtml

SNMPv1 traps are defined in RFC 1157, with these fields:

  • Enterprise—Identifies the type of managed object that generates the trap.

  • Agent address—Provides the address of the managed object that generates the trap.

  • Generic trap type—Indicates one of a number of generic trap types.

  • Specific trap code—Indicates one of a number of specific trap codes.

  • Time stamp—Provides the amount of time that has elapsed between the last network reinitialization and generation of the trap.

  • Variable bindings—The data field of the trap that contains PDU. Each variable binding associates a particular MIB object instance with its current value.

Standard generic traps are: coldStart, warmStart, linkDown, linkUp, authenticationFailure, egpNeighborLoss. For generic SNMPv1 traps, Enterprise field contains value of sysObjectID  of the device that sends trap. For vendor specific traps, Generic trap type field is set to enterpriseSpecific(6).

In SNMPv2c trap is defined as NOTIFICATION and formatted differently compared to SNMPv1. It has these parameters:

  • sysUpTime—This is the same as Time stamp in SNMPv1 trap.

  • snmpTrapOID  —Trap identification field. For generic traps, values are defined in RFC 1907, for vendor specific traps snmpTrapOID is essentially a concatenation of the SNMPv1 Enterprise parameter and two additional sub-identifiers, '0', and the SNMPv1 Specific trap code parameter.

  • VarBindList—This is a list of variable-bindings.

In order for a management system to understand a trap sent to it by an agent, the management system must know what the object identifier (OID) defines. Therefore, it must have the MIB for that trap loaded. This provides the correct OID information so that the network management system can understand the traps sent to it.

3.11.10

Trap in SNMP4j-Agent

SNMP Trap is something very interesting. The Agent is responsible for sending trap/notification, while the client works in the daemon mode to receive trap messages.

Who is the server? Well, server is not a concept in SNMP v1 and v2. People call them agents.

How Trap is Implemented in SNMP4J Agent?

That is a good question. Just take a look at BaseAgent. Here is some excerpt from SNMP4J Agent 1.4.1.

134   /**
135 * Initialize transport mappings, message dispatcher, basic MIB modules,
136 * proxy forwarder, VACM and USM security, and custom MIB modules and objects
137 * provided by sub-classes.
138 *
139 * @throws IOException
140 * if initialization fails because transport initialization fails.
141 */
142 public void init() throws IOException {
143 agentState = STATE_INIT_STARTED;
144 initTransportMappings();
145 initMessageDispatcher();
146 server.addContext(new OctetString());
147 snmpv2MIB = new SNMPv2MIB(sysDescr, sysOID, sysServices);
148
149 // register Snmp counters for updates
150 dispatcher.addCounterListener(snmpv2MIB);
151 agent.addCounterListener(snmpv2MIB);
152 snmpFrameworkMIB =
153 new SnmpFrameworkMIB((USM)
154 mpv3.getSecurityModel(SecurityModel.SECURITY_MODEL_USM),
155 dispatcher.getTransportMappings());
156 usmMIB = new UsmMIB(usm, SecurityProtocols.getInstance());
157 usm.addUsmUserListener(usmMIB);
158
159 vacmMIB = new VacmMIB(new MOServer[] { server });
160 snmpTargetMIB = new SnmpTargetMIB(dispatcher);
161 snmpNotificationMIB = new SnmpNotificationMIB();
162 snmpCommunityMIB = new SnmpCommunityMIB(snmpTargetMIB);
163 initConfigMIB();
164 snmpProxyMIB = new SnmpProxyMIB();
165 notificationOriginator =
166 new NotificationOriginatorImpl(session, vacmMIB,
167 snmpv2MIB.getSysUpTime(),
168 snmpTargetMIB, snmpNotificationMIB);

169 snmpv2MIB.setNotificationOriginator(agent);
170
171 setupDefaultProxyForwarder();
172 // add USM users
173 addUsmUser(usm);
174 // add SNMPv1/v2c community to SNMPv3 security name mappings
175 addCommunities(snmpCommunityMIB);
176 addViews(vacmMIB);
177 addNotificationTargets(snmpTargetMIB, snmpNotificationMIB);
178
179 registerSnmpMIBs();
180 }


At line 161, an SnmpNotificationMIB instance is created. From line 165 to 168, a NotificationOriginator is created by using NotificationOriginatorImpl. Line 169 sets up the notification originator for SNMPv2 MIB object, while line 177 calls the abstract method, addNotificationTargets() to add more things into the target MIB and notification MIB.


addNotificationTargets()?


If you are using BaseAgent just like I do now, overriding addNotificationTargets() method could be a very good access point for your trap targets.This method is abstract in BaseAgent, and you must override them, although you can just put it as empty in your derived class.


Target MIB?


The method addNotificationTargets() uses two parameters: Target MIB and Notification MIB.


Target MIB is an instance of SnmpTargetMIB class. It is initialized at line 160 above with a dispatcher, which is an instance of MessageDispatcherImpl, initialized by initMessageDispatcher().


From the SNMP protocol’s point of view, SnmpTargetMIB takes care of SNMP Target Objects in SNMP-TARGET-MIB, which is a version 2 MIB.


OID Base









snmpTargetObjects
1.3.6.1.6.3.12.1

iso(1). org(3). dod(6). internet(1). snmpV2(6). snmpModules(3). snmpTargetMIB(12). snmpTargetObjects(1)


Details










































































































































































































snmpTargetObjectsGROUP1.3.6.1.6.3.12.1
iso(1). org(3). dod(6). internet(1). snmpV2(6). snmpModules(3). snmpTargetMIB(12). snmpTargetObjects(1)
  snmpTargetSpinLockSCALARread-writeTestAndIncr1.3.6.1.6.3.12.1.1.0
  snmpTargetAddrTableTABLEnot-accessibleSEQUENCE OF1.3.6.1.6.3.12.1.2
    snmpTargetAddrEntryENTRYnot-accessibleSnmpTargetAddrEntry1.3.6.1.6.3.12.1.2.1
      snmpTargetAddrNameTABULARnot-accessibleSnmpAdminString ( 1..32 )1.3.6.1.6.3.12.1.2.1.1
      snmpTargetAddrTDomainTABULARread-createTDomain1.3.6.1.6.3.12.1.2.1.2
      snmpTargetAddrTAddressTABULARread-createTAddress1.3.6.1.6.3.12.1.2.1.3
      snmpTargetAddrTimeoutTABULARread-createTimeInterval1.3.6.1.6.3.12.1.2.1.4
      snmpTargetAddrRetryCountTABULARread-createInteger32 ( 0..255 )1.3.6.1.6.3.12.1.2.1.5
      snmpTargetAddrTagListTABULARread-createSnmpTagList1.3.6.1.6.3.12.1.2.1.6
      snmpTargetAddrParamsTABULARread-createSnmpAdminString ( 1..32 )1.3.6.1.6.3.12.1.2.1.7
      snmpTargetAddrStorageTypeTABULARread-createStorageType1.3.6.1.6.3.12.1.2.1.8
      snmpTargetAddrRowStatusTABULARread-createRowStatus1.3.6.1.6.3.12.1.2.1.9
  snmpTargetParamsTableTABLEnot-accessibleSEQUENCE OF1.3.6.1.6.3.12.1.3
    snmpTargetParamsEntryENTRYnot-accessibleSnmpTargetParamsEntry1.3.6.1.6.3.12.1.3.1
      snmpTargetParamsNameTABULARnot-accessibleSnmpAdminString ( 1..32 )1.3.6.1.6.3.12.1.3.1.1
      snmpTargetParamsMPModelTABULARread-createSnmpMessageProcessingModel1.3.6.1.6.3.12.1.3.1.2
      snmpTargetParamsSecurityModelTABULARread-createSnmpSecurityModel ( 1..2147483647 )1.3.6.1.6.3.12.1.3.1.3
      snmpTargetParamsSecurityNameTABULARread-createSnmpAdminString1.3.6.1.6.3.12.1.3.1.4
      snmpTargetParamsSecurityLevelTABULARread-createSnmpSecurityLevel1.3.6.1.6.3.12.1.3.1.5
      snmpTargetParamsStorageTypeTABULARread-createStorageType1.3.6.1.6.3.12.1.3.1.6
      snmpTargetParamsRowStatusTABULARread-createRowStatus1.3.6.1.6.3.12.1.3.1.7
  snmpUnavailableContextsSCALARread-onlyCounter321.3.6.1.6.3.12.1.4.0
  snmpUnknownContextsSCALARread-onlyCounter321.3.6.1.6.3.12.1.5.0

What can I do with SNMP4J Agent?


You might probably don’t care about SNMP Target MIB. All you need could only by how to use SNMP4J Agent. Good.


Although the Java API documentation for SNMP4J Agent is very useless, they provided meaningful naming, as well as, and more importantly, source code. That is way we like open-source, right?


 


Notification MIB?

25.10.10

Net-SNMP Tutorial -- traps

Traps can be used by network entities to signal abnormal conditions to management stations. The following paragraphs will describe how traps are defined in MIB files, how they are generated by the snmptrap utlity, and how they are received and processed by the snmptrapd utitlity.

Note: as I prefer the OID notations using the MODULE::identifier notation, this is used throughout in the following examples, and the snmptrapd output similarly assumes the -OS option.

Definition of traps

Traps comes in two destinctly different forms, SNMPv1 traps, and SNMPv2 traps (or notifications)
The SNMPv1 trap
The SNMPv1 trap is defined in the MIB file using the TRAP-TYPE macro, as in the following example
TRAP-TEST-MIB DEFINITIONS ::= BEGIN
IMPORTS ucdExperimental FROM UCD-SNMP-MIB;

demotraps OBJECT IDENTIFIER ::= { ucdExperimental 990 }

demo-trap TRAP-TYPE
STATUS current
ENTERPRISE demotraps
VARIABLES { sysLocation }
DESCRIPTION "This is just a demo"
::= 17

END

This defines a single enterprise specific trap, that can be issued as follows


% snmptrap -v 1 -c public host TRAP-TEST-MIB::demotraps localhost 6 17 '' \
SNMPv2-MIB::sysLocation.0 s "Just here"

and when received by snmptrapd is displayed as follows

    1999-11-12 23:26:07 localhost [127.0.0.1] TRAP-TEST-MIB::demotraps:
    Enterprise Specific Trap (demo-trap) Uptime: 1 day, 5:34:06
    SNMPv2-MIB::sysLocation.0 = "Just here"

The SNMPv2 notification
The format of the SNMPv2 notification is somewhat different. The definition in the MIB file looks as follows


NOTIFICATION-TEST-MIB DEFINITIONS ::= BEGIN
IMPORTS ucdavis FROM UCD-SNMP-MIB;

demonotifs OBJECT IDENTIFIER ::= { ucdavis 991 }

demo-notif NOTIFICATION-TYPE
STATUS current
OBJECTS { sysLocation }
DESCRIPTION "Just a test notification"
::= { demonotifs 17 }

END



This is a definition that is similar to the SNMPv1 trap given above. Issuing this notification looks as follows

    % snmptrap -v 2c -c public localhost '' NOTIFICATION-TEST-MIB::demo-notif \
    SNMPv2-MIB::sysLocation.0 s "just here"


and the resulting output from the trap daemon is


1999-11-13 08:31:33 localhost [127.0.0.1]:
SNMPv2-MIB::sysUpTime.0 = Timeticks: (13917129) 1 day, 14:39:31.29
SNMPv2-MIB::snmpTrapOID.0 = OID: NOTIFICATION-TEST-MIB::demo-notif
SNMPv2-MIB::sysLocation.0 = "just here"


Defining trap handlers

The snmptrapd utility has the ability to execute other programs on the reception of a trap. This is controlled by the traphandle directive, with the syntax


traphandle OID command

Notice, that this only takes an OID to determine which trap (or notification) is received. This means that SNMPv1 traps need to be represented in SNMPv2 format, which is described in RFC 2089. Basically, the OID for our above defined trap is created by taking the ENTERPRISE parameter and adding the sub-ids 0 and 17. Similarly, OID values for the generic SNMPv1 traps are defined to be the same as for SNMPv2.

The command specifies a command to be executed by snmptrapd upon reception by the command. This command is executed with the data of the trap as its standard input. The first line is the host name, the second the IP address of the trap sender, and the following lines consists of an OID VALUE pair with the data from the received trap.

A simple shell script to be called from snmptrapd is the following:


    #!/bin/sh

    read host
    read ip
    vars=

    while read oid val
    do
    if [ "$vars" = "" ]
    then
    vars="$oid = $val"
    else
    vars="$vars, $oid = $val"
    fi
    done

    echo trap: $1 $host $ip $vars
Now, given the following sample snmptrapd.conf file,

    # the generic traps
    traphandle SNMPv2-MIB::coldStart /home/nba/bin/traps cold
    traphandle SNMPv2-MIB::warmStart /home/nba/bin/traps warm
    traphandle IF-MIB::linkDown /home/nba/bin/traps down
    traphandle IF-MIB::linkUp /home/nba/bin/traps up
    traphandle SNMPv2-MIB::authenticationFailure /home/nba/bin/traps auth
    # this one is deprecated
    traphandle .1.3.6.1.6.3.1.1.5.6 /home/nba/bin/traps egp-neighbor-loss

    # enterprise specific traps
    traphandle TRAP-TEST-MIB::demo-trap /home/nba/bin/traps demo-trap
    traphandle NOTIFICATION-TEST-MIB::demo-notif /home/nba/bin/traps demo-notif
the following snmptrap invocation, to issue a generic Link down trap,

    % snmptrap -v 1 -c public localhost TRAP-TEST-MIB::demotraps localhost 2 0 '' \
    IF-MIB::ifIndex i 1

results in the following output from snmptrapd

    1999-11-13 12:46:49 localhost [127.0.0.1] TRAP-TEST-MIB::traps:
    Link Down Trap (0) Uptime: 1 day, 18:54:46.27
    IF-MIB::ifIndex.0 = 1
and the following output from the handler

    trap: down localhost 127.0.0.1 SNMPv2-MIB::sysUpTime = 1:18:54:46.27, SNMPv2-MIB::snmpTrapOID = IF-MIB::linkDown, IF-MIB::ifIndex.0 = 1, SNMPv2-MIB::snmpTrapEnterprise = TRAP-TEST-MIB::traps
and issuing our enterprise specific trap gives this output from our handler

    trap: demo-trap localhost 127.0.0.1 SNMPv2-MIB::sysUpTime = 1:19:00:48.01, SNMPv2-MIB::snmpTrapOID = TRAP-TEST-MIB::demo-trap, SNMPv2-MIB::sysLocation.0 = "just here", SNMPv2-MIB::snmpTrapEnterprise = TRAP-TEST-MIB::traps
and finally our enterprise specific notification

    trap: demo-notif localhost 127.0.0.1 SNMPv2-MIB::sysUpTime.0 = 1:19:02:06.33, SNMPv2-MIB::snmpTrapOID.0 = NOTIFICATION-TEST-MIB::demo-notif, SNMPv2-MIB::sysLocation.0 = "just here"

Generating traps from the agent

The agent is able to generate a few traps by itself. When starting up, it will generate a SNMPv2-MIB::coldStart trap, and when shutting down a UCD-SNMP-MIB::ucdShutDown.

These traps are sent to managers specified in the snmpd.conffile, using the trapsink or trap2sink directive (SNMPv1 and SNMPv2 trap respectively)


    # send v1 traps
    trapsink nms.system.com public
    # also send v2 traps
    trap2sink nms.system.com secret
    # send traps on authentication failures
    authtrapenable 1
In addition, the agent is able to send authentication failure traps, to the same hosts as above, controlled by the authtrapenable directive in snmpd.conf, or by setting SNMPv2-MIB::snmpEnableAuthenTraps variable

    $ snmpset -c public agent SNMPv2-MIB::snmpEnableAuthenTraps s enable

Note: the current 4.0 version of Net-SNMP does not generate authentication failure traps. This will hopefully be corrected before the next release. [an error occurred while processing this directive]

11.10.10

SNMP Trap Example Shipped with SNMP4J

http://www.snmp4j.org/doc/org/snmp4j/package-summary.html

SNMPv1 TRAP PDU

import org.snmp4j.PDUv1;
...
PDUv1 pdu = new PDUv1();
pdu.setType(PDU.V1TRAP);
pdu.setGenericTrap(PDUv1.COLDSTART);
...

SNMPv2c/SNMPv3 INFORM PDU

import org.snmp4j.ScopedPDU;
...
ScopedPDU pdu = new ScopedPDU();
pdu.setType(PDU.INFORM);
// sysUpTime
long sysUpTime = (System.currentTimeMillis() - startTime) / 10;
pdu.add(new VariableBinding(SnmpConstants.sysUpTime, new TimeTicks(sysUpTime)));
pdu.add(new VariableBinding(SnmpConstants.snmpTrapOID, SnmpConstants.linkDown));
// payload
pdu.add(new VariableBinding(new OID("1.3.6.1.2.1.2.2.1.1"+downIndex),
new Integer32(downIndex)));
...

Multi Threaded Trap Receiver using SNMP4J

Just a bookmark here. Please refer to the original blog for it.

http://www.ashishpaliwal.com/blog/2008/12/multi-threaded-trap-receiver-using-snmp4j/