Define MIB and send SNMP trap under .net

10,961

Solution 1

I am not an expert, but have experience of being in the same spot as you were and finally found something enough for my project.

I used this codeplex library you referred before and found to be quite good. Here are some pointers for you to go through the http://sharpsnmplib.codeplex.com/.

  • As far as my understanding goes an MIB file is required when you need to do a snmpget and snmpset
    • This is sort of you informing the server and the server coming back to your client asking for more detailed info, and may be required if you require extensive info to be shared about your app
  • If all you need is just traps, you can do this
    • Run the snmpd.exe which will act like the snmp server receiving traps sent from your application.
    • Write a program similar the snmptrapd.exe and send your traps.

The link http://www.net-snmp.org/wiki/index.php/TUT:snmptrap is a good one for getting a basic understanding and get more library info.

Edit: Just missed one point. You'll need MIB file if you use get and set for snmp. If all you have is traps, you may not need it.

Solution 2

So from http://www.net-snmp.org/tutorial/tutorial-5/commands/snmptrap.html

The SNMPv2 notification

The format of the SNMPv2 notification is somewhat different. The definition in the MIB file looks as follows

NOTIFICATION-TEST-MIB DEFINITIONS ::= BEGIN
        IMPORTS ucdavis FROM UCD-SNMP-MIB;

demonotifs OBJECT IDENTIFIER ::= { ucdavis 991 }

demo-notif NOTIFICATION-TYPE
    STATUS current
        OBJECTS { sysLocation }
    DESCRIPTION "Just a test notification"
        ::= { demonotifs 17 }

END

Solution 3

Since apparently you're using Windows OS to work with SNMP then you need to create custom SNMP extension agent in order to add custom SNMP OIDs and corresponding traps in to your system. Each OID will literally present a variable where you can store your information. Here is an example of such DLL. As far as I know MIB files are only used in Windows as reference for SNMP agents and can't be used to add new OIDs without creating custom SNMP agent.

When you'll have your custom SNMP agent DLL, you need to point it in registry. Can be done like that (example):

[HKEY_LOCAL_MACHINE\SOFTWARE\Symbol\MyAgent\CurrentVersion]
"Pathname"="C:\\MyCustomAgent\\MyAgent.dll"

;Add number of agent to the list (max num registered + 1)
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\SNMP\Parameters\ExtensionAgents]
"10"="SOFTWARE\\Symbol\\MyAgent\\CurrentVersion"

After that you'll need to restart your SNMP service. If everything is right you'll be able to use your new custom OID to set\get some data.

In order to read\write in to your SNMP variables you'll need to use SNMP manager. I'm usually using Sharpsnmplib to do so. There are also GUI managers that will be usefull during debug process, for example: iReasoning MIB Browser.

Here is sample code to use that lib:

using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
// SNMP Lib references
using Lextm.SharpSnmpLib;
using Mono.Options;
using Lextm.SharpSnmpLib.Messaging;
using Lextm.SharpSnmpLib.Security;
...
List vList = new List();
ISnmpData data;
data = new OctetString("test"); // variable to add in to string OID
Variable test = new Variable(new ObjectIdentifier("1.3.6.1.2.1.1.6.0"), data); // OID
vList.Add(test);
VersionCode versionH = VersionCode.V2; // SNMP protocol version to use
IPAddress ipH;
IPAddress.TryParse("127.0.0.1", out ipH);
IPEndPoint receiverH = new IPEndPoint(ipH, 161);
foreach (Variable variable in
        Messenger.Set(versionH, receiverH, new OctetString("private"), vList, 10))// set variable
        // Arguments: (ver of SNMP, IP, group, iList containing OID and variable, timeoout)
        {
            // output variable
        }
...

Also you can read article about working with SNMP in Windows OS on technet: link. This Article is not new but might help you to understand how to wok with SNMP in Windows.

Solution 4

In SNMP V2 MIBs, traps are described as NOTIFICATION. See a full example of such a MIB here: http://tools.cisco.com/Support/SNMP/do/BrowseMIB.do?local=en&mibName=CISCO-EPM-NOTIFICATION-MIB

Solution 5

If you just need to send traps, you do not have to define MIBs. MIBs are for other parties to understand the meaning of variables in it. You can document it using other format such as a text file.

For sending traps, you can make use of a C# SNMP library, or just invoke snmptrap program directly. Net-snmp provides free command line tools for sending traps. ( http://net-snmp.sf.net ). Its usage is pretty simple.

To verify if the traps have been sent out successfully, you can use the free ireasoning MIB browser ( http://ireasoning.com/mibbrowser.shtml ) to receive them. If it can be received, it usually means your traps are fine.

Share:
10,961
Tom
Author by

Tom

Updated on June 13, 2022

Comments

  • Tom
    Tom almost 2 years

    I have developed a special application for a company under .net in C# and it is used for years. Now it is developed on, one of the main new features I have to implement is to integrate it with another software by sending SNMP traps to it in some alert situations.

    I am an experienced developer but I have never used SNMP. I googled the whole day but getting more and more confused about this topic. I don't have a clear 'starting point'. I understand the basics of SNMP but I don't know where and how to begin implementation.

    I have the data I should send via SNMP trap, it has some 10 properties, some dates, numbers and strings. I should create MIB definition(s) on this properties. Later I should implement a feature in my application that sends SNMP traps based on this MIBs.

    The project is developed under .net 4 in c#. I found this library that seems promising: http://sharpsnmplib.codeplex.com/ It has some sample about sending SNMP traps.

    My problem is: where to begin? How can I define MIB files? I know they're some text files that must be compiled, but have not find any MIB editors and help on this topic.

    Any help is appreciated!

    Thanks!

  • arx
    arx about 12 years
    "you need to create custom SNMP extension agent". This was true fifteen years ago (when I last did any SNMP programming) but you can now send a trap using SnmpSendMsg from the newer WinSNMP API (though that's more than a decade old now). Example here: stackoverflow.com/questions/4498618/…
  • Tom
    Tom about 12 years
    If so, then how can I define the format ('data fields') of an SNMP trap?
  • ThN
    ThN almost 6 years
    user814168... I under most of your answer, but why do you say, you do not have to define MIBs. MIB are for other parties to understand. What do you mean by that exactly? Even if you are JUST sending trap and don't require MIBs, don't you still need all the SNMP configuration like MIBname, MIBvalue, MIBtype, community, OID, generic, specific, etc. to successfully send a trap that the other machine can successfully accept and process. Otherwise, it will throw away the packet(s). Am I right in thinking that?