Counter32

 

Counter32 specifies a value which represents a count. The range is 0 to 4294967295.

 

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

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

 

The string value should only be in decimal.

 

Valid Usage

 

SnmpVar snmpvar = SnmpVar.createVariable("4294967295", SnmpAPI.COUNTER);// 0xffffffff

SnmpVar snmpvar = SnmpVar.createVariable("0", SnmpAPI.COUNTER);

SnmpVar snmpvar = SnmpVar.createVariable("4294967296", SnmpAPI.COUNTER);// 0x100000000

 

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 snmpvar = SnmpVar.createVariable("test", SnmpAPI.COUNTER);

 

The above usage is invalid because only decimal values are accepted.

 

SnmpVar snmpvar = SnmpVar.createVariable("429496729542949672954294967295", SnmpAPI.COUNTER);

 

The above usage is invalid because, the value given exceeds the maximum limit of the "long" value.

 

SnmpVar snmpvar = SnmpVar.createVariable("0x123abc", SnmpAPI.COUNTER);

 

The above usage is invalid because only decimal values are accepted and not any hexadecimal values. For all of the above invalid usages, an SnmpException is thrown.

 

Using SnmpCounter class

 

The constructor of this class creates an SnmpCounterapi.jpg object by taking a long type as argument.

 

long value = 406744073709551600;

SnmpCounter counter32 = new SnmpCounter(value);

 

Using mibs package

 

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

 

String mibFile = "<mib name>";

String moduleName = "IF-MIB";

String nodeName = "ifInOctets";

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

SnmpVar snmpvar = syntax.createVariable(value);

 

Valid Usage

 

SnmpVar snmpvar = syntax.createVariable("65535");

SnmpVar snmpvar = syntax.createVariable("'1111111111111111'B");

SnmpVar snmpvar = syntax.createVariable("'FFFF'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 SnmpCounterapi.jpg class that are used to retrieve value from the created variable.

 

1. The getVarObject() returns the SnmpCounter value as Long object.

 

long value = 0xffffffffL; // the maximum value of an Unsigned32

SnmpCounter counter32 = new SnmpCounter(value);

Long obj = (Long)counter32.getVarObject();

 

2. The toValue() method returns the SnmpCounter value as Long object.

 

Long obj = (Long)counter32.toValue();

 

3. The toBytes() method returns the integer as a byte array of length 4.

 

byte[] array = counter32.toBytes();

 

This byte array will contain the following values.

array[0] = 0xff;

array[1] = 0xff;

array[2] = 0xff;

array[3] = 0xff;

 

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

 

String value = counter32.toString();

 

This string contains the value "4294967295".

 

5. The toTagString() method returns the integer value in decimal with the tag "Counter" attached to it.

 

String tagstring = counter32.toTagString();

 

This string contains the value "Counter: 4294967295".

 

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 "4294967295".

 

String value = mibOps.toString(snmpvarbind);

 

This string contains the value "ifInOctets:-->4294967295".

 

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

 

This string contains the value "4294967295".

 

String value = mibOps.toByteString(snmpvarbind);

 

This string contains the value "ifInOctets:-->4294967295".

 

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.interfaces.ifTable.ifEntry.ifInOctets

COUNTER: 4294967295

 

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.interfaces.ifTable.ifEntry.ifInOctets

COUNTER: 4294967295

 

String value = mibOps.toTagString(snmpvarbind);

 

This string contains the following value.

Object ID:

.iso.org.dod.internet.mgmt.mib-2.interfaces.ifTable.ifEntry.ifInOctets

COUNTER: 4294967295

 

note.jpg

Note: In high-level API, these objects do not have separate classes because they are handled internally.



Copyright © 2012, ZOHO Corp. All Rights Reserved.