How to pretty print a row index?
Last modified by Frank Fock on 2024/05/25 12:55
A SNMP table row index is technically an OID suffix. Thus it is a sequence of numbers between 0 and 2^32-1 separated by dots.
The standard output when using indexOID.toString() is then based on the initialisation code below:
Sample Initialisation Code
// Sample Index OID definitions
public static final OID oidId = new OID(new int[] { 1,3,6,1,4,1,4976,1,1,1,4,4,2,1,1 });
public static final OID oidVersion = new OID(new int[] { 1,3,6,1,4,1,4976,1,1,1,4,4,2,1,2 });
public static final OID oidInstance = new OID(new int[] { 1,3,6,1,4,1,4976,1,1,1,4,4,2,1,3 });
DefaultMOFactory moFactory = new DefaultMOFactory();
MOTableSubIndex[] moTableSubIndices =
new MOTableSubIndex[] {
moFactory.createSubIndex(oidId, SMIConstants.SYNTAX_OCTET_STRING, 1, 32),
moFactory.createSubIndex(oidVersion,
SMIConstants.SYNTAX_OCTET_STRING, 11, 12),
moFactory.createSubIndex(oidInstance, SMIConstants.SYNTAX_INTEGER, 1, 1) };
MOTableIndex moTableIndex = new MOTableIndex(moTableSubIndices, false);
Variable[] testIndex = new Variable[] {
new OctetString("TestSignal"),
new OctetString("V01.01.01.0"),
new Integer32(5)
};
OID indexOID = moTableIndex.getIndexOID(testIndex);
public static final OID oidId = new OID(new int[] { 1,3,6,1,4,1,4976,1,1,1,4,4,2,1,1 });
public static final OID oidVersion = new OID(new int[] { 1,3,6,1,4,1,4976,1,1,1,4,4,2,1,2 });
public static final OID oidInstance = new OID(new int[] { 1,3,6,1,4,1,4976,1,1,1,4,4,2,1,3 });
DefaultMOFactory moFactory = new DefaultMOFactory();
MOTableSubIndex[] moTableSubIndices =
new MOTableSubIndex[] {
moFactory.createSubIndex(oidId, SMIConstants.SYNTAX_OCTET_STRING, 1, 32),
moFactory.createSubIndex(oidVersion,
SMIConstants.SYNTAX_OCTET_STRING, 11, 12),
moFactory.createSubIndex(oidInstance, SMIConstants.SYNTAX_INTEGER, 1, 1) };
MOTableIndex moTableIndex = new MOTableIndex(moTableSubIndices, false);
Variable[] testIndex = new Variable[] {
new OctetString("TestSignal"),
new OctetString("V01.01.01.0"),
new Integer32(5)
};
OID indexOID = moTableIndex.getIndexOID(testIndex);
Sample Output
System.out.println("> "+indexOID);
> 10.84.101.115.116.83.105.103.110.97.108.11.86.48.49.46.48.49.46.48.49.46.48.5
System.out.println("> "+Arrays.toString(moTableIndex.getIndexValues(indexOID)));
> [TestSignal, V01.01.01.0, 5]
> 10.84.101.115.116.83.105.103.110.97.108.11.86.48.49.46.48.49.46.48.49.46.48.5
System.out.println("> "+Arrays.toString(moTableIndex.getIndexValues(indexOID)));
> [TestSignal, V01.01.01.0, 5]
If you use SNMP4J-SMI-PRO in your agent/application, you can even get more formatting, because then also DISPLAY-HINT information of the MIB can be used to format the index values (for SNMP4J-SMI-PRO initialisation see "How to use the extended features SNMP4J-SMI-PRO provides?"):
SmiManager smiManager = new SmiManager(...);
// Register SNMP4J-SMI-PRO to SNMP4J(-Agent):
SNMP4JSettings.setOIDTextFormat(smiManager);
SNMP4JSettings.setVariableTextFormat(smiManager);
smiManager.setOidFormat(SmiManager.OIDFormat.ObjectNameAndDecodedIndex4RoundTrip);
// load SNMP-VIEW-BASED-ACM-MIB (needs to be compiled from SMIv2 source before, see link above)
smiManager.loadModule("SNMP-VIEW-BASED-ACM-MIB");
OID vacmViewTreeFamilyEntryOID = new OID(new int[]{1, 3, 6, 1, 6, 3, 16, 1, 5, 2, 1});
OID vacmViewTreeFamilyMaskColumnOID = new OID("3");
OID vacmViewTreeFamilyRowIndex= new OID("22.117.110.114.101.115.116.114.105.99.116.101.100.39.46.82.101.97.100.86.105.101.119.3.1.3.6");
OID vacmViewTreeFamilyMaskInstanceOID = vacmViewTreeFamilyEntryOID.append(vacmViewTreeFamilyMaskColumnOID).append(vacmViewTreeFamilyRowIndex);
System.out.println("> "+vacmViewTreeFamilyMaskInstanceOID);
> vacmViewTreeFamilyMask.'unrestricted\'.ReadView'.'dod'
OID vacmAccessContextMatch = new OID("1.3.6.1.6.3.16.1.4.1.4.7.118.51.103.114.111.117.112.0.3.1");
> vacmAccessContextMatch."v3group"."".3.'noAuthNoPriv(1)'
// Register SNMP4J-SMI-PRO to SNMP4J(-Agent):
SNMP4JSettings.setOIDTextFormat(smiManager);
SNMP4JSettings.setVariableTextFormat(smiManager);
smiManager.setOidFormat(SmiManager.OIDFormat.ObjectNameAndDecodedIndex4RoundTrip);
// load SNMP-VIEW-BASED-ACM-MIB (needs to be compiled from SMIv2 source before, see link above)
smiManager.loadModule("SNMP-VIEW-BASED-ACM-MIB");
OID vacmViewTreeFamilyEntryOID = new OID(new int[]{1, 3, 6, 1, 6, 3, 16, 1, 5, 2, 1});
OID vacmViewTreeFamilyMaskColumnOID = new OID("3");
OID vacmViewTreeFamilyRowIndex= new OID("22.117.110.114.101.115.116.114.105.99.116.101.100.39.46.82.101.97.100.86.105.101.119.3.1.3.6");
OID vacmViewTreeFamilyMaskInstanceOID = vacmViewTreeFamilyEntryOID.append(vacmViewTreeFamilyMaskColumnOID).append(vacmViewTreeFamilyRowIndex);
System.out.println("> "+vacmViewTreeFamilyMaskInstanceOID);
> vacmViewTreeFamilyMask.'unrestricted\'.ReadView'.'dod'
OID vacmAccessContextMatch = new OID("1.3.6.1.6.3.16.1.4.1.4.7.118.51.103.114.111.117.112.0.3.1");
> vacmAccessContextMatch."v3group"."".3.'noAuthNoPriv(1)'