SNMP functions
SNMP
In order to use the SNMP functions on Unix you need to install the UCD SNMP package. On Windows these functions
are only available on NT and not on Win95/98.
Important: In order to use the UCD SNMP package, you need to define
NO_ZEROLENGTH_COMMUNITY to 1 before compiling it. After configuring UCD
SNMP, edit config.h and search for NO_ZEROLENGTH_COMMUNITY. Uncomment the
#define line. It should look like this afterwards:
#define NO_ZEROLENGTH_COMMUNITY 1
If you see strange segmentation faults in combination with SNMP commands,
you did not follow the above instructions. If you do not want to recompile
UCD SNMP, you can compile PHP with the --enable-ucd-snmp-hack switch which
will work around the misfeature.
snmpget
Fetch an SNMP object
Description
string snmpget
string hostname
string community
string object_id
int timeout
int retries
Returns SNMP object value on success and false on error.
The snmpget function is used to read the
value of an SNMP object specified by the
object_id. SNMP agent is specified by the
hostname and the read community is
specified by the community parameter.
$syscontact = snmpget("127.0.0.1", "public", "system.SysContact.0")
snmpset
Set an SNMP object
Description
string snmpget
string hostname
string community
string object_id
string type
mixed value
int timeout
int retries
Sets the specified SNMP object value, returning true on success
and false on error.
The snmpset function is used
to set the value of an SNMP object specified by the
object_id. SNMP agent is specified by the
hostname and the read community is specified
by the community parameter.
snmpwalk
Fetch all the SNMP objects from an agent
Description
array snmpwalk
string hostname
string community
string object_id
int timeout
int retries
Returns an array of SNMP object values starting from the
object_id as root and false on error.
snmpwalk function is used to read all the
values from an SNMP agent specified by the
hostname. Community
specifies the read community for that agent. A null
object_id is taken as the root of the SNMP
objects tree and all objects under that tree are returned as an
array. If object_id is specified, all the
SNMP objects below that object_id are
returned.
$a = snmpwalk("127.0.0.1", "public", "");
Above function call would return all the SNMP objects from the
SNMP agent running on localhost. One can step through the values
with a loop
for ($i=0; $i<count($a); $i++) {
echo $a[$i];
}
snmpwalkoid
Query for a tree of information about a network entity
Description
array snmpwalkoid
string hostname
string community
string object_id
int timeout
int retries
Returns an associative array with object ids and their respective
object value starting from the object_id
as root and false on error.
snmpwalkoid function is used to read all
object ids and their respective values from an SNMP agent
specified by the hostname. Community specifies the read
community for that agent. A null
object_id is taken as the root of the SNMP
objects tree and all objects under that tree are returned as an
array. If object_id is specified, all the
SNMP objects below that object_id are
returned.
The existence of snmpwalkoid and
snmpwalk has historical reasons. Both
functions are provided for backward compatibility.
$a = snmpwalkoid("127.0.0.1", "public", "");
Above function call would return all the SNMP objects from the
SNMP agent running on localhost. One can step through the values
with a loop
for (reset($a); $i = key($a); next($a)) {
echo "$i: $a[$i]<br>\n";
}
snmp_get_quick_print
Fetch the current value of the UCD library's quick_print setting
Description
boolean snmp_get_quick_print
void
Returns the current value stored in the UCD Library for quick_print.
quick_print is off by default.
$quickprint = snmp_get_quick_print();
Above function call would return false if
quick_print is on, and true if quick_print
is on.
snmp_get_quick_print is only available when using
the UCD SNMP library. This function is not available when using the
Windows SNMP library.
See: snmp_set_quick_print for a full description
of what quick_print does.
snmp_set_quick_print
Set the value of quick_print within the UCD SNMP library.
Description
void snmp_set_quick_print
boolean quick_print
Sets the value of quick_print within the UCD SNMP library. When this
is set (1), the SNMP library will return 'quick printed' values. This
means that just the value will be printed. When quick_print is not
enabled (default) the UCD SNMP library prints extra information
including the type of the value (i.e. IpAddress or OID). Additionally,
if quick_print is not enabled, the library prints additional hex values
for all strings of three characters or less.
Setting quick_print is often used when using the information returned
rather then displaying it.
snmp_set_quick_print(0);
$a = snmpget("127.0.0.1", "public", ".1.3.6.1.2.1.2.2.1.9.1");
echo "$a<BR>\n";
snmp_set_quick_print(1);
$a = snmpget("127.0.0.1", "public", ".1.3.6.1.2.1.2.2.1.9.1");
echo "$a<BR>\n";
The first value printed might be: 'Timeticks: (0) 0:00:00.00', whereas
with quick_print enabled, just '0:00:00.00' would be printed.
By default the UCD SNMP library returns verbose values, quick_print is
used to return only the value.
Currently strings are still returned with extra quotes, this will be
corrected in a later release.
snmp_set_quick_print is only available when using
the UCD SNMP library. This function is not available when using
the Windows SNMP library.