-
Notifications
You must be signed in to change notification settings - Fork 57
Example SNMP Send Trap
TrapAgent class is used to simplify sending of SNMP Trap notifications for all protocol versions by hiding the underlying Socket methods and packet creation process.
To dive into the code, this is how to send a simple Trap using SNMP version 1:
TrapAgent agent = new TrapAgent();
// Variable Binding collection to send with the trap
VbCollection col = new VbCollection();
col.Add(new Oid("1.3.6.1.2.1.1.1.0"), new OctetString("Test string"));
col.Add(new Oid("1.3.6.1.2.1.1.2.0"), new Oid("1.3.6.1.9.1.1.0"));
col.Add(new Oid("1.3.6.1.2.1.1.3.0"), new TimeTicks(2324));
col.Add(new Oid("1.3.6.1.2.1.1.4.0"), new OctetString("Milan"));
// Send the trap to the localhost port 162
agent.SendV1Trap(new IpAddress("localhost"), 162, "public",
new Oid("1.3.6.1.2.1.1.1.0"), new IpAddress("127.0.0.1"),
SnmpConstants.LinkUp, 0, 13432, col);
In VB.Net
Imports SnmpSharpNet
Module Module1
Sub Main()
Dim host As String = "localhost"
Dim community As String = "public"
Dim agent As TrapAgent = New TrapAgent()
Dim col As VbCollection = New VbCollection()
col.Add(New Oid("1.3.6.1.2.1.1.1.0"), New OctetString("Test string"))
col.Add(New Oid("1.3.6.1.2.1.1.2.0"), New Oid("1.3.6.1.9.1.1.0"))
Dim timeTickVal As UInt32 = 2324
col.Add(New Oid("1.3.6.1.2.1.1.3.0"), New TimeTicks(timeTickVal))
col.Add(New Oid("1.3.6.1.2.1.1.4.0"), New OctetString("MyName"))
agent.SendV1Trap(New IpAddress("localhost"), 162, "public", _
New Oid("1.3.6.1.2.1.1.1.0"), New IpAddress("127.0.0.1"), _
SnmpConstants.LinkUp, 0, 13432, col)
End Sub
End Module
In the above example, we have send a SNMP version 1 LinkUp trap to the localhost port 162 with the community name public.
Parameters are as follows:
- IP address or DNS host name of the host that you want to send the SNMP trap
- UDP port number of the host you wish to send the trap
- SNMP community name for the SNMP trap
- sysObjectID value of your host (the sender)
- Your hosts (sender) IP address
- Generic TRAP integer value
- Specific TRAP integer value
- Your hosts sysUpTime value. This value is the number of 100ms periods since the host last booted.
- VbCollection holding OID/value pairs to include in the trap
Sending a SNMP version 2 trap is very similar:
agent.SendV2Trap(new IpAddress("localhost"), 162, "public", 13433,
new Oid(".1.3.6.1.6.3.1.1.5"), col);
Same in VB.Net
agent.SendV2Trap(New IpAddress("localhost"), 162, "public", 13433, _
New Oid(".1.3.6.1.6.3.1.1.5"), col)
V2Trap, specific Trap format for SNMP versions 2c and 3, is different from the format of SNMP Traps with SNMP version 1. For that reason, parameters are different. When sending SNMP version 2c notifications, you have to provide following parameters:
- IP address or DNS host name of the host that you want to send the SNMP trap
- UDP port number of the host you wish to send the trap
- SNMP community name for the SNMP trap
- Your hosts sysUpTime value. This value is the number of 100ms periods since the host last booted.
- SNMP TrapObjectID.0 OID value. This is the OID that defines what kind of trap you are sending.
- VbCollection holding OID/value pairs to include in the trap
Sending SNMP version 3 has more options and more methods available to accommodate different security levels that are possible.
Simplest format SNMP version 3 Trap uses noAuthNoPriv (no authentication or privacy) security model. This is how you send a noAuthNoPriv Trap:
agent.SendV3Trap(new IpAddress("localhost"), 162,
new byte[] { 0x00, 0x08, 0x02, 0x01, 0x20, 0x12, 0x14, 0xa0, 0xb1, 0xc2 },
1, 500, "mysecurityname", 13434, new Oid(".1.3.6.1.6.3.1.1.5"), col);
Same in VB.Net
agent.SendV3Trap(New IpAddress("localhost"), 162, _
New Byte() {&H0, &H8, &H2, &H1, &H20, &H12, &H14, &HA0, &HB1, &HC2}, _
1, 500, "mysecurityname", 13434, New Oid(".1.3.6.1.6.3.1.1.5"), col)
Parameters for SNMP version 3 traps is different from previous versions of the protocol because of the security model implementation that needs to be present event when privacy and authentication is not used. To successfuly send a SNMPv3 Trap you will need to supply following values:
- IP address or DNS host name of the host that you want to send the SNMP trap
- UDP port number of the host you wish to send the trap
- authoritativeEngineID for your host. Basically, this is just an array of hex values representing your hosts ID that you should generate/make up. For details on the format of the engineId field, please see SNMP RFCs in the references section of the home page.
- engineBoots value that represents the number your SNMP engine has started up (booted). For testing purposes you can set this value to 0
- engineTime value represents the number of seconds since the last startup of your SNMP engine. You can leave this at 0 during testing
- Security name configured on the receiver. This value is also referred to as user name.
- Your hosts sysUpTime value. This value is the number of 100ms periods since the host last booted.
- SNMP TrapObjectID.0 OID value. This is the OID that defines what kind of trap you are sending.
- VbCollection holding OID/value pairs to include in the trap
Above example is going to send a trap without authentication or privacy available in SNMP version 3. To add authentication for a authNoPriv (authentication without privacy encryption) security model, you can call a method that adds 2 parameters to the end of the parameter list, AuthenticationDigest, which digest to use to authenticate the trap, and a byte array holding authentication secret (password):
agent.SendV3Trap(new IpAddress("localhost"), 162,
new byte[] { 0x00, 0x08, 0x02, 0x01, 0x20, 0x12, 0x14, 0xa0, 0xb1, 0xc2 },
1, 501, "myauthenticationname", 13435, new Oid(".1.3.6.1.6.3.1.1.5"), col,
AuthenticationDigests.MD5,ASCIIEncoding.UTF8.GetBytes("authpasswd"));
In VB.Net
agent.SendV3Trap(New IpAddress("localhost"), 162, _
New Byte() {&H0, &H8, &H2, &H1, &H20, &H12, &H14, &HA0, &HB1, &HC2}, _
1, 501, "myauthenticationname", 13435, New Oid(".1.3.6.1.6.3.1.1.5"), col, _
AuthenticationDigests.MD5, _
System.Text.ASCIIEncoding.UTF8.GetBytes("authpasswd"))
As you can see, noAuthNoPriv and authPriv SNMP version 3 Trap calls are identical except for the two additional parameters when adding authentication to the notifications. Similar approach is made when adding privacy to the authNoPriv notification. There are two additional parameters to specify PrivacyProtocol used to encrypt the information inside the SNMP version 3 trap and a byte array holding the privacy secret (password used to encrypt data):
agent.SendV3Trap(new IpAddress("localhost"), 162,
new byte[] { 0x00, 0x08, 0x02, 0x01, 0x20, 0x12, 0x14, 0xa0, 0xb1, 0xc2 },
1, 501, "myauthenticationname", 13435, new Oid(".1.3.6.1.6.3.1.1.5"), col,
AuthenticationDigests.MD5, ASCIIEncoding.UTF8.GetBytes("authpasswd"),
PrivacyProtocols.DES, ASCIIEncoding.UTF8.GetBytes("privpasswd"));
In VB.Net
agent.SendV3Trap(New IpAddress("localhost"), 162, _
New Byte() {&H0, &H8, &H2, &H1, &H20, &H12, &H14, &HA0, &HB1, &HC2}, _
1, 501, "myauthenticationname", 13435, _
New Oid(".1.3.6.1.6.3.1.1.5"), col, _
AuthenticationDigests.MD5, _
System.Text.ASCIIEncoding.UTF8.GetBytes("authpasswd"), _
PrivacyProtocols.DES, _
System.Text.ASCIIEncoding.UTF8.GetBytes("privpasswd"))
This Trap will be sent both authenticated and privacy encrypted. You can changed authentication digest to SHA1 or privacy encryption to AES128 if that is what your implementation requires but base operation remains the same.
One important thing to remember is that noAuthPriv security model, without authentication and with privacy encryption, is not supported by the SNMP version 3.