Search code examples
c#snmpsharp-snmpsnmp-trap

SharpSnmpLib SendTrap


I create this button in C#:

private void sendTrapBtn_Click(object sender, EventArgs e)
{
    Messenger.SendTrapV1(new IPEndPoint(IPAddress.Parse("172.29.16.200"), 162), 
             IPAddress.Parse("172.29.16.203"), // my machine 
             new OctetString("MNTCORPRD"),
             new ObjectIdentifier("1.3.6.1.4.1.791"), 
             0, 
             0, 
             0, 
             new List<Variable>());
}

I can see that I can choose the Submitter IP, Destination IP, SNMP Community String, Enterprise OID, Generic OID, Specific OID, but I don't know how to use the variable List to put the OIDs embedded with some information like:

OID: 1.3.6.1.4.1.791.0.1.4.1 Type: Integer Data: 2345
OID: 1.3.6.1.4.1.791.0.1.4.2 Type: String Data: "DB Error occurred"

A received trap example.

Trap identifier 1.3.6.1.4.1.791.2.10.2.90.6.1
Trap var bind data: 
OID: 1.3.6.1.4.1.791.2.10.2.90.0 Value: application Process
OID: 1.3.6.1.4.1.791.2.10.2.90.1 Value: Cluster.exe
OID: 1.3.6.1.4.1.791.2.10.2.90.2 Value: Running

How could I do this? one example is enough!


Solution

  • I found the answer doing tests by my self and I will share this knowledge!

    To create a varbind variable in your trap, you need to create first a Lextm.SharpSnmpLib.Variable.

    After create a list and send your trap.

    Lextm.SharpSnmpLib.Variable oid = new Lextm.SharpSnmpLib.Variable(new ObjectIdentifier("1.3.6.1.4.1.9999.0.0.1"),new OctetString("Cluster.exe"));
    
    List<Variable> oidList = new List<Variable>();
    
    oidList.Add(oid);
    
    Messenger.SendTrapV1(new IPEndPoint(IPAddress.Parse("172.29.16.200"), 162),
                         IPAddress.Parse("172.29.16.20"), // my machine 
                         new OctetString("MNTCORPR"),
                         new ObjectIdentifier("1.3.6.1.4.1.9999"), 
                         0, 
                         0, 
                         0, 
                         oidList);