Today, I walked around the park and suddenly realized the logic of SNMP Trap that I had been thinking about for several days. Combine several tools

  1. The node package node – net – SNMP
  2. mib browser
  3. An MIB file that I wrote, which has an OID and a trap OID, points to the first OID

Run this process successfully. Quick notes.

Prepare applications for receiving SNMP traps

Download the Node-net-SNMP source code library and then:

    cd example    
    node snmp-receiver.js -v
Copy the code

-v indicates that the output should be verbose so that we can see the specific trap content.

The MIB browser records mib files and sends traps to the trap receiver on the host

output

2020-4-9 17:46:09: TrapV2 received: { "pdu": { "type": 167, "id": 402812005, "varbinds": [ { "oid": "1.3.6.1.2.1.1.3.0", "type" : 67, the "value" : 0}, {" oid ":" 1.3.6.1.6.3.1.1.4.1.0 ", "type" : 6, "value" : "1.3.6.1.4.1.66666.3"}, {" oid ":" 1.3.6.1.4.1.66666.1.1.0 ", "type" : 4, "value" : {" type ":" Buffer ", "data" : [97, 98, 99, 100]}}], "scoped" : false}, "rinfo" : {" address ":" 127.0.0.1 ", "family" : "IPv4", "port" : 63984, "size": 90 } }Copy the code

explain

Varsharing is made of three, among which

  1. Oid “1.3.6.1.2.1.1.3.” indicates sysuptime, varbind for each trap
  2. Oid 1.3.6.1.6.3.1.1.4.1 indicates the OID of the trap. The names in dataAlarmTrap.objects defined in the MIB file refer to the OID used for this name.
  3. Oid 1.3.6.1.4.1.66666.1.1 is the new value of the TRAP OID. Each “data” in value: [97,98,99,100] is an asccii representation of a number, and its string can be retrieved by toString()

Appendix: Contents of MIB files

tbit DEFINITIONS ::= BEGIN
    IMPORTS
        enterprises, IpAddress, Integer32, Unsigned32, Counter64, 
        OBJECT-TYPE,NOTIFICATION-TYPE
            FROM SNMPv2-SMI;
    tbit OBJECT IDENTIFIER ::= { enterprises 66666 }
    RecoDevice OBJECT IDENTIFIER ::= { tbit 1 }
    RecoDevice1 OBJECT IDENTIFIER ::= { tbit 2 }
    Name OBJECT-TYPE
        SYNTAX OCTET STRING
        MAX-ACCESS read-write
        STATUS current
        DESCRIPTION
            "Description."
        ::= { RecoDevice 1 }
	dataAlarmTrap NOTIFICATION-TYPE
			OBJECTS { Name }
			STATUS current
			DESCRIPTION 
				"Description."
			::= { tbit 3 }
END
Copy the code