lextudio / sharpsnmplib

Sharp SNMP Library- Open Source SNMP for .NET
https://sharpsnmp.com
MIT License
356 stars 152 forks source link

SNMP Library usage #468

Closed lextudio-support closed 2 hours ago

lextudio-support commented 2 hours ago

I'm very interested in using your library for a new project that I am working on. I've been able to use your browser to access the data I need, so I know your library is suitable.

Can you point me in the right direction to get started? I need to compile .mib files and work with the MibModules returned. From what I have been trying, I have had a hard time subclassing or using the objects that are part of your library. For instance, I wanted to work with MibModule, but that is internal protected. So I tried creating a similar class using the IModule interface. But in order to implement that interface, I need access to Lexer, which is also internal protected. I also tried to implement a subclass of ObjectRegistryBase, but quickly ran into problems requiring internal protected classes (MibModule being one). Is is possible to work with your library of objects to get the functionality I need, or am I going to end up reimplementing all of the work you've already done?

regard,

jim

Original Reported Date: 2009-10-09T19:08:10.397-07:00 Original CodePlex Discussion Thread ID: 71560

lextudio-support commented 2 hours ago

Copied from CodePlex without authors:

Hi Jim,

First, you need to use the Compiler.exe to compile the .mib files into .module files.

Second, you need to follow the Browser.exe's manner. The simplest way is to make use of built in classes such as ReloadableObjectRegistry. Its constructor takes a directory that contains the *.module files, and using that it automatically loads the objects. In this way you don't even need to know anything about Lexer or MibModule.

Third, you can soon move on to notice that ReloadableObjectRegistry can work fine with other parts in the library.

Let me know if you have more questions.

Regards,

Lex

Original Posted Date: 2009-10-10T00:59:57.137-07:00

lextudio-support commented 2 hours ago

Copied from CodePlex without authors:

So what I was hoping to do is compile the mibs from within my application. I am going to use a sql server to periodically collect some of the values identified in the mibs. I am going to have to persist some data about each mib file (like which oids I need to get values for). I was hoping to be able to compile and work directly with the MibModule and perhaps even the Lexer. I really don't want my end user having to compile from one app, then get into another in order to select values to retrieve, etc.

Jim

Original Posted Date: 2009-10-10T10:43:29.107-07:00

lextudio-support commented 2 hours ago

Copied from CodePlex without authors:

Hi Jim,

The latest description on your need shows that you need to host some part of Compiler.exe inside your code. Luckily, you can check out CompileCore.cs in Compiler project, which makes use of Parser and Assembler classes to compile *.mib files. They are high level classes recommended instead of low level classes such as MibModule and that's why I set MibModule and so on internal.

In 4.0 release, we are going to ship a new compiler architecture, so the internal classes and algorithm can change a lot (or saying totally). I can only guarantee that we will keep the high level classes untouched.

Regards,

Lex

Original Posted Date: 2009-10-10T16:43:03.88-07:00

lextudio-support commented 2 hours ago

Copied from CodePlex without authors:

Do you have a timeframe for 4.0? I will work with compilerCore and review your other code again and see what I can make do. You seem responsive, so I will just ask when I get to a spot I feel I need more control over and we'll see what you suggest.

Thanks,

Jim

Original Posted Date: 2009-10-10T17:23:31.843-07:00

lextudio-support commented 2 hours ago

Copied from CodePlex without authors:

Hi Jim and Lex,

don't know exactly what are you trying to achieve, anyway, I'll share my experience here.

I'm actually compiling my custom MIB at runtime, extracting relevant informations and executing Snmp queries using custom MIB data. Basically, my application (it's a device dumper), can load my MIB (with some dependencies), compile it at runtime and then loop inside Oids to query my devices. This is done because my actual MIB is subject to frequent changes.

This is relevant code:

string snmpv2TCFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SNMPv2-TC.txt");
string snmpv2SMIFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SNMPv2-SMI.txt");
string snmpv2CONFFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SNMPv2-CONF.txt");

// this is my custom MIB, that has dependencies on previous ones
string snmpv2CustomMibFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "MY-CUSTOM-MIB.txt");

IList snmpTCModule = Parser.Compile(snmpv2TCFilename);
DefaultObjectRegistry.Instance.Import(snmpTCModule);

IList snmpSMIModule = Parser.Compile(snmpv2SMIFilename);
DefaultObjectRegistry.Instance.Import(snmpSMIModule);

IList snmpCONFModule = Parser.Compile(snmpv2CONFFilename);
DefaultObjectRegistry.Instance.Import(snmpCONFModule);

IList customMibModule = Parser.Compile(snmpv2CustomMibFilename);
DefaultObjectRegistry.Instance.Import(customMibModule);

DefaultObjectRegistry.Instance.Refresh();

if (customMibModule.Count > 0) {
foreach (IEntity entity in customMibModule[0].Entities) {
IDefinition node = DefaultObjectRegistry.Instance.Tree.Find(customMibModule[0].Name, entity.Name);
if (node == null) {
continue;
}

    uint[] id = node.GetNumericalForm();  

    if (entity.GetType().FullName.Equals("Lextm.SharpSnmpLib.Mib.ObjectType", StringComparison.OrdinalIgnoreCase)) {  
        if (entity.Name.EndsWith("Table")) {  
            Console.WriteLine(ObjectIdentifier.Convert(id) + "," \+ entity.Name);  
            // Get Snmp table data  
        }  
        else if ((!entity.Parent.EndsWith("Entry")) && (!entity.Name.EndsWith("Entry"))) {  
            Console.WriteLine(ObjectIdentifier.Convert(id) + ".0" \+ "," \+ entity.Name);  
            // Get Snmp scalar data  
        }  
    }  
}  

}

Hope this helps,

Andrea

Original Posted Date: 2009-10-11T02:37:34.44-07:00

lextudio-support commented 2 hours ago

Copied from CodePlex without authors:

Andrea,

Thanks so much for the sample code. I will walk through it and see if it helps me get what I need. Hopefully it's just a matter of me not understanding how to properly use the library.

I'll let you know how I make out,

Jim

Original Posted Date: 2009-10-11T17:17:40.383-07:00

lextudio-support commented 2 hours ago

Copied from CodePlex without authors:

Andrea,

I'm looking at the DefaultObjectRegistry.Instance.Trre and notice it already has modules loaded. Is there a way for me to start up "clean" so I do not have these already loaded?

Jim

Original Posted Date: 2009-10-11T19:08:19.933-07:00

lextudio-support commented 2 hours ago

Copied from CodePlex without authors:

Hi Jim,

you can see DefaultObjectRegistry.Instance has five default loaded modules having a look at its implementation, in file:

SharpSnmpLib\Mib\DefaultObjectRegistry.cs, LoadDefaultModules method.

This modules are normally required in every MIB operation because they are imported almost everywhere.

Anyway, you can easily unload this modules using:

DefaultObjectRegistry.Instance.Tree.Remove("SNMPV2-SMI");
DefaultObjectRegistry.Instance.Tree.Remove("SNMPV2-CONF");
DefaultObjectRegistry.Instance.Tree.Remove("SNMPV2-TC");
DefaultObjectRegistry.Instance.Tree.Remove("SNMPV2-MIB");
DefaultObjectRegistry.Instance.Tree.Remove("SNMPV2-TM");
DefaultObjectRegistry.Instance.Refresh();

Hope this helps,

Andrea

Original Posted Date: 2009-10-11T23:11:34.957-07:00

lextudio-support commented 2 hours ago

Copied from CodePlex without authors:

Andrea,

With regards to the sample code you posted above. I'm trying to find all the nodes that are part of the module I just compiled. Since there are other prerequisites, I'm having a hard time. The Find method does not always find the nodes that contain the parts from the module I just compiled. I suspect because they are lower in the tree. The specific example I found was compiling wins.mib. What I need to do is gather the Oid's and descriptions of all things I can get values for in a specific .mib.

Thanks,

Jim

Original Posted Date: 2010-03-09T18:04:35.593-08:00

lextudio-support commented 2 hours ago

Copied from CodePlex without authors:

The reason why I suggest you use Compiler to compile first, is because that compilation can tell which dependencies are missing. Only when all dependencies are present, our Compiler.exe can generate *.module files.

Then when you load all the *.module files into your IObjectRegistry you can see the objects in the tree via IObjectTree.Find.

When you followed Andrea's code to compile the modules in code, you would lose that chance to know missing dependencies, and you must check IObjectTree.PendingModules to see whether WINS-MIB is pending there.

Regards,

Lex

Original Posted Date: 2010-03-09T20:48:55.127-08:00

lextudio-support commented 2 hours ago

Copied from CodePlex without authors:

I see. I'll work on the managing the pendingModules before I Find.

Thanks for your help

Original Posted Date: 2010-03-10T03:24:00.7-08:00

lextudio-support commented 2 hours ago

Marked as Answer Date: 2013-10-06T20:53:10.213-07:00