Click here to Skip to main content
15,881,092 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

 
QuestionProject Pin
Member 1603556123-Jun-23 6:21
Member 1603556123-Jun-23 6:21 
QuestionI ran the exe and it throws timeout exception. Am I missing something Pin
Samir Joshi3-Feb-21 19:13
Samir Joshi3-Feb-21 19:13 
QuestionBad Image Format Exception was Unhandled Pin
Member 829385831-Oct-16 1:29
Member 829385831-Oct-16 1:29 
QuestionConfiguration Type of SnmpComp.dll Pin
Vimesh Shah2-Mar-15 16:59
Vimesh Shah2-Mar-15 16:59 
QuestionBuild of SnmpComp.dll Pin
Vimesh Shah26-Feb-15 1:26
Vimesh Shah26-Feb-15 1:26 
Questionnull referenceexception was unhandled Pin
Member 1143821815-Feb-15 23:09
Member 1143821815-Feb-15 23:09 
QuestionSNMP variable does not exist, NoSuchName; index: 1 Pin
carlos ariel alt2-Feb-15 8:53
carlos ariel alt2-Feb-15 8:53 
QuestionLoadmib throws exception Pin
sagar_25324-Jun-14 1:00
sagar_25324-Jun-14 1:00 
QuestionStackOverFlowexception Pin
sagar_25323-Jun-14 20:18
sagar_25323-Jun-14 20:18 
AnswerRe: StackOverFlowexception Pin
Member 1086297713-Jul-14 20:18
Member 1086297713-Jul-14 20:18 
Questionerror in conversion OctecString Pin
Member 1006039611-Jun-14 13:26
Member 1006039611-Jun-14 13:26 
QuestionError on Visual Studio 2012 Pin
Filipe D. Silva23-Oct-13 6:18
professionalFilipe D. Silva23-Oct-13 6:18 
QuestionHelp me restore Sysname Value Pin
Member 1020754414-Aug-13 1:21
Member 1020754414-Aug-13 1:21 
Questionhowever the set dosen't work? Pin
Member 1006039620-May-13 12:12
Member 1006039620-May-13 12:12 
QuestionGet ink and ERROR Pin
Itamar Tziger18-Apr-13 23:29
Itamar Tziger18-Apr-13 23:29 
QuestionWindows 7 Pin
Member 997609213-Apr-13 12:40
Member 997609213-Apr-13 12:40 
Questionexeption Pin
soheaila16-Mar-13 8:04
soheaila16-Mar-13 8:04 
AnswerRe: exeption Pin
Lex Li18-Mar-13 17:05
professionalLex Li18-Mar-13 17:05 
That's expected. The MIB compiler bundled in this library is far from perfect, so a lot of MIB documents can break it.
#SNMP, http://sharpsnmplib.codeplex.com

Questionsnmp c sharp Pin
Member 98700584-Mar-13 3:05
Member 98700584-Mar-13 3:05 
AnswerRe: snmp c sharp Pin
Lex Li18-Mar-13 17:05
professionalLex Li18-Mar-13 17:05 
GeneralRe: snmp c sharp Pin
Member 987005819-Mar-13 0:27
Member 987005819-Mar-13 0:27 
Question#SNMP Suite 7.6 is released Pin
Lex Li22-Dec-12 18:23
professionalLex Li22-Dec-12 18:23 
AnswerRe: #SNMP Suite 7.6 is released Pin
Lex Li31-Jan-13 3:54
professionalLex Li31-Jan-13 3:54 
QuestionSNMP Trap messages Pin
shagil3-Oct-12 23:00
shagil3-Oct-12 23:00 
AnswerRe: SNMP Trap messages Pin
Lex Li11-Oct-12 2:54
professionalLex Li11-Oct-12 2:54 

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.