PHP 8.4.1 Released!

snmprealwalk

(PHP 4, PHP 5, PHP 7, PHP 8)

snmprealwalkВозвращает все объекты, включая их идентификатор

Описание

snmprealwalk(
    string $hostname,
    string $community,
    array|string $object_id,
    int $timeout = -1,
    int $retries = -1
): array|false

Функция snmprealwalk() обходит объекты SNMP, начиная со значения object_id и возвращает не только значения, но и идентификаторы объектов.

Список параметров

hostname

Имя хоста агента (сервера) SNMP.

community

Read-сообщество.

object_id

Идентификатор объекта SNMP, который предшествует желаемому.

timeout

Время ожидания в микросекундах.

retries

Количество повторных попыток при истечении времени ожидания.

Возвращаемые значения

Функция возвращает ассоциативный массив идентификаторов объектов SNMP и их значений в случае успешного выполнения или false, если возникла ошибка. Функция выводит ошибку уровня E_WARNING, если возникла ошибка.

Примеры

Пример #1 Пример использования функция snmprealwalk()

<?php

print_r
(snmprealwalk("localhost", "public", "IF-MIB::ifName"));

?>

Вышеупомянутый пример выведет что-то вроде:

Array
      (
      [IF-MIB::ifName.1] => STRING: lo
      [IF-MIB::ifName.2] => STRING: eth0
      [IF-MIB::ifName.3] => STRING: eth2
      [IF-MIB::ifName.4] => STRING: sit0
      [IF-MIB::ifName.5] => STRING: sixxs
    )

Смотрите также

  • snmpwalk() - Получает все объекты SNMP из агента

Добавить

Примечания пользователей 7 notes

up
1
Lars Troen
21 years ago
snmprealwalk indexes the values using the oid instead of an integer. This is useful when you need data that is contained in the oid as well as the value.

Here's an example for retrieving and printing vlan info:
//
// I have collected the vlan identifiers earlier from the 3com mib and they are stored in the $vlan table.
//
for($n=0;$n<count($vlan);$n++){
print $vlan[$n][id]." ".$vlan[$n][name]."<br>\n";
$ifStackStatusTable=@snmprealwalk($switch, $community, ".1.3.6.1.2.1.31.1.2.1.3.".$vlan[$n][id]); // ifMIB.ifMIBObjects.ifStackTable.ifStackEntry.ifStackStatus
for(reset($ifStackStatusTable); $port = key($ifStackStatusTable); next($ifStackStatusTable)){
print "$port=$ifStackStatusTable[$port]<br>";
}
up
0
Anonymous
16 years ago
To check if there were any results found you have to use the empty() function. The count() function always returns a number 1 or larger.

$walk_result = snmprealwalk($machine_ip, $community, $snmpcodes['interface_names']);

if (empty($walk_result)) {
print "No network interfaces found.<br>\n";
exit(0);
}
up
0
scot at indievisible dot org
18 years ago
Since PHP doesn't have a nice snmptable-like function... here is a quick-and-dirty hack that works for me. Works on complete and sparse tables. The example oids are for the route (complete) and interface (often sparse) tables.

<?php
$a
= snmptable("10.1.1.1", "public", ".1.3.6.1.2.1.4.21") or die("error");
print_r($a);
$a = snmptable("10.1.1.1", "public", ".1.3.6.1.2.1.2.2") or die("error");
print_r($a);

function
snmptable($host, $community, $oid) {
// TODO: get original state and restore at bottom
snmp_set_oid_numeric_print(TRUE);
snmp_set_quick_print(TRUE);
snmp_set_enum_print(TRUE);

$retval = array();
$raw = snmprealwalk($host, $community, $oid) or die("snmptable: unable to walk OID $oid");

$prefix_length = 0;

foreach (
$raw as $key => $value) {
if (
$prefix_length == 0) {
// don't just use $oid's length since it may be non-numeric
$prefix_elements = count(explode('.',$oid));
$tmp = '.' . strtok($key, '.');
while (
$prefix_elements > 1) {
$tmp .= '.' . strtok('.');
$prefix_elements--;
}
$tmp .= '.';
$prefix_length = strlen($tmp);
}
$key = substr($key, $prefix_length);
$index = explode('.', $key, 2);
isset(
$retval[$index[1]]) or $retval[$index[1]] = array();
isset(
$firstrow) or $firstrow = $index[1];
$retval[$index[1]][$index[0]] = $value;
}

// check for holes in the table and fill them in
foreach ($retval[$firstrow] as $key => $tmp) {
foreach(
$retval as $check => $tmp2) {
if (! isset(
$retval[$check][$key])) {
$retval[$check][$key] = '';
}
}
}

return(
$retval);
}
?>
up
0
Stephen Cope
20 years ago
Here's a way to find the uptime and number of users on a machine. (Note that uptime is the uptime of the snmpd daemon, which should be fairly close to the uptime for the host.)

<?php
$state
= snmprealwalk($host, "public", ".1.3.6.1.2.1.25.1", 50, 1);
$uptime = ereg_replace("^.*\) ([0-9]+ .*):[0-9][0-9]\.[0-9]{2}.*$", "\\1", $state['host.hrSystem.hrSystemUptime.0']);
$users = (int)ereg_replace("Gauge32: ", "", $state['host.hrSystem.hrSystemNumUsers.0']);
printf('<div class="machine"><dt>%s</dt><dd>%s</dd>', $host, $desc);
printf('<dd>up %s</dd>', $uptime);
if (
$users ) printf('<dd>%d user%s</dd>', $users, ($users > 1) ? 's' : '');
printf('</div>');
?>
up
-1
aleksander dot sztramski at kpsi dot pl
15 years ago
If you wish to use version 2c or 3, use the following functions:

snmp v2c functions:

snmp2_get (string host, string community, string object_id [, int timeout [, int retries]])
snmp2_getnext (string host, string community, string object_id [, int timeout [, int retries]])
snmp2_walk (string host, string community, string object_id [, int timeout [, int retries]])
snmp2_real_walk (string host, string community, string object_id [, int timeout [, int retries]])
snmp2_set (string host, string community, string object_id, string type, mixed value [, int timeout [, int retries]])

snmp v3 functions:

snmp3_get (string host, string sec_name, string sec_level, string auth_protocol, string auth_passphrase, string priv_pr)
snmp3_getnext (string host, string sec_name, string sec_level, string auth_protocol, string auth_passphrase, string pri)
snmp3_walk (string host, string sec_name, string sec_level, string auth_protocol, string auth_passphrase, string priv_p)
snmp3_real_walk (string host, string sec_name, string sec_level, string auth_protocol, string auth_passphrase, string p)
snmp3_set (string host, string sec_name, string sec_level, string auth_protocol, string auth_passphrase, string priv_pr)
up
-1
railson at amixsi dot com dot br
18 years ago
Attention: timeout is in microseconds (multiply by 1,000,000 for seconds)
up
-2
scot at indievisible dot org
18 years ago
Some improvements based on testing a lot of OIDs on a lot of devices.

<?php
function snmptable($host, $community, $oid) {
// TODO: get original state and restore at bottom
snmp_set_oid_numeric_print(TRUE);
snmp_set_quick_print(TRUE);
snmp_set_enum_print(TRUE);

$retval = array();
$raw = snmprealwalk($host, $community, $oid);
if (
count($raw) == 0) return ($retval); // no data

$prefix_length = 0;
$largest = 0;
foreach (
$raw as $key => $value) {
if (
$prefix_length == 0) {
// don't just use $oid's length since it may be non-numeric
$prefix_elements = count(explode('.',$oid));
$tmp = '.' . strtok($key, '.');
while (
$prefix_elements > 1) {
$tmp .= '.' . strtok('.');
$prefix_elements--;
}
$tmp .= '.';
$prefix_length = strlen($tmp);
}
$key = substr($key, $prefix_length);
$index = explode('.', $key, 2);
isset(
$retval[$index[1]]) or $retval[$index[1]] = array();
if (
$largest < $index[0]) $largest = $index[0];
$retval[$index[1]][$index[0]] = $value;
}

if (
count($retval) == 0) return ($retval); // no data

// fill in holes and blanks the agent may "give" you
foreach($retval as $k => $x) {
for (
$i = 1; $i <= $largest; $i++) {
if (! isset(
$retval[$k][$i])) {
$retval[$k][$i] = '';
}
}
ksort($retval[$k]);
}
return(
$retval);
}
?>
To Top