16.3 Fetching All SNMP Objects into an Array


You want to fetch all the SNMP objects from an agent into an array.

Technique

Use the snmpwalk () function, which does exactly that:

 <?php $obj_array = snmpwalk("localhost", "public", ""); while ($idx < count($obj_array)) {     echo "$idx. $obj_array[$idx]\n"; } ?> 

Comments

The snmpwalk() function returns an array of SNMP object values starting from the object ID. (If the third argument is left as an empty string "" , all objects are fetched .)

If you want to fetch all objects into an associative array where the object ID is the key and the value is the corresponding SNMP object value, use the snmpwalkoid() function:

 <?php $obj_array = snmpwalkoid("localhost", "public", ""); while (list($object_id, $object_value) = each($obj_array)) {     echo "Object ID: $object_id\n<br>\n";     echo "Object Value: $object_value\n<br>\n<br>\n"; } ?> 

Please note that the snmpwalk() and snmpwalkoid() functions have two optional arguments: the timeout argument and the retry argument. The timeout argument, as its name suggests, is an integer value of how long to wait before timing out. If this argument is not set, it defaults to SNMP_DEFAULT_TIMEOUT or, in other words, it never times out. The retry argument is the number of times to retry the operation until giving up and reporting an error. If no argument is supplied, the default is SNMP_DEFAULT_RETRIES ; or, in other words, one strike and you're out.



PHP Developer's Cookbook
PHP Developers Cookbook (2nd Edition)
ISBN: 0672323257
EAN: 2147483647
Year: 2000
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net