Simple SNMP with SimpleSnmp

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.

 

 摘自:http://www.snmpsharpnet.com/node/19

posted on 2010-01-23 21:29  迷你软件  阅读(797)  评论(0编辑  收藏  举报

本网站绝大部分资源来源于Internet,本站所有作品版权归原创作者所有!!如有以下内容:章节错误、非法内容、作者署名出错、版权疑问、作品内容有违相关法律等请及时与我联系. 我将在第一时间做出响应!本站所有文章观点不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。