Store data from multiple table

Hi Frank,

Is it possible to store multiple sets of data of same MIB as snmp tables (scalar as well)?

For example, I have multiple routers and I have a machine which collects interface data (IF-MIB) from these routers.

  • What is the preferred approach where I can store multiple different sets of data for same MIB?
  • How do I differentiate these data sets?

–Anirban

Hi Arniban,

You can use SNMPv3 contexts for that. This use case is exactly for what that context stuff has been designed for.

Best regards
Frank

Oh!! Yes. Many thanks Frank.
I have never used it before. If you have any example of its use, it would help immensely.

The atm_mib example provides an example adding a mib object in the non-default context “other”.

Thanks for your reply Frank.

But with contexts introduced, my AgentConfigSetup.properties file is not even parsing. It is throwing following exception

Caused by: java.lang.NumberFormatException: For input string: “ctx”
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Long.parseLong(Long.java:589)
at java.lang.Long.parseLong(Long.java:631)
at org.snmp4j.util.SimpleOIDTextFormat.parseOID(SimpleOIDTextFormat.java:127)
at org.snmp4j.util.SimpleOIDTextFormat.parse(SimpleOIDTextFormat.java:150)

My AgentConfigSetup.properties is like this:

snmp4j.agent.cfg.contexts={s}INVIMP03BPL3SPNE1BER
snmp4j.agent.cfg.oid.ctx.INVIMP03BPL3SPNE1BER.1.3.6.1.2.1.1.2.0={o}1.3.6.1.4.1.193
snmp4j.agent.cfg.oid.ctx.INVIMP03BPL3SPNE1BER.1.3.6.1.2.1.1.4.0={s}System Administrator
snmp4j.agent.cfg.oid.ctx.INVIMP03BPL3SPNE1BER.1.3.6.1.2.1.1.5.0={s}System Descr
snmp4j.agent.cfg.oid.ctx.INVIMP03BPL3SPNE1BER.1.3.6.1.2.1.1.6.0={s}
snmp4j.agent.cfg.oid.ctx.INVIMP03BPL3SPNE1BER.1.3.6.1.2.1.1.7.0={i}10
snmp4j.agent.cfg.oid.ctx.INVIMP03BPL3SPNE1BER.1.3.6.1.2.1.1.9.1=1:2
snmp4j.agent.cfg.index.ctx.INVIMP03BPL3SPNE1BER.1.3.6.1.2.1.1.9.1.0={o}1

What Am I Missing here?
Please help.

–Anirban

The “.ctx” must be written before “.oid” or “.index”. But even then, you cannot create contexts using the properties file. Contexts must be added programmatically and the MIB objects need to be registered to contexts of the DefaultMOServer programmatically.

You can then initialise the (context specific) MIB objects using the properties file.

I found this piece of code in PropertyMOInput. This says that the “.ctx” must be written before “.oid ” or “.index ”. This matches the documentation also.

String prefix = CONFIG_PREFIX+OID_ID;
    String ctxSuffix = "";
    if (ctx != null) {
      ctxSuffix = CTX_ID+ctx.toString()+".";
    }
    prefix += ctxSuffix;

As you mentioned I have already added contexts in code while creating the MOServer and MOTable.

I am getting FOllowing error now:

Caused by: java.io.IOException: null
at org.snmp4j.agent.io.prop.PropertyMOInput.readContext(PropertyMOInput.java:358)
at org.snmp4j.agent.io.MOServerPersistence.loadData(MOServerPersistence.java:110)
at org.snmp4j.agent.AgentConfigManager.configure(AgentConfigManager.java:674)

This is being thrown from PropertyMOInput.readContext() method.

Inside the MOServerPersistence class, when it reaches the “// load contexts” section, and does the following loop

for (int i = 0; i < contextSequence.getSize(); i++) {
Context context = input.readContext();

The state value already changes to 3. How to load the contexts then? What Am I missing for context loading?

I think there was a bug in the PropertyMOInput class. The state++; in the third case (STATE_CTX_SEQ) needs to be removed:

    public Sequence readSequence() throws IOException {
        switch (state) {
            case STATE_ALL_CTX_DATA_SEQ: {
                state++;
                oids = scanOIDs(properties, null);
                return new Sequence(oids.numElements);
            }
            case STATE_ALL_CTX_DATA:
                if (data != null && !oids.iterator.hasNext()) {
                    state++;
                    // fall through
                } else {
                    DataInfo<IndexedVariables> indexedVariablesDataInfo = new DataInfo<IndexedVariables>(oids.curContext);
                    data = indexedVariablesDataInfo;
                    scanDataIndexVariables(properties, oids.curOID.getOID(), indexedVariablesDataInfo);
                    return new Sequence(data.size());
                }
            case STATE_CTX_SEQ: {
                state++; // <==== Remove this line to fix the IOException!
                return new Sequence(contexts.numContexts);
            }
        }
        return null;
    }

But unfortunately it seems that the context data loading has not been completely implemented in SNMP4J-Aent yet. I am working on it to fix it ASAP…

Thanks Frank. Is it possible to to tell me what are the changes you identified context data loading in SNMP4J-Agent, I can implement those separately and continue and when your fix patch arrives, will adopt that.

Also, is it possible to tell a timeline when can you provide a fix?

I have devised an workaround for this. I am running a separate java process with default context for each context (supposed to be) on different port. A proxy forwarder forwards the request to corresponding java process/port.

Please let me know in case any update.
Thanks.

Any tentative update on this sir?

I am working on it. It will take some more days, I guess. But the state++ in the above code snippet was not the source of the problem, but missing code for the context specific data loading:

            case STATE_CTX_SEQ: {
                state++; // This line was correct but an additional case statement is needed!
                return new Sequence(contexts.numContexts);
            }

Thanks Frank. I will continuously bug you in few days. :slight_smile:

This SNMP4J-Agent 3.6.1 SNAPSHOT contains the fix for the context specific MIB data loading from properties file: https://snmp.app/dist/snapshot/org/snmp4j/snmp4j-agent/3.6.1-SNAPSHOT/snmp4j-agent-3.6.1-20220927.224406-8-distribution.tar.gz
This is the latest snapshot for the 2.x branch with a back ported fix for the context specific property loading (currently untested, will be tested until release, of course):
https://snmp.app/dist/snapshot/org/snmp4j/snmp4j-agent/2.7.5-SNAPSHOT/snmp4j-agent-2.7.5-20220928.221955-3-sources.jar

Many thanks Frank.

One question, Is this agent is it Java 9 compliant? My application is in Java 8.
Is it backward compatible?

Another question Frank.
The agents, Snmp4j-Agent 3.x and Snmp4j-Agent 2.x, are they feature compliant?

No, it is not fully backward compatible to Java 8. That is why SNMP4J-Agent 3.x depends on Java 9 or later.

No, SNMP4J-Agent adds support for SNMP4J-Agent-DB and other features like DTLS support, better event handling for DefaultMOServer.

Hi Frank.

I cant thank you enough. The context use case works. Though it is a little complex to implement.

One more query. Context feature is not available in v2c? Because the contextName property is only available in ScopedPDU.

My agent has co-existence. How to move ahead with v2c then? Please help.

-Anirban

Hi Anirban,

You can map communities to SNMPv3 contexts using the SNMP-COMMUNITY-MIB. See the corresponding Coexistence RFC 3584 for details.

Best regards,
Frank