The Nstrument .Net Snmp LibraryNstrument's Snmp Library for .Net includes a Snmp Control, a Mib Parser and a Trap Receiver. The library also includes a C# unit test project (excellent for examples) and a Wpf Mib Browser with full source code. Currently, there is support for v1 and v2c. Nstrument offers a run time royalty free license model with a 30 day Trial Period. Purchases include 1 year free maintenance which gets you fee upgrades. SnmpService and SnmpSap
The SnmpService provides synchronous and asynchronous implementations for the following SNMP operations: Get, GetNext, GetBulk, Set, TableWalk, and Walk. The SnmpSap (ServiceAccessPoint) encapsulates all of the parameters that define a specific SNMP agent endpoint (IP Address, Version, Credentials, and Timeout). You can create a SnmpSap by calling the CreateSap function on the SnmpService. The following example illustrates how to use the SnmpService and SnmpSap to perform basic operations: public void BasicOperations() { using (SnmpService snmpService = new SnmpService()) {
//Define some objectIdentifers that were going to use. Variable sysDescr = new Variable("1.3.6.1.2.1.1.1.0"); Variable sysName = new Variable("1.3.6.1.2.1.1.5.0"); //To set a variable you have to know it's type. Variable sysContact = new OctetStringVariable("1.3.6.1.2.1.1.4.0", "acme, inc"); Variable ifDescr = new Variable("1.3.6.1.2.1.2.2.1.2");
//Define the hosts we going to communicate with. List<SnmpSap> hosts = new List<SnmpSap>(); hosts.Add(snmpService.CreateSap("192.168.1.203", "public", "private")); hosts.Add(snmpService.CreateSap("192.168.1.203", "public", "private"));
foreach (SnmpSap host in hosts) { //Get the host name and description Response getResponse = host.Get(new Collection<Variable> { sysName, sysDescr } ); string hostName = getResponse[sysName].ToString(); string hostDescription = getResponse[sysDescr].ToString();
//Set the contact information in the host. Response setResponse = host.Set(sysContact);
//Get all of the interface descriptions on this host. MibTable table = host.TableWalk(ifDescr); foreach (MibTableRow row in table.Rows) { string interfaceDescription = row[ifDescr].ToString(); } } } } MibService
The MibService converts SNMP Mib files into an object graph. This object graph consists of 6 different node types Node, MibTable, MibTableRow, Variable, Notification, and Group. These node objects can be used in conjunction with the SnmpSap to fetch their instances from SNMP agents. The following example shows how to use the MibService to fetch the ifTable from a SnmpSap. public void GetTable() { //Create SnmpService and a Service Access Point. SnmpService snmpService = new SnmpService(); SnmpSap snmpSap = SnmpService.CreateSap("192.168.1.203", "public");
//Create MibService and load the RFC1213 Mib. MibService mibService = new MibService(MibsPath); Module module = mibService.Load("RFC1213-MIB");
//Get the 'ifTable' from the module and fetch it from the SnmpSap MibTable ifTable = module.GetNodeByName<MibTable>("ifTable"); MibTable fetchedTable = ifTable.Get(snmpSap);
//Iterate each row in the table and show it's variables foreach (MibTableRow row in fetchedTable.Rows) { foreach (Variable variable in row.Variables) { Console.WriteLine(variable.Name " = " variable.ValueAsString); } } } LINQ to SNMP using strongly typed objects. The MibService is also capable of turning the loaded object graph into a set of strongly typed classes. These classes can be used to make your code easier to read and maintain. In addition they can be used with LINQ. The following examples show how the strongly typed classes can be used: public void UsingStaticVariables() { //Create SnmpService and a Service Access Point. SnmpService snmpService = new SnmpService(); SnmpSap snmpSap = snmpService.CreateSap("192.168.1.203", "public");
//Get Variables without having to use object identifiers. Response response = snmpSap.Get( new Collection<Variable> { SystemNode.SysNameVariable, Interfaces.IfNumberVariable }); string systemName = response[SystemNode.SysNameVariable].ValueAsString; string totInterface = response[Interfaces.IfNumberVariable].ValueAsString; } public void LinqQuery() { //Create SnmpService and a Service Access Point. SnmpService snmpService = new SnmpService(); SnmpSap snmpSap = snmpService.CreateSap("192.168.1.203", "public");
//Query MibTable objects using LINQ. IfTable ifTable = new IfTable(snmpSap); var interfaces = from inter in ifTable where inter.IfType != IfType.SoftwareLoopback select inter; foreach (IfEntry entry in interfaces) { Console.WriteLine(entry.IfDescr " Speed = " entry.IfSpeed); } } Trap Service.
The TrapService listens for traps sent from remote agents. Traps sent by remote agents are raised as events. public void BasicTrapFunctions() { using (TrapService trapService = new
TrapService()) { //Listen for
traps trapService.TrapReceived += new TrapReceivedEventHandler(trapService_TrapReceived); //Send trap
to ourselves. SnmpService
snmpService = new SnmpService(); SnmpSap
snmpSap = snmpService.CreateSap(IpAddress, ReadCommunity); snmpSap.SendTrap( new Collection<Variable>() { new
OctetStringVariable("1.3.6.1.2.1.1.6.0",
"test") }); System.Threading.Thread.Sleep(10000); } } void trapService_TrapReceived(object sender, TrapEventArgs e) { string
trapOid = e.Trap.TrapObjectIdentifier; string
value = e.Trap["1.3.6.1.2.1.1.6.0"].ValueAsString; } |
|