Simple SNMP with SimpleSnmp class

Apparently not everybody is as interested in the background workings of the SNMP protocol as I am. For this reason I have created a very simple to use utility class that will allow you to make requests and collect replies without worrying too much about how it’s all done.

For that reason I have created SimpleSnmp class. It makes common SNMP version 1 and 2c operations simple. Version 3 is not included because there is a security aspect that, if simplified too much, will no longer be secure.

To dive straight in, here is a SNMP-Get request example:

string host = "localhost";
string community = "public";
SimpleSnmp snmp = new SimpleSnmp(host, community);
 
if (!snmp.Valid)
{
	Console.WriteLine("SNMP agent host name/ip address is invalid.");
	return;
}
Dictionary<Oid,AsnType> result = snmp.Get(SnmpVersion.Ver1, 
                                          new string[] { ".1.3.6.1.2.1.1.1.0"} );
if (result == null)
{
	Console.WriteLine("No results received.");
	return;
}
 
foreach (KeyValuePair kvp in result)
{
	Console.WriteLine("{0}: {1} {2}", kvp.Key.ToString(), 
                          SnmpConstants.GetTypeName(kvp.Value.Type), 
                          kvp.Value.ToString());
}

On my laptop, result looks like this:

1.3.6.1.2.1.1.1.0: OctetString "Dual core Intel notebook"

Obviously, you can request multiple values in a single request just by adding them to the SimpleSnmp.Get() OID string array.

Methods are also available for GetNext:

Dictionary<Oid,AsnType> result = snmp.GetNext(SnmpVersion.Ver2, 
                                                    new string[] { 
                                                        ".1.3.6.1.2.1.1.1", 
                                                        ".1.3.6.1.2.1.1.2"
                                                    } );

GetBulk:

Dictionary<Oid,AsnType> result = snmp.GetBulk(new string[] { ".1.3.6.1.2", ".1.3.6.1.3"} );

Set:

Dictionary<Oid,AsnType> result = snmp.Set(SnmpVersion.Ver2, 
                                                    new Vb[] { 
                                                    new Vb(new Oid(".1.3.6.1.2.1.1.1.0"), 
                                                           new OctetString("New sysDescr.0")
                                                    } );

and Walk:

Dictionary result = snmp.Walk(SnmpVersion.Ver2, ".1.3.6.1.2.1.1.1");

SimpleSnmp.Walk uses GetBulk with SNMP version 2c. If SNMP version 1 is selected, GetNext operation is used and is considerably slower.

To control how GetBulk and SNMP version 2 walk performs, you can set SimpleSnmp.NonRepeaters and SimpleSnmp.MaxRepetitions values to adjust how GetBulk calls are made.

Prior to using the class, you can check the status of the SimpleSnmp.Valid property to verify class is in correctly initialized. This property will validate that agent name was correctly resolved to an IP address and that community name is set.