Reference

Helper Functions

You can use helper functions in scripts within the SNMP walk data file.

long getSysUpTime()

Returns the simulator uptime in centiseconds (1/100 second).

Usage example:

            time = getSysUpTime();

exec(String command)

This function executes an external command.

Usage example:

	exec(“command1.exe”);

int getSubtreeInstanceCount(String subTreeOID)

Returns the number of MIB instances under the specified subtree.

Parameters:

subTreeOID: OID of a MIB tree node

Usage example:

//gets the total number of MIB instances under subtree
//".1.3.6.1.2.1.2.1"
      	count = getSubtreeInstanceCount(".1.3.6.1.2.1.2.1");

void sleep(long milliseconds)

Pauses the current simulator for the specified number of milliseconds.

Parameters:

milliseconds: pause duration, in milliseconds

Usage example:

            sleep(1000);//sleep for 1 second

VarBind createVarBind(String oid, String type, String value)

Creates an SNMP variable binding.

Parameters:

oid: object identifier of the variable binding

value: value of the variable binding, as a string

type: data type of the value

The available types are case-insensitive:

OctetString: octet string

Null: null

OID: object identifier

BITS: SNMP BITS

UnsignedInteger: unsigned integer

Counter32: 32-bit counter

Gauge: 32-bit gauge

TimeTicks: time ticks

IpAddress: ip address

Opaque: opaque

Integer: integer

Counter64: 64-bit counter

Usage example:

        // Creates a variable binding whose OID is ".1.3.6.1.2.1.1.1.5"
// with an integer value of 5
        VarBind varbind1 = createVarBind(".1.3.6.1.2.1.1.1.5", "integer", "5");

void sendV1Trap(int generic, int specific, VarBind varbinds)

Sends an SNMPv1 trap.

Parameters:

generic: value of the generic field

specific: value of the specific field

varbinds: variable bindings

Usage example:

        sendV1Trap(6, 110, null); //null means no variable bindings for this trap
        //A trap with two variable bindings
        sendV1Trap(1, 0, createVarBind(".1.3.6.1.2.1.1.1.5", "integer",

"5").createVarBind(".1.3.6.1.2.1.1.1.5", "timeticks", "51211"));

void sendV2Trap(String snmpTrapOid, VarBind varbinds)

Sends an SNMPv2 trap.

Parameters:

snmpTrapOid: OID value of the snmpTrapOid object

varbinds: variable bindings

Usage example:

        //create a trap with only one variable binding
        sendV2Trap(".1.3.6.1.4.1155.1", createVarBind(".1.3.6.1.2.1.1.1.5",

"integer", "5"));

     //create a trap with two variable bindings
     sendV2Trap(".1.3.6.1.4.1155.1", createVarBind(".1.3.6.1.2.1.1.1.5",

"integer", "5").createVarBind(".1.3.6.1.2.1.1.1.5", "timeticks",

"51211"));

void sendTrap(String trapName)

Sends an SNMPv1 or SNMPv2 trap to all destinations defined in the simulator configuration file.

Parameters:

trapName: name of a trap node in the data XML file

Usage example:

         sendTrap("trap1");

int atoi(String stringValue)

Converts a string to an integer.

Parameters:

stringValue: string to convert to an integer

Usage example:

        int i = atoi("511");

String itoa(int i)

Converts an integer to a string.

Parameters:

i: integer to convert to a string

Usage example:

        String s = itoa(511);

int random(int uppperbound)

Returns a uniformly distributed pseudorandom integer from 0 (inclusive) to uppperbound (exclusive).

Parameters:

uppperbound: exclusive upper bound; must be positive

Usage example:

        	 i = 1000 + random(500);

int random(int lowerbound, int uppperbound)

Returns a uniformly distributed pseudorandom integer from lowerbound (inclusive) to uppperbound (exclusive).

Parameters:

lowerbound: inclusive lower bound; must be positive

uppperbound: exclusive upper bound; must be positive

Usage example:

        	i = 1000 + random(100, 500);

previousValue(initialValue)

Returns the previous value of a MIB instance: the value returned for the preceding SNMP request.

Parameters:

initialValue: initial value of the MIB instance

Usage example:

Object getValue(String oid, SnmpPdu pdu)
{
   int ret = previousValue(50) + 10;
   //for this MIB instance, its first value is 50,
   	   //the second value is 50 + 10 = 60,
	   //the third value is 60 + 10 = 70. ...
    return "" + ret;
}