code and formatting cleanups

git-svn-id: http://www.observium.org/svn/observer/trunk@1804 61d68cd4-352d-0410-923a-c4978735b2b8
This commit is contained in:
Tom Laermans
2011-03-08 17:12:43 +00:00
parent 773ee9243d
commit 59171bf068
4 changed files with 368 additions and 279 deletions

View File

@@ -70,12 +70,12 @@ if (file_exists('.svn'))
list(,$dbu_rev) = preg_split('/: /',@shell_exec('svn info database-update.sql|grep ^Revision')); list(,$dbu_rev) = preg_split('/: /',@shell_exec('svn info database-update.sql|grep ^Revision'));
$device_query = mysql_query("SELECT revision FROM `dbSchema`"); $device_query = mysql_query("SELECT revision FROM `dbSchema`");
if ($rev = @mysql_fetch_array($device_query)) if ($rev = @mysql_fetch_array($device_query))
{ {
$db_rev = $rev['revision']; $db_rev = $rev['revision'];
} }
else else
{ {
$db_rev = 0; $db_rev = 0;
} }

View File

@@ -40,7 +40,7 @@ function zeropad_lineno($num, $length)
{ {
$num = '0'.$num; $num = '0'.$num;
} }
return $num; return $num;
} }
@@ -61,7 +61,7 @@ function logfile($string)
function set_dev_attrib($device, $attrib_type, $attrib_value) function set_dev_attrib($device, $attrib_type, $attrib_value)
{ {
$count_sql = "SELECT COUNT(*) FROM devices_attribs WHERE `device_id` = '" . mres($device['device_id']) . "' AND `attrib_type` = '$attrib_type'"; $count_sql = "SELECT COUNT(*) FROM devices_attribs WHERE `device_id` = '" . mres($device['device_id']) . "' AND `attrib_type` = '$attrib_type'";
if (mysql_result(mysql_query($count_sql),0)) if (mysql_result(mysql_query($count_sql),0))
{ {
$update_sql = "UPDATE devices_attribs SET attrib_value = '$attrib_value' WHERE `device_id` = '" . mres($device['device_id']) . "' AND `attrib_type` = '$attrib_type'"; $update_sql = "UPDATE devices_attribs SET attrib_value = '$attrib_value' WHERE `device_id` = '" . mres($device['device_id']) . "' AND `attrib_type` = '$attrib_type'";
mysql_query($update_sql); mysql_query($update_sql);
@@ -71,14 +71,14 @@ function set_dev_attrib($device, $attrib_type, $attrib_value)
$insert_sql = "INSERT INTO devices_attribs (`device_id`, `attrib_type`, `attrib_value`) VALUES ('" . mres($device['device_id'])."', '$attrib_type', '$attrib_value')"; $insert_sql = "INSERT INTO devices_attribs (`device_id`, `attrib_type`, `attrib_value`) VALUES ('" . mres($device['device_id'])."', '$attrib_type', '$attrib_value')";
mysql_query($insert_sql); mysql_query($insert_sql);
} }
return mysql_affected_rows(); return mysql_affected_rows();
} }
function get_dev_attrib($device, $attrib_type) function get_dev_attrib($device, $attrib_type)
{ {
$sql = "SELECT attrib_value FROM devices_attribs WHERE `device_id` = '" . mres($device['device_id']) . "' AND `attrib_type` = '$attrib_type'"; $sql = "SELECT attrib_value FROM devices_attribs WHERE `device_id` = '" . mres($device['device_id']) . "' AND `attrib_type` = '$attrib_type'";
if ($row = mysql_fetch_assoc(mysql_query($sql))) if ($row = mysql_fetch_assoc(mysql_query($sql)))
{ {
return $row['attrib_value']; return $row['attrib_value'];
} }
@@ -118,7 +118,7 @@ function device_array($device_id)
function getHostOS($device) function getHostOS($device)
{ {
global $config; global $config;
$sysDescr = snmp_get ($device, "SNMPv2-MIB::sysDescr.0", "-Ovq"); $sysDescr = snmp_get ($device, "SNMPv2-MIB::sysDescr.0", "-Ovq");
$sysObjectId = snmp_get ($device, "SNMPv2-MIB::sysObjectID.0", "-Ovqn"); $sysObjectId = snmp_get ($device, "SNMPv2-MIB::sysObjectID.0", "-Ovqn");
@@ -127,13 +127,13 @@ function getHostOS($device)
$dir_handle = @opendir($config['install_dir'] . "/includes/discovery/os") or die("Unable to open $path"); $dir_handle = @opendir($config['install_dir'] . "/includes/discovery/os") or die("Unable to open $path");
while ($file = readdir($dir_handle)) while ($file = readdir($dir_handle))
{ {
if ( preg_match("/.php$/", $file) ) if (preg_match("/.php$/", $file) )
{ {
include($config['install_dir'] . "/includes/discovery/os/" . $file); include($config['install_dir'] . "/includes/discovery/os/" . $file);
} }
} }
closedir($dir_handle); closedir($dir_handle);
if ($os) { return $os; } else { return "generic"; } if ($os) { return $os; } else { return "generic"; }
} }
@@ -142,15 +142,15 @@ function formatRates($rate) {
return $rate; return $rate;
} }
function formatstorage($rate, $round = '2') function formatstorage($rate, $round = '2')
{ {
$rate = format_bi($rate, $round) . "B"; $rate = format_bi($rate, $round) . "B";
return $rate; return $rate;
} }
function format_si($rate) function format_si($rate)
{ {
if($rate >= "0.1") { if ($rate >= "0.1") {
$sizes = Array('', 'k', 'M', 'G', 'T', 'P', 'E'); $sizes = Array('', 'k', 'M', 'G', 'T', 'P', 'E');
$round = Array('2','2','2','2','2','2','2','2','2'); $round = Array('2','2','2','2','2','2','2','2','2');
$ext = $sizes[0]; $ext = $sizes[0];
@@ -165,7 +165,7 @@ function format_si($rate)
return round($rate, $round[$i]).$ext; return round($rate, $round[$i]).$ext;
} }
function format_bi($size, $round = '2') function format_bi($size, $round = '2')
{ {
$sizes = Array('', 'k', 'M', 'G', 'T', 'P', 'E'); $sizes = Array('', 'k', 'M', 'G', 'T', 'P', 'E');
$ext = $sizes[0]; $ext = $sizes[0];
@@ -178,17 +178,18 @@ function percent_colour($perc)
$r = min(255, 5 * ($perc - 25)); $r = min(255, 5 * ($perc - 25));
$b = max(0, 255 - (5 * ($perc + 25))); $b = max(0, 255 - (5 * ($perc + 25)));
return sprintf('#%02x%02x%02x', $r, $b, $b); return sprintf('#%02x%02x%02x', $r, $b, $b);
} }
function interface_errors ($rrd_file, $period = '-1d') // Returns the last in/out errors value in RRD function interface_errors ($rrd_file, $period = '-1d') // Returns the last in/out errors value in RRD
{ {
global $config; global $config;
$cmd = $config['rrdtool']." fetch -s $period -e -300s $rrd_file AVERAGE | grep : | cut -d\" \" -f 4,5"; $cmd = $config['rrdtool']." fetch -s $period -e -300s $rrd_file AVERAGE | grep : | cut -d\" \" -f 4,5";
$data = trim(shell_exec($cmd)); $data = trim(shell_exec($cmd));
foreach( explode("\n", $data) as $entry) { foreach (explode("\n", $data) as $entry)
list($in, $out) = explode(" ", $entry); {
$in_errors += ($in * 300); list($in, $out) = explode(" ", $entry);
$out_errors += ($out * 300); $in_errors += ($in * 300);
$out_errors += ($out * 300);
} }
$errors['in'] = round($in_errors); $errors['in'] = round($in_errors);
$errors['out'] = round($out_errors); $errors['out'] = round($out_errors);
@@ -196,23 +197,23 @@ function interface_errors ($rrd_file, $period = '-1d') // Returns the last in/ou
} }
# FIXME: below function is unused, only commented out in html/pages/device/overview/ports.inc.php - do we still need it? # FIXME: below function is unused, only commented out in html/pages/device/overview/ports.inc.php - do we still need it?
function device_traffic_image($device, $width, $height, $from, $to) function device_traffic_image($device, $width, $height, $from, $to)
{ {
return "<img src='graph.php?device=" . $device . "&amp;type=device_bits&amp;from=" . $from . "&amp;to=" . $to . "&amp;width=" . $width . "&amp;height=" . $height . "&amp;legend=no' />"; return "<img src='graph.php?device=" . $device . "&amp;type=device_bits&amp;from=" . $from . "&amp;to=" . $to . "&amp;width=" . $width . "&amp;height=" . $height . "&amp;legend=no' />";
} }
function getImage($host) function getImage($host)
{ {
global $config; global $config;
$sql = "SELECT * FROM `devices` WHERE `device_id` = '$host'"; $sql = "SELECT * FROM `devices` WHERE `device_id` = '$host'";
$data = mysql_fetch_array(mysql_query($sql)); $data = mysql_fetch_array(mysql_query($sql));
$type = strtolower($data['os']); $type = strtolower($data['os']);
if ($config['os'][$type]['icon'] && file_exists($config['html_dir'] . "/images/os/" . $config['os'][$type]['icon'] . ".png")) if ($config['os'][$type]['icon'] && file_exists($config['html_dir'] . "/images/os/" . $config['os'][$type]['icon'] . ".png"))
{ {
$image = '<img src="'.$config['base_url'].'/images/os/'.$config['os'][$type]['icon'].'.png" />'; $image = '<img src="'.$config['base_url'].'/images/os/'.$config['os'][$type]['icon'].'.png" />';
} elseif ($config['os'][$type]['icon'] && file_exists($config['html_dir'] . "/images/os/". $config['os'][$type]['icon'] . ".gif")) } elseif ($config['os'][$type]['icon'] && file_exists($config['html_dir'] . "/images/os/". $config['os'][$type]['icon'] . ".gif"))
{ {
$image = '<img src="'.$config['base_url'].'/images/os/'.$config['os'][$type]['icon'].'.gif" />'; $image = '<img src="'.$config['base_url'].'/images/os/'.$config['os'][$type]['icon'].'.gif" />';
} else { } else {
if (file_exists($config['html_dir'] . "/images/os/$type" . ".png")){ $image = '<img src="'.$config['base_url'].'/images/os/'.$type.'.png" />'; if (file_exists($config['html_dir'] . "/images/os/$type" . ".png")){ $image = '<img src="'.$config['base_url'].'/images/os/'.$type.'.png" />';
} elseif (file_exists($config['html_dir'] . "/images/os/$type" . ".gif")){ $image = '<img src="'.$config['base_url'].'/images/os/'.$type.'.gif" />'; } } elseif (file_exists($config['html_dir'] . "/images/os/$type" . ".gif")){ $image = '<img src="'.$config['base_url'].'/images/os/'.$type.'.gif" />'; }
@@ -234,7 +235,7 @@ function renamehost($id, $new) {
eventlog("Hostname changed -> $new (console)", $id); eventlog("Hostname changed -> $new (console)", $id);
} }
function delete_port($int_id) function delete_port($int_id)
{ {
$interface = mysql_fetch_assoc(mysql_query("SELECT * FROM `ports` AS P, `devices` AS D WHERE P.interface_id = '".$int_id."' AND D.device_id = P.device_id")); $interface = mysql_fetch_assoc(mysql_query("SELECT * FROM `ports` AS P, `devices` AS D WHERE P.interface_id = '".$int_id."' AND D.device_id = P.device_id"));
mysql_query("DELETE from `adjacencies` WHERE `interface_id` = '$int_id'"); mysql_query("DELETE from `adjacencies` WHERE `interface_id` = '$int_id'");
@@ -250,7 +251,7 @@ function delete_port($int_id)
shell_exec("rm -rf ".trim($config['rrd_dir'])."/".trim($interface['hostname'])."/".$interface['ifIndex'].".rrd"); shell_exec("rm -rf ".trim($config['rrd_dir'])."/".trim($interface['hostname'])."/".$interface['ifIndex'].".rrd");
} }
function delete_device($id) function delete_device($id)
{ {
global $config; global $config;
$host = mysql_result(mysql_query("SELECT hostname FROM devices WHERE device_id = '$id'"), 0); $host = mysql_result(mysql_query("SELECT hostname FROM devices WHERE device_id = '$id'"), 0);
@@ -266,7 +267,7 @@ function delete_device($id)
mysql_query("DELETE FROM `devices_attribs` WHERE `device_id` = '$id'"); mysql_query("DELETE FROM `devices_attribs` WHERE `device_id` = '$id'");
mysql_query("DELETE FROM `devices_perms` WHERE `device_id` = '$id'"); mysql_query("DELETE FROM `devices_perms` WHERE `device_id` = '$id'");
mysql_query("DELETE FROM `bgpPeers` WHERE `device_id` = '$id'"); mysql_query("DELETE FROM `bgpPeers` WHERE `device_id` = '$id'");
mysql_query("DELETE FROM `vlans` WHERE `device_id` = '$id'"); mysql_query("DELETE FROM `vlans` WHERE `device_id` = '$id'");
mysql_query("DELETE FROM `vrfs` WHERE `device_id` = '$id'"); mysql_query("DELETE FROM `vrfs` WHERE `device_id` = '$id'");
mysql_query("DELETE FROM `storage` WHERE `device_id` = '$id'"); mysql_query("DELETE FROM `storage` WHERE `device_id` = '$id'");
mysql_query("DELETE FROM `alerts` WHERE `device_id` = '$id'"); mysql_query("DELETE FROM `alerts` WHERE `device_id` = '$id'");
@@ -284,15 +285,19 @@ function delete_device($id)
return $ret; return $ret;
} }
function addHost($host, $community, $snmpver, $port = 161) function addHost($host, $community, $snmpver, $port = 161)
{ {
global $config; global $config;
list($hostshort) = explode(".", $host); list($hostshort) = explode(".", $host);
if ( isDomainResolves($host)) { if (isDomainResolves($host))
if ( isPingable($host)) { {
if ( mysql_result(mysql_query("SELECT COUNT(*) FROM `devices` WHERE `hostname` = '$host'"), 0) == '0' ) { if (isPingable($host))
{
if (mysql_result(mysql_query("SELECT COUNT(*) FROM `devices` WHERE `hostname` = '$host'"), 0) == '0' )
{
$snmphost = shell_exec($config['snmpget'] ." -m SNMPv2-MIB -Oqv -$snmpver -c $community $host:$port sysName.0"); $snmphost = shell_exec($config['snmpget'] ." -m SNMPv2-MIB -Oqv -$snmpver -c $community $host:$port sysName.0");
if ($snmphost == $host || $hostshort = $host) { if ($snmphost == $host || $hostshort = $host)
{
createHost ($host, $community, $snmpver, $port); createHost ($host, $community, $snmpver, $port);
} else { echo("Given hostname does not match SNMP-read hostname!\n"); } } else { echo("Given hostname does not match SNMP-read hostname!\n"); }
} else { echo("Already got host $host\n"); } } else { echo("Already got host $host\n"); }
@@ -300,35 +305,35 @@ function addHost($host, $community, $snmpver, $port = 161)
} else { echo("Could not resolve $host\n"); } } else { echo("Could not resolve $host\n"); }
} }
function scanUDP ($host, $port, $timeout) function scanUDP ($host, $port, $timeout)
{ {
$handle = fsockopen($host, $port, $errno, $errstr, 2); $handle = fsockopen($host, $port, $errno, $errstr, 2);
if (!$handle) { if (!$handle) {
} }
socket_set_timeout ($handle, $timeout); socket_set_timeout ($handle, $timeout);
$write = fwrite($handle,"\x00"); $write = fwrite($handle,"\x00");
if (!$write) { next; } if (!$write) { next; }
$startTime = time(); $startTime = time();
$header = fread($handle, 1); $header = fread($handle, 1);
$endTime = time(); $endTime = time();
$timeDiff = $endTime - $startTime; $timeDiff = $endTime - $startTime;
if ($timeDiff >= $timeout) { if ($timeDiff >= $timeout) {
fclose($handle); return 1; fclose($handle); return 1;
} else { fclose($handle); return 0; } } else { fclose($handle); return 0; }
} }
function netmask2cidr($netmask) function netmask2cidr($netmask)
{ {
list ($network, $cidr) = explode("/", trim(`ipcalc $address/$mask | grep Network | cut -d" " -f 4`)); list ($network, $cidr) = explode("/", trim(`ipcalc $address/$mask | grep Network | cut -d" " -f 4`));
return $cidr; return $cidr;
} }
function cidr2netmask() function cidr2netmask()
{ {
return (long2ip(ip2long("255.255.255.255") << (32-$netmask))); return (long2ip(ip2long("255.255.255.255") << (32-$netmask)));
} }
function formatUptime($diff, $format="long") function formatUptime($diff, $format="long")
{ {
$yearsDiff = floor($diff/31536000); $yearsDiff = floor($diff/31536000);
$diff -= $yearsDiff*31536000; $diff -= $yearsDiff*31536000;
@@ -339,9 +344,9 @@ function formatUptime($diff, $format="long")
$minsDiff = floor($diff/60); $minsDiff = floor($diff/60);
$diff -= $minsDiff*60; $diff -= $minsDiff*60;
$secsDiff = $diff; $secsDiff = $diff;
$uptime = ""; $uptime = "";
if ($format == "short") { if ($format == "short") {
if ($yearsDiff > '0') { $uptime .= $yearsDiff . "y "; } if ($yearsDiff > '0') { $uptime .= $yearsDiff . "y "; }
if ($daysDiff > '0') { $uptime .= $daysDiff . "d "; } if ($daysDiff > '0') { $uptime .= $daysDiff . "d "; }
@@ -358,7 +363,7 @@ function formatUptime($diff, $format="long")
return trim($uptime); return trim($uptime);
} }
function isSNMPable($hostname, $community, $snmpver, $port) function isSNMPable($hostname, $community, $snmpver, $port)
{ {
global $config; global $config;
$pos = shell_exec($config['snmpget'] ." -m SNMPv2-MIB -$snmpver -c $community -t 1 $hostname:$port sysObjectID.0"); $pos = shell_exec($config['snmpget'] ." -m SNMPv2-MIB -$snmpver -c $community -t 1 $hostname:$port sysObjectID.0");
@@ -379,8 +384,8 @@ function isPingable($hostname) {
} }
} }
function is_odd($number) { function is_odd($number) {
return $number & 1; // 0 = even, 1 = odd return $number & 1; // 0 = even, 1 = odd
} }
function isValidInterface($if) { function isValidInterface($if) {
@@ -400,9 +405,9 @@ function isValidInterface($if) {
} else { return 0; } } else { return 0; }
} }
function utime() function utime()
{ {
$time = explode( " ", microtime()); $time = explode(" ", microtime());
$usec = (double)$time[0]; $usec = (double)$time[0];
$sec = (double)$time[1]; $sec = (double)$time[1];
return $sec + $usec; return $sec + $usec;
@@ -465,7 +470,7 @@ function createHost ($host, $community, $snmpver, $port = 161)
{ {
$host = trim(strtolower($host)); $host = trim(strtolower($host));
$device = array('hostname' => $host, 'community' => $community, 'snmpver' => $snmpver, 'port' => $port); $device = array('hostname' => $host, 'community' => $community, 'snmpver' => $snmpver, 'port' => $port);
$host_os = getHostOS($device); $host_os = getHostOS($device);
if ($host_os) if ($host_os)
{ {
$sql = mysql_query("INSERT INTO `devices` (`hostname`, `sysName`, `community`, `port`, `os`, `status`,`snmpver`) VALUES ('$host', '$host', '$community', '$port', '$host_os', '1','$snmpver')"); $sql = mysql_query("INSERT INTO `devices` (`hostname`, `sysName`, `community`, `port`, `os`, `status`,`snmpver`) VALUES ('$host', '$host', '$community', '$port', '$host_os', '1','$snmpver')");
@@ -475,9 +480,9 @@ function createHost ($host, $community, $snmpver, $port = 161)
# vv FIXME set_dev_attrib() # vv FIXME set_dev_attrib()
mysql_query("INSERT INTO devices_attribs (attrib_type, attrib_value, device_id) VALUES ('discover','1','$device_id')"); mysql_query("INSERT INTO devices_attribs (attrib_type, attrib_value, device_id) VALUES ('discover','1','$device_id')");
return("Created host : $host (id:$device_id) (os:$host_os)"); return("Created host : $host (id:$device_id) (os:$host_os)");
} }
else else
{ {
return FALSE; return FALSE;
} }
} }
@@ -492,11 +497,11 @@ function isDomainResolves($domain)
return gethostbyname($domain) != $domain; return gethostbyname($domain) != $domain;
} }
function hoststatus($id) function hoststatus($id)
{ {
$sql = mysql_query("SELECT `status` FROM `devices` WHERE `device_id` = '$id'"); $sql = mysql_query("SELECT `status` FROM `devices` WHERE `device_id` = '$id'");
$result = @mysql_result($sql, 0); $result = @mysql_result($sql, 0);
return $result; return $result;
} }
@@ -519,7 +524,7 @@ function match_network ($nets, $ip, $first=false)
if ($first && $return) return true; if ($first && $return) return true;
} }
} }
return $return; return $return;
} }
@@ -528,7 +533,7 @@ function snmp2ipv6($ipv6_snmp)
$ipv6 = explode('.',$ipv6_snmp); $ipv6 = explode('.',$ipv6_snmp);
for ($i = 0;$i <= 15;$i++) { $ipv6[$i] = zeropad(dechex($ipv6[$i])); } for ($i = 0;$i <= 15;$i++) { $ipv6[$i] = zeropad(dechex($ipv6[$i])); }
for ($i = 0;$i <= 15;$i+=2) { $ipv6_2[] = $ipv6[$i] . $ipv6[$i+1]; } for ($i = 0;$i <= 15;$i+=2) { $ipv6_2[] = $ipv6[$i] . $ipv6[$i+1]; }
return implode(':',$ipv6_2); return implode(':',$ipv6_2);
} }
@@ -538,7 +543,7 @@ function ipv62snmp($ipv6)
for ($i = 0;$i < 8;$i++) { $ipv6_ex[$i] = zeropad_lineno($ipv6_ex[$i],4); } for ($i = 0;$i < 8;$i++) { $ipv6_ex[$i] = zeropad_lineno($ipv6_ex[$i],4); }
$ipv6_ip = implode('',$ipv6_ex); $ipv6_ip = implode('',$ipv6_ex);
for ($i = 0;$i < 32;$i+=2) $ipv6_split[] = hexdec(substr($ipv6_ip,$i,2)); for ($i = 0;$i < 32;$i+=2) $ipv6_split[] = hexdec(substr($ipv6_ip,$i,2));
return implode('.',$ipv6_split); return implode('.',$ipv6_split);
} }
@@ -548,13 +553,14 @@ function discover_process_ipv6($ifIndex,$ipv6_address,$ipv6_prefixlen,$ipv6_orig
$ipv6_network = trim(shell_exec($config['sipcalc']." $ipv6_address/$ipv6_prefixlen | grep Subnet | cut -f 2 -d '-'")); $ipv6_network = trim(shell_exec($config['sipcalc']." $ipv6_address/$ipv6_prefixlen | grep Subnet | cut -f 2 -d '-'"));
$ipv6_compressed = trim(shell_exec($config['sipcalc']." $ipv6_address/$ipv6_prefixlen | grep Compressed | cut -f 2 -d '-'")); $ipv6_compressed = trim(shell_exec($config['sipcalc']." $ipv6_address/$ipv6_prefixlen | grep Compressed | cut -f 2 -d '-'"));
$ipv6_type = trim(shell_exec($config['sipcalc']." $ipv6_address/$ipv6_prefixlen | grep \"Address type\" | cut -f 2- -d '-'")); $ipv6_type = trim(shell_exec($config['sipcalc']." $ipv6_address/$ipv6_prefixlen | grep \"Address type\" | cut -f 2- -d '-'"));
if ($ipv6_type == "Link-Local Unicast Addresses") return; # ignore link-locals (coming from IPV6-MIB) if ($ipv6_type == "Link-Local Unicast Addresses") return; # ignore link-locals (coming from IPV6-MIB)
if (mysql_result(mysql_query("SELECT count(*) FROM `ports` if (mysql_result(mysql_query("SELECT count(*) FROM `ports`
WHERE device_id = '".$device['device_id']."' AND `ifIndex` = '$ifIndex'"), 0) != '0' && $ipv6_prefixlen > '0' && $ipv6_prefixlen < '129' && $ipv6_compressed != '::1') { WHERE device_id = '".$device['device_id']."' AND `ifIndex` = '$ifIndex'"), 0) != '0' && $ipv6_prefixlen > '0' && $ipv6_prefixlen < '129' && $ipv6_compressed != '::1')
{
$i_query = "SELECT interface_id FROM `ports` WHERE device_id = '".$device['device_id']."' AND `ifIndex` = '$ifIndex'"; $i_query = "SELECT interface_id FROM `ports` WHERE device_id = '".$device['device_id']."' AND `ifIndex` = '$ifIndex'";
$interface_id = mysql_result(mysql_query($i_query), 0); $interface_id = mysql_result(mysql_query($i_query), 0);
if (mysql_result(mysql_query("SELECT COUNT(*) FROM `ipv6_networks` WHERE `ipv6_network` = '$ipv6_network'"), 0) < '1') { if (mysql_result(mysql_query("SELECT COUNT(*) FROM `ipv6_networks` WHERE `ipv6_network` = '$ipv6_network'"), 0) < '1') {
@@ -573,7 +579,11 @@ function discover_process_ipv6($ifIndex,$ipv6_address,$ipv6_prefixlen,$ipv6_orig
mysql_query("INSERT INTO `ipv6_addresses` (`ipv6_address`, `ipv6_compressed`, `ipv6_prefixlen`, `ipv6_origin`, `ipv6_network_id`, `interface_id`) mysql_query("INSERT INTO `ipv6_addresses` (`ipv6_address`, `ipv6_compressed`, `ipv6_prefixlen`, `ipv6_origin`, `ipv6_network_id`, `interface_id`)
VALUES ('$ipv6_address', '$ipv6_compressed', '$ipv6_prefixlen', '$ipv6_origin', '$ipv6_network_id', '$interface_id')"); VALUES ('$ipv6_address', '$ipv6_compressed', '$ipv6_prefixlen', '$ipv6_origin', '$ipv6_network_id', '$interface_id')");
echo("+"); echo("+");
} else { echo("."); } }
else
{
echo(".");
}
$full_address = "$ipv6_address/$ipv6_prefixlen"; $full_address = "$ipv6_address/$ipv6_prefixlen";
$valid = $full_address . "-" . $interface_id; $valid = $full_address . "-" . $interface_id;
$valid_v6[$valid] = 1; $valid_v6[$valid] = 1;
@@ -614,36 +624,36 @@ function eventlog($eventtext,$device_id = "", $interface_id = "")
} }
# Use this function to write to the eventlog table # Use this function to write to the eventlog table
function log_event($text, $device = NULL, $type = NULL, $reference = NULL) function log_event($text, $device = NULL, $type = NULL, $reference = NULL)
{ {
global $debug; global $debug;
if(!is_array($device)) { $device = device_by_id_cache($device); } if (!is_array($device)) { $device = device_by_id_cache($device); }
$event_query = "INSERT INTO eventlog (host, reference, type, datetime, message) VALUES (" . ($device['device_id'] ? $device['device_id'] : "NULL"); $event_query = "INSERT INTO eventlog (host, reference, type, datetime, message) VALUES (" . ($device['device_id'] ? $device['device_id'] : "NULL");
$event_query .= ", '" . ($reference ? $reference : "NULL") . "', '" . ($type ? $type : "NULL") . "', NOW(), '" . mres($text) . "')"; $event_query .= ", '" . ($reference ? $reference : "NULL") . "', '" . ($type ? $type : "NULL") . "', NOW(), '" . mres($text) . "')";
if ($debug) { echo($event_query . "\n"); } if ($debug) { echo($event_query . "\n"); }
mysql_query($event_query); mysql_query($event_query);
} }
function notify($device,$title,$message) function notify($device,$title,$message)
{ {
global $config; global $config;
if($config['alerts']['email']['enable']) if ($config['alerts']['email']['enable'])
{ {
if($config['alerts']['email']['default_only']) if ($config['alerts']['email']['default_only'])
{ {
$email = $config['alerts']['email']['default']; $email = $config['alerts']['email']['default'];
} else { } else {
if ($device['sysContact']) if ($device['sysContact'])
{ {
$email = $device['sysContact']; $email = $device['sysContact'];
} else { } else {
$email = $config['alerts']['email']['default']; $email = $config['alerts']['email']['default'];
} }
} }
if($email) if ($email)
{ {
mail($email, $title, $message, $config['email_headers']); mail($email, $title, $message, $config['email_headers']);
} }
@@ -701,14 +711,14 @@ function isHexString($str)
function include_dir($dir, $regex = "") function include_dir($dir, $regex = "")
{ {
global $device, $config, $debug; global $device, $config, $debug;
if ( $regex == "") if ($regex == "")
{ {
$regex = "/\.inc\.php$/"; $regex = "/\.inc\.php$/";
} }
if ($handle = opendir($config['install_dir'] . '/' . $dir)) if ($handle = opendir($config['install_dir'] . '/' . $dir))
{ {
while (false !== ($file = readdir($handle))) while (false !== ($file = readdir($handle)))
{ {
if (filetype($config['install_dir'] . '/' . $dir . '/' . $file) == 'file' && preg_match($regex, $file)) if (filetype($config['install_dir'] . '/' . $dir . '/' . $file) == 'file' && preg_match($regex, $file))
{ {

View File

@@ -1,11 +1,5 @@
<?php <?php
#-r RETRIES set the number of retries
#-t TIMEOUT set the request timeout (in seconds)
#$config['snmp']['timeout'] = 300; # timeout in ms
#$config['snmp']['retries'] = 6; # how many times to retry the query
function snmp_get_multi($device, $oids, $options = "-OQUs", $mib = NULL, $mibdir = NULL) function snmp_get_multi($device, $oids, $options = "-OQUs", $mib = NULL, $mibdir = NULL)
{ {
global $debug,$config,$runtime_stats,$mibs_loaded; global $debug,$config,$runtime_stats,$mibs_loaded;
@@ -27,7 +21,7 @@ function snmp_get_multi($device, $oids, $options = "-OQUs", $mib = NULL, $mibdir
if(isset($timeout)) { $timeout = $timeout*1000*1000; } if(isset($timeout)) { $timeout = $timeout*1000*1000; }
foreach($oids as $oid) foreach($oids as $oid)
{ {
if ($device['snmpver'] == "v2c") if ($device['snmpver'] == "v2c")
{ {
$data = @snmp2_get($device['hostname'].":".$device['port'], $device['community'], $oid, $timeout, $retries); $data = @snmp2_get($device['hostname'].":".$device['port'], $device['community'], $oid, $timeout, $retries);
} }
@@ -40,8 +34,8 @@ function snmp_get_multi($device, $oids, $options = "-OQUs", $mib = NULL, $mibdir
if ($data) { $array[$index][$oid] = $data; } if ($data) { $array[$index][$oid] = $data; }
else { $array[$index][$oid] = null; } else { $array[$index][$oid] = null; }
} }
} }
else else
{ {
$cmd = $config['snmpget'] . " -" . $device['snmpver'] . " -c " . $device['community'] . " "; $cmd = $config['snmpget'] . " -" . $device['snmpver'] . " -c " . $device['community'] . " ";
if ($options) { $cmd .= " " . $options; } if ($options) { $cmd .= " " . $options; }
@@ -57,7 +51,7 @@ function snmp_get_multi($device, $oids, $options = "-OQUs", $mib = NULL, $mibdir
$data = trim(shell_exec($cmd)); $data = trim(shell_exec($cmd));
$runtime_stats['snmpget']++; $runtime_stats['snmpget']++;
if ($debug) { echo("$data\n"); } if ($debug) { echo("$data\n"); }
foreach(explode("\n", $data) as $entry) foreach(explode("\n", $data) as $entry)
{ {
list($oid,$value) = explode("=", $entry); list($oid,$value) = explode("=", $entry);
$oid = trim($oid); $value = trim($value); $oid = trim($oid); $value = trim($value);
@@ -73,182 +67,223 @@ function snmp_get_multi($device, $oids, $options = "-OQUs", $mib = NULL, $mibdir
function snmp_get($device, $oid, $options = NULL, $mib = NULL, $mibdir = NULL) function snmp_get($device, $oid, $options = NULL, $mib = NULL, $mibdir = NULL)
{ {
global $debug,$config,$runtime_stats,$mibs_loaded; global $debug,$config,$runtime_stats,$mibs_loaded;
if (is_numeric($device['timeout'])) { $timeout = $device['timeout']; } elseif (isset($config['snmp']['timeout'])) { $timeout = $config['snmp']['timeout']; }
if (is_numeric($device['retries'])) { $retries = $device['retries']; } elseif (isset($config['snmp']['retries'])) { $retries = $config['snmp']['retries']; }
if ($config['snmp']['internal'] == true)
{
if ($mib && $mibdir && !$mibs_loaded[$mib])
{
@snmp_read_mib($mibdir."/".$mib);
$mibs_loaded[$mib] = TRUE;
}
snmp_set_quick_print(1);
// s->ms - php snmp extension requires the timeout in microseconds.
if(isset($timeout)) { $timeout = $timeout*1000*1000; }
if ($device['snmpver'] == "v2c")
{
$data = @snmp2_get($device['hostname'].":".$device['port'], $device['community'], $oid, $timeout, $retries);
} elseif ( $device['snmpver'] == "v1") {
$data = @snmpget($device['hostname'].":".$device['port'], $device['community'], $oid, $timeout, $retries);
}
if ($debug) { print "DEBUG: $oid: $data\n"; }
} else {
$cmd = $config['snmpget'] . " -" . $device['snmpver'] . " -c " . $device['community'] . " ";
if ($options) { $cmd .= " " . $options; }
if ($mib) { $cmd .= " -m " . $mib; }
if ($mibdir) { $cmd .= " -M " . $mibdir; } else { $cmd .= " -M ".$config['mibdir']; }
if (isset($timeout)) { $cmd .= " -t " . $timeout; }
if (isset($retries)) { $cmd .= " -r " . $retries; }
$cmd .= " ".$device['hostname'].":".$device['port']." ".$oid;
if (!$debug) { $cmd .= " 2>/dev/null"; }
if ($debug) { echo("$cmd\n"); }
$data = trim(shell_exec($cmd));
if ($debug) { echo("$data\n"); }
}
$runtime_stats['snmpget']++;
if (is_string($data) && (preg_match("/No Such Instance/i", $data) || preg_match("/No Such Object/i", $data) || preg_match("/No more variables left/i", $data))) { return false; }
elseif ($data) { return $data; }
else { return false; }
}
function snmp_walk($device, $oid, $options = NULL, $mib = NULL, $mibdir = NULL) {
global $debug,$config,$runtime_stats;
if (is_numeric($device['timeout'])) { $timeout = $device['timeout']; } elseif (isset($config['snmp']['timeout'])) { $timeout = $config['snmp']['timeout']; } if (is_numeric($device['timeout'])) { $timeout = $device['timeout']; } elseif (isset($config['snmp']['timeout'])) { $timeout = $config['snmp']['timeout']; }
if (is_numeric($device['retries'])) { $retries = $device['retries']; } elseif (isset($config['snmp']['retries'])) { $retries = $config['snmp']['retries']; } if (is_numeric($device['retries'])) { $retries = $device['retries']; } elseif (isset($config['snmp']['retries'])) { $retries = $config['snmp']['retries']; }
// php has no bulkwalk functionality, so use binary for this.
if ($device['snmpver'] == 'v1' || $config['os'][$device['os']]['nobulk']) { if ($config['snmp']['internal'] == true)
$snmpcommand = $config['snmpwalk']; {
} if ($mib && $mibdir && !$mibs_loaded[$mib])
else { {
$snmpcommand = $config['snmpbulkwalk']; @snmp_read_mib($mibdir."/".$mib);
} $mibs_loaded[$mib] = TRUE;
$cmd = $snmpcommand . " -" . $device['snmpver'] . " -c " . $device['community'] . " "; }
if ($options) { $cmd .= " $options "; } snmp_set_quick_print(1);
if ($mib) { $cmd .= " -m $mib"; } // s->ms - php snmp extension requires the timeout in microseconds.
if ($mibdir) { $cmd .= " -M " . $mibdir; } else { $cmd .= " -M ".$config['mibdir']; } if(isset($timeout)) { $timeout = $timeout*1000*1000; }
if (isset($timeout)) { $cmd .= " -t " . $timeout; } if ($device['snmpver'] == "v2c")
if (isset($retries)) { $cmd .= " -r " . $retries; } {
$cmd .= " ".$device['hostname'].":".$device['port']." ".$oid; $data = @snmp2_get($device['hostname'].":".$device['port'], $device['community'], $oid, $timeout, $retries);
if (!$debug) { $cmd .= " 2>/dev/null"; } } elseif ( $device['snmpver'] == "v1") {
if ($debug) { echo("$cmd\n"); } $data = @snmpget($device['hostname'].":".$device['port'], $device['community'], $oid, $timeout, $retries);
$data = trim(shell_exec($cmd)); }
if ($debug) { echo("$data\n"); } if ($debug) { print "DEBUG: $oid: $data\n"; }
if (is_string($data) && (preg_match("/No Such (Object|Instance)/i", $data))) }
{ else
$data = false; {
} $cmd = $config['snmpget'] . " -" . $device['snmpver'] . " -c " . $device['community'] . " ";
else {
if (preg_match("/No more variables left in this MIB View \(It is past the end of the MIB tree\)$/",$data)) { if ($options) { $cmd .= " " . $options; }
# Bit ugly :-( if ($mib) { $cmd .= " -m " . $mib; }
$d_ex = explode("\n",$data); if ($mibdir) { $cmd .= " -M " . $mibdir; } else { $cmd .= " -M ".$config['mibdir']; }
unset($d_ex[count($d_ex)-1]); if (isset($timeout)) { $cmd .= " -t " . $timeout; }
$data = implode("\n",$d_ex); if (isset($retries)) { $cmd .= " -r " . $retries; }
}
} $cmd .= " ".$device['hostname'].":".$device['port']." ".$oid;
$runtime_stats['snmpwalk']++; if (!$debug) { $cmd .= " 2>/dev/null"; }
return $data; if ($debug) { echo("$cmd\n"); }
$data = trim(shell_exec($cmd));
if ($debug) { echo("$data\n"); }
}
$runtime_stats['snmpget']++;
if (is_string($data) && (preg_match("/No Such Instance/i", $data) || preg_match("/No Such Object/i", $data) || preg_match("/No more variables left/i", $data)))
{
return false;
}
elseif ($data) { return $data; }
else { return false; }
} }
function snmpwalk_cache_cip($device, $oid, $array, $mib = 0) { function snmp_walk($device, $oid, $options = NULL, $mib = NULL, $mibdir = NULL)
global $config;
if (is_numeric($device['timeout'])) { $timeout = $device['timeout']; } elseif (isset($config['snmp']['timeout'])) { $timeout = $config['snmp']['timeout']; }
if (is_numeric($device['retries'])) { $retries = $device['retries']; } elseif (isset($config['snmp']['retries'])) { $retries = $config['snmp']['retries']; }
if ($device['snmpver'] == 'v1' || $config['os'][$device['os']]['nobulk']) {
$snmpcommand = $config['snmpwalk'];
}
else {
$snmpcommand = $config['snmpbulkwalk'];
}
$cmd = $snmpcommand . " -O snQ -" . $device['snmpver'] . " -c " . $device['community'] . " ";
if ($mib) { $cmd .= " -m $mib"; }
$cmd .= " -M ".$config['install_dir']."/mibs/";
if (isset($timeout)) { $cmd .= " -t " . $timeout; }
if (isset($retries)) { $cmd .= " -r " . $retries; }
$cmd .= " ".$device['hostname'].":".$device['port']." ".$oid;
if (!$debug) { $cmd .= " 2>/dev/null"; }
$data = trim(shell_exec($cmd));
$device_id = $device['device_id'];
#echo("Caching: $oid\n");
foreach(explode("\n", $data) as $entry) {
list ($this_oid, $this_value) = preg_split("/=/", $entry);
$this_oid = trim($this_oid);
$this_value = trim($this_value);
$this_oid = substr($this_oid, 30);
list($ifIndex,$dir,$a,$b,$c,$d,$e,$f) = explode(".", $this_oid);
$h_a = zeropad(dechex($a));
$h_b = zeropad(dechex($b));
$h_c = zeropad(dechex($c));
$h_d = zeropad(dechex($d));
$h_e = zeropad(dechex($e));
$h_f = zeropad(dechex($f));
$mac = "$h_a$h_b$h_c$h_d$h_e$h_f";
if ($dir == "1") { $dir = "input"; } elseif ($dir == "2") { $dir = "output"; }
if ($mac && $dir) {
$array[$ifIndex][$mac][$oid][$dir] = $this_value;
}
return $array;
}
}
function snmp_cache_ifIndex($device)
{ {
global $config; global $debug,$config,$runtime_stats;
if (is_numeric($device['timeout'])) { $timeout = $device['timeout']; } elseif (isset($config['snmp']['timeout'])) { $timeout = $config['snmp']['timeout']; } if (is_numeric($device['timeout'])) { $timeout = $device['timeout']; } elseif (isset($config['snmp']['timeout'])) { $timeout = $config['snmp']['timeout']; }
if (is_numeric($device['retries'])) { $retries = $device['retries']; } elseif (isset($config['snmp']['retries'])) { $retries = $config['snmp']['retries']; } if (is_numeric($device['retries'])) { $retries = $device['retries']; } elseif (isset($config['snmp']['retries'])) { $retries = $config['snmp']['retries']; }
// php has no bulkwalk functionality, so use binary for this.
if ($device['snmpver'] == 'v1' || $config['os'][$device['os']]['nobulk']) if ($device['snmpver'] == 'v1' || $config['os'][$device['os']]['nobulk'])
{ {
$snmpcommand = $config['snmpwalk']; $snmpcommand = $config['snmpwalk'];
} }
else else
{ {
$snmpcommand = $config['snmpbulkwalk']; $snmpcommand = $config['snmpbulkwalk'];
} }
$cmd = $snmpcommand . " -" . $device['snmpver'] . " -c " . $device['community'] . " ";
if ($options) { $cmd .= " $options "; }
if ($mib) { $cmd .= " -m $mib"; }
if ($mibdir) { $cmd .= " -M " . $mibdir; } else { $cmd .= " -M ".$config['mibdir']; }
if (isset($timeout)) { $cmd .= " -t " . $timeout; }
if (isset($retries)) { $cmd .= " -r " . $retries; }
$cmd .= " ".$device['hostname'].":".$device['port']." ".$oid;
if (!$debug) { $cmd .= " 2>/dev/null"; }
if ($debug) { echo("$cmd\n"); }
$data = trim(shell_exec($cmd));
if ($debug) { echo("$data\n"); }
if (is_string($data) && (preg_match("/No Such (Object|Instance)/i", $data)))
{
$data = false;
}
else
{
if (preg_match("/No more variables left in this MIB View \(It is past the end of the MIB tree\)$/",$data)) {
# Bit ugly :-(
$d_ex = explode("\n",$data);
unset($d_ex[count($d_ex)-1]);
$data = implode("\n",$d_ex);
}
}
$runtime_stats['snmpwalk']++;
return $data;
}
function snmpwalk_cache_cip($device, $oid, $array, $mib = 0)
{
global $config;
if (is_numeric($device['timeout'])) { $timeout = $device['timeout']; } elseif (isset($config['snmp']['timeout'])) { $timeout = $config['snmp']['timeout']; }
if (is_numeric($device['retries'])) { $retries = $device['retries']; } elseif (isset($config['snmp']['retries'])) { $retries = $config['snmp']['retries']; }
if ($device['snmpver'] == 'v1' || $config['os'][$device['os']]['nobulk'])
{
$snmpcommand = $config['snmpwalk'];
}
else
{
$snmpcommand = $config['snmpbulkwalk'];
}
$cmd = $snmpcommand . " -O snQ -" . $device['snmpver'] . " -c " . $device['community'] . " ";
if ($mib) { $cmd .= " -m $mib"; }
$cmd .= " -M ".$config['install_dir']."/mibs/";
if (isset($timeout)) { $cmd .= " -t " . $timeout; }
if (isset($retries)) { $cmd .= " -r " . $retries; }
$cmd .= " ".$device['hostname'].":".$device['port']." ".$oid;
if (!$debug) { $cmd .= " 2>/dev/null"; }
$data = trim(shell_exec($cmd));
$device_id = $device['device_id'];
#echo("Caching: $oid\n");
foreach(explode("\n", $data) as $entry)
{
list ($this_oid, $this_value) = preg_split("/=/", $entry);
$this_oid = trim($this_oid);
$this_value = trim($this_value);
$this_oid = substr($this_oid, 30);
list($ifIndex,$dir,$a,$b,$c,$d,$e,$f) = explode(".", $this_oid);
$h_a = zeropad(dechex($a));
$h_b = zeropad(dechex($b));
$h_c = zeropad(dechex($c));
$h_d = zeropad(dechex($d));
$h_e = zeropad(dechex($e));
$h_f = zeropad(dechex($f));
$mac = "$h_a$h_b$h_c$h_d$h_e$h_f";
if ($dir == "1") { $dir = "input"; } elseif ($dir == "2") { $dir = "output"; }
if ($mac && $dir)
{
$array[$ifIndex][$mac][$oid][$dir] = $this_value;
}
return $array;
}
}
function snmp_cache_ifIndex($device)
{
// FIXME: this has no internal version, and is not yet using our own snmp_*
global $config;
if (is_numeric($device['timeout'])) { $timeout = $device['timeout']; } elseif (isset($config['snmp']['timeout'])) { $timeout = $config['snmp']['timeout']; }
if (is_numeric($device['retries'])) { $retries = $device['retries']; } elseif (isset($config['snmp']['retries'])) { $retries = $config['snmp']['retries']; }
if ($device['snmpver'] == 'v1' || $config['os'][$device['os']]['nobulk'])
{
$snmpcommand = $config['snmpwalk'];
}
else
{
$snmpcommand = $config['snmpbulkwalk'];
}
$cmd = $snmpcommand . " -O Qs -" . $device['snmpver'] . " -c " . $device['community'] . " "; $cmd = $snmpcommand . " -O Qs -" . $device['snmpver'] . " -c " . $device['community'] . " ";
$cmd .= " -M ".$config['install_dir']."/mibs/"; $cmd .= " -M ".$config['install_dir']."/mibs/";
$cmd .= " -m IF-MIB ifIndex"; $cmd .= " -m IF-MIB ifIndex";
if (isset($timeout)) { $cmd .= " -t " . $timeout; } if (isset($timeout)) { $cmd .= " -t " . $timeout; }
if (isset($retries)) { $cmd .= " -r " . $retries; } if (isset($retries)) { $cmd .= " -r " . $retries; }
if (!$debug) { $cmd .= " 2>/dev/null"; } if (!$debug) { $cmd .= " 2>/dev/null"; }
$data = trim(shell_exec($cmd)); $data = trim(shell_exec($cmd));
$device_id = $device['device_id']; $device_id = $device['device_id'];
foreach(explode("\n", $data) as $entry) {
foreach(explode("\n", $data) as $entry)
{
list ($this_oid, $this_value) = preg_split("/=/", $entry); list ($this_oid, $this_value) = preg_split("/=/", $entry);
list ($this_oid, $this_index) = explode(".", $this_oid); list ($this_oid, $this_index) = explode(".", $this_oid);
$this_index = trim($this_index); $this_index = trim($this_index);
$this_oid = trim($this_oid); $this_oid = trim($this_oid);
$this_value = trim($this_value); $this_value = trim($this_value);
if (!strstr($this_value, "at this OID") && $this_index) { if (!strstr($this_value, "at this OID") && $this_index)
{
$array[] = $this_value; $array[] = $this_value;
} }
} }
return $array; return $array;
} }
function snmpwalk_cache_oid($device, $oid, $array, $mib = NULL, $mibdir = NULL) function snmpwalk_cache_oid($device, $oid, $array, $mib = NULL, $mibdir = NULL)
{ {
$data = snmp_walk($device, $oid, "-OQUs", $mib, $mibdir); $data = snmp_walk($device, $oid, "-OQUs", $mib, $mibdir);
foreach(explode("\n", $data) as $entry) { foreach(explode("\n", $data) as $entry)
{
list($oid,$value) = explode("=", $entry); list($oid,$value) = explode("=", $entry);
$oid = trim($oid); $value = trim($value); $oid = trim($oid); $value = trim($value);
list($oid, $index) = explode(".", $oid); list($oid, $index) = explode(".", $oid);
if (!strstr($value, "at this OID") && isset($oid) && isset($index)) { if (!strstr($value, "at this OID") && isset($oid) && isset($index))
{
$array[$index][$oid] = $value; $array[$index][$oid] = $value;
} }
} }
return $array; return $array;
} }
function snmpwalk_cache_multi_oid($device, $oid, $array, $mib = NULL, $mibdir = NULL) function snmpwalk_cache_multi_oid($device, $oid, $array, $mib = NULL, $mibdir = NULL)
{ {
global $cache; global $cache;
if (!(is_array($cache['snmp'][$device['device_id']]) && array_key_exists($oid,$cache['snmp'][$device['device_id']]))) if (!(is_array($cache['snmp'][$device['device_id']]) && array_key_exists($oid,$cache['snmp'][$device['device_id']])))
{ {
$data = snmp_walk($device, $oid, "-OQUs", $mib, $mibdir); $data = snmp_walk($device, $oid, "-OQUs", $mib, $mibdir);
foreach(explode("\n", $data) as $entry) foreach(explode("\n", $data) as $entry)
{ {
list($r_oid,$value) = explode("=", $entry); list($r_oid,$value) = explode("=", $entry);
$r_oid = trim($r_oid); $value = trim($value); $r_oid = trim($r_oid); $value = trim($value);
@@ -260,7 +295,7 @@ function snmpwalk_cache_multi_oid($device, $oid, $array, $mib = NULL, $mibdir =
if (isset($oid_parts['4'])) { $index .= ".".$oid_parts['4']; } if (isset($oid_parts['4'])) { $index .= ".".$oid_parts['4']; }
if (isset($oid_parts['5'])) { $index .= ".".$oid_parts['5']; } if (isset($oid_parts['5'])) { $index .= ".".$oid_parts['5']; }
if (isset($oid_parts['6'])) { $index .= ".".$oid_parts['6']; } if (isset($oid_parts['6'])) { $index .= ".".$oid_parts['6']; }
if (!strstr($value, "at this OID") && isset($r_oid) && isset($index)) if (!strstr($value, "at this OID") && isset($r_oid) && isset($index))
{ {
$array[$index][$r_oid] = $value; $array[$index][$r_oid] = $value;
} }
@@ -272,45 +307,53 @@ function snmpwalk_cache_multi_oid($device, $oid, $array, $mib = NULL, $mibdir =
} }
function snmpwalk_cache_double_oid($device, $oid, $array, $mib = NULL, $mibdir = NULL) function snmpwalk_cache_double_oid($device, $oid, $array, $mib = NULL, $mibdir = NULL)
{ {
$data = snmp_walk($device, $oid, "-OQUs", $mib, $mibdir); $data = snmp_walk($device, $oid, "-OQUs", $mib, $mibdir);
foreach(explode("\n", $data) as $entry) {
foreach(explode("\n", $data) as $entry)
{
list($oid,$value) = explode("=", $entry); list($oid,$value) = explode("=", $entry);
$oid = trim($oid); $value = trim($value); $oid = trim($oid); $value = trim($value);
list($oid, $first, $second) = explode(".", $oid); list($oid, $first, $second) = explode(".", $oid);
if (!strstr($value, "at this OID") && isset($oid) && isset($first) && isset($second)) { if (!strstr($value, "at this OID") && isset($oid) && isset($first) && isset($second))
{
$double = $first.".".$second; $double = $first.".".$second;
$array[$double][$oid] = $value; $array[$double][$oid] = $value;
} }
} }
return $array; return $array;
} }
function snmpwalk_cache_triple_oid($device, $oid, $array, $mib = NULL, $mibdir = NULL) function snmpwalk_cache_triple_oid($device, $oid, $array, $mib = NULL, $mibdir = NULL)
{ {
$data = snmp_walk($device, $oid, "-OQUs", $mib, $mibdir); $data = snmp_walk($device, $oid, "-OQUs", $mib, $mibdir);
foreach(explode("\n", $data) as $entry) { foreach(explode("\n", $data) as $entry) {
list($oid,$value) = explode("=", $entry); list($oid,$value) = explode("=", $entry);
$oid = trim($oid); $value = trim($value); $oid = trim($oid); $value = trim($value);
list($oid, $first, $second, $third) = explode(".", $oid); list($oid, $first, $second, $third) = explode(".", $oid);
if (!strstr($value, "at this OID") && isset($oid) && isset($first) && isset($second)) { if (!strstr($value, "at this OID") && isset($oid) && isset($first) && isset($second))
{
$index = $first.".".$second.".".$third; $index = $first.".".$second.".".$third;
$array[$index][$oid] = $value; $array[$index][$oid] = $value;
} }
} }
return $array; return $array;
} }
function snmpwalk_cache_twopart_oid($device, $oid, $array, $mib = 0) function snmpwalk_cache_twopart_oid($device, $oid, $array, $mib = 0)
{ {
global $config; global $config;
if (is_numeric($device['timeout'])) { $timeout = $device['timeout']; } elseif (isset($config['snmp']['timeout'])) { $timeout = $config['snmp']['timeout']; } if (is_numeric($device['timeout'])) { $timeout = $device['timeout']; } elseif (isset($config['snmp']['timeout'])) { $timeout = $config['snmp']['timeout']; }
if (is_numeric($device['retries'])) { $retries = $device['retries']; } elseif (isset($config['snmp']['retries'])) { $retries = $config['snmp']['retries']; } if (is_numeric($device['retries'])) { $retries = $device['retries']; } elseif (isset($config['snmp']['retries'])) { $retries = $config['snmp']['retries']; }
if ($device['snmpver'] == 'v1' || $config['os'][$device['os']]['nobulk']) if ($device['snmpver'] == 'v1' || $config['os'][$device['os']]['nobulk'])
{ {
$snmpcommand = $config['snmpwalk']; $snmpcommand = $config['snmpwalk'];
} }
else else
@@ -326,29 +369,36 @@ function snmpwalk_cache_twopart_oid($device, $oid, $array, $mib = 0)
if (!$debug) { $cmd .= " 2>/dev/null"; } if (!$debug) { $cmd .= " 2>/dev/null"; }
$data = trim(shell_exec($cmd)); $data = trim(shell_exec($cmd));
$device_id = $device['device_id']; $device_id = $device['device_id'];
foreach(explode("\n", $data) as $entry) { foreach(explode("\n", $data) as $entry)
{
list($oid,$value) = explode("=", $entry); list($oid,$value) = explode("=", $entry);
$oid = trim($oid); $value = trim($value); $value = str_replace("\"", "", $value); $oid = trim($oid); $value = trim($value); $value = str_replace("\"", "", $value);
list($oid, $first, $second) = explode(".", $oid); list($oid, $first, $second) = explode(".", $oid);
if (!strstr($value, "at this OID") && isset($oid) && isset($first) && isset($second)) { if (!strstr($value, "at this OID") && isset($oid) && isset($first) && isset($second))
{
$array[$first][$second][$oid] = $value; $array[$first][$second][$oid] = $value;
} }
} }
return $array; return $array;
} }
function snmpwalk_cache_threepart_oid($device, $oid, $array, $mib = 0) { function snmpwalk_cache_threepart_oid($device, $oid, $array, $mib = 0)
{
global $config, $debug; global $config, $debug;
if (is_numeric($device['timeout'])) { $timeout = $device['timeout']; } elseif (isset($config['snmp']['timeout'])) { $timeout = $config['snmp']['timeout']; } if (is_numeric($device['timeout'])) { $timeout = $device['timeout']; } elseif (isset($config['snmp']['timeout'])) { $timeout = $config['snmp']['timeout']; }
if (is_numeric($device['retries'])) { $retries = $device['retries']; } elseif (isset($config['snmp']['retries'])) { $retries = $config['snmp']['retries']; } if (is_numeric($device['retries'])) { $retries = $device['retries']; } elseif (isset($config['snmp']['retries'])) { $retries = $config['snmp']['retries']; }
if ($device['snmpver'] == 'v1' || $config['os'][$device['os']]['nobulk']) if ($device['snmpver'] == 'v1' || $config['os'][$device['os']]['nobulk'])
{ {
$snmpcommand = $config['snmpwalk']; $snmpcommand = $config['snmpwalk'];
} }
else else
{ {
$snmpcommand = $config['snmpbulkwalk']; $snmpcommand = $config['snmpbulkwalk'];
} }
$cmd = $snmpcommand . " -O QUs -" . $device['snmpver'] . " -c " . $device['community'] . " "; $cmd = $snmpcommand . " -O QUs -" . $device['snmpver'] . " -c " . $device['community'] . " ";
$cmd .= " -M ".$config['install_dir']."/mibs/"; $cmd .= " -M ".$config['install_dir']."/mibs/";
if ($mib) { $cmd .= " -m $mib"; } if ($mib) { $cmd .= " -m $mib"; }
@@ -357,31 +407,39 @@ function snmpwalk_cache_threepart_oid($device, $oid, $array, $mib = 0) {
$cmd .= " ".$device['hostname'].":".$device['port']." ".$oid; $cmd .= " ".$device['hostname'].":".$device['port']." ".$oid;
if (!$debug) { $cmd .= " 2>/dev/null"; } if (!$debug) { $cmd .= " 2>/dev/null"; }
$data = trim(shell_exec($cmd)); $data = trim(shell_exec($cmd));
$device_id = $device['device_id']; $device_id = $device['device_id'];
foreach(explode("\n", $data) as $entry) { foreach(explode("\n", $data) as $entry)
{
list($oid,$value) = explode("=", $entry); list($oid,$value) = explode("=", $entry);
$oid = trim($oid); $value = trim($value); $value = str_replace("\"", "", $value); $oid = trim($oid); $value = trim($value); $value = str_replace("\"", "", $value);
list($oid, $first, $second, $third) = explode(".", $oid); list($oid, $first, $second, $third) = explode(".", $oid);
if ($debug) {echo("$entry || $oid || $first || $second || $third\n");} if ($debug) {echo("$entry || $oid || $first || $second || $third\n");}
if (!strstr($value, "at this OID") && isset($oid) && isset($first) && isset($second) && isset($third)) { if (!strstr($value, "at this OID") && isset($oid) && isset($first) && isset($second) && isset($third))
{
$array[$first][$second][$third][$oid] = $value; $array[$first][$second][$third][$oid] = $value;
} }
} }
return $array; return $array;
} }
function snmp_cache_slotport_oid($oid, $device, $array, $mib = 0) { function snmp_cache_slotport_oid($oid, $device, $array, $mib = 0)
{
global $config; global $config;
if (is_numeric($device['timeout'])) { $timeout = $device['timeout']; } elseif (isset($config['snmp']['timeout'])) { $timeout = $config['snmp']['timeout']; } if (is_numeric($device['timeout'])) { $timeout = $device['timeout']; } elseif (isset($config['snmp']['timeout'])) { $timeout = $config['snmp']['timeout']; }
if (is_numeric($device['retries'])) { $retries = $device['retries']; } elseif (isset($config['snmp']['retries'])) { $retries = $config['snmp']['retries']; } if (is_numeric($device['retries'])) { $retries = $device['retries']; } elseif (isset($config['snmp']['retries'])) { $retries = $config['snmp']['retries']; }
if ($device['snmpver'] == 'v1' || $config['os'][$device['os']]['nobulk']) if ($device['snmpver'] == 'v1' || $config['os'][$device['os']]['nobulk'])
{ {
$snmpcommand = $config['snmpwalk']; $snmpcommand = $config['snmpwalk'];
} }
else else
{ {
$snmpcommand = $config['snmpbulkwalk']; $snmpcommand = $config['snmpbulkwalk'];
} }
$cmd = $snmpcommand . " -O QUs -" . $device['snmpver'] . " -c " . $device['community'] . " "; $cmd = $snmpcommand . " -O QUs -" . $device['snmpver'] . " -c " . $device['community'] . " ";
if ($mib) { $cmd .= " -m $mib"; } if ($mib) { $cmd .= " -m $mib"; }
$cmd .= " -M ".$config['install_dir']."/mibs/"; $cmd .= " -M ".$config['install_dir']."/mibs/";
@@ -391,20 +449,23 @@ function snmp_cache_slotport_oid($oid, $device, $array, $mib = 0) {
if (!$debug) { $cmd .= " 2>/dev/null"; } if (!$debug) { $cmd .= " 2>/dev/null"; }
$data = trim(shell_exec($cmd)); $data = trim(shell_exec($cmd));
$device_id = $device['device_id']; $device_id = $device['device_id'];
foreach(explode("\n", $data) as $entry) {
foreach(explode("\n", $data) as $entry)
{
$entry = str_replace($oid.".", "", $entry); $entry = str_replace($oid.".", "", $entry);
list($slotport, $value) = explode("=", $entry); list($slotport, $value) = explode("=", $entry);
$slotport = trim($slotport); $value = trim($value); $slotport = trim($slotport); $value = trim($value);
if ($array[$slotport]['ifIndex']) { if ($array[$slotport]['ifIndex'])
{
$ifIndex = $array[$slotport]['ifIndex']; $ifIndex = $array[$slotport]['ifIndex'];
#$array[$slotport][$oid] = $value;
$array[$ifIndex][$oid] = $value; $array[$ifIndex][$oid] = $value;
} }
} }
return $array; return $array;
} }
function snmp_cache_oid($oid, $device, $array, $mib = 0) function snmp_cache_oid($oid, $device, $array, $mib = 0)
{ {
$array = snmpwalk_cache_oid($device, $oid, $array, $mib); $array = snmpwalk_cache_oid($device, $oid, $array, $mib);
return $array; return $array;
@@ -412,11 +473,15 @@ function snmp_cache_oid($oid, $device, $array, $mib = 0)
function snmp_cache_port_oids($oids, $port, $device, $array, $mib=0) { function snmp_cache_port_oids($oids, $port, $device, $array, $mib=0) {
global $config; global $config;
if (is_numeric($device['timeout'])) { $timeout = $device['timeout']; } elseif (isset($config['snmp']['timeout'])) { $timeout = $config['snmp']['timeout']; } if (is_numeric($device['timeout'])) { $timeout = $device['timeout']; } elseif (isset($config['snmp']['timeout'])) { $timeout = $config['snmp']['timeout']; }
if (is_numeric($device['retries'])) { $retries = $device['retries']; } elseif (isset($config['snmp']['retries'])) { $retries = $config['snmp']['retries']; } if (is_numeric($device['retries'])) { $retries = $device['retries']; } elseif (isset($config['snmp']['retries'])) { $retries = $config['snmp']['retries']; }
foreach($oids as $oid){
foreach($oids as $oid)
{
$string .= " $oid.$port"; $string .= " $oid.$port";
} }
$cmd = $config['snmpget'] . " -O vq -" . $device['snmpver'] . " -c " . $device['community'] . " "; $cmd = $config['snmpget'] . " -O vq -" . $device['snmpver'] . " -c " . $device['community'] . " ";
if (isset($timeout)) { $cmd .= " -t " . $timeout; } if (isset($timeout)) { $cmd .= " -t " . $timeout; }
if (isset($retries)) { $cmd .= " -r " . $retries; } if (isset($retries)) { $cmd .= " -r " . $retries; }
@@ -435,13 +500,17 @@ function snmp_cache_port_oids($oids, $port, $device, $array, $mib=0) {
} }
$x++; $x++;
} }
return $array; return $array;
} }
function snmp_cache_portIfIndex ($device, $array) { function snmp_cache_portIfIndex ($device, $array)
{
global $config; global $config;
if (is_numeric($device['timeout'])) { $timeout = $device['timeout']; } elseif (isset($config['snmp']['timeout'])) { $timeout = $config['snmp']['timeout']; } if (is_numeric($device['timeout'])) { $timeout = $device['timeout']; } elseif (isset($config['snmp']['timeout'])) { $timeout = $config['snmp']['timeout']; }
if (is_numeric($device['retries'])) { $retries = $device['retries']; } elseif (isset($config['snmp']['retries'])) { $retries = $config['snmp']['retries']; } if (is_numeric($device['retries'])) { $retries = $device['retries']; } elseif (isset($config['snmp']['retries'])) { $retries = $config['snmp']['retries']; }
$cmd = $config['snmpwalk'] . " -CI -m CISCO-STACK-MIB -O q -" . $device['snmpver'] . " -c " . $device['community'] . " "; $cmd = $config['snmpwalk'] . " -CI -m CISCO-STACK-MIB -O q -" . $device['snmpver'] . " -c " . $device['community'] . " ";
$cmd .= " -M ".$config['install_dir']."/mibs/"; $cmd .= " -M ".$config['install_dir']."/mibs/";
if (isset($timeout)) { $cmd .= " -t " . $timeout; } if (isset($timeout)) { $cmd .= " -t " . $timeout; }
@@ -449,7 +518,9 @@ function snmp_cache_portIfIndex ($device, $array) {
$cmd .= " ".$device['hostname'].":".$device['port']." portIfIndex"; $cmd .= " ".$device['hostname'].":".$device['port']." portIfIndex";
$output = trim(shell_exec($cmd)); $output = trim(shell_exec($cmd));
$device_id = $device['device_id']; $device_id = $device['device_id'];
foreach(explode("\n", $output) as $entry){
foreach(explode("\n", $output) as $entry)
{
$entry = str_replace("CISCO-STACK-MIB::portIfIndex.", "", $entry); $entry = str_replace("CISCO-STACK-MIB::portIfIndex.", "", $entry);
list($slotport, $ifIndex) = explode(" ", $entry); list($slotport, $ifIndex) = explode(" ", $entry);
if ($slotport && $ifIndex){ if ($slotport && $ifIndex){
@@ -457,13 +528,17 @@ function snmp_cache_portIfIndex ($device, $array) {
$array[$slotport]['ifIndex'] = $ifIndex; $array[$slotport]['ifIndex'] = $ifIndex;
} }
} }
return $array; return $array;
} }
function snmp_cache_portName ($device, $array) { function snmp_cache_portName ($device, $array)
{
global $config; global $config;
if (is_numeric($device['timeout'])) { $timeout = $device['timeout']; } elseif (isset($config['snmp']['timeout'])) { $timeout = $config['snmp']['timeout']; } if (is_numeric($device['timeout'])) { $timeout = $device['timeout']; } elseif (isset($config['snmp']['timeout'])) { $timeout = $config['snmp']['timeout']; }
if (is_numeric($device['retries'])) { $retries = $device['retries']; } elseif (isset($config['snmp']['retries'])) { $retries = $config['snmp']['retries']; } if (is_numeric($device['retries'])) { $retries = $device['retries']; } elseif (isset($config['snmp']['retries'])) { $retries = $config['snmp']['retries']; }
$cmd = $config['snmpwalk'] . " -CI -m CISCO-STACK-MIB -O Qs -" . $device['snmpver'] . " -c " . $device['community'] . " "; $cmd = $config['snmpwalk'] . " -CI -m CISCO-STACK-MIB -O Qs -" . $device['snmpver'] . " -c " . $device['community'] . " ";
$cmd .= " -M ".$config['install_dir']."/mibs/"; $cmd .= " -M ".$config['install_dir']."/mibs/";
if (isset($timeout)) { $cmd .= " -t " . $timeout; } if (isset($timeout)) { $cmd .= " -t " . $timeout; }
@@ -472,16 +547,20 @@ function snmp_cache_portName ($device, $array) {
$output = trim(shell_exec($cmd)); $output = trim(shell_exec($cmd));
$device_id = $device['device_id']; $device_id = $device['device_id'];
#echo("Caching: portName\n"); #echo("Caching: portName\n");
foreach(explode("\n", $output) as $entry){
foreach(explode("\n", $output) as $entry)
{
$entry = str_replace("portName.", "", $entry); $entry = str_replace("portName.", "", $entry);
list($slotport, $portName) = explode("=", $entry); list($slotport, $portName) = explode("=", $entry);
$slotport = trim($slotport); $portName = trim($portName); $slotport = trim($slotport); $portName = trim($portName);
if ($array[$slotport]['ifIndex']) { if ($array[$slotport]['ifIndex'])
{
$ifIndex = $array[$slotport]['ifIndex']; $ifIndex = $array[$slotport]['ifIndex'];
$array[$slotport]['portName'] = $portName; $array[$slotport]['portName'] = $portName;
$array[$ifIndex]['portName'] = $portName; $array[$ifIndex]['portName'] = $portName;
} }
} }
return $array; return $array;
} }
?> ?>

View File

@@ -18,18 +18,18 @@ elseif ($options['h'] == "all") { $where = " "; $doing = "all"; }
elseif ($options['h']) { elseif ($options['h']) {
if (is_numeric($options['h'])) if (is_numeric($options['h']))
{ {
$where = "AND `device_id` = '".$options['h']."'"; $where = "AND `device_id` = '".$options['h']."'";
$doing = $options['h']; $doing = $options['h'];
} }
else else
{ {
$where = "AND `hostname` LIKE '".str_replace('*','%',mres($options['h']))."'"; $where = "AND `hostname` LIKE '".str_replace('*','%',mres($options['h']))."'";
$doing = $options['h']; $doing = $options['h'];
} }
} }
if (isset($options['i']) && $options['i'] && isset($options['n'])) { if (isset($options['i']) && $options['i'] && isset($options['n'])) {
$where = "AND MOD(device_id,".$options['i'].") = '" . $options['n'] . "'"; $where = "AND MOD(device_id,".$options['i'].") = '" . $options['n'] . "'";
$doing = $options['n'] ."/".$options['i']; $doing = $options['n'] ."/".$options['i'];
} }
@@ -46,7 +46,7 @@ if (!$where) {
exit; exit;
} }
if (isset($options['d'])) { if (isset($options['d'])) {
echo("DEBUG!\n"); echo("DEBUG!\n");
$debug = TRUE; $debug = TRUE;
ini_set('display_errors', 1); ini_set('display_errors', 1);
@@ -65,7 +65,7 @@ if (isset($options['d'])) {
echo("Starting polling run:\n\n"); echo("Starting polling run:\n\n");
$polled_devices = 0; $polled_devices = 0;
$device_query = mysql_query("SELECT `device_id` FROM `devices` WHERE `ignore` = 0 AND `disabled` = 0 $where ORDER BY `device_id` ASC"); $device_query = mysql_query("SELECT `device_id` FROM `devices` WHERE `ignore` = 0 AND `disabled` = 0 $where ORDER BY `device_id` ASC");
while ($device = mysql_fetch_assoc($device_query)) while ($device = mysql_fetch_assoc($device_query))
{ {
$device = mysql_fetch_assoc(mysql_query("SELECT * FROM `devices` WHERE `device_id` = '".$device['device_id']."'")); $device = mysql_fetch_assoc(mysql_query("SELECT * FROM `devices` WHERE `device_id` = '".$device['device_id']."'"));
$status = 0; unset($array); $status = 0; unset($array);
@@ -75,7 +75,7 @@ while ($device = mysql_fetch_assoc($device_query))
if($config['os'][$device['os']]['group']) {$device['os_group'] = $config['os'][$device['os']]['group']; echo("(".$device['os_group'].")");} if($config['os'][$device['os']]['group']) {$device['os_group'] = $config['os'][$device['os']]['group']; echo("(".$device['os_group'].")");}
echo("\n"); echo("\n");
unset($poll_update); unset($poll_update_query); unset($poll_separator); unset($version); unset($uptime); unset($features); unset($poll_update); unset($poll_update_query); unset($poll_separator); unset($version); unset($uptime); unset($features);
unset($sysLocation); unset($hardware); unset($sysDescr); unset($sysContact); unset($sysName); unset($serial); unset($sysLocation); unset($hardware); unset($sysDescr); unset($sysContact); unset($sysName); unset($serial);
$host_rrd = $config['rrd_dir'] . "/" . $device['hostname']; $host_rrd = $config['rrd_dir'] . "/" . $device['hostname'];
@@ -106,8 +106,8 @@ while ($device = mysql_fetch_assoc($device_query))
notify($device, "Device ".($status == '1' ? 'Up' : 'Down').": " . $device['hostname'], "Device ".($status == '1' ? 'up' : 'down').": " . $device['hostname'] . " at " . date($config['timestamp_format'])); notify($device, "Device ".($status == '1' ? 'Up' : 'Down').": " . $device['hostname'], "Device ".($status == '1' ? 'up' : 'down').": " . $device['hostname'] . " at " . date($config['timestamp_format']));
} }
if ($status == "1") if ($status == "1")
{ {
$graphs = array(); $graphs = array();
$oldgraphs = array(); $oldgraphs = array();
@@ -130,7 +130,7 @@ while ($device = mysql_fetch_assoc($device_query))
$agent_uptime = $uptime; ## Move uptime into agent_uptime $agent_uptime = $uptime; ## Move uptime into agent_uptime
#HOST-RESOURCES-MIB::hrSystemUptime.0 = Timeticks: (63050465) 7 days, 7:08:24.65 #HOST-RESOURCES-MIB::hrSystemUptime.0 = Timeticks: (63050465) 7 days, 7:08:24.65
$hrSystemUptime = str_replace("(", "", $hrSystemUptime); $hrSystemUptime = str_replace("(", "", $hrSystemUptime);
$hrSystemUptime = str_replace(")", "", $hrSystemUptime); $hrSystemUptime = str_replace(")", "", $hrSystemUptime);
list($days,$hours, $mins, $secs) = explode(":", $hrSystemUptime); list($days,$hours, $mins, $secs) = explode(":", $hrSystemUptime);
list($secs, $microsecs) = explode(".", $secs); list($secs, $microsecs) = explode(".", $secs);
$hours = $hours + ($days * 24); $hours = $hours + ($days * 24);
@@ -150,17 +150,17 @@ while ($device = mysql_fetch_assoc($device_query))
$uptime = $secs; $uptime = $secs;
} }
if (is_numeric($uptime)) if (is_numeric($uptime))
{ {
if ( $uptime < $device['uptime'] ) { if ( $uptime < $device['uptime'] ) {
notify($device,"Device rebooted: " . $device['hostname'], "Device Rebooted : " . $device['hostname'] . " " . formatUptime($uptime) . " ago."); notify($device,"Device rebooted: " . $device['hostname'], "Device Rebooted : " . $device['hostname'] . " " . formatUptime($uptime) . " ago.");
log_event('Device rebooted after '.formatUptime($device['uptime']), $device['device_id'], 'reboot', $device['uptime']); log_event('Device rebooted after '.formatUptime($device['uptime']), $device['device_id'], 'reboot', $device['uptime']);
} }
$uptimerrd = $config['rrd_dir'] . "/" . $device['hostname'] . "/uptime.rrd"; $uptimerrd = $config['rrd_dir'] . "/" . $device['hostname'] . "/uptime.rrd";
if (!is_file($uptimerrd)) if (!is_file($uptimerrd))
{ {
rrdtool_create ($uptimerrd, "DS:uptime:GAUGE:600:0:U RRA:AVERAGE:0.5:1:600 RRA:AVERAGE:0.5:6:700 RRA:AVERAGE:0.5:24:775 RRA:AVERAGE:0.5:288:797"); rrdtool_create ($uptimerrd, "DS:uptime:GAUGE:600:0:U RRA:AVERAGE:0.5:1:600 RRA:AVERAGE:0.5:6:700 RRA:AVERAGE:0.5:24:775 RRA:AVERAGE:0.5:288:797");
} }
@@ -172,14 +172,14 @@ while ($device = mysql_fetch_assoc($device_query))
$poll_update .= $poll_separator . "`uptime` = '$uptime'"; $poll_update .= $poll_separator . "`uptime` = '$uptime'";
$poll_separator = ", "; $poll_separator = ", ";
} }
if (is_file($config['install_dir'] . "/includes/polling/os/".$device['os'].".inc.php")) if (is_file($config['install_dir'] . "/includes/polling/os/".$device['os'].".inc.php"))
{ {
/// OS Specific /// OS Specific
include($config['install_dir'] . "/includes/polling/os/".$device['os'].".inc.php"); include($config['install_dir'] . "/includes/polling/os/".$device['os'].".inc.php");
} }
elseif ($device['os_group'] && is_file($config['install_dir'] . "/includes/polling/os/".$device['os_group'].".inc.php")) elseif ($device['os_group'] && is_file($config['install_dir'] . "/includes/polling/os/".$device['os_group'].".inc.php"))
{ {
/// OS Group Specific /// OS Group Specific
include($config['install_dir'] . "/includes/polling/os/".$device['os_group'].".inc.php"); include($config['install_dir'] . "/includes/polling/os/".$device['os_group'].".inc.php");
@@ -191,8 +191,8 @@ while ($device = mysql_fetch_assoc($device_query))
echo("Hardware: ".$hardware." Version: ".$version." Features: ".$features."\n"); echo("Hardware: ".$hardware." Version: ".$version." Features: ".$features."\n");
$sysLocation = str_replace("\"","", $sysLocation); $sysLocation = str_replace("\"","", $sysLocation);
include("includes/polling/ipmi.inc.php"); include("includes/polling/ipmi.inc.php");
include("includes/polling/temperatures.inc.php"); include("includes/polling/temperatures.inc.php");
include("includes/polling/humidity.inc.php"); include("includes/polling/humidity.inc.php");
@@ -222,49 +222,49 @@ while ($device = mysql_fetch_assoc($device_query))
unset( $update ) ; unset( $update ) ;
unset( $seperator) ; unset( $seperator) ;
if ( $serial && $serial != $device['serial'] ) { if ( $serial && $serial != $device['serial'] ) {
$poll_update .= $poll_separator . "`serial` = '".mres($serial)."'"; $poll_update .= $poll_separator . "`serial` = '".mres($serial)."'";
$poll_separator = ", "; $poll_separator = ", ";
log_event("Serial -> $serial", $device['device_id'], 'system'); log_event("Serial -> $serial", $device['device_id'], 'system');
} }
if ( $sysContact && $sysContact != $device['sysContact'] ) { if ( $sysContact && $sysContact != $device['sysContact'] ) {
$poll_update .= $poll_separator . "`sysContact` = '".mres($sysContact)."'"; $poll_update .= $poll_separator . "`sysContact` = '".mres($sysContact)."'";
$poll_separator = ", "; $poll_separator = ", ";
log_event("Contact -> $sysContact", $device['device_id'], 'system'); log_event("Contact -> $sysContact", $device['device_id'], 'system');
} }
if ( $sysName && $sysName != $device['sysName'] ) { if ( $sysName && $sysName != $device['sysName'] ) {
$poll_update .= $poll_separator . "`sysName` = '$sysName'"; $poll_update .= $poll_separator . "`sysName` = '$sysName'";
$poll_separator = ", "; $poll_separator = ", ";
log_event("sysName -> $sysName", $device['device_id'], 'system'); log_event("sysName -> $sysName", $device['device_id'], 'system');
} }
if ( $sysDescr && $sysDescr != $device['sysDescr'] ) { if ( $sysDescr && $sysDescr != $device['sysDescr'] ) {
$poll_update .= $poll_separator . "`sysDescr` = '$sysDescr'"; $poll_update .= $poll_separator . "`sysDescr` = '$sysDescr'";
$poll_separator = ", "; $poll_separator = ", ";
log_event("sysDescr -> $sysDescr", $device['device_id'], 'system'); log_event("sysDescr -> $sysDescr", $device['device_id'], 'system');
} }
if ( $sysLocation && $device['location'] != $sysLocation ) { if ( $sysLocation && $device['location'] != $sysLocation ) {
$poll_update .= $poll_separator . "`location` = '$sysLocation'"; $poll_update .= $poll_separator . "`location` = '$sysLocation'";
$poll_separator = ", "; $poll_separator = ", ";
log_event("Location -> $sysLocation", $device['device_id'], 'system'); log_event("Location -> $sysLocation", $device['device_id'], 'system');
} }
if ( $version && $device['version'] != $version ) { if ( $version && $device['version'] != $version ) {
$poll_update .= $poll_separator . "`version` = '$version'"; $poll_update .= $poll_separator . "`version` = '$version'";
$poll_separator = ", "; $poll_separator = ", ";
log_event("OS Version -> $version", $device['device_id'], 'system'); log_event("OS Version -> $version", $device['device_id'], 'system');
} }
if ( $features != $device['features'] ) { if ( $features != $device['features'] ) {
$poll_update .= $poll_separator . "`features` = '$features'"; $poll_update .= $poll_separator . "`features` = '$features'";
$poll_separator = ", "; $poll_separator = ", ";
log_event("OS Features -> $features", $device['device_id'], 'system'); log_event("OS Features -> $features", $device['device_id'], 'system');
} }
if ( $hardware && $hardware != $device['hardware'] ) { if ( $hardware && $hardware != $device['hardware'] ) {
$poll_update .= $poll_separator . "`hardware` = '$hardware'"; $poll_update .= $poll_separator . "`hardware` = '$hardware'";
$poll_separator = ", "; $poll_separator = ", ";
@@ -276,7 +276,7 @@ while ($device = mysql_fetch_assoc($device_query))
$polled_devices++; $polled_devices++;
echo("\n"); echo("\n");
} }
## FIX ME EVENTLOGGING ## FIX ME EVENTLOGGING
### This code cycles through the graphs already known in the database and the ones we've defined as being polled here ### This code cycles through the graphs already known in the database and the ones we've defined as being polled here
@@ -287,7 +287,7 @@ while ($device = mysql_fetch_assoc($device_query))
while($graph = mysql_fetch_array($query)){ while($graph = mysql_fetch_array($query)){
if(!isset($graphs[$graph[0]])) if(!isset($graphs[$graph[0]]))
{ {
mysql_query("DELETE FROM `device_graphs` WHERE `device_id` = '".$device['device_id']."' AND `graph` = '".$graph[0]."'"); mysql_query("DELETE FROM `device_graphs` WHERE `device_id` = '".$device['device_id']."' AND `graph` = '".$graph[0]."'");
} else { } else {
$oldgraphs[$graph[0]] = TRUE; $oldgraphs[$graph[0]] = TRUE;
} }
@@ -312,16 +312,16 @@ while ($device = mysql_fetch_assoc($device_query))
if($debug) {echo("Updating " . $device['hostname'] . " - $poll_update_query \n");} if($debug) {echo("Updating " . $device['hostname'] . " - $poll_update_query \n");}
$poll_update_result = mysql_query($poll_update_query); $poll_update_result = mysql_query($poll_update_query);
if(mysql_affected_rows() == "1") { echo("UPDATED!\n"); } else { echo("NOT UPDATED!\n"); } if(mysql_affected_rows() == "1") { echo("UPDATED!\n"); } else { echo("NOT UPDATED!\n"); }
unset($storage_cache); // Clear cache of hrStorage ** MAYBE FIXME? ** unset($storage_cache); // Clear cache of hrStorage ** MAYBE FIXME? **
unset($cache); // Clear cache (unify all things here?) unset($cache); // Clear cache (unify all things here?)
} }
$poller_end = utime(); $poller_run = $poller_end - $poller_start; $poller_time = substr($poller_run, 0, 5); $poller_end = utime(); $poller_run = $poller_end - $poller_start; $poller_time = substr($poller_run, 0, 5);
if($polled_devices) { if($polled_devices) {
mysql_query("INSERT INTO `perf_times` (`type`, `doing`, `start`, `duration`, `devices`) mysql_query("INSERT INTO `perf_times` (`type`, `doing`, `start`, `duration`, `devices`)
VALUES ('poll', '$doing', '$poller_start', '$poller_time', '$polled_devices')"); VALUES ('poll', '$doing', '$poller_start', '$poller_time', '$polled_devices')");
} }