Click here to Skip to main content
Click here to Skip to main content

An introduction to #SNMP, an Open Source SNMP implementation

By , 2 Oct 2012
 

Introduction

#SNMP (SharpSNMP) Suite is a set of free SNMP tools based on an Open Source library for developers who target Microsoft .NET/Xamarin Mono platforms. It's developed in C# and can be used for VB.NET, Delphi Prism, and many other programming languages.

This article is released under MIT license (so the sample code shown in this article is also released under MIT license). 

Background 

TCP/IP based socket programming is fully supported by .NET Framework since its day 1. However, as there is no class in .NET Framework Base Class Library that is specific for SNMP, developers have to resort to very expensive third party libraries. There were a few free solutions, but they either lacks of SNMP v3, MIB support, or other advanced features. I wrote a blog post back in 2007 which listed more information if you want to investigate.

In April 2008 I announced an open source project called #SNMP to address this problem, as I believe that via collaboration a better solution can be achieved to benefit whoever wants a good enough free solution. After more than 4 years, this library grows to its current status, with a strong enough library, plus many useful sample projects.

The project homepage is at CodePlex, and the source code is at GitHub. Note that the code base is split into different modules, and released under different licenses, as stated in KB600012

Primarily #SNMP can be used on Windows/.NET platforms, such as Windows XP/Windows Vista/Windows 7 and Windows Server OS, supporting .NET Framework 3.5/4.0/4.5. As a pure .NET based code base, it has been successfully ported to Windows CE/.NET CF, Mono, MonoTouch, as well as Mono for Android.

Using the library

The documentation package contains a SandCastle generated help file, which can be used to glance on all classes provided in #SNMP Library (Lextm.SharpSnmpLib.*.dll).

Below a few SNMP operations (GET, SET and so on) are translated to #SNMP function calls,

GET Operation

The following code shows how to send an SNMP v1 GET message to an SNMP agent located at 192.168.1.2 and query on OID 1.3.6.1.2.1.1.1.0,

var result = Messenger.Get(VersionCode.V1, 
                           new IPEndPoint(IPAddress.Parse("192.168.1.2"), 161), 
                           new OctetString("public"),
                           new List<Variable>{new Variable(new ObjectIdentifier("1.3.6.1.2.1.1.1.0"))},
                           60000);

This operation will time out if no reply is received after 60 seconds (1 minute), and throw an exception (TimeoutException). If any error occurs, an ErrorException can be caught. All #SNMP exceptions are derived from SnmpException

The result returned is a list that matches the list of Variable objects sent. The Variable in this list contains the value of the OID.

SET Operation

The following code shows how to send an SNMP v1 SET message to an SNMP agent located at 192.168.1.2 and set the value of OID 1.3.6.1.2.1.1.6.0 to "Shanghai",

var result = Messenger.Set(VersionCode.V1, 
                           new IPEndPoint(IPAddress.Parse("192.168.1.2"), 161), 
                           new OctetString("public"),
                           new List<Variable>{new Variable(new ObjectIdentifier("1.3.6.1.2.1.1.6.0"), new OctetString("Shanghai"))},
                           60000);

GET-NEXT Operation

The following code shows how to send an SNMP v1 GET-NEXT message to an SNMP agent located at 192.168.1.2 and query on OID 1.3.6.1.2.1.1.1.0,

GetNextRequestMessage message = new GetNextRequestMessage(0,
                              VersionCode.V1,
                              new OctetString("public"),
                              new List<Variable>{new Variable(new ObjectIdentifier("1.3.6.1.2.1.1.6.0"))});
ISnmpMessage response = message.GetResponse(60000, new IPEndPoint(IPAddress.Parse("192.168.1.2"), 161));
if (response.Pdu().ErrorStatus.ToInt32() != 0) 
{
    throw ErrorException.Create(
        "error in response",
        receiver.Address,
        response);
}

var result = response.Pdu().Variables;

GET-BULK Operation

The following code shows how to send an SNMP v2 GET-BULK message to an SNMP agent located at 192.168.1.2 and query on OID 1.3.6.1.2.1.1.1.0,

GetBulkRequestMessage message = new GetBulkRequestMessage(0,
                      VersionCode.V2,
                      new OctetString("public"),
                      0,
                      10,
                      new List<Variable>{new Variable(new ObjectIdentifier("1.3.6.1.2.1.1.6.0"))});
ISnmpMessage response = message.GetResponse(60000, new IPEndPoint(IPAddress.Parse("192.168.1.2"), 161));
if (response.Pdu().ErrorStatus.ToInt32() != 0)
{
    throw ErrorException.Create(
        "error in response",
        receiver.Address,
        response);
}

var result = response.Pdu().Variables;

Walk Operation

Walk is not an atomic operation. That means, it utilizes several GET-NEXT (SNMP v1 walk) or GET-BULK (v2 and above). The following code shows how to perform walk on an SNMP agent located at 192.168.1.2 starting at 1.3.6.1.2.1.1,

var result = new List<Variable>();
Messenger.Walk(VersionCode.V1, 
               new IPEndPoint(IPAddress.Parse("192.168.1.2"), 161), 
               new OctetString("public"), 
               new ObjectIdentifier("1.3.6.1.2.1.1"), 
               result, 
               60000, 
               WalkMode.WithinSubtree);

The result returned contains a list of all available OIDs (as Variable) in this SNMP agent that under tree node of 1.3.6.1.2.1.1.

#SNMP supports two walk modes, Default and WithinSubtree.

Messenger.Walk is built GET-NEXT. Note that Messenger.BulkWalk should be used if the device supports SNMP v2, as it is built on GET-BULK and provide better performance.

var result = new List<Variable>();
Messenger.BulkWalk(VersionCode.V2, 
                   new IPEndPoint(IPAddress.Parse("192.168.1.2"), 161), 
                   new OctetString("public"), 
                   new ObjectIdentifier("1.3.6.1.2.1.1"), 
                   result, 
                   60000, 
                   10, 
                   WalkMode.WithinSubtree, 
                   null, 
                   null);

TRAP Operation

It is usually an SNMP agent that sends out TRAP messages. The following code shows how to send an empty SNMP v1 TRAP message from 192.168.1.2 to an SNMP manager located at 192.168.1.3,

Messenger.SendTrapV1(new IPEndPoint(IPAddress.Parse("192.168.1.3"), 162), 
                     IPAddress.Parse("192.168.1.2"), 
                     new OctetString("public"), 
                     new ObjectIdentifier("1.3.6.1.2.1.1"), 
                     GenericCode.ColdStart, 
                     0, 
                     0, 
                     new List<Variable>();

SNMP v2 and above introduces a simplified TRAP v2 message,

Messenger.SendTrapV2(0, 
                     VersionCode.V2, 
                     new IPEndPoint(IPAddress.Parse("192.168.1.3"), 162), 
                     new OctetString("public"), 
                     new ObjectIdentifier("1.3.6.1.2.1.1"), 
                     0, 
                     new List<Variable>());

INFORM Operation

It is usually an SNMP agent that sends out INFORM messages. The following code shows how to send an empty INFORM message to an SNMP manager located at 192.168.1.3,

Messenger.SendInform(0, 
                     VersionCode.V2, 
                     new IPEndPoint(IPAddress.Parse("192.168.1.3"), 162), 
                     new OctetString("public"), 
                     new ObjectIdentifier("1.3.6.1.2.1.1"), 
                     0, 
                     new List<Variable>(), 
                     2000, 
                     null, 
                     null);

The manager should send back a reply to this INFORM message. Otherwise, a TimeoutException occurs.

To help you understand how to use the API provided by #SNMP Library, there are more sample projects you can find under Samples folder in source code package. Both C# and VB.NET samples are available.

Points of Interest

The above samples should give you a taste on how simple it is to perform common SNMP operations with #SNMP API.

I am going to write more articles on advanced usage such as compiling MIB document, SNMP v3 operations, and big samples such as #SNMP Compiler/Browser/Agent.

History

October 3rd, 2012: Added more operations and sample code.

October 2nd, 2012: Revised open source license descriptions.

September 30th, 2012: Initial release of this article.

License

This article, along with any associated source code and files, is licensed under The MIT License

About the Author

Lex Li
Software Developer (Senior)
China China
Member
Developer, Microsoft IIS/ASP.NET MVP

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionSend Trap with some messagemembervasanthneel9 May '13 - 5:35 
i am new to this SNMP.How to send trap with some message without using MIB? i am glad if got some reply.
AnswerRe: Send Trap with some messagememberLex Li13 May '13 - 20:15 
It is clear that TRAP and INFORM operations are described above. You will have to grab a MIB document and send the TRAP or INFORM message accordingly. It is SNMP, a protocol between you as the sender, and the management software as receiver. If there is no MIB document to set up a contract between the two, what you send is meaningless.
#SNMP, http://sharpsnmplib.codeplex.com

GeneralRe: Send Trap with some messagemembervasanthneel16 May '13 - 5:46 
Hello Lex Li,
 
I appreciate your response. i understand we need mib files to translate the messages for the manager with the use of oid which the TRAP and INFORM message holds.
I need to know do i need to compile mib file in Agent exe? or compiling in Manager side is enough.
Correct me if i am wrong.
 
Still i have many doubts in snmp. i am sending traps using sharpsnmp library. if you can show me an example of a sample mib file that used by an agent to send a simple trap message and also used by manager to identify the exact description in the trap that will be great helpfull for me. i don't see any documentation that exaclty speaks on how to use mib file just to send traps.
 
what steps do i need to take?
 
I am glad to hear from you on this, this will be very helpfull for me. Thanks again for your response.
GeneralRe: Send Trap with some messagememberLex Li16 May '13 - 16:27 
You can send/receive TRAP and INFORM messages without MIB compilation.
 
The open source edition of #SNMP MIB Browser does not support TRAP description identification via MIB documents. That's a feature planned for #SNMP MIB Browser Pro (which will be a commercial product).
#SNMP, http://sharpsnmplib.codeplex.com

GeneralRe: Send Trap with some messagemembervasanthneel16 May '13 - 20:08 
Thanks for your response.
 
OK. if i want to send trap based on a process status. how can i send trap with out mib at the same time other SNMP managers like HP etc., receive and show "XX Process crashed". How to make #agent inorder to get this stuff done.
 
I am still confused.
 
Waiting for your reply.
GeneralRe: Send Trap with some messagememberLex Li17 May '13 - 0:54 
That's something you should ask SNMP manager vendor. They should tell you how to implement it.
 
#SNMP is only a framework, which handles the messages. What data is passed on via the messages is out of its scope.
#SNMP, http://sharpsnmplib.codeplex.com

QuestionsnmpmemberMember 850756831 Mar '13 - 1:05 
Dear Ramanan.T,
we are under graduate students,we need to use SNMP in our graduation project and we need more information about how to create MIB using c#,how to save it and the steps to create the connection between a manager and an agent..

we are looking to hear from you soon..
thank you

sahar.h
QuestionMIB WalkmemberPedro Benevides8 Nov '12 - 4:17 
I should use Messenger.Walk to get all OIDs, but how can i get a description of each OID ? Is there a method that returns a description ? Thumbs Up | :thumbsup:
AnswerRe: MIB WalkmemberLex Li9 Nov '12 - 2:53 
Generally speaking, SNMP protocol does not cover that. An OID can legally have no description at all in the MIB documents, while any SNMP agent does not expose such descriptions in SNMP packet.
 
You need to obtain the SNMP MIB documents from the vendor and then manually translate each OID to an entity definition in one of the MIB documents.
 
A MIB compiler might make it simpler, by removing the manual translation part. But at this moment (#SNMP 7.5), #SNMP MIB compiler is not yet capable of doing that.
 
-Lex
#SNMP, http://sharpsnmplib.codeplex.com

QuestionDeleted [modified]memberLex Li1 Oct '12 - 15:23 
Deleted
#SNMP, http://sharpsnmplib.codeplex.com


modified 4 Oct '12 - 3:51.

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 3 Oct 2012
Article Copyright 2012 by Lex Li
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid