sleepinggenius2/gosmi

how to find node with OID?

wxzSpirent opened this issue · 11 comments

hi,
I'm trying to get a node by its OID.
A mib file is passed in and the module is loaded fine. It returns the json object that contains the loaded module and nodes. It works if I get the node by name (i.e., call GetNode()), but GetNodeByOID() always fails.
For example, with OID 1.3.6.1.4.1.5655.1.3, it failed after traverse to the second number - 3.
How should I use GetNodeByOID() ? Thanks

I'm not currently familiar with the formerly P-Cube, now Cisco MIBs. Let me grab them and try to replicate what you're seeing and I'll let you know what I find.

Thanks for your quick reply.
P-Cube is just an example I randomly pick to test with. In my use case, the application may have to deal with OIDs from many different vendors, hardware platforms and protocols (BGP, OSPF etc.).

Right now, I'm testing with a cisco router, and I downloaded all MIB files for that platform, it's already more than 1400 files. If I don't know what kind of traps are enabled on the router, do I have to load all those modules?

Kiwi server seems to have compiled their own db which include most vendors. It works with the cisco router, showing the name, description etc., of the trap.

Is this something we can do with gosmi?

With the example OID given (1.3.6.1.4.1.5655.1.3), I was able to load the SNMPv2-SMI, PCUBE-SMI, and PCUBE-PRODUCTS-MIB modules and GetNodeByOID(types.OidMustFromString("1.3.6.1.4.1.5655.1.3")) returned the sce2000 node, as expected. My best guess would be that you're not loading all the necessary modules to build out the OID tree before performing your search. You can reference the code in my mib2go repository for an example of how to do this automatically.

If you are not sure which traps are enabled, then you would need to load any module with a TRAP-TYPE or NOTIFICATION-TYPE macro along with their dependencies.

There is no pre-compiled database, but it should be possible to create one, if you have all the necessary MIBs.

Thanks, I'll take a look on mib2go.
I probably will need to load all modules available with their dependencies for my use case. Does mib2go show how to do that too?

I would suggest looking at one of the options provided by the standard library to generate the list of files to load:
https://golang.org/pkg/path/filepath/#Walk
https://golang.org/pkg/io/ioutil/#ReadDir
https://golang.org/pkg/os/#File.Readdir

would you please provide an example on how to run mib2go command?

In its simplest form, something like this: mib2go generate MY-FIRST-MIB MY-SECOND-MIB ...

You can run mib2go help generate for additional usage information.

I also wanted to apologize; I was just re-testing and I was not entirely correct before. The dependencies of a module will be loaded as long as they can be found in the list of paths that has been set.

yes, when I test get node by name, I iterate thru all loaded modules:

for _, m := range gosmi.GetLoadedModules() {
if node, err := gosmi.GetNode("sce2000", m); err != nil {
fmt.Printf("Get by name error: %s\n", err)
} else {
subtree := node.GetSubtree()
jsonBytes, _ := json.Marshal(subtree)
os.Stdout.Write(jsonBytes)
}
}
and I see output like this:

Get by name error: Could not find node named sce2000 in module
Get by name error: Could not find node named sce2000 in module SNMPv2-SMI
Get by name error: Could not find node named sce2000 in module PCUBE-SMI
[{"Access":"Unknown","Decl":"ValueAssignment","Description":"","Kind":"Node","Name":"sce2000","Oid":[1,3,6,1,4,1,5655,1,3],"OidLen":9,"Status":"Unknown","Type":null,"SmiType":null}]

so it looks like the dependency modules are loaded.

But gosmi.GetNodeByOID() does not require a module to be passed in. It searches from the root node and fails to reach the node I look for. Do you mind post the code you use to get that node from OID?

my bad, the code is working fine, it's just the way I pass in the oid is wrong. Thanks.

I have updated the example in cmd/smi to additionally include the option of searching by OID. I ran my test like this:
smi -p /path/to/ietf/mibs -p /path/to/cisco/mibs -m PCUBE-PRODUCTS-MIB 1.3.6.1.4.1.5655.1.3

Also, you do not need to manually loop through all the loaded modules. Running gosmi.GetNode() without the module parameter will search all modules by default.

thanks for all the help.
gosmi.GetNode("sce2000") didn't work for me probably due to other mistakes I made. When I look at the code, I got the impression that it only checks the first loaded module:

func GetNode(name string, module ...SmiModule) (node SmiNode, err error) {
var smiModule *types.SmiModule
if len(module) > 0 {
smiModule = module[0].GetRaw()
}
smiNode := smi.GetNode(smiModule, name)
if smiNode == nil {
......

But it's working now. Thanks a lot