From this article you learn how SNMP operations can be done by consuming the open source SNMP API. A question then is what values MIB documents provide, as they are said to be an important part of SNMP protocol but not seem to be utilized anywhere if we solely use #SNMP Library.
Well, a rough answer is MIB documents mean everything,
Thus, a MIB specific library such as SharpSnmpPro.Mib can help build a much more powerful SNMP manager.
![mib.png mib.png]()
Now let us see a few examples.
SimpleObjectRegistry is a class that holds metadata in memory. It can be used to import metadata generated via Parser.Compile. ErrorRegistry is the container of errors and warnings. When SimpleObjectRegistry.Refresh is called, all errors and warnings can be found in the ErrorRegistry instance, to help you identify why some MIB documents cannot be compiled or imported.
We can see that if we are looking for SNMPv2-MIB::sysDescr (whose OID is 1.3.6.1.2.1.1.1), we can use SimpleObjectRegistry.Tree.Find method to locate the Definition instance. Each such instance contains one or more IEntity instances to match their entity definition in MIB documents.
From Definition.DisplayEntity we can get one of the entities, and check its properties such as IEntity.DescriptionFormatted, IEntity.Status, and IEntity.Reference.
Since SNMPv2-MIB::sysDescr is an OBJECT-TYPE macro entity, we can further cast it to IObjectTypeMacro to access more properties, such as IObjectTypeMacro.MibAccess and IObjectTypeMacro.BaseSyntax. It is obvious that the data type of SNMPv2-MIB::sysDescr is OCTET STRING.
There are of course other properties you can review, which are documented online at http://help.sharpsnmp.com. Note that our trial version limits which attributes you can see, while the full version does not have such limitations.
We can easily test if the data is valid for SNMPv2-MIB::sysDescr. Note that our trial version does only support data validation against a limited set of default types (defined in core MIB documents), while the full version supports even custom types such as BITS, CiscoRowOperStatus, and CiscoPort.
Well, a rough answer is MIB documents mean everything,
- They tell what each object identifiers (OID) mean.
- They tell which OID is for a table, a row, and a column.
- They tell which kind of data we should expect for an object, OCTET STRING or any other valid types.
Thus, a MIB specific library such as SharpSnmpPro.Mib can help build a much more powerful SNMP manager.
Now let us see a few examples.
MIB Document Compilation
The following code shows how to compile several essential MIB documents and load the metadata into memory,var registry = new SimpleObjectRegistry(); var collector = new ErrorRegistry(); registry.Import(Parser.Compile(new MemoryStream(Resources.SNMPv2_SMI), collector)); registry.Import(Parser.Compile(new MemoryStream(Resources.SNMPv2_CONF), collector)); registry.Import(Parser.Compile(new MemoryStream(Resources.SNMPv2_TC), collector)); registry.Import(Parser.Compile(new MemoryStream(Resources.SNMPv2_MIB), collector)); registry.Import(Parser.Compile(new MemoryStream(Resources.SNMPv2_TM), collector)); registry.Refresh();
SimpleObjectRegistry is a class that holds metadata in memory. It can be used to import metadata generated via Parser.Compile. ErrorRegistry is the container of errors and warnings. When SimpleObjectRegistry.Refresh is called, all errors and warnings can be found in the ErrorRegistry instance, to help you identify why some MIB documents cannot be compiled or imported.
Name/OID Translation
The translation is two-way. You can convert from names to OIDs or vice verse.const string textual = "SNMPv2-SMI::zeroDotZero"; var number = new uint[] { 0, 0 }; Assert.AreEqual(textual, registry.Translate(number)); Assert.AreEqual(number, registry.Translate(textual));
Extract Object Identifier Metadata
Once all metadata are loaded in a SimpleObjectRegistry instance we can easily extract the information for individual objects,Definition item = registry.Tree.Find("SNMPv2-MIB", "sysDescr"); IEntity entity = item.DisplayEntity; Assert.AreEqual("A textual description of the entity. This value should include the full name and version identification of the system's hardware type, software operating-system, and networking software.", entity.DescriptionFormatted()); Assert.AreEqual(EntityStatus.Current, entity.Status); Assert.AreEqual(string.Empty, entity.Reference); var obj = entity as IObjectTypeMacro; Assert.AreEqual(Access.ReadOnly, obj.MibAccess); Assert.AreEqual(SnmpType.OctetString, obj.BaseSyntax);
We can see that if we are looking for SNMPv2-MIB::sysDescr (whose OID is 1.3.6.1.2.1.1.1), we can use SimpleObjectRegistry.Tree.Find method to locate the Definition instance. Each such instance contains one or more IEntity instances to match their entity definition in MIB documents.
From Definition.DisplayEntity we can get one of the entities, and check its properties such as IEntity.DescriptionFormatted, IEntity.Status, and IEntity.Reference.
Since SNMPv2-MIB::sysDescr is an OBJECT-TYPE macro entity, we can further cast it to IObjectTypeMacro to access more properties, such as IObjectTypeMacro.MibAccess and IObjectTypeMacro.BaseSyntax. It is obvious that the data type of SNMPv2-MIB::sysDescr is OCTET STRING.
There are of course other properties you can review, which are documented online at http://help.sharpsnmp.com. Note that our trial version limits which attributes you can see, while the full version does not have such limitations.
Table Validation
With MIB documents, it is very easy to determine if an OID is a table.var table = new ObjectIdentifier(new uint[] { 1, 3, 6, 1, 2, 1, 1, 9 }); var entry = new ObjectIdentifier(new uint[] { 1, 3, 6, 1, 2, 1, 1, 9, 1 }); var unknown = new ObjectIdentifier(new uint[] { 1, 3, 6, 8, 18579, 111111 }); Assert.IsTrue(registry.ValidateTable(table)); Assert.IsFalse(registry.ValidateTable(entry)); Assert.IsFalse(registry.ValidateTable(unknown));
Data Validation
In SNMP managers or agents, it is a common need to determine if a piece of data is valid for an OID. Various constraints can be defined at MIB document level, but it is often difficult to extract that from the files. With a few lines of code you can now do thatAssert.IsTrue(registry.Verify("SNMPv2-MIB", "sysDescr", new OctetString("test"))); Assert.IsTrue(registry.Verify("SNMPv2-MIB", "sysDescr", new OctetString(string.Empty))); Assert.IsFalse(registry.Verify("SNMPv2-MIB", "sysDescr", new Integer32(2)));
We can easily test if the data is valid for SNMPv2-MIB::sysDescr. Note that our trial version does only support data validation against a limited set of default types (defined in core MIB documents), while the full version supports even custom types such as BITS, CiscoRowOperStatus, and CiscoPort.