Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

SNMP library

0.00/5 (No votes)
20 Apr 2006 42  
Make SNMP request to get or set value on your computer, CISCO server, server in general, appliance,...

Introduction

Dear friends, ladies and gentleman welcome to the fabulous world of SNMP... With SNMP you can check many status of a server, switch, computer or whatever, you can modify values or parameters of that status. So a lot of thing very interesting...

Get value or set value by an SNMP request. How easy it should be, how easy I though it would be. With linux you can use the command snmpget or snmpset and you make the query and get your answer in 5 seconds.

I wanted the same with windows or more precisely I wanted to do it from a C# code. The idea was to create a very simple interface (dll) to use it in many applications. I didn't find any library easy to use, I didn't find exactly what I want.

So here it is... I create to project : one for the library-dll and another to test this dll and to show you how it's work.

First look !

Console.WriteLine("test simple get");
Console.WriteLine("--------");


//Here is the point

SNMPObject s = new SNMPObject("1.3.6.1.2.1.1.5.0");
//With this OID you can get the name 

//of the computer/server you request


string myValue = s.getSimpleValue(new SNMPAgent("127.0.0.1"));
//Look on my computer


Console.WriteLine("My value is : " + myValue);

The result is :
Test simple get
--------------
My value is MyComputer-One

In two lines you have what you want...

Rewind...

What do we need... 2 things + 1 optional thing

The SNMPAgent : It's the server/computer requested. This server is defined by 2 variables : the IP address and the community string which is "public" by default. The community string is like a password.

The SNMPObject : It's the request itself. This object is defined by an OID. An OID is a series of number separated by a dot which describe the "place" where lives a kind of value (ex : name of computer = 1.3.6.1.2.1.1.5.0).

The Mib(Optional) : This object make the link between the OID and what it is. If you already know the OID you want, you don't need the Mib. But if you want to know for example the load of the CPU and do not know the OID, you need the Mib to know it. It's similar as a DNS with IP address.

Test and explanation:

Tester

//LOAD all the MIB found into the directory c:\windows\System32\****************

    Mib myMib = new Mib();
  Console.WriteLine("**************Loading MIB's files**************");
  myMib.loadDirectoryMib(Environment.GetFolderPath(
                Environment.SpecialFolder.System));
//****************************************************************************



 //**************DISPLAY THE loading MIB************************

  //myMib.walk();

  //*************************************************************



 //*************INIT SNMP AGENT*********************

  SNMPAgent myAgent = new SNMPAgent("127.0.0.1","public","public");

  //if you do that please active the snmp capabilities

  //into your compture : "Add/remove windows component"

  //Becarefull => a firewall that stop traffic from localhost to localhost

  //will prevent this test to work. 

  //- "Management and monitoring tools"

  //************INIT SNMP OBJECT*********************

  SNMPObject myRequest = new SNMPObject("1.3.6.1.2.1.1.5.0",myMib);
  Console.WriteLine();
  Console.WriteLine("***************Make the request***************");
  Console.WriteLine("I am looking for the value : " + 
                            myRequest.getFullName());
  Console.WriteLine("My value is : " + myRequest.getSimpleValue(myAgent));
  Console.WriteLine("The type of my value is : " + myRequest.getType());
  Console.WriteLine("The description of my value is : " + 
                             myRequest.getDescription());

 //**********SOME OTHER TEST**********************

  testMultiGet();  
  testWalk();
  testSet();

  Console.ReadLine();

MIB loading

The first part of the code loads different mib files, usually on your computer in c:\windows\System32. As you should see all the files are read without any problem.
If you want you can walk into that mib to see all the value-OID-description found in the mib's files (by making myMib.walk()).
My experience shows me that sometimes a mib file cannot be loaded because the parser is not enough good to understand it. But most of the times there will be no problem.

When you create the object "Mib", it loads the files " SNMPv2-SMI.mib" which is hardcoded and is the base of all mib.
Remember you don't need to create an object Mib to make SNMP request, it's just a link between the OID, his name and his description.

Get value

This is the simplest thing you can do. First you need to defined a SNMP agent, then an SNMPObject and finally you can get the value and the type of this value.

If you give in argument, when you create the SNMPObject, a Mib object, you can also get the description and the full name of the OID.
This is the point I never found in any free source code in c#: Mix an MIB parser and an SNMP requester...

You can also get multiple value in one shot. See inside the procedure testMultiGet().

Walk

If you can to launch an SNMP request on all OID starting by "1.3.6.1.2.1" just launch the command : myAgent.walk(new SNMPObject("1.3.6.1.2.1"))

Set

The set command is the last thing you can do. Be sure that you have the right to make the command on the given OID.
In this example the set command won't work because you can not change the name of your computer by an SNMP request.

Result

Here is what you should get after

**************Loading MIB's files**************
  Load C:\WINDOWS\System32\accserv.mib
  Load C:\WINDOWS\System32\authserv.mib
  Load C:\WINDOWS\System32\dhcp.mib
  Load C:\WINDOWS\System32\ftp.mib
  Load C:\WINDOWS\System32\hostmib.mib
  Load C:\WINDOWS\System32\http.mib
  Load C:\WINDOWS\System32\inetsrv.mib
  Load C:\WINDOWS\System32\ipforwd.mib
  Load C:\WINDOWS\System32\lmmib2.mib
  Load C:\WINDOWS\System32\mcastmib.mib
  Load C:\WINDOWS\System32\mib_ii.mib
  Load C:\WINDOWS\System32\mipx.mib
  Load C:\WINDOWS\System32\mripsap.mib
  Load C:\WINDOWS\System32\msft.mib
  Load C:\WINDOWS\System32\msipbtp.mib
  Load C:\WINDOWS\System32\msiprip2.mib
  Load C:\WINDOWS\System32\nipx.mib
  Load C:\WINDOWS\System32\smi.mib
  Load C:\WINDOWS\System32\wfospf.mib
  Load C:\WINDOWS\System32\wins.mib

***************Make the request***************
  I am looking for the value : mgmt.mib-2.system.sysName.0
  My value is : MYCOMPUTER-ONE
  The type of my value is : OctetString
  The description of my value is : "An administratively-assigned 
  name for this managed node. By convention, this is the node's
  fully-qualified domain name."


  test walk
  ---------

  Rentre dans le Walk
  []: 1.3.6.1.2.1.2.2.1.1.1,1,Int
  []: 1.3.6.1.2.1.2.2.1.1.2,2,Int
  []: 1.3.6.1.2.1.2.2.1.2.1,MS TCP Loopback interface ,OctetString
  []: 1.3.6.1.2.1.2.2.1.2.2,3Com EtherLink XL 10/100 PCI 
          TX NIC (3C905B-TX) - Packet Scheduler Miniport, OctetString
  []: 1.3.6.1.2.1.2.2.1.3.1,24,Int
  []: 1.3.6.1.2.1.2.2.1.3.2,6,Int
  []: 1.3.6.1.2.1.2.2.1.4.1,1520,Int
  []: 1.3.6.1.2.1.2.2.1.4.2,1500,Int
  []: 1.3.6.1.2.1.2.2.1.5.1,10000000,Gauge32
  []: 1.3.6.1.2.1.2.2.1.5.2,100000000,Gauge32
  []: 1.3.6.1.2.1.2.2.1.6.1,,OctetString
  []: 1.3.6.1.2.1.2.2.1.6.2, P♦r☺?,OctetString
  []: 1.3.6.1.2.1.2.2.1.7.1,1,Int
  []: 1.3.6.1.2.1.2.2.1.7.2,1,Int
  []: 1.3.6.1.2.1.2.2.1.8.1,1,Int
  []: 1.3.6.1.2.1.2.2.1.8.2,1,Int
  []: 1.3.6.1.2.1.2.2.1.9.1,0:00:00.00,TimeTicks
  []: 1.3.6.1.2.1.2.2.1.9.2,0:00:00.00,TimeTicks
  []: 1.3.6.1.2.1.2.2.1.10.1,298230716,Counter32
  []: 1.3.6.1.2.1.2.2.1.10.2,363462946,Counter32
  []: 1.3.6.1.2.1.2.2.1.11.1,3525581,Counter32
  []: 1.3.6.1.2.1.2.2.1.11.2,16924899,Counter32
  []: 1.3.6.1.2.1.2.2.1.12.1,0,Counter32
  []: 1.3.6.1.2.1.2.2.1.12.2,1091948,Counter32
  []: 1.3.6.1.2.1.2.2.1.13.1,0,Counter32
  []: 1.3.6.1.2.1.2.2.1.13.2,0,Counter32
  []: 1.3.6.1.2.1.2.2.1.14.1,0,Counter32
  []: 1.3.6.1.2.1.2.2.1.14.2,0,Counter32
  []: 1.3.6.1.2.1.2.2.1.15.1,0,Counter32
  []: 1.3.6.1.2.1.2.2.1.15.2,112810,Counter32
  []: 1.3.6.1.2.1.2.2.1.16.1,298230716,Counter32
  []: 1.3.6.1.2.1.2.2.1.16.2,3148083931,Counter32
  []: 1.3.6.1.2.1.2.2.1.17.1,3516517,Counter32
  []: 1.3.6.1.2.1.2.2.1.17.2,14109746,Counter32
  []: 1.3.6.1.2.1.2.2.1.18.1,0,Counter32
  []: 1.3.6.1.2.1.2.2.1.18.2,18964,Counter32
  []: 1.3.6.1.2.1.2.2.1.19.1,0,Counter32
  []: 1.3.6.1.2.1.2.2.1.19.2,0,Counter32
  []: 1.3.6.1.2.1.2.2.1.20.1,0,Counter32
  []: 1.3.6.1.2.1.2.2.1.20.2,0,Counter32
  []: 1.3.6.1.2.1.2.2.1.21.1,0,Gauge32
  []: 1.3.6.1.2.1.2.2.1.21.2,4294967258,Gauge32
  []: 1.3.6.1.2.1.2.2.1.22.1,0.0,Oid
  []: 1.3.6.1.2.1.2.2.1.22.2,0.0,Oid

  test multi get
  --------------
  oid : 1.3.6.1.2.1.1.5.0
  value : MYCOMPUTER-ONE
  type : OctetString
  oid : 1.3.6.1.2.1.2.2.1.16.1
  value : 298230716
  type : Counter32

  test Set
  --------
  Read the value before the modification
  oid : 1.3.6.1.2.1.1.5.0
  value : MYCOMPUTER-ONE
  type : OctetString

  Set the value
  Error: SNMP: Variable does not exist; status: NoSuchName; index: 0
  *** SNMP protocol error while processing <op>:
  SNMP error status: NoSuchName
  SNMP error index: 0
  Error: SNMP: Variable does not exist; status: NoSuchName; index: 0

  Read the value after the modification
  oid : 1.3.6.1.2.1.1.5.0
  value : MYCOMPUTER-ONE
  type : OctetString

Points of Interest

Firstly I needed the possibilities to make some SNMP request. After using it, I saw the billion of possibilities SNMP can give me. So now I use SNMP every days in a pleiad of projects.

I hope you'll get fun with SNMP as I do.

History

I based this dll from 4 other dll dispatched in 2 parts :

SnmpComp and tableReader with the namespace : Org.Snmp.Snmp_pp. This dll allow me to make any snmp request I want it. It's quite difficult to understand at first, it's difficult to compile but when you are in... well, it should be ok. Here is the web site where you can find it : http://www.republika.pl/maom_onet/snmp/snmp_ppnet/. Please note that I modified the original dll to adapt it to my need, to the current project.

mib and tools with the namespace : RFC1157 . This dll allow me to read a MIB files, to parse it and finaly to make the link between an OID, its type, its description and its fullname. It was developped by Malcolm Crowe and you can find the code : http://cis.paisley.ac.uk/crow-ci0/#Research. Take care that some of mib files cannot be read by his parser...

This project was born with these two priceless sources...

Feel free to send feed back, I'll correct all the error you may see...

- 8 February 2006 : release version 1.0 of SNMPDLL
- 20 March 2006 : add the source code of snmp_ppnet (for the last version see http://www.republika.pl/maom_onet/snmp/snmp_ppnet/). This code source is only for people who want to see the code of SNMPComp.dll used by SNMPDLL.dll

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here