cederberg / mibble

Mibble is an open-source SNMP MIB parser library for Java.
https://www.mibble.org/
Other
113 stars 68 forks source link

Missing ReferenceSymbol on variable MibValue retrieved from SnmpTrapType Object #39

Closed stroeste-sms-group closed 3 years ago

stroeste-sms-group commented 3 years ago

For a trap sender utilities UI, I try to determine the variables and their data types and description provided with the trap that should be sent.

From the SnmpTrapType Object I can get a List of MibValue Objects, representing the variables, but as I understood so far, I need the corresponding MibValueSymbol Objects to get access to the oids and data types of the variables. The getReferenceSymbol() Method of the MibValue Object should return this, but returns 'null' instead.

Is that a bug, or did I misunderstand the API in that point?

Example code:

MibValueSymbol v1Trap = ...

SnmpTrapType snmpTrapType = (SnmpTrapType)v1Trap.getType();

//get enterprise and according oid
MibValue enterprise = snmpTrapType.getEnterprise();
System.out.print("\tenterprise: ");
System.out.print(enterprise.getName());
System.out.print(" ");
System.out.print((enterprise.getReferenceSymbol() == null ? "'no reference symbol'" : enterprise.getReferenceSymbol().getOid().toString()));
System.out.println();

// get the variables and according oids
ArrayList<MibValue> variables = snmpTrapType.getVariables();
if (!variables.isEmpty()) {
    System.out.println("\tvariables:");
    for (MibValue variable : variables) {
        System.out.print("\t\t");
        System.out.print(variable.getName());
        System.out.print(" ");
        System.out.print((variable.getReferenceSymbol() == null ? "'no reference symbol'" : variable.getReferenceSymbol().getOid().toString()));
        System.out.println();
    }
}
cederberg commented 3 years ago

If you try the MibblePrinter utility, you'll see the following output for a TRAP-TYPE:

VALUE frDLCIStatusChange TRAP-TYPE (
  Enterprise: 1.3.6.1.2.1.10.32
  Variables: [1.3.6.1.2.1.10.32.2.1.1, 1.3.6.1.2.1.10.32.2.1.2, 1.3.6.1.2.1.10.32.2.1.3]
  ...

From the example above, you'll note that the variables have been resolved to their OID values. So the list of MibValue objects are actually ObjectIdentifierValue instances (but always double-check with instanceof). From such a value, you can easily get the symbol you want with oid.getSymbol()

stroeste-sms-group commented 3 years ago

Dear Per Cederberg,

thanks for your immediate answer. I hope you are willing to help me a little more, because I seem to be lost in that structure of classes. Even a close look at the MibBrowser and MibPrinter does not get me out of this.

Now that I have the Object IdentifierValue Objects for the variables, I hoped I could find out easily if the variable is an integer, string or octet string etc., but that was false hope.

I know the type of the variable is hidden in oidValue.getSymbol(), but how do I get it out? I always get stuck, getType() somehow has the information I want to have (underlined in the output), but I have no clue how to retrieve it.

Example Code:

        for (MibValueSymbol v1Trap : v1Traps) {
            SnmpTrapType snmpTrapType = (SnmpTrapType)v1Trap.getType();

            System.out.print(v1Trap.getName());
            System.out.print(" ");
            System.out.print(v1Trap.getValue());
            System.out.print(" ");
            System.out.print(snmpTrapType.getName());
            System.out.println();

            MibValue enterprise = snmpTrapType.getEnterprise();
            System.out.print("\tenterprise: ");
            System.out.print(enterprise.getName());
            if (enterprise instanceof ObjectIdentifierValue) {
                ObjectIdentifierValue enterpriseOidValue = (ObjectIdentifierValue)enterprise;
                System.out.print(" ");
                System.out.print(enterpriseOidValue.toString());
                System.out.println();
            }

            // get the variables
            ArrayList<MibValue> variables = snmpTrapType.getVariables();
            if (!variables.isEmpty()) {
                System.out.println("\tvariables:");
                for (MibValue variable : variables) {
                    System.out.print("\t\t");
                    System.out.print(variable.getName());
                    if (variable instanceof ObjectIdentifierValue) {
                        ObjectIdentifierValue variableOidValue = (ObjectIdentifierValue)variable;
                        MibValueSymbol variableValueSymbol = variableOidValue.getSymbol();
                        System.out.print(" ");
                        System.out.print(variableOidValue.toString());
                        System.out.print(" ");
                        System.out.print(variableValueSymbol.getName());
                        System.out.print(" ");
                        System.out.print(variableValueSymbol.isScalar());
                        System.out.print(" ");
                        System.out.print(variableValueSymbol.isTable());
                        System.out.print(" ");
                        System.out.print(variableValueSymbol.isTableColumn());
                        System.out.print(" ");
                        System.out.print(variableValueSymbol.isTableRow());
                        System.out.print(" ");
                        System.out.print(variableValueSymbol.getType().toString());
                        System.out.println();
                    }
                }
            }

Output:

linkUp 3 TRAP-TYPE enterprise: snmp 1.3.6.1.2.1.11 variables: ifIndex 1.3.6.1.2.1.2.2.1.1 ifIndex false false true false OBJECT-TYPE ( Syntax: [UNIVERSAL 2] INTEGER Access: read-only Status: mandatory

egpNeighborLoss 5 TRAP-TYPE enterprise: snmp 1.3.6.1.2.1.11 variables: egpNeighAddr 1.3.6.1.2.1.8.5.1.2 egpNeighAddr false false true false OBJECT-TYPE ( Syntax: [APPLICATION 0] OCTET STRING (SIZE (4)) Access: read-only Status: mandatory

Mit freundlichen Grüßen / Best regards

SMS group GmbH

Stefan Strömer LPST Technologie Nahtlosanlagen

-- Ohlerkirchweg 66, 41069 Moenchengladbach / Germany Telefon/Phone: +49 2161 350 1813 Telefax/Fax: +49 2161 350 1225 E-Mail: stefan.stroemer@sms-group.com

Von: Per Cederberg notifications@github.com Gesendet: Montag, 11. Januar 2021 19:43 An: cederberg/mibble mibble@noreply.github.com Cc: Strömer, Stefan (SMS group GmbH) Stefan.Stroemer@sms-group.com; Author author@noreply.github.com Betreff: Re: [cederberg/mibble] Missing ReferenceSymbol on variable MibValue retrieved from SnmpTrapType Object (#39)

If you try the MibblePrinter utility, you'll see the following output for a TRAP-TYPE:

VALUE frDLCIStatusChange TRAP-TYPE (

Enterprise: 1.3.6.1.2.1.10.32

Variables: [1.3.6.1.2.1.10.32.2.1.1, 1.3.6.1.2.1.10.32.2.1.2, 1.3.6.1.2.1.10.32.2.1.3]

...

From the example above, you'll note that the variables have been resolved to their OID values. So the list of MibValue objects are actually ObjectIdentifierValue instances (but always double-check with instanceof). From such a value, you can easily get the symbol you want with oid.getSymbol()

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/cederberg/mibble/issues/39#issuecomment-758145814, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ASNVKCA26R3GWEZGV5MA4HDSZNBB5ANCNFSM4V5Z7V5Q.

SMS group GmbH Vorsitzender des Aufsichtsrates: Edwin Eichler Geschaeftsfuehrung: Burkhard Dahmen, Vorsitzender Dr. Hans Ferkel, Torsten Heising, Michael Rzepczyk, Prof. Dr.-Ing. Katja Windt

Sitz der Gesellschaft: Duesseldorf Registergericht: Amtsgericht Duesseldorf, HRB 75040 USt-IdNr: DE811136276

Wir verarbeiten personenbezogene Daten über unsere Geschaeftspartner, Bewerber und Besucher unserer Webseite in Uebereinstimmung mit den Datenschutzgesetzen. Einzelheiten zu uns, unserer Verarbeitung, den Rechtsgrundlagen und Ihren Rechten entnehmen Sie bitte unseren Datenschutzhinweisen Datenschutz | SMS group https://www.sms-group.com/de/legal/datenschutz/.

stroeste-sms-group commented 3 years ago

Dear Per Cederberg,

I desperately need help.

I did some more experiments on trying to figure out the types of the variables of a trap. But without much success. I read your Q&A on Java API Usage again, but I do not get any useful results, not via MibTypeTags nor via instanceof.

Where is my fault?

Heres my code:

        for (MibValueSymbol v1Trap : v1Traps) {
            SnmpTrapType snmpTrapType = (SnmpTrapType)v1Trap.getType();

            System.out.print(v1Trap.getName());
            System.out.print(" ");
            System.out.print(v1Trap.getValue());
            System.out.print(" ");
            System.out.print(snmpTrapType.getName());
            System.out.println();

            MibValue enterprise = snmpTrapType.getEnterprise();
            System.out.print("\tenterprise: ");
            System.out.print(enterprise.getName());
            if (enterprise instanceof ObjectIdentifierValue) {
                ObjectIdentifierValue enterpriseOidValue = (ObjectIdentifierValue)enterprise;
                System.out.print(" ");
                System.out.print(enterpriseOidValue.toString());
                System.out.println();
            }

            // get the variables
            ArrayList<MibValue> variables = snmpTrapType.getVariables();
            if (!variables.isEmpty()) {
                System.out.println("\tvariables:");
                for (MibValue variable : variables) {
                    System.out.print("\t\t");
                    System.out.print(variable.getName());
                    if (variable instanceof ObjectIdentifierValue) {
                        ObjectIdentifierValue variableOidValue = (ObjectIdentifierValue)variable;

                        System.out.print(" ");
                        System.out.print(variableOidValue.toString());
                        System.out.print(" ");
                        try {
                            System.out.print(getSyntax(variable));
                        } catch (UnsupportedOperationException ex) {
                            System.out.print("UNSUPPORTED");
                        }

                        System.out.println();
                    }
                }
            }

            // get the description of the trap type
            printMultilineText(snmpTrapType.getDescription(), "description");

            // get the comment of the trap type
            printMultilineText(snmpTrapType.getComment(), "trap type comment");

// // get the source text // printMultilineText(v1Trap.getText(), "text");

            // get the comment of the symbol
            printMultilineText(v1Trap.getComment(), "symbol comment");
        }

private static String getSyntax(MibValue mibValue) {
    if (mibValue instanceof BitSetValue) {
        throw new UnsupportedOperationException("Not yet implemented for class " + mibValue.getClass().getName());
    } else if (mibValue instanceof BooleanValue) {
        throw new UnsupportedOperationException("Not yet implemented for class " + mibValue.getClass().getName());
    } else if (mibValue instanceof NullValue) {
        throw new UnsupportedOperationException("Not yet implemented for class " + mibValue.getClass().getName());
    } else if (mibValue instanceof NumberValue) {
        throw new UnsupportedOperationException("Not yet implemented for class " + mibValue.getClass().getName());
    } else if (mibValue instanceof ObjectIdentifierValue) {
        ObjectIdentifierValue oidValue = (ObjectIdentifierValue)mibValue;
        MibValueSymbol mibValueSymbol = oidValue.getSymbol();
        MibType mibType = mibValueSymbol.getType();

        if (mibType.hasTag(MibTypeTag.BIT_STRING)) {
            return "BitString";
        } else if (mibType.hasTag(MibTypeTag.BOOLEAN)) {
            return "Boolean";
        } else if (mibType.hasTag(MibTypeTag.INTEGER)) {
            return "Integer";
        } else if (mibType.hasTag(MibTypeTag.NULL)) {
            return "Null";
        } else if (mibType.hasTag(MibTypeTag.OBJECT_IDENTIFIER)) {
            return "ObjectIdentifier";
        } else if (mibType.hasTag(MibTypeTag.OCTET_STRING)) {
            return "OctetString";
        } else if (mibType.hasTag(MibTypeTag.REAL)) {
            return "Real";
        } else if (mibType.hasTag(MibTypeTag.SEQUENCE)) {
           return "Sequence";
        } else if (mibType.hasTag(MibTypeTag.SET)) {
            return "Set";
        } else if (mibType.hasTag(MibTypeTag.APPLICATION_CATEGORY, 0)) { //IpAddress
            return "IpAddress";
        } else if (mibType.hasTag(MibTypeTag.APPLICATION_CATEGORY, 1)) { //Counter32
            return "Counter32";
        } else if (mibType.hasTag(MibTypeTag.APPLICATION_CATEGORY, 2)) { //Gauge32 or Unsigned32
            if (mibType.hasReferenceTo("Gauge32")) {
                return "Gauge32";
            } else if (mibType.hasReferenceTo("Unsigned32")) {
                return "Unsigned32";
            } else {
                throw new UnsupportedOperationException("Unknown case for MibTypeTag.APPLICATION_CATEGORY 2, neither Gauge32 nor Unsigned32");
            }
        } else if (mibType.hasTag(MibTypeTag.APPLICATION_CATEGORY, 3)) { //TimeTicks
            return "TimeTicks";
        } else if (mibType.hasTag(MibTypeTag.APPLICATION_CATEGORY, 4)) { //Opaque
            return "Opaque";
        } else if (mibType.hasTag(MibTypeTag.APPLICATION_CATEGORY, 6)) { //Counter64
            return "Counter64";
        //} else if (mibType.hasTag(MibTypeTag.CONTEXT_SPECIFIC_CATEGORY, 0)) {
        //} else if (mibType.hasTag(MibTypeTag.PRIVATE_CATEGORY, 0)) {
        //} else if (mibType.hasTag(MibTypeTag.UNIVERSAL_CATEGORY, 0)) {
        } else {
            if (mibType instanceof BitSetType) {
                return "BitSetType";
            } else if (mibType instanceof BooleanType) {
                return "BooleanType";
            } else if (mibType instanceof ChoiceType) {
                return "ChoiceType";
            } else if (mibType instanceof ElementType) {
                return "ElementType";
            } else if (mibType instanceof IntegerType) {
                return "IntegerType";
            } else if (mibType instanceof NullType) {
                throw new UnsupportedOperationException("Not yet implemented for class " + mibType.getClass().getName());
            } else if (mibType instanceof ObjectIdentifierType) {
                throw new UnsupportedOperationException("Not yet implemented for class " + mibType.getClass().getName());
            } else if (mibType instanceof RealType) {
                return "RealType";
            } else if (mibType instanceof SequenceOfType) {
                throw new UnsupportedOperationException("Not yet implemented for class " + mibType.getClass().getName());
            } else if (mibType instanceof SequenceType) {
                throw new UnsupportedOperationException("Not yet implemented for class " + mibType.getClass().getName());
            } else if (mibType instanceof SnmpType) {
                SnmpType snmpType = (SnmpType)mibType;

                if (snmpType instanceof SnmpAgentCapabilities) {
                    throw new UnsupportedOperationException("Not yet implemented for class " + snmpType.getClass().getName());
                } else if (snmpType instanceof SnmpModuleCompliance) {
                    throw new UnsupportedOperationException("Not yet implemented for class " + snmpType.getClass().getName());
                } else if (snmpType instanceof SnmpModuleIdentity) {
                    throw new UnsupportedOperationException("Not yet implemented for class " + snmpType.getClass().getName());
                } else if (snmpType instanceof SnmpNotificationGroup) {
                    throw new UnsupportedOperationException("Not yet implemented for class " + snmpType.getClass().getName());
                } else if (snmpType instanceof SnmpNotificationType) {
                    throw new UnsupportedOperationException("Not yet implemented for class " + snmpType.getClass().getName());
                } else if (snmpType instanceof SnmpObjectGroup) {
                    throw new UnsupportedOperationException("Not yet implemented for class " + snmpType.getClass().getName());
                } else if (snmpType instanceof SnmpObjectIdentity) {
                    throw new UnsupportedOperationException("Not yet implemented for class " + snmpType.getClass().getName());
                } else if (snmpType instanceof SnmpObjectType) {
                    throw new UnsupportedOperationException("Not yet implemented for class " + snmpType.getClass().getName());
                } else if (snmpType instanceof SnmpTextualConvention) {
                    throw new UnsupportedOperationException("Not yet implemented for class " + snmpType.getClass().getName());
                } else if (snmpType instanceof SnmpTrapType) {
                    throw new UnsupportedOperationException("Not yet implemented for class " + snmpType.getClass().getName());
                } else {
                    throw new UnsupportedOperationException("Unknown SnmpType subclass " + snmpType.getClass().getName());
                }
            } else if (mibType instanceof StringType) {
                return "StringType";
            } else if (mibType instanceof TypeReference) {
                throw new UnsupportedOperationException("Not yet implemented for class " + mibType.getClass().getName());
            } else {
                throw new UnsupportedOperationException("Unknown MibType subclass " + mibType.getClass().getName());
            }
        }
    } else if (mibValue instanceof StringValue) {
        throw new UnsupportedOperationException("Not yet implemented for class " + mibValue.getClass().getName());
    } else if (mibValue instanceof ValueReference) {
        throw new UnsupportedOperationException("Not yet implemented for class " + mibValue.getClass().getName());
    } else {
        throw new UnsupportedOperationException("Unknown MibValue subclass " + mibValue.getClass().getName());
    }
}

and here is the example output to it:

linkDown 2 TRAP-TYPE enterprise: snmp 1.3.6.1.2.1.11 variables: ifIndex 1.3.6.1.2.1.2.2.1.1 UNSUPPORTED description: A linkDown trap signifies that the sending protocol entity recognizes a failure in one of the communication links represented in the agent's configuration. symbol comment:

TYPE "Link Down"

      #SUMMARY "Link Down."
      #ARGUMENTS {0}
      #SEVERITY INFORMATIONAL
      #TIMEINDEX 99

linkUp 3 TRAP-TYPE enterprise: snmp 1.3.6.1.2.1.11 variables: ifIndex 1.3.6.1.2.1.2.2.1.1 UNSUPPORTED description: A linkUp trap signifies that the sending protocol entity recognizes that one of the communication links represented in the agent's configuration has come up. symbol comment:

TYPE "Link Up"

      #SUMMARY "Link Up."
      #ARGUMENTS {0}
      #SEVERITY INFORMATIONAL
      #TIMEINDEX 99

Mit freundlichen Grüßen / Best regards

SMS group GmbH

Stefan Strömer LPST Technologie Nahtlosanlagen

-- Ohlerkirchweg 66, 41069 Moenchengladbach / Germany Telefon/Phone: +49 2161 350 1813 Telefax/Fax: +49 2161 350 1225 E-Mail: stefan.stroemer@sms-group.com

Von: Per Cederberg notifications@github.com Gesendet: Montag, 11. Januar 2021 19:43 An: cederberg/mibble mibble@noreply.github.com Cc: Strömer, Stefan (SMS group GmbH) Stefan.Stroemer@sms-group.com; Author author@noreply.github.com Betreff: Re: [cederberg/mibble] Missing ReferenceSymbol on variable MibValue retrieved from SnmpTrapType Object (#39)

If you try the MibblePrinter utility, you'll see the following output for a TRAP-TYPE:

VALUE frDLCIStatusChange TRAP-TYPE (

Enterprise: 1.3.6.1.2.1.10.32

Variables: [1.3.6.1.2.1.10.32.2.1.1, 1.3.6.1.2.1.10.32.2.1.2, 1.3.6.1.2.1.10.32.2.1.3]

...

From the example above, you'll note that the variables have been resolved to their OID values. So the list of MibValue objects are actually ObjectIdentifierValue instances (but always double-check with instanceof). From such a value, you can easily get the symbol you want with oid.getSymbol()

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/cederberg/mibble/issues/39#issuecomment-758145814, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ASNVKCA26R3GWEZGV5MA4HDSZNBB5ANCNFSM4V5Z7V5Q.

SMS group GmbH Vorsitzender des Aufsichtsrates: Edwin Eichler Geschaeftsfuehrung: Burkhard Dahmen, Vorsitzender Dr. Hans Ferkel, Torsten Heising, Michael Rzepczyk, Prof. Dr.-Ing. Katja Windt

Sitz der Gesellschaft: Duesseldorf Registergericht: Amtsgericht Duesseldorf, HRB 75040 USt-IdNr: DE811136276

Wir verarbeiten personenbezogene Daten über unsere Geschaeftspartner, Bewerber und Besucher unserer Webseite in Uebereinstimmung mit den Datenschutzgesetzen. Einzelheiten zu uns, unserer Verarbeitung, den Rechtsgrundlagen und Ihren Rechten entnehmen Sie bitte unseren Datenschutzhinweisen Datenschutz | SMS group https://www.sms-group.com/de/legal/datenschutz/.

cederberg commented 3 years ago

I think you're looking for something like:

ObjectIdentifierValue oid = (ObjectIdentifierValue) variable;
MibValueSymbol sym = oid.getSymbol();
MibType type = sym.getType();
if (type.hasTag(MibTypeTag.OCTET_STRING)) {
    // variable is string...
} else if (type.hasTag(MibTypeTag.INTEGER)) {
    // variable is integer...
} // ...

Reason for using type tags and not instanceof here, is because these variables are OBJECT-TYPE macros, so there is another level of indirection to follow otherwise (via SnmpObjectType instance or similar). Type tags are also used to distinguish between various types of binary strings. See the Java API FAQ for some more examples — https://www.mibble.org/doc/faq-java-api.html#Q6

stroeste-sms-group commented 3 years ago

Hey,

sorry to write again on that subject, but checking the type tags doesn’t help.

I followed your description in the FAQ, the following turn out to be fact.

  1. the variable is an instance of ObjectIdendifierValue
  2. none of the .hasTag() checks on the MibType of the corresponding MibValueSymbol returns true
  3. The MibType is in fact an instance of SnmpType, more specific SnmpObjectType

I attached a zip of my whole testing project this time. What am I missing, or is it a bug this time?

Mit freundlichen Grüßen / Best regards

Stefan Strömer

mibblePlayground.zip