--- functions.php.orig Tue Feb 11 15:40:05 2003
+++ functions.php Tue Feb 11 15:39:52 2003
@@ -143,6 +143,12 @@
echo "
Error:
\n$in
\n";
}
+function result_fix($val) {
+// This works on strings AND each element of arrays; $val can be either.
+ $replacements=array('/^[^=]+=\s*/', '/"/');
+ return preg_replace($replacements, '', $val);
+}
+
function get_interfaces($hostname, $comm_arr, $method) {
// This returns an array of arrays of IF names keyed on the communities in $comm_arr.
snmp_set_quick_print(1);
@@ -152,9 +158,9 @@
foreach ($comm_arr as $c) {
$c=trim($c);
if ($a = &snmpwalkoid($hostname, $c, $oid[$method])) {
+ $a=result_fix($a);
$out[$c]=array();
- for (reset($a); $k=key($a); next($a)) {
- $tmp=ereg_replace('"', '', $a[$k]);
+ foreach($a as $tmp) {
array_push($out[$c], $tmp);
}
} else {
@@ -222,6 +228,7 @@
$v2="";
// The long number below is for the v2 ifHCInOctets OID, not ifInOctets.
if ($tget=snmpget($h, $c, ".1.3.6.1.2.1.31.1.1.1.6.$i")) {
+ $tget=result_fix($tget);
if (! preg_match('/\D/', trim($tget))) {
$v2="y";
}
@@ -237,10 +244,11 @@
$oid=set_oids("y");
$alias="";
if ($tget=snmpget($h, $c, "{$oid["Alias"]}.$i")) {
+ $tget=result_fix($tget);
$alias=trim($tget);
if (eregi("no such object", $alias)) { $alias=""; }
}
- return ereg_replace('"', '', $alias);
+ return $alias;
}
$font="";
--- snmp_collect.php.orig Tue Feb 11 15:21:41 2003
+++ snmp_collect.php Tue Feb 11 15:25:48 2003
@@ -61,6 +61,9 @@
// Set the ifDescr-to-ifNum map for each host.
if ($debug) {
+ echo "\n--------------------------------------------------------------\n";
+ echo "Debug mode is on; I won't be writing anything to the database.\n";
+ echo "--------------------------------------------------------------\n\n";
echo "Getting name-to-number map for all hosts; please wait...\n"; flush();
$starttime=getmicrotime();
}
@@ -72,12 +75,12 @@
foreach ($interfaces[$h] as $harray) {
$c=$harray["community"];
$method=$harray["method"];
- $a=snmpwalkoid($h, $c, $oid[$method]);
- for(reset($a); $k=key($a); next($a)) {
- $if_num=preg_replace('/^.+\.(\d+)$/', '\\1', $k);
- $tmp=ereg_replace('"', '', $a[$k]);
- if ($tmp == $harray["if_name"]) {
- $if_name[$h][$c][$tmp]=$if_num;
+ $a=result_fix(snmpwalkoid($h, $c, $oid[$method]));
+ foreach(array_keys($a) as $k) {
+ preg_match('/^.+\.(\d+)$/', $k, $regs);
+ $if_num=$regs[1];
+ if ($a[$k] == $harray["if_name"]) {
+ $if_name[$h][$c][$a[$k]]=$if_num;
}
}
}
@@ -86,8 +89,8 @@
}
}
if ($debug) {
- $elapsed=getmicrotime()-$starttime;
- echo "\nMapping time: $elapsed seconds.\n";
+ $elapsed=formattime(getmicrotime()-$starttime);
+ echo "\nMapping time: $elapsed.\n";
}
while (1==1) {
@@ -105,15 +108,14 @@
foreach ($interfaces[$h] as $harray) {
$c=$harray["community"];
$method=$harray["method"];
- $a=snmpwalkoid($h, $c, $oid[$method]);
+ $a=result_fix(snmpwalkoid($h, $c, $oid[$method]));
for(reset($a); $k=key($a); next($a)) {
$if_num=preg_replace('/^.+\.(\d+)$/', '\\1', $k);
- $tmp=ereg_replace('"', '', $a[$k]);
- if ($tmp == $harray["if_name"]) {
+ if ($a[$k] == $harray["if_name"]) {
if ($debug) {
- echo "Added $tmp to interface list for $h\n"; flush();
+ echo "Added {$a[$k]} to interface list for $h\n"; flush();
}
- $if_name[$h][$c][$tmp]=$if_num;
+ $if_name[$h][$c][$a[$k]]=$if_num;
}
}
}
@@ -134,19 +136,18 @@
$lastcommunity=$c;
$oid=set_oids($harray["snmpv2"]);
if ($debug) {
- if ($harray["snmpv2"] != "") { echo " Using 64-bit counters on this interface.\n"; flush; }
- else { echo " Using 32-bit counters on this interface.\n"; flush; }
+ if ($harray["snmpv2"] != "") { echo " Using 64-bit counters on this interface.\n"; flush(); }
+ else { echo " Using 32-bit counters on this interface.\n"; flush(); }
}
if ($harray["dynamic"] != "" || !is_array($if_name[$h][$c]) || count($if_name[$h][$c]) < 1) {
if ($debug) {
echo " Rereading name-number map for $h.\n"; flush();
}
- $a=snmpwalkoid($h, $c, $oid[$method]);
+ $a=result_fix(snmpwalkoid($h, $c, $oid[$method]));
for(reset($a); $k=key($a); next($a)) {
$if_num=preg_replace('/^.+\.(\d+)$/', '\\1', $k);
- $tmp=ereg_replace('"', '', $a[$k]);
- if ($tmp == $harray["if_name"]) {
- $if_name[$h][$c][$tmp]=$if_num;
+ if ($a[$k] == $harray["if_name"]) {
+ $if_name[$h][$c][$a[$k]]=$if_num;
}
}
}
@@ -156,60 +157,63 @@
if ($debug) {
echo " $iname ($inum):\n"; flush();
}
+////////////////////////////////////////////////////////////////////////////////////
if (! $rbytes = snmpget($h, $c, $oid["InOctets"] . "." . $inum)) {
- if ($debug) {
- echo " Aborting this read ($iname.$inum: InOctets does not exist).\n"; flush();
- }
- continue;
- }
- if (preg_match('/\D/', $rbytes)) {
- if ($debug) {
- echo " Aborting $iname.$inum: InOctets does not exist.\n"; flush();
- }
- continue;
+ $rbytes=0;
+ } else {
+ $rbytes=result_fix($rbytes);
}
+ if (preg_match('/\D/', $rbytes)) { $rbytes=0; }
+////////////////////////////////////////////////////////////////////////////////////
if (! $tbytes = snmpget($h, $c, $oid["OutOctets"] . "." . $inum)) {
- if ($debug) {
- echo " Aborting this read ($iname.$inum: OutOctets does not exist).\n"; flush();
- }
- continue;
- }
- if (preg_match('/\D/', $tbytes)) {
- if ($debug) {
- echo " Aborting $iname.$inum: OutOctets does not exist.\n"; flush();
- }
- continue;
+ $tbytes=0;
+ } else {
+ $tbytes=result_fix($tbytes);
}
+ if (preg_match('/\D/', $tbytes)) { $tbytes=0; }
+////////////////////////////////////////////////////////////////////////////////////
if (! $rpackets = snmpget($h, $c, $oid["InUcastPkts"] . "." . $inum)) {
-//echo " $iname.$inum: InUcastPkts does not exist.\n"; flush();
$rpackets=0;
+ } else {
+ $rpackets=result_fix($rpackets);
}
if (preg_match('/\D/', $rpackets)) { $rpackets=0; }
+////////////////////////////////////////////////////////////////////////////////////
if (! $tpackets = snmpget($h, $c, $oid["OutUcastPkts"] . "." . $inum)) {
-//echo " $iname.$inum: OutUcastPkts does not exist.\n"; flush();
$tpackets=0;
+ } else {
+ $tpackets=result_fix($tpackets);
}
if (preg_match('/\D/', $tpackets)) { $tpackets=0; }
+////////////////////////////////////////////////////////////////////////////////////
if (! $rerrors = snmpget($h, $c, $oid["InErrors"] . "." . $inum)) {
-//echo " $iname.$inum: InErrors does not exist.\n"; flush();
$rerrors=0;
+ } else {
+ $rerrors=result_fix($rerrors);
}
if (preg_match('/\D/', $rerrors)) { $rerrors=0; }
+////////////////////////////////////////////////////////////////////////////////////
if (! $terrors = snmpget($h, $c, $oid["OutErrors"] . "." . $inum)) {
-//echo " $iname.$inum: OutErrors does not exist.\n"; flush();
$terrors=0;
+ } else {
+ $terrors=result_fix($terrors);
}
if (preg_match('/\D/', $terrors)) { $terrors=0; }
+////////////////////////////////////////////////////////////////////////////////////
if (! $rdropped = snmpget($h, $c, $oid["InDiscards"] . "." . $inum)) {
-//echo " $iname.$inum: InDiscards does not exist.\n"; flush();
$rdropped=0;
+ } else {
+ $rdropped=result_fix($rdropped);
}
if (preg_match('/\D/', $rdropped)) { $rdropped=0; }
+////////////////////////////////////////////////////////////////////////////////////
if (! $tdropped = snmpget($h, $c, $oid["OutDiscards"] . "." . $inum)) {
-//echo " $iname.$inum: OutDiscards does not exist.\n"; flush();
$tdropped=0;
+ } else {
+ $tdropped=result_fix($tdropped);
}
if (preg_match('/\D/', $tdropped)) { $tdropped=0; }
+////////////////////////////////////////////////////////////////////////////////////
// If this is an interface that has never been put into the stats table,
// add a new entry in stats_ifs for it and define it in the $if_id array.
@@ -248,8 +252,8 @@
}
}
if ($debug) {
- $elapsed=getmicrotime()-$starttime;
- echo "Collection time: $elapsed seconds.\n\n";
+ $elapsed=formattime(getmicrotime()-$starttime);
+ echo "Collection time: $elapsed.\nSleeping for $delay seconds...\n\n";
}
if ($runasdaemon != "y") { break; }
write_pid();