Using the MibService it is possible to generate strongly typed objects for the nodes that are loaded into the MibService. This is done by calling the GenerateCode method and passing the following arguments:
void GenerateCode(Node node, string namespaceName, string outputPath)
node – a node to be converted into code, all child nodes of this node will be generated too.
namespace - the namespace the generated code will be created in.
outputPath – the path the generated code will be saved at.
The code generated by the MibService has the following format each node encountered during generation is turned into a class; its name is the name of the node. If the name of the node is a reserved word in C# then ‘Node’ is appended to it. Each child Node, Variable, and Table is exposed as a property returning the type created for it.
1: public class NodeName : Node {
2: //Child nodes are exposed as properties returning their type.
3: public ChildNode ChildNode { get; }
4: //Instance definition of Variable
5: public string scalar { get; }
6: //Static definition of Variable
7: public static ObjectIdentifierVariable scalarVariable { get; }
8: //Tables are exposed as properties returning their type.
9: public MibTableNode MibTableNode { get; }
10: }
11:
12: public class ChildNode : Node {
13: //May contain other Nodes,Variables, or Tables
14: }
15:
16: public class MibTableNode : MibTable, IEnumerable<MibTableRowNode> {
17: //MibTable.Rows is hidden with a new strongly typed signature.
18: new public Collection<MibTableRowNode> Rows { get; }
19: }
20:
21: public class MibTableRowNode : MibTableRow {
22: //Instance definition of Variable
23: public EnumVar enumVar { get; }
24: //Static definition of Variable
25: public static EnumerationVariable scalarVariable { get; }
26: }
27: public enum EnumVar { Option1 = 1, Option2 = 2 }
The strongly typed MibTables hide their base classes Rows property with a signature returning a collection of the generated Mib TableRows class. Each generated MibTable also implements IEnumerable where T is the generated MibTableRows class. EnumerationVariables are turned into enumerations and properties that use the enumeration return the generated type for the enum.
Strongly Typed Objects Usage Scenarios:
The Nstrument library ships with RFC1213 and HOST-RESOURCES Mibs already generated. They are located in the ‘Nstrument.Snmp.Mib.Compiled’ namespace. You can use these generated classes in a couple of different ways:
Using Static Variable Definitions:
The static variables that were defined during the code generation process were defined to support the following usage scenario:
1: public void UsingStaticVariables() {
2: SnmpService snmpService = new SnmpService();
3: SnmpSap snmpSap = snmpService.CreateSap("192.168.1.203", "public");
4: Response response = snmpSap.Get(
5: new Collection<Variable> {
6: SystemNode.SysNameVariable,
7: Interfaces.IfNumberVariable
8: });
9: string systemName = response[SystemNode.SysNameVariable].ValueAsString;
10: string totInterface = response[Interfaces.IfNumberVariable].ValueAsString;
11: }
Writing your code using this format has the following advantages:
· No object identifiers are defined as strings “1.3.6.1.2.1.2.1.0“. The code is easier to read and understand its function.
· It’s efficient, were only fetching the object identifiers that were going to use.
Using Strongly Typed Tables:
The generated tables can be used as in the following example:
1: public void StronglyTypedTables() {
2: SnmpService snmpService = new SnmpService();
3: SnmpSap snmpSap = SnmpService.CreateSap("192.168.1.203", "public");
4:
5: Mib2 mib2 = new Mib2(snmpSap);
6: foreach (IfEntry ifEntry in mib2.Interfaces.IfTable) {
7: if (ifEntry.IfType != IfType.SoftwareLoopback) {
8: Console.WriteLine(ifEntry.IfDescr + " Speed = " + ifEntry.IfSpeed);
9: }
10: }
11: }
Note above I did not create an instance of a IfTable but instead a Mib2 object and accessed IFTable thru it. This can be useful if you’re accessing multiple objects in the Mib as you only have to declare one variable. Also you can use Intellisence to help find what you are looking for in the Mib. Another advantage of using the generated code is the use of the EnumerationVariable in the following line:
if (ifEntry.IfType != IfType.SoftwareLoopback)
Anyone reading this code won’t be wondering under what condition we will be branching.
Using LINQ to SNMP:
Because the generated tables implement IEnumerable they can be used in LINQ queries. The following example is identical to the last one only it uses LINQ:
1: public void BasicLinqQuery() {
2: SnmpService snmpService = new SnmpService();
3: SnmpSap snmpSap = snmpService.CreateSap("192.168.1.203", "public");
4:
5: IfTable ifTable = new IfTable(snmpSap);
6: var interfaces = from inter in ifTable
7: where inter.IfType != IfType.SoftwareLoopback
8: select inter;
9: foreach (IfEntry entry in interfaces) {
10: Console.WriteLine(entry.IfDescr + " Speed = " + entry.IfSpeed);
11: }
12: }
13:
From the examples shown in this post you can see that using the classes generated by the MibService in your Snmp code have the following advantages:
· Code is easier to read and maintain.
· Values can be accessed as their actual data types as opposed to strings.
· Can be used with LINQ.
Feedback appreciated,
Craig R.