Using the MibService

by Administrator 29. September 2008 15:59

The MibService performs two functions:
Mib parsing - converts Mib files into an object graph.  This graph consists of 6 different object types (Node, MibTable, MibTableRow, Variable, Notification, and Group) all of these classes derive from NodeBase.
Strongly typed class generator – creates C# classes that contain properties representing the actual Snmp variables.

Loading Mib files:

To use the MibService you have to create an instance and pass it a directory where the Mibs are located.  Notice I said ‘Mibs’, Mib files can contain references to other Mib files.  In order to load a Mib all references to included Mibs need to be loaded first.  A FileNotFoundException will be thrown if a required Mib cannot be located.  The name of the missing Mib can be read from the Exception.Message property.   Mibs are loaded into the MibService by calling the Load method and passing the name of the Mib to load.  Each loaded Mib is turned into a graph of objects.  This graph consists of 6 node types:

Node – can contain other Nodes, Tables and Variables 
Table – defines a table.
TableRow – defines the columns in a table.
Variable – defines a named symbol.
Notification – defines a trap which contains a collection of Variables.
Group – defines a named collection of Variables

The MibService exposes the graph of objects it has loaded thru the Nodes collection and the following methods:

   1: public NodeBase GetNodeByName(string name)
   2: public T GetNodeByName(string name)
   3:  
   4: public NodeBase GetNodeByOid(string objectIdentifier)
   5: public T GetNodeByOid(string objectIdentifier)
   6:  
   7: public Collection GetNodesByType()

These methods exist on the MibService, Module, and NodeBase objects so you can begin your search for specific nodes from different scopes depending on your needs.  It’s worth noting that the GetNodeByName and GetNodeByOid functions return null if the node your searching for cannot be found.  You can load multiple Mibs into the Mib service by repeatedly calling load with a new Mib Name.  Each loaded Mib is inserted into the existing graph of objects contained by the MibService.  The following diagram depicts the relationships between the MibService, Module and the Nodes.

MibServiceRelationships]

Working with loaded modules:

The following example shows how to load the RFC1213 mib and dump all of it’s nodes in a hierarctical fashion.   Using the children collection on NodeBase we can recursivly call DumpNode for each child node.

   1: public void DumpTree() {
   2:     string mibsPath = @"C:\Program Files\Nstrument\SnmpLibrary\Mibs"; 
   3:     MibService mibService = new MibService(mibsPath);
   4:     Module module = mibService.Load("RFC1213-MIB");
   5:     DumpNode(module.Nodes[0], 0);
   6: }
   7:  
   8:  void DumpNode(NodeBase node, int depth) {
   9:      string padding = string.Empty;
  10:      for (int idx = 0; idx < depth; idx++) { padding += "\t"; }
  11:      Console.WriteLine(padding + node.Name);
  12:  
  13:      foreach (NodeBase childNode in node.Children) {
  14:          DumpNode(childNode, depth + 1);
  15:      }
  16:  }

MibService & SnmpService Integration:

Once you have a Mib module loaded the next logical step would be to fetch some of the objects in the module from a SNMP agent.  You can accomplish this using the Get method on (Node, MibTable, and Variable) nodes.  This method takes in a single parameter an SnmpSap that indicates the agent to fetch the data from.  The following example illustrats how you can fetch a MibTable from a SnmpSap:

   1: public void GetTable() {
   2:     //Create SnmpService and a Service Access Point.
   3:     SnmpService snmpService = new SnmpService();
   4:     SnmpSap snmpSap = SnmpService.CreateSap("192.168.1.203", "public");
   5:  
   6:     //Create MibService and load the RFC1213 Mib.
   7:     MibService mibService = new MibService(MibsPath);
   8:     Module module = mibService.Load("RFC1213-MIB");
   9:  
  10:     //Get the 'ifTable' from the module and fetch it from the SnmpSap
  11:     MibTable ifTable = module.GetNodeByName<MibTable>("ifTable");
  12:     MibTable fetchedTable = ifTable.Get(snmpSap);
  13:  
  14:     //Iterate each row in the table and show it's variables
  15:     foreach (MibTableRow row in fetchedTable.Rows) {
  16:         foreach (Variable var in row.Variables) {
  17:             Console.WriteLine(var.Name + “ = “ + var.ValueAsString);
  18:         }
  19:     }
  20: }

In the next installment of this series I will show you how to use the MibService to generate stronly typed objects to represent the nodes loaded into the MibService.  Using the stronly typed objects gets you out of the business of typing object identifiers.  In addition we will use these generated classes to perform Linq to Snmp.


Feedback Appreciated,
Craig R.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Comments

Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET 1.4.5.0

Month List