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

 
GeneralWhy re-invent the wheel Pin
Socrates#22-May-11 23:18
Socrates#22-May-11 23:18 
GeneralRe: Why re-invent the wheel Pin
Lex Li4-Jun-11 15:57
professionalLex Li4-Jun-11 15:57 
GeneralCheck if OID exists? Pin
ccoho5-May-11 10:52
ccoho5-May-11 10:52 
QuestionHow to get community string only? Pin
sangeetamanna200828-Mar-11 22:52
sangeetamanna200828-Mar-11 22:52 
AnswerRe: How to get community string only? Pin
Lex Li2-Apr-11 0:24
professionalLex Li2-Apr-11 0:24 
News#SNMP 6 is available Pin
Lex Li12-Nov-10 14:31
professionalLex Li12-Nov-10 14:31 
GeneralMy vote of 5 Pin
Ch00sen12-Nov-10 11:50
Ch00sen12-Nov-10 11:50 
News#SNMP 5.0 is released (SNMP library and tools for .NET and Mono) [modified] Pin
Lex Li30-May-10 0:02
professionalLex Li30-May-10 0:02 
A lot of new things are added in this brand new release,

http://sharpsnmplib.codeplex.com/[^]

Free and open source with many features you want,

1. SNMP v1, v2, and v3.
2. MIB browser, compiler, and agent.
3. Tested on .NET/Windows, and Mono/openSUSE. But theoretically more operating systems are supported, such as Mono/Mac OS X, Mono/Solaris and so on.
#SNMP, http://sharpsnmplib.codeplex.com
modified on Saturday, July 3, 2010 10:48 PM

GeneralRe: #SNMP 5.0 is released (SNMP library and tools for .NET and Mono) Pin
Tavox0111-Jun-10 8:07
Tavox0111-Jun-10 8:07 
Generalsnmp walk on asp.net Pin
Dragon_Heart4-May-10 1:53
Dragon_Heart4-May-10 1:53 
GeneralSNMP Library OID Problem Pin
Agah Burak DEMİRKAN3-May-10 22:56
Agah Burak DEMİRKAN3-May-10 22:56 
General#SNMP Suite 4 is released Pin
Lex Li23-Mar-10 16:55
professionalLex Li23-Mar-10 16:55 
QuestionHow to receive this trap infomation? [modified] Pin
cagy1-Dec-09 21:29
cagy1-Dec-09 21:29 
AnswerRe: How to receive this trap infomation? Pin
Lex Li5-Dec-09 16:02
professionalLex Li5-Dec-09 16:02 
GeneralMy vote of 1 Pin
Duraipandian22-Oct-09 22:53
Duraipandian22-Oct-09 22:53 
GeneralRe: My vote of 1 Pin
Lex Li28-Oct-09 12:38
professionalLex Li28-Oct-09 12:38 
GeneralRe: My vote of 1 Pin
CoderOnline2-Nov-09 19:51
CoderOnline2-Nov-09 19:51 
GeneralRe: My vote of 1 Pin
CoderOnline2-Nov-09 19:48
CoderOnline2-Nov-09 19:48 
GeneralRe: My vote of 1 Pin
zitun3-Nov-09 23:05
zitun3-Nov-09 23:05 
General[Message Deleted] Pin
merry368819-Oct-09 22:04
merry368819-Oct-09 22:04 
GeneralRe: How to set MAC address Pin
Lex Li21-Oct-09 0:07
professionalLex Li21-Oct-09 0:07 
GeneralWindows 7 Pin
Carlos Flávio Barreto Ferreira de Souza24-Sep-09 4:49
professionalCarlos Flávio Barreto Ferreira de Souza24-Sep-09 4:49 
GeneralWant SNMP v3 support? Try #SNMP Suite 3 Pin
Lex Li30-Aug-09 2:12
professionalLex Li30-Aug-09 2:12 
QuestionHow are MIB files generated? Pin
Jun Du13-Aug-09 10:13
Jun Du13-Aug-09 10:13 
AnswerRe: How are MIB files generated? Pin
Lex Li30-Aug-09 2:17
professionalLex Li30-Aug-09 2:17 

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.