Click here to Skip to main content
11,576,667 members (57,850 online)
Click here to Skip to main content

An introduction to #SNMP, an Open Source SNMP implementation

, 9 Jun 2013 MIT 52.3K 6.1K 36
Rate this:
Please Sign up or sign in to vote.
This article introduces #SNMP and how to use it to accomplish SNMP operations such as managing SNMP enabled devices.

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

June 9th, 2013: Upgraded binaries to 8.0 release. 

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

Share

About the Author

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

You may also be interested in...

Comments and Discussions

 
QuestionMIB parser/reader Pin
RudyL7-Apr-15 9:11
memberRudyL7-Apr-15 9:11 
AnswerRe: MIB parser/reader Pin
Lex Li22-Jun-15 1:50
memberLex Li22-Jun-15 1:50 
QuestionPassing a 'string' value as part of the ISnmpData Pin
Member 103011359-Mar-15 0:06
memberMember 103011359-Mar-15 0:06 
AnswerRe: Passing a 'string' value as part of the ISnmpData Pin
Lex Li22-Jun-15 1:41
memberLex Li22-Jun-15 1:41 
GeneralMy vote of 3 Pin
phil.o21-Feb-15 0:13
professionalphil.o21-Feb-15 0:13 
Questionload or parser .mib file Pin
carlos ariel alt2-Feb-15 9:31
membercarlos ariel alt2-Feb-15 9:31 
AnswerRe: load or parser .mib file Pin
Lex Li22-Jun-15 1:46
memberLex Li22-Jun-15 1:46 
QuestionCan the lib be used for SNMP proxy? Pin
dristan29-Oct-14 14:49
memberdristan29-Oct-14 14:49 
AnswerRe: Can the lib be used for SNMP proxy? Pin
Lex Li23-Nov-14 18:57
memberLex Li23-Nov-14 18:57 
QuestionSNMP WALK with V3 Pin
Member 105448871-Jul-14 6:47
memberMember 105448871-Jul-14 6:47 
AnswerRe: SNMP WALK with V3 Pin
Lex Li2-Jul-14 12:34
memberLex Li2-Jul-14 12:34 
GeneralRe: SNMP WALK with V3 Pin
Member 105448873-Jul-14 2:46
memberMember 105448873-Jul-14 2:46 
GeneralRe: SNMP WALK with V3 Pin
Lex Li3-Jul-14 17:10
memberLex Li3-Jul-14 17:10 
QuestionPlease help me in send a custom dynamic long messaage to SNMP using sendtrap Pin
Ravindranp10-Apr-14 9:07
memberRavindranp10-Apr-14 9:07 
AnswerRe: Please help me in send a custom dynamic long messaage to SNMP using sendtrap Pin
Lex Li11-Apr-14 2:39
memberLex Li11-Apr-14 2:39 
QuestionTimeout exception Get Method Pin
Daniel Dimitri18-Mar-14 5:08
memberDaniel Dimitri18-Mar-14 5:08 
AnswerRe: Timeout exception Get Method Pin
Lex Li19-Mar-14 0:54
memberLex Li19-Mar-14 0:54 
GeneralRe: Timeout exception Get Method Pin
Daniel Dimitri21-Mar-14 0:28
memberDaniel Dimitri21-Mar-14 0:28 
QuestionClasses to import Pin
Member 1059613017-Feb-14 23:17
memberMember 1059613017-Feb-14 23:17 
AnswerRe: Classes to import Pin
Lex Li2-Jul-14 23:54
memberLex Li2-Jul-14 23:54 
QuestionXAMARIN and SNMP library/commands - C# android Pin
Kema Tala30-Jan-14 5:24
memberKema Tala30-Jan-14 5:24 
AnswerRe: XAMARIN and SNMP library/commands - C# android Pin
Lex Li30-Jan-14 19:17
memberLex Li30-Jan-14 19:17 
GeneralRe: XAMARIN and SNMP library/commands - C# android Pin
Kema Tala7-Feb-14 9:06
memberKema Tala7-Feb-14 9:06 
GeneralRe: XAMARIN and SNMP library/commands - C# android Pin
Lex Li10-Jun-14 17:13
memberLex Li10-Jun-14 17:13 
QuestionLink to BigDiper looks corrupt when downloaded Pin
arian7418-Jun-13 19:38
memberarian7418-Jun-13 19:38 
AnswerRe: Link to BigDiper looks corrupt when downloaded Pin
Lex Li22-Jun-13 20:44
memberLex Li22-Jun-13 20:44 
GeneralRe: Link to BigDiper looks corrupt when downloaded Pin
Lex Li27-Jun-13 2:30
memberLex Li27-Jun-13 2:30 
GeneralMy vote of 5 Pin
Volynsky Alex9-Jun-13 2:05
memberVolynsky Alex9-Jun-13 2:05 
QuestionArtificial and arbitrary limitation Pin
Nesa Simon David5-Jun-13 19:03
memberNesa Simon David5-Jun-13 19:03 
AnswerRe: Artificial and arbitrary limitation Pin
Lex Li6-Jun-13 3:17
memberLex Li6-Jun-13 3:17 
QuestionSend Trap with some message Pin
vasanthneel9-May-13 5:35
membervasanthneel9-May-13 5:35 
AnswerRe: Send Trap with some message Pin
Lex Li13-May-13 20:15
memberLex Li13-May-13 20:15 
GeneralRe: Send Trap with some message Pin
vasanthneel16-May-13 5:46
membervasanthneel16-May-13 5:46 
GeneralRe: Send Trap with some message Pin
Lex Li16-May-13 16:27
memberLex Li16-May-13 16:27 
GeneralRe: Send Trap with some message Pin
vasanthneel16-May-13 20:08
membervasanthneel16-May-13 20:08 
GeneralRe: Send Trap with some message Pin
Lex Li17-May-13 0:54
memberLex Li17-May-13 0:54 
Questionsnmp Pin
Member 850756831-Mar-13 1:05
memberMember 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 Walk Pin
Pedro Benevides8-Nov-12 4:17
memberPedro Benevides8-Nov-12 4:17 
AnswerRe: MIB Walk Pin
Lex Li9-Nov-12 2:53
memberLex Li9-Nov-12 2:53 
QuestionDeleted Pin
Lex Li1-Oct-12 15:23
memberLex Li1-Oct-12 15:23 
AnswerMessage Removed Pin
MatthysDT1-Oct-12 20:47
memberMatthysDT1-Oct-12 20:47 
AnswerMessage Removed Pin
Clifford Nelson2-Oct-12 15:10
memberClifford Nelson2-Oct-12 15:10 
AnswerMessage Removed Pin
Clifford Nelson2-Oct-12 16:02
memberClifford Nelson2-Oct-12 16:02 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150603.1 | Last Updated 9 Jun 2013
Article Copyright 2012 by Lex Li
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid