Reader Level:
ARTICLE

An SNMP Library for .NET Framework

Posted by Malcolm Crowe Articles | Networking September 10, 2002
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.
  • 0
  • 0
  • 114176
Download Files:
 

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 1.3.6.1.2.1.1.4.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));
Console.WriteLine(mi.Value.ToString());

This code is explained as follows.

1. You will need a an instance of Mgmt() to encode and decode IODs:

new RFC1157.Mgmt()

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

new ManagerSession(localhost,public)

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: 

new
ManagerItem(sess,oid)

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

Def corresponds to a single MIB node definition.

Properties

string name The readable name of this node
uint[] path Read-only. The OID of this node

Methods

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.

Mgmt

Constructor

new Mgmt() Create a new Mgmt instance, inirialised using all the mib definitions in the system folder.

Properties

Def def The internet node in the MIB (1.3.6.)

Methods

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

ManagerItem

Constructor

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.

Properties

uint[] Oid Read-only. The Oid found in the MIB.
X690.Universal Value Read-only. The Value returned in ASN-1 format.

ManagerSession

Constructor

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.

Properties

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.

ManagerSubTree

Constructor

new ManagerSubTree(ManagerSession,uint[]) Create a new ManagerSubTree from the given ManagerSession and OID

Properties

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[0],..,ms[ms.Length-1] are valid.

Method

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.

SnmpBER

Implements the encoding rules for the additional ASN.1 types defined for SNMP. Subclasses X690.Universal.

SnmpTag

Implements the list of tags for the additional ASN.1 types defined for SNMP. Subclasses X690.BERTag.

SnmpType

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.

BER

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.

BERTag

A subclass of BER which specifies the rules for encoding the tag (type component) in an encoding.

BERType

Enumeration: the ASN.1 type classes Universal, Application, Context, and Private.

BitSet

A utility class for handling bits. The .NET Framework contains a BitArray which aims to do something similar.

Integer

Implements the universal integer class (arbitrary precision), and provides automatic conversion to various standard types in the .NET framework such as short, ulong etc.

Real

Implements the universal real class (arbitrary precision), and provides automatic conversion to various standard types in the .NET framework such as float, double.

Universal

A subclass of BER which implements the encoding of the standard Universal types, including Integer and Real.

UniversalType

Enumerates all the Universal types defined in X690.

COMMENT USING

Trending up