Counter64

 

Counter64 is similar to Counter32, except the range is now (0 to 18446744073709551615). This type may only be used when a 32-bit counter rollover could occur in less than an hour. It may be used only when backward compatibility is not a requirement because this type is not available in SNMPv1.

 

The Counter64 value has 64 bits (unsigned long in Java). As Java does not support unsigned data type, Counter64 is stored as an array of two longs in the SnmpCounter64 object each containing 32-bit value. Of the total 8 bytes of the Counter64 value, long[1] contains the high order 32 bit value of the Counter64 data and long[0] contains the low order 32 bit value. Therefore, if you need to construct Counter64 object using long array, you can split two long values with high and low 32 bits.

 

Create Variable

 

Using SnmpVar class

 

The SnmpVar class is the base class for all SNMP variable classes. The createVariable(String value, byte type)api.jpg method creates a new SnmpVar object with the specified type and value.

 

String value = "16446740073709451310";

SnmpVar snmpvar = SnmpVar.createVariable(value, SnmpAPI.COUNTER64)

 

The string value should only be in decimal.

 

Valid Usage

 

SnmpVar var = SnmpVar.createVariable("1", SnmpAPI.COUNTER64);

SnmpVar var = SnmpVar.createVariable("18446744073709551615", SnmpAPI.COUNTER64);

SnmpVar var = SnmpVar.createVariable("118446744073709551615", SnmpAPI.COUNTER64);

 

The last value clearly exceeds the maximum of Counter64. The least significant 8 bytes are taken as the real value.

 

Invalid Usage

 

SnmpVar var = SnmpVar.createVariable("123ab3af", SnmpAPI.COUNTER64);

 

The above usage is invalid because the value should be given only in decimal and not in hexadecimal.

 

SnmpVar var = SnmpVar.createVariable("abdck", SnmpAPI.COUNTER64);

 

The above usage is invalid because the value should be given as a decimal number and not as a string.

 

Using SnmpCounter64 Class

 

We can use the SnmpCounter64 constructor which takes BigInteger value as argument to construct the SnmpCounter64 object as follows.

 

SnmpCounter64 counter = the value;

long[] array = (long[])counter.getVarObject();

BigInteger big = new BigInteger(Long.toString(array[1], 16), 16);

big = big.shiftLeft(32);

big = big.or(new BigInteger(Long.toString(array[0], 16), 16));

 

You can also get the BigInteger using the toBigInteger() method. The following table shows the values that are present in the long array.

 

Value of SnmpCounter64 in Hex Long[1]

Long[0]

0

0

0

aa aa

0

aa aa

aa aa aa aa

0

aa aa aa aa

bb aa aa aa aa

bb

aa aa aa aa

bb bb aa aa aa aa

bb bb

aa aa aa aa

ff ff ff ff ee ee ee ee

ff ff ff

ee ee ee ee

 

Valid Usage

 

java.math.BigInteger big = new java.math.BigInteger("1234567890");

SnmpCounter64 counter64 = new SnmpCounter64(big);

java.math.BigInteger big = new java.math.BigInteger("1234567890");

byte[] array = big.toByteArray();

SnmpCounter64 counter64 = new SnmpCounter64(array);

long[] array = { 0xffL , 0xffffffffL };

SnmpCounter64 counter64 = new SnmpCounter64(array);

 

Invalid Usage

 

long[] array = { 0x1f, 0xffL , 0xffffffffL };

SnmpCounter64 counter64 = new SnmpCounter64(array);

 

In the above usage, the long array passed in should be of length 2.

 

Using mibs package

 

The following code snippet creates an SnmpVar object of Counter64 data type using the LeafSyntaxapi.jpg class.

 

String mibFile = "<mib name>";

String moduleName = "IF-MIB";

String nodeName = "ifHCInOctets";

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 = "18446744073709551615";

SnmpVar snmpvar = syntax.createVariable(value);

 

Valid Usage

 

SnmpVar snmpvar = syntax.createVariable("'1111 1111 1111 1111 1111 1111 1111 1111

1111 1111 1111 1111 1111 1111 1111 1111'b");

SnmpVar snmpvar = syntax.createVariable("'FFFFFFFFFFFFFFFF'h");

 

note.jpg

Note: The value can be given in decimal, hex or binary format while sending an SNMP request because only the least significant 4 bytes are taken.

 

Retrieve Value from Variable

 

Using SnmpVar Class

 

Following are the methods of SnmpCounter64api.jpg class that are used to retrieve value from the created variable.

 

1. The getVarObject() returns the SnmpCounter64 value as an long array of length 2.

 

BigInteger big = new BigInteger("1234567890");

SnmpCounter64 counter64 = new SnmpCounter64(big);

long[] longarray = (long[]) counter64.getVarObject();

 

This long array will be of length two and the long array[1] will contain the most significant 4 bytes and the longarray[0] will contain the least significant 4 bytes.

longarray[1] = 0x00;

longarray[1] = 0x499602d2;

 

2. The toValue() method returns the SnmpCounter64 value as an object of long array of length 2.

 

long[] longarray = (long[]) counter64.getVarObject();

 

This long array will be of length two and the long array[1] will contain the most significant 4 bytes and the longarray[0] will contain the least significant 4 bytes.

longarray[1] = 0x00;

longarray[1] = 0x499602d2;

 

3. The toBytes() method returns the value as bytes.

 

byte[] array = counter64.toBytes();

 

This byte array will contain the value split in the form of byte array of length 8. The byte array will contain the following bytes.

array[0] = 0x00;

array[1] = 0x00;

array[2] = 0x00;

array[3] = 0x00;

array[4] = 0x49;

array[5] = 0x96;

array[6] = 0x02;

array[7] = 0xD2;

 

4. The toString() method returns the value as a printable form.

 

String str = counter64.toString();

 

This string will return the value 0x499602d2.

 

5. The toTagString() method returns the Counter64 value with the tag "COUNTER64" attached to it.

 

String tagstring = snmpoid.toTagString();

 

This string contains the value "COUNTER64: 0x499602d2".

 

6. The longValue() method returns the SnmpCounter value as an "long"

 

long value = counter32.longValue();

 

Using mibs package

 

The toString()api.jpg and toByteString()api.jpg 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 "18446744073709551615".

 

String value = mibOps.toString(snmpvarbind);

 

This string contains the value "ifHCInOctets:-->18446744073709551615".

 

String value = mibOps.toByteString(snmpvar, snmpoid);

 

This string contains the value "18446744073709551615".

 

String value = mibOps.toByteString(snmpvarbind);

 

This string contains the value "ifHCInOctets:-->18446744073709551615".

 

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.ifMIB.ifMIBObjects.ifXTable.ifXEntry.ifHCInOctets

COUNTER64: 18446744073709551615

 

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.ifMIB.ifMIBObjects.ifXTable.ifXEntry.ifHCInOctets

COUNTER64: 18446744073709551615

 

String value = mibOps.toTagString(snmpvarbind);

 

This string contains the following value.

Object ID:

.iso.org.dod.internet.mgmt.mib-2.ifMIB.ifMIBObjects.ifXTable.ifXEntry.ifHCInOctets

COUNTER64: 18446744073709551615



Copyright © 2012, ZOHO Corp. All Rights Reserved.