|
DateAndTime is a standard Textual Convention which is defined in the SNMPv2-TC. DateAndTime is resolved to base data type OCTET STRING. The DISPLAY-HINT format for DataAndTime is given as follows.
DISPLAY-HINT "2d-1d-1d,1d:1d:1d.1d,1a1d:1d"
The date-time specification is as follows.
|
Field |
Octets |
Contents |
Range |
|---|---|---|---|
|
1 |
1-2 |
year |
0..65536 |
|
2 |
3 |
month |
1..12 |
|
3 |
4 |
day |
1..31 |
|
4 |
5 |
hour |
0..23 |
|
5 |
6 |
minutes |
0..59 |
|
6 |
7 |
seconds (use 60 for leap-second) |
0..60 |
|
7 |
8 |
deci-seconds |
0..9 |
|
8 |
9 |
direction from UTC |
'+' / '-' |
|
9 |
10 |
hours from UTC |
0..13 |
|
10 |
11 |
minutes from UTC |
0..59 |
|
|
Note: The value of year is in network-byte order.
|
Create Variable with DateAndTime Syntax
Using mibs package
For creating a value for a node, the createVariable() method in the LeafSyntax class is used. The DateAndTime format value can be either 8 or 11 bytes. If the value is given in hex format, each octets should be separated by a colon and the value should be enclosed within single quotes.
The following code snippet creates an SnmpVar object of DateAndTime type using the LeafSyntax
class.
|
String mibFile = "<mib name>"; String moduleName = "HOST-RESOURCES-MIB"; String nodeName = "hrSystemDate"; 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 = "2002-9-3,12:20:32.3,+5:30"; SnmpVar snmpvar = syntax.createVariable(value); |
Valid Usage
|
SnmpVar snmpvar = syntax.createVariable("'07:D2:09:03:0C:14:20:03:2B:07:00'");//hex format, length 11 bytes SnmpVar snmpvar = syntax.createVariable("'07:D2:09:03:0C:14:20:03'");//hex format, length 8 bytes SnmpVar snmpvar = syntax.createVariable("2002-9-21,13:53:32.3,-7:0");//string format, length 11 bytes SnmpVar snmpvar = syntax.createVariable("2002-9-21,13:53:32.3");//string format, length 8 bytes |
|
|
Note: The hex value should be enclosed with the single quotes. |
If the value is in any of the above formats, the value is recognized as DateAndTime value. In the first format, each octets separated by colon is considered as a byte and these bytes are stored in a byte array of length 8 or 11. If the value is in the second format, the value is translated to hex string based on DISPLAY-HINT and the bytes are stored in the byte array.
Retrieve Value from Variable
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 "2002-9-3,12:20:32.3,+5:30".
|
String value = mibOps.toString(snmpvarbind); |
This string contains the value "hrSystemDate:-->2002-9-3,12:20:32.3,+5:30".
|
String value = mibOps.toByteString(snmpvar, snmpoid); |
This string contains the value "2002-9-3,12:20:32.3,+5:30".
|
String value = mibOps.toByteString(snmpvarbind); |
This string contains the value "hrSystemDate:-->2002-9-3,12:20:32.3,+5:30".
|
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.host.hrSystem.hrSystemDate
STRING: 2002-9-3,12:20:32.3,+5:30
|
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.host.hrSystem.hrSystemDate
STRING: 2002-9-3,12:20:32.3,+5:30
|
String value = mibOps.toTagString(snmpvarbind); |
This string contains the following value.
Object ID: .iso.org.dod.internet.mgmt.mib-2.host.hrSystem.hrSystemDate
STRING: 2002-9-3,12:20:32.3,+5:30
|