An SNMP Library for .NET Framework
Snmp.dll is a C# class library for the .NET framework. It has been developed on the Windows platform and may be useful on others also.
This document forms part of the documentation set for the accompanying software and describes the usable interfaces exposed by snmp.dll and mib.dll.
Snmp.dll is a C# class library for the .NET framework. It has been developed on the Windows platform and may be useful on others also. In contains two namespaces, X690 and Snmp.
The X690 namespace contains an implementation of the Basic Encoding Rules (BER) of Abstract Syntax Notation 1 (ASN.1) as specified by international standards (ISOs code for this standard in X.690) and used within Snmp.
Snmp is specified in an Internet Standard, and uses a logical Management Information Base (MIB). Implementation of at least the mib-2 (RFC1213) is mandatory for all computer systems connected to the Internet.
Mib.dll is a C# class library that handles the translation of MIB object identifiers (OID) sch as 184.108.40.206.220.127.116.11.0 to readable names such as system.sysContact.0. It also collects the help strings from the system mib files (on windows systems these are in the system folder, usually c:\windows\system32.) It contains one namespace, RFC1157.
Getting Started with Snmp.dll and Mib.dll
The simplest possible call on the Snmp protocol is to GET a single MIB entry such as system.sysContact.0 from an agent (host) such as localhost with community public. The traditional snmputil.exe would do this using the command
snmputil get ict-main-s.msroot.student.paisley.ac.uk public system.sysContact.0
To do this programmatically, proceed as follows:
RFC1157.Mgmt mib = new RFC1157.Mgmt();
ManagerSession sess=new ManagerSession(localhost,public);
ManagerItem mi=new ManagerItem(sess,mib.OID(mgmt.mib-2.system.sysContact.0));
This code is explained as follows.
1. You will need a an instance of Mgmt() to encode and decode IODs:
Store the returned value in a RFC1157.Mgmt variable, mib, say. This constructor takes some time at present, as it reads all the mib definitions it finds in the system folder.
2. Create a ManagerSession to the chosen agent and community by
Store the returned value in a ManagerSession variable, sess, say.
3. Translate the given OID into a uint:
uint oid = mib.OID(mgmt.mib-2.system.sysContact.0);
The prefix mgmt.mib-2 is added to provide a uniform starting point for the numerous mibs you will find on your system. A typical Windows-2000 system has 20 mibs defined. All of the mibs begin iso.dod.internet. followed by such things as mgmt.mib-2.
4. Create a ManagerItem to obtain the result:
Store the returned value in a ManagerItem variable, mi, say.
5. Now mi.Oid contains the actual OID returned (which can be converted to a string using mib.OID() again), and mi.Value the actual value object. You can call ToString on this value to obtain a readable version.
You can also retrieve a SubTree by using the GET/.GETNEXT combination.
The RFC1157 namespace
Def corresponds to a single MIB node definition.
|string name ||The readable name of this node |
|uint path ||Read-only. The OID of this node |
|string Kids ||The children of this node |
|Def this ||Two indexers of a Def are provided: by the OID component in binary or string form. |
|new Mgmt() ||Create a new Mgmt instance, inirialised using all the mib definitions in the system folder. |
|Def def ||The internet node in the MIB (1.3.6.) |
|string OID(uint) ||Lookup a given OID and return the cooresponding readable version. |
|uint OID(string) ||Lookup a given OID and return the corresponding binary version |
The Snmp namespace
|new ManagerItem(ManagerSession,uint) ||Create a new ManagerItem from the given ManagerSession and OID |
|new ManagerItem(ManagerSession, X690.Universal) ||Used by ManagerSubTree to construct a list of items based on information returned by the agent. |
|uint Oid ||Read-only. The Oid found in the MIB.|
|X690.Universal Value ||Read-only. The Value returned in ASN-1 format.|
|new ManagerSession(string agt,string com) ||Create a new ManagerSession for the given agent and community. The agent string is passed to DNS to resolve. |
|string agentAddress ||The string identifying the agent|
|string agentCommunity ||The string identifying the community |
Methods (Advanced methods in italics)
|void Close() ||Close the connection with the agent|
|X690.Universal Get ( params X690.Universal) ||Perform a GET according to the Snmp protocol, for the given variable bindings. |
|bool GetNext(ref X690.Universal) ||Perform a GETNEXT for the given variable binding reference (which is replaced by the returned binding). The returned value indicates success or failure. |
|X690.Universal PDU(SnmpType, params X690.Universal) ||Construct an SNMP PDU for the given list of variable bindings |
|X690.Universal VarBind(uint) ||Construct a variable binding for a given oid.|
|new ManagerSubTree(ManagerSession,uint) ||Create a new ManagerSubTree from the given ManagerSession and OID |
|int Length ||Read-only. The number of items found in the MIB.|
|ManagerItem this[ix] ||Read-only. The list of ManagerItems recovered. If the ManagerSubTree is called ms, then ms,..,ms[ms.Length-1] are valid. |
|void Refresh() ||Fetch the SubTree anew from the agent. Note that this may change the length and contents of the list of items completely.|
The remaining classes in this namespace are advanced.
Implements the encoding rules for the additional ASN.1 types defined for SNMP. Subclasses X690.Universal.
Implements the list of tags for the additional ASN.1 types defined for SNMP. Subclasses X690.BERTag.
Enumerates the ASN.1 tags defined for SNMP.
The X690 namespace
This is not a full implementation of the X690 standard, but appears to be sufficient for use here.
The BER class defined protected methods ReadByte and WriteByte. It has one public bool member Encode, which specifies whether encoding or decoding is in progress. It is used as the base class for other classes in this namespace.
A subclass of BER which specifies the rules for encoding the tag (type component) in an encoding.
Enumeration: the ASN.1 type classes Universal, Application, Context, and Private.
A utility class for handling bits. The .NET Framework contains a BitArray which aims to do something similar.
Implements the universal integer class (arbitrary precision), and provides automatic conversion to various standard types in the .NET framework such as short, ulong etc.
Implements the universal real class (arbitrary precision), and provides automatic conversion to various standard types in the .NET framework such as float, double.
A subclass of BER which implements the encoding of the standard Universal types, including Integer and Real.
Enumerates all the Universal types defined in X690.