Click here to Skip to main content
15,884,473 members
Articles / Programming Languages / C#
Article

SNMP library

Rate me:
Please Sign up or sign in to vote.
4.56/5 (62 votes)
20 Apr 2006CPOL5 min read 1.4M   57.7K   144   254
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 !

C#
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

C#
//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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Belgium Belgium
Discover www.plugandtable.com

You'll be surprise about this incredible tool !

Comments and Discussions

 
QuestionSNMP v3 Pin
oualido11-Jun-07 5:50
oualido11-Jun-07 5:50 
GeneralRe: SNMP v3 Pin
Lex Li26-Apr-08 21:18
professionalLex Li26-Apr-08 21:18 
Generalsnmp traps Pin
cool8329-May-07 22:28
cool8329-May-07 22:28 
GeneralRe: snmp traps Pin
Lex Li26-Apr-08 21:31
professionalLex Li26-Apr-08 21:31 
GeneralProblems using within a Windows Service Pin
jazper29-Apr-07 8:51
jazper29-Apr-07 8:51 
GeneralRe: Problems using within a Windows Service Pin
vovix2-May-07 4:52
vovix2-May-07 4:52 
GeneralRe: Problems using within a Windows Service Pin
jazper3-May-07 8:19
jazper3-May-07 8:19 
QuestionHow Can I Get IP address OID of a interface Pin
abdulmp22-Apr-07 4:51
abdulmp22-Apr-07 4:51 
AnswerRe: How Can I Get IP address OID of a interface Pin
abdulmp8-May-07 20:11
abdulmp8-May-07 20:11 
GeneralRe: How Can I Get IP address OID of a interface Pin
WilliamBillCollins29-Aug-07 8:30
WilliamBillCollins29-Aug-07 8:30 
GeneralSNMP GETNEXT Pin
gaay21-Apr-07 3:13
gaay21-Apr-07 3:13 
QuestionAdditional methods in mib.dll Pin
cubaner15-Apr-07 21:06
cubaner15-Apr-07 21:06 
GeneralRe: Additional methods in mib.dll Pin
gary85385614-Apr-08 0:19
gary85385614-Apr-08 0:19 
QuestionNot providing source or is it a mistake? Pin
manuelricca3-Apr-07 6:20
manuelricca3-Apr-07 6:20 
AnswerRe: Not providing source or is it a mistake? Pin
manuelricca3-Apr-07 6:22
manuelricca3-Apr-07 6:22 
Question&quot;Object reference not set to an instance of an object.&quot; problem Pin
samentu2-Apr-07 3:19
samentu2-Apr-07 3:19 
AnswerRe: &quot;Object reference not set to an instance of an object.&quot; problem Pin
zitun10-Apr-07 22:58
zitun10-Apr-07 22:58 
GeneralRe: &quot;Object reference not set to an instance of an object.&quot; problem Pin
samentu10-Apr-07 23:05
samentu10-Apr-07 23:05 
QuestionAnyone know how to catch SNMP Exception in Window app? Pin
svalley21-Mar-07 5:44
svalley21-Mar-07 5:44 
AnswerRe: Anyone know how to catch SNMP Exception in Window app? Pin
zitun10-Apr-07 22:57
zitun10-Apr-07 22:57 
GeneralRe: Anyone know how to catch SNMP Exception in Window app? Pin
svalley23-Apr-07 4:42
svalley23-Apr-07 4:42 
GeneralRe: Anyone know how to catch SNMP Exception in Window app? Pin
w.taylor7-Aug-07 2:35
w.taylor7-Aug-07 2:35 
QuestionSNMP trap Pin
adi__14-Mar-07 3:07
adi__14-Mar-07 3:07 
GeneralAreca MIBs Pin
DanielWehrle12-Mar-07 6:06
DanielWehrle12-Mar-07 6:06 
QuestionUse it under linux with MONO Pin
RobyNova1-Mar-07 4:45
RobyNova1-Mar-07 4:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.