|
OBJECT IDENTIFIER identifies a type that has an assigned Object Identifier value.
Create Variable
Using SnmpVar class
The SnmpVar class is the base class for all SNMP variable classes. The createVariable(String value, byte type)
method creates a new SnmpVar object with the specified type and value. If the OID does not start with a dot, the string ".1.3.6.1.2.1." is prefixed to the OID.
|
String value = ".1.3.6.1.2.1.1.2"; SnmpVar snmpvar= SnmpVar.createVariable(value, SnmpAPI.OBJID); |
Valid Usage
|
SnmpVar var = SnmpVar.createVariable("1.1.0", SnmpAPI.OBJID); SnmpVar var = SnmpVar.createVariable(".1.3.6.1.2.1.1.5.0", SnmpAPI.OBJID); |
The above usage should be used carefully. The given value will be converted to zero while sending an SNMP request because only the least significant 4 bytes are taken.
Invalid Usage
|
SnmpVar var = SnmpVar.createVariable("sysDescr", SnmpAPI.OBJID); /td> |
The above usage is invalid because, the string argument will accept OIDs only in dotted form and not as a string.
|
SnmpVar var = SnmpVar.createVariable(".1.3.6.1.2.1.abcd", SnmpAPI.OBJID); |
The above usage is invalid because, each of the sub-identifier should be an integer in decimal form.
|
SnmpVar var = SnmpVar.createVariable(".1a.b3.6d.ff.1.2.a", SnmpAPI.OBJID); |
The above usage is invalid because, each of the sub-identifier should be an integer in decimal form and not in hexa decimal form.
Using SnmpOID class
The constructor of this class creates an SnmpOID
object by taking String as argument.
|
String value = ".1.3.6.1.2.1.1.2"; SnmpOID snmpoid = new SnmpOID(value); |
The following command also creates an SnmpOID object.
|
int[] oids = {1, 3, 6, 1, 2, 1, 1, 1, 0}; SnmpOID oid = new SnmpOID(oids); |
Using mibs package
The following code snippet creates an SnmpVar object of OBJECT IDENTIFIER data type using the LeafSyntax
class.
|
String mibFile = "<mib name>"; String moduleName = "<RFC1213-MIB>"; String nodeName = "sysObjectID"; MibOperations mibOps = new MibOperations(); mibOps.loadMibModule(mibFile); MibModule module = mibOps.getMibModule(moduleName); SnmpOID snmpoid = mibOps.getSnmpOID(nodeName); MibNode node = module.getMibNode(snmpoid); LeafSyntax syntax = node.getSyntax(); String value = ".1.3.6.1.2.1.1.1.0"; //valid numbered OID SnmpVar snmpvar = syntax.createVariable(value); |
Valid Usage
|
SnmpVar snmpvar = syntax.createVariable(".1.3.6.1.2.1.1.1.0"); SnmpVar snmpvar = syntax.createVariable("1.1.0"); |
|
|
Note: If the OID does not start with a dot, the string ".1.3.6.1.2.1." is prefixed to the OID. |
Retrieve Value from Variable
Using SnmpVar Class
Following are the methods of SnmpOID
class that are used to retrieve value from the created variable.
1. The getVarObject() returns the OBJECT IDENTIFIER value as Long object.
|
String str = ".1.3.6.1.2.1.1.1.0"; SnmpOID oid = new SnmpOID(str); String obj = (String)snmpoid.getVarObject(); |
This string contains the value ".1.3.6.1.2.1.1.1.0".
2. The toValue() method returns the OBJECT IDENTIFIER value as an array of ints.
|
int[] obj = (int[])snmpoid.toValue(); |
This integer array contains the following values in the given order.
obj[0] = 1;
obj[1] = 3;
obj[2] = 6;
obj[3] = 1;
obj[4] = 2;
obj[5] = 1;
obj[6] = 1;
obj[7] = 1;
obj[8] = 0;
3. The toBytes() method returns the OBJECT IDENTIFIER as a byte array.
|
byte[] array = snmpoid.toBytes(); |
This byte array will contain the following values.
array[0] = 0x00; array[1] = 0x00; array[2] = 0x00; array[3] = 0x01;
array[4] = 0x00; array[5] = 0x00; array[6] = 0x00; array[7] = 0x03;
array[8] = 0x00; array[9] = 0x00; array[10] = 0x00; array[11] = 0x06;
array[12] = 0x00; array[13] = 0x00; array[14] = 0x00; array[15] = 0x01;
array[16] = 0x00; array[17] = 0x00; array[18] = 0x00; array[19] = 0x02;
array[20] = 0x00; array[21] = 0x00; array[22] = 0x00; array[23] = 0x01;
array[24] = 0x00; array[25] = 0x00; array[26] = 0x00; array[27] = 0x01;
array[28] = 0x00; array[29] = 0x00; array[30] = 0x00; array[31] = 0x01;
array[32] = 0x00; array[33] = 0x00; array[34] = 0x00; array[35] = 0x00;
Here each of the value in the integer array will be converted into an array of 4 bytes.
4. The toString() method returns the OBJECT IDENTIFIER value as a printable form.
|
String value = snmpoid.toString(); |
This string will contain the value ".1.3.6.1.2.1.1.1.0".
5. The toTagString() method returns the integer value in decimal with the tag "Object ID" attached to it.
|
String tagstring = snmpoid.toTagString(); |
This string will contain the value "Object ID: .1.3.6.1.2.1.1.1.0".
Using mibs package
The toString()
and toByteString()
methods of the MibOperations class are used to retrieve the value of the variable as String.
The following commands retrieve the value of the created variable.
|
String value = mibOps.toString(snmpvar, snmpoid); |
This string contains the value ".iso.org.dod.internet.mgmt.mib-2.system.sysDescr.0".
|
String value = mibOps.toString(snmpvarbind); |
This string contains the value "sysObjectID:-->.iso.org.dod.internet.mgmt.mib-2.system.sysDescr.0".
|
String value = mibOps.toByteString(snmpvar, snmpoid); |
This string contains the value ".iso.org.dod.internet.mgmt.mib-2.system.sysDescr.0".
|
String value = mibOps.toByteString(snmpvarbind); |
This string contains the value "sysObjectID:-->.iso.org.dod.internet.mgmt.mib-2.system.sysDescr.0".
|
String value = mibOps.toString(snmppdu); |
This string contains the following value.
SNMP PDU (type unknown/unspecified)
SNMP Version: Version 1
Remote Port: 0
Community: null
Request ID: 0
Timeout: 0
Retries: 0
Error Status: no error
SNMP PDU Variable Bindings:
Object ID: .iso.org.dod.internet.mgmt.mib-2.system.sysObjectID
OBJID: .iso.org.dod.internet.mgmt.mib-2.system.sysDescr.0
|
String value = mibOps.toByteString(snmppdu); |
This string contains the following value.
SNMP PDU (type unknown/unspecified)
SNMP Version: Version 1
Remote Port: 0
Community: null
Request ID: 0
Timeout: 0
Retries: 0
Error Status: no error
SNMP PDU Variable Bindings:
Object ID: .iso.org.dod.internet.mgmt.mib-2.system.sysObjectID
OBJID: .iso.org.dod.internet.mgmt.mib-2.system.sysDescr.0
|
String value = mibOps.toTagString(snmpvarbind); |
This string contains the following value.
Object ID: .iso.org.dod.internet.mgmt.mib-2.system.sysObjectID
OBJID: .iso.org.dod.internet.mgmt.mib-2.system.sysDescr.0
|
|
Note: In high-level API, these objects do not have separate classes because they are handled internally. |
|