diff --git a/includes/polling/functions.inc.php b/includes/polling/functions.inc.php index af9e5d538..57e9d1787 100644 --- a/includes/polling/functions.inc.php +++ b/includes/polling/functions.inc.php @@ -11,7 +11,20 @@ function poll_sensor($device, $class, $unit) { echo("Checking $class " . $sensor['sensor_descr'] . "... "); - $sensor_value = snmp_get($device, $sensor['sensor_oid'], "-OUqnv", "SNMPv2-MIB"); + if($class == "temperature") + { + for ($i = 0;$i < 5;$i++) # Try 5 times to get a valid temp reading + { + if ($debug) echo("Attempt $i "); + $sensor_value = snmp_get($device, $sensor['sensor_oid'], "-OUqnv", "SNMPv2-MIB"); + $sensor_value = trim(str_replace("\"", "", $sensor_value)); + + if ($sensor_value != 9999) break; # TME sometimes sends 999.9 when it is right in the middle of an update; + sleep(1); # Give the TME some time to reset + } + } else { + $sensor_value = snmp_get($device, $sensor['sensor_oid'], "-OUqnv", "SNMPv2-MIB"); + } if ($sensor['sensor_divisor']) { $sensor_value = $sensor_value / $sensor['sensor_divisor']; } if ($sensor['sensor_multiplier']) { $sensor_value = $sensor_value * $sensor['sensor_multiplier']; } diff --git a/includes/polling/port-adsl.inc.php b/includes/polling/port-adsl.inc.php index 44145af94..ffa91542c 100755 --- a/includes/polling/port-adsl.inc.php +++ b/includes/polling/port-adsl.inc.php @@ -88,21 +88,18 @@ if (isset($port_stats[$port['ifIndex']]['adslLineCoding'])) $this_port[$oid] = $this_port[$oid] / 10; } - if (mysql_result(mysql_query("SELECT COUNT(*) FROM `ports_adsl` WHERE `interface_id` = '".$port['interface_id']."'"),0) == "0") + if (dbFetchCell("SELECT COUNT(*) FROM `ports_adsl` WHERE `interface_id` = ?", array($port['interface_id'])) == "0") { - mysql_query("INSERT INTO `ports_adsl` (`interface_id`) VALUES ('".$port['interface_id']."')"); + dbInsert(array('interface_id' => $port['interface_id']), 'ports_adsl'); } - $mysql_update = "UPDATE `ports_adsl` SET `port_adsl_updated` = NOW()"; + $port['adsl_update'] = array('port_adsl_updated' => array('NOW()')); foreach ($adsl_db_oids as $oid) { - $data = str_replace("\"", "", $this_port[$oid]); - $mysql_update .= ",`".$oid."` = '".$data."'"; + $data = str_replace("\"", "", $this_port[$oid]); ## FIXME - do we need this? + $port['adsl_update'][$oid] = $data; } - $mysql_update .= "WHERE `interface_id` = '".$port['interface_id']."'"; - mysql_query($mysql_update); - - if ($debug) { echo($mysql_update); echo(mysql_affected_rows()); echo(mysql_error()); } + dbUpdate($port['adsl_update'], 'ports_adsl', '`interface_id` = ?', array($port['interface_id'])); $rrdupdate = "N"; foreach ($adsl_oids as $oid) @@ -117,7 +114,7 @@ if (isset($port_stats[$port['ifIndex']]['adslLineCoding'])) if (!is_file($rrdfile)) { rrdtool_create ($rrdfile, $rrd_create); } rrdtool_update ($rrdfile, $rrdupdate); - echo("ADSL "); + echo("ADSL (".$this_port['adslLineCoding']."/".formatRates($this_port['adslAtucChanCurrTxRate'])."/".formatRates($this_port['adslAturChanCurrTxRate']).")"); } -?> \ No newline at end of file +?> diff --git a/includes/polling/ports.inc.php b/includes/polling/ports.inc.php index 4ab7d2db1..ec3985ad2 100755 --- a/includes/polling/ports.inc.php +++ b/includes/polling/ports.inc.php @@ -73,24 +73,6 @@ if ($device['adsl_count'] > "0") $port_stats = snmpwalk_cache_oid($device, ".1.3.6.1.2.1.10.94.1.1.7.1.7", $port_stats, "ADSL-LINE-MIB"); } -## Alcatel Detailed Statistics ## FIXME make this disableable -if($device['os'] == "aos") { - $stat_oids = array('alcetherStatsCRCAlignErrors', 'alcetherStatsRxUndersizePkts','alcetherStatsTxUndersizePkts', 'alcetherStatsTxOversizePkts','alcetherStatsRxJabbers', 'alcetherStatsRxCollisions', 'alcetherStatsTxCollisions', - 'alcetherStatsPkts64Octets','alcetherStatsPkts65to127Octets', 'alcetherStatsPkts128to255Octets','alcetherStatsPkts256to511Octets','alcetherStatsPkts512to1023Octets','alcetherStatsPkts1024to1518Octets', - 'gigaEtherStatsPkts1519to4095Octets','gigaEtherStatsPkts4096to9215Octets','alcetherStatsPkts1519to2047Octets','alcetherStatsPkts2048to4095Octets','alcetherStatsPkts4096Octets','alcetherStatsRxGiantPkts', - 'alcetherStatsRxDribbleNibblePkts','alcetherStatsRxLongEventPkts','alcetherStatsRxVlanTagPkts','alcetherStatsRxControlPkts','alcetherStatsRxLenChkErrPkts','alcetherStatsRxCodeErrPkts','alcetherStatsRxDvEventPkts', - 'alcetherStatsRxPrevPktDropped','alcetherStatsTx64Octets','alcetherStatsTx65to127Octets','alcetherStatsTx128to255Octets','alcetherStatsTx256to511Octets','alcetherStatsTx512to1023Octets', - 'alcetherStatsTx1024to1518Octets','alcetherStatsTx1519to2047Octets','alcetherStatsTx2048to4095Octets','alcetherStatsTx4096Octets','alcetherStatsTxRetryCount','alcetherStatsTxVlanTagPkts', - 'alcetherStatsTxControlPkts','alcetherStatsTxLatePkts','alcetherStatsTxTotalBytesOnWire','alcetherStatsTxLenChkErrPkts','alcetherStatsTxExcDeferPkts'); - - foreach($stat_oids as $oid) - { - $port_stats = snmpwalk_cache_oid($device, "alcetherStatsEntry", $port_stats, "ALCATEL-IND1-PORT-MIB"); - } -} - -echo("\n"); - /// FIXME This probably needs re-enabled. We need to clear these things when they get unset, too. #foreach ($etherlike_oids as $oid) { $port_stats = snmpwalk_cache_oid($device, $oid, $port_stats, "EtherLike-MIB"); } #foreach ($cisco_oids as $oid) { $port_stats = snmpwalk_cache_oid($device, $oid, $port_stats, "OLD-CISCO-INTERFACES-MIB"); } @@ -115,24 +97,45 @@ $polled = time(); if ($debug) { print_r($port_stats); } +### Build array of ports in the database + +## FIXME -- this stuff is a little messy, looping the array to make an array just seems wrong. :> + +$ports_db = dbFetchRows("SELECT * FROM `ports` WHERE `device_id` = ?", array($device['device_id'])); +foreach ($ports_db as $port) { $ports[$port['ifIndex']] = $port; } + /// New interface detection -///// FIXME +foreach ($port_stats as $ifIndex => $port) +{ + if (!is_array($ports[$port['ifIndex']])) + { + $interface_id = dbInsert(array('device_id' => $device['device_id'], 'ifIndex' => $ifIndex), 'ports'); + $ports[$port['ifIndex']] = dbFetchRow("SELECT * FROM `ports` WHERE `interface_id` = ?", array($interface_id)); + mysql_error(); + echo("Adding: ".$port['ifName']."(".$ifIndex.")(".$ports[$port['ifIndex']]['interface_id'].")"); + #print_r($ports); + } elseif ($ports[$ifIndex]['deleted'] == "1") { + dbUpdate(array('deleted' => '0'), 'ports', '`interface_id` = ?', array($ports[$ifIndex]['interface_id'])); + $ports[$ifIndex]['deleted'] = "0"; + } +} /// End New interface detection +echo("\n"); /// Loop ports in the DB and update where necessary -$port_query = mysql_query("SELECT * FROM `ports` WHERE `device_id` = '".$device['device_id']."' AND `deleted` = 0"); -while ($port = mysql_fetch_assoc($port_query)) +foreach ($ports as $port) { - echo("Port " . $port['ifDescr'] . " "); + echo ("Port " . $port['ifDescr'] . "(".$port['ifIndex'].") "); if ($port_stats[$port['ifIndex']] && $port['disabled'] != "1") { // Check to make sure Port data is cached. $this_port = &$port_stats[$port['ifIndex']]; if ($device['os'] == "vmware" && preg_match("/Device ([a-z0-9]+) at .*/", $this_port['ifDescr'], $matches)) { $this_port['ifDescr'] = $matches[1]; } $polled_period = $polled - $port['poll_time']; - $update .= "`poll_time` = '".$polled."'"; - $update .= ", `poll_prev` = '".$port['poll_time']."'"; - $update .= ", `poll_period` = '".$polled_period."'"; + $port['update'] = array(); + $port['update']['poll_time'] = $polled; + $port['update']['poll_prev'] = $port['poll_time']; + $port['update']['poll_period'] = $polled_period; ### Copy ifHC[In|Out]Octets values to non-HC if they exist if ($this_port['ifHCInOctets'] > 0 && is_numeric($this_port['ifHCInOctets']) && $this_port['ifHCOutOctets'] > 0 && is_numeric($this_port['ifHCOutOctets'])) @@ -144,7 +147,7 @@ while ($port = mysql_fetch_assoc($port_query)) ### rewrite the ifPhysAddress - if(strpos($this_port['ifPhysAddress'], ":")) + if (strpos($this_port['ifPhysAddress'], ":")) { list($a_a, $a_b, $a_c, $a_d, $a_e, $a_f) = explode(":", $this_port['ifPhysAddress']); $ah_a = zeropad($a_a); @@ -185,12 +188,12 @@ while ($port = mysql_fetch_assoc($port_query)) { if ($port[$oid] != $this_port[$oid] && !isset($this_port[$oid])) { - $update .= ", `$oid` = NULL"; + $port['update'][$oid] = array(NULL); log_event($oid . ": ".$port[$oid]." -> NULL", $device, 'interface', $port['interface_id']); if ($debug) { echo($oid . ": ".$port[$oid]." -> NULL "); } else { echo($oid . " "); } } elseif ($port[$oid] != $this_port[$oid]) { - $update .= ", `$oid` = '".mres($this_port[$oid])."'"; - log_event($oid . ": ".$port[$oid]." -> " . $this_port[$oid], $device, 'interface', $port['interface_id']); + $port['update'][$oid] = $this_port[$oid]; + log_event($oid . ": ".$port[$oid]." -> " . $this_port[$oid], $device, 'interface', $port['interface_id']); if ($debug) { echo($oid . ": ".$port[$oid]." -> " . $this_port[$oid]." "); } else { echo($oid . " "); } } } @@ -207,7 +210,7 @@ while ($port = mysql_fetch_assoc($port_query)) $attrib_key = "port_descr_".$attrib; if ($port_ifAlias[$attrib] != $port[$attrib_key]) { - $update .= ", `".$attrib_key."` = '".$port_ifAlias[$attrib]."'"; + $port['update'][$attrib_key] = $port_ifAlias[$attrib]; log_event($attrib . ": ".$port[$attrib_key]." -> " . $port_ifAlias[$attrib], $device, 'interface', $port['interface_id']); } } @@ -218,20 +221,23 @@ while ($port = mysql_fetch_assoc($port_query)) /// Update IF-MIB metrics foreach ($stat_oids_db as $oid) { - $update .= ", `$oid` = '".$this_port[$oid]."'"; - $update .= ", `".$oid."_prev` = '".$port[$oid]."'"; + $port['update'][$oid] = $this_port[$oid]; + $port['update'][$oid.'_prev'] = $port[$oid]; $oid_prev = $oid . "_prev"; if ($port[$oid]) { $oid_diff = $this_port[$oid] - $port[$oid]; $oid_rate = $oid_diff / $polled_period; if ($oid_rate < 0) { $oid_rate = "0"; } - $update .= ", `".$oid."_rate` = '".$oid_rate."'"; - $update .= ", `".$oid."_delta` = '".$oid_diff."'"; + $port['update'][$oid.'_rate'] = $oid_rate; + $port['update'][$oid.'_delta'] = $oid_diff; if ($debug) {echo("\n $oid ($oid_diff B) $oid_rate Bps $polled_period secs\n"); } } } + echo('bits('.formatRates($port['update']['ifInOctets_rate']).'/'.formatRates($port['update']['ifOutOctets_rate']).')'); + echo('pkts('.format_si($port['update']['ifInUcastPkts_rate']).'pps/'.format_si($port['update']['ifOutUcastPkts_rate']).'pps)'); + /// Update RRDs $rrdfile = $host_rrd . "/port-" . safename($port['ifIndex'] . ".rrd"); if (!is_file($rrdfile)) @@ -287,7 +293,7 @@ while ($port = mysql_fetch_assoc($port_query)) { // Loop the OIDs if ($this_port[$oid] != $port[$oid]) { // If data has changed, build a query - $update .= ", `$oid` = '".mres($this_port[$oid])."'"; + $port['update'][$oid] = $this_port[$oid]; echo("PAgP "); log_event("$oid -> ".$this_port[$oid], $device, 'interface', $port['interface_id']); } @@ -309,16 +315,13 @@ while ($port = mysql_fetch_assoc($port_query)) // Update MySQL - if ($update) + if (count($port['update'])) { - $update_query = "UPDATE `ports` SET ".$update." WHERE `interface_id` = '" . $port['interface_id'] . "'"; - @mysql_query($update_query); - if ($debug) {echo("\nMYSQL : [ $update_query ]"); } + $updated = dbUpdate($port['update'], 'ports', '`interface_id` = ?', array($port['interface_id'])); + if($debug) { echo("$updated updated"); } } // End Update MySQL - unset($update_query); unset($update); - // Send alerts for interface flaps. if ($config['warn']['ifdown'] && ($port['ifOperStatus'] != $this_port['ifOperStatus']) && $port['ignore'] == 0) { @@ -342,7 +345,7 @@ while ($port = mysql_fetch_assoc($port_query)) elseif ($port['disabled'] != "1") { echo("Port Deleted"); // Port missing from SNMP cache. - mysql_query("UPDATE `ports` SET `deleted` = '1' WHERE `device_id` = '".$device['device_id']."' AND `ifIndex` = '".$this_port['ifIndex']."'"); + dbUpdate(array('deleted' => '1'), 'ports', '`device_id` = ? AND `ifIndex` = ?', array($device['device_id'], $port['ifIndex'])); } else { echo("Port Disabled."); } diff --git a/includes/polling/processors.inc.php b/includes/polling/processors.inc.php index 732372e8e..9621397d8 100755 --- a/includes/polling/processors.inc.php +++ b/includes/polling/processors.inc.php @@ -1,8 +1,6 @@ $proc), 'processors', '`processor_id` = ?', array($processor['processor_id'])); } -?> \ No newline at end of file +?> diff --git a/includes/polling/sensors.inc.php b/includes/polling/sensors.inc.php index 7fb274fcb..a0f52d2c0 100644 --- a/includes/polling/sensors.inc.php +++ b/includes/polling/sensors.inc.php @@ -5,12 +5,13 @@ poll_sensor($device,'frequency', 'Hz'); poll_sensor($device,'fanspeed', 'rpm'); poll_sensor($device,'humidity', '%'); poll_sensor($device,'power', 'W'); -#poll_sensor($device,'voltage', 'V'); +poll_sensor($device,'voltage', 'V'); +poll_sensor($device,'temperature', 'C'); # FIXME voltages have other filenames -include('includes/polling/voltages.inc.php'); +#include('includes/polling/voltages.inc.php'); # FIXME also convert temperature, but there's some special code in there? -include('includes/polling/temperatures.inc.php'); +#include('includes/polling/temperatures.inc.php'); -?> \ No newline at end of file +?> diff --git a/includes/polling/storage.inc.php b/includes/polling/storage.inc.php index afef38523..3c15d4740 100755 --- a/includes/polling/storage.inc.php +++ b/includes/polling/storage.inc.php @@ -2,11 +2,7 @@ $storage_cache = array(); -#echo("Storage: "); - -$query = "SELECT * FROM storage WHERE device_id = '" . $device['device_id'] . "'"; -$storage_data = mysql_query($query); -while ($storage = mysql_fetch_assoc($storage_data)) +foreach (dbFetchRows("SELECT * FROM storage WHERE device_id = ?", array($device['device_id'])) as $storage) { echo("Storage ".$storage['storage_descr'] . ": "); @@ -54,16 +50,12 @@ while ($storage = mysql_fetch_assoc($storage_data)) rrdtool_update($storage_rrd,"N:".$storage['used'].":".$storage['free']); - $update_query = "UPDATE `storage` SET `storage_used` = '".$storage['used']."'"; - $update_query .= ", `storage_free` = '".$storage['free']."', `storage_size` = '".$storage['size']."'"; - $update_query .= ", `storage_units` = '".$storage['units']."', `storage_perc` = '".$percent."'"; - $update_query .= " WHERE `storage_id` = '".$storage['storage_id']."'"; - if ($debug) { echo("$update_query\n"); } - mysql_query($update_query); + $update = dbUpdate(array('storage_used' => $storage['used'], 'storage_free' => $storage['free'], 'storage_size' => $storage['size'], 'storage_units' => $storage['units'], 'storage_perc' => $percent), + 'storage', '`storage_id` = ?', array($storage['storage_id'])); echo("\n"); } unset($storage); -?> \ No newline at end of file +?> diff --git a/includes/polling/temperatures.inc.php b/includes/polling/temperatures.inc.php deleted file mode 100755 index 7244ddac1..000000000 --- a/includes/polling/temperatures.inc.php +++ /dev/null @@ -1,66 +0,0 @@ -= $sensor['sensor_limit']) - { - $msg = "Temp Alarm: " . $device['hostname'] . " " . $sensor['sensor_descr'] . " is " . $sensor_value . " (Limit " . $sensor['sensor_limit']; - $msg .= ") at " . date($config['timestamp_format']); - notify($device, "Temp Alarm: " . $device['hostname'] . " " . $sensor['sensor_descr'], $msg); - echo("Alerting for " . $device['hostname'] . " " . $sensor['sensor_descr'] . "\n"); - log_event('Temperature ' . $sensor['sensor_descr'] . " over threshold: " . $sensor_value . " " . html_entity_decode('°') . "$unit (>= " . $sensor['sensor_limit'] . " " . html_entity_decode('°') . "$unit)", $device, $class, $sensor['sensor_id']); - } - - mysql_query("UPDATE sensors SET sensor_current = '$sensor_value' WHERE sensor_class='$class' AND sensor_id = '" . $sensor['sensor_id'] . "'"); -} - -?> diff --git a/includes/polling/toner.inc.php b/includes/polling/toner.inc.php index 92010b227..2a0ed0f1d 100755 --- a/includes/polling/toner.inc.php +++ b/includes/polling/toner.inc.php @@ -2,14 +2,13 @@ if ($config['enable_printers']) { - $query = "SELECT * FROM toner WHERE device_id = '" . $device['device_id'] . "'"; - $toner_data = mysql_query($query); + $toner_data = dbFetchRows("SELECT * FROM toner WHERE device_id = ?", array($device['device_id'])); - while ($toner = mysql_fetch_assoc($toner_data)) + foreach ($toner_data as $toner) { echo("Checking toner " . $toner['toner_descr'] . "... "); -# FIXME poll capacity maybe also here, when toner is replaced capacity can change, leading to "154% of toner" + # FIXME poll capacity maybe also here, when toner is replaced capacity can change, leading to "154% of toner" $tonerperc = snmp_get($device, $toner['toner_oid'], "-OUqnv") / $toner['toner_capacity'] * 100; @@ -43,7 +42,7 @@ if ($config['enable_printers']) log_event('Toner ' . $toner['toner_descr'] . ' was replaced (new level: ' . $tonerperc . '%)', $device, 'toner', $toner['toner_id']); } - mysql_query("UPDATE toner SET toner_current = '$tonerperc', toner_capacity = '" . $toner['toner_capacity'] . "' WHERE toner_id = '" . $toner['toner_id'] . "'"); + dbUpdate(array('toner_current' => $tonerperc, 'toner_capacity' => $toner['toner_capacity']), 'toner', '`toner_id` = ?', array($toner['toner_id'])); } } diff --git a/includes/polling/ucd-diskio.inc.php b/includes/polling/ucd-diskio.inc.php index 74dc32c71..742fd8ba5 100644 --- a/includes/polling/ucd-diskio.inc.php +++ b/includes/polling/ucd-diskio.inc.php @@ -1,17 +1,15 @@ \ No newline at end of file +?> diff --git a/includes/polling/voltages.inc.php b/includes/polling/voltages.inc.php deleted file mode 100755 index 87d88e640..000000000 --- a/includes/polling/voltages.inc.php +++ /dev/null @@ -1,56 +0,0 @@ - $sensor['sensor_limit_low'] && $sensor_value <= $sensor['sensor_limit_low']) - { - $msg = "Voltage Alarm: " . $device['hostname'] . " " . $sensor['sensor_descr'] . " is " . $sensor_value . "$unit (Limit " . $sensor['sensor_limit']; - $msg .= "$unit) at " . date($config['timestamp_format']); - notify($device, "Voltage Alarm: " . $device['hostname'] . " " . $sensor['sensor_descr'], $msg); - echo("Alerting for " . $device['hostname'] . " " . $sensor['sensor_descr'] . "\n"); - log_event('Voltage ' . $sensor['sensor_descr'] . " under threshold: " . $sensor_value . " $unit (< " . $sensor['sensor_limit_low'] . " $unit)", $device, $class, $sensor['sensor_id']); - } - else if ($sensor['sensor_limit'] != "" && $sensor['sensor_current'] < $sensor['sensor_limit'] && $sensor_value >= $sensor['sensor_limit']) - { - $msg = "Voltage Alarm: " . $device['hostname'] . " " . $sensor['sensor_descr'] . " is " . $sensor_value . "$unit (Limit " . $sensor['sensor_limit']; - $msg .= "$unit) at " . date($config['timestamp_format']); - notify($device, "Voltage Alarm: " . $device['hostname'] . " " . $sensor['sensor_descr'], $msg); - echo("Alerting for " . $device['hostname'] . " " . $sensor['sensor_descr'] . "\n"); - log_event('Voltage ' . $sensor['sensor_descr'] . " above threshold: " . $sensor_value . " $unit (> " . $sensor['sensor_limit'] . " $unit)", $device, $class, $sensor['sensor_id']); - } - mysql_query("UPDATE sensors SET sensor_current = '$sensor_value' WHERE sensor_class='$class' AND sensor_id = '" . $sensor['sensor_id'] . "'"); -} - -?> diff --git a/includes/polling/wifi.inc.php b/includes/polling/wifi.inc.php index 94852da39..a400f581e 100644 --- a/includes/polling/wifi.inc.php +++ b/includes/polling/wifi.inc.php @@ -33,8 +33,7 @@ if ($device['type'] == 'network' || $device['type'] == 'firewall') $wirelesscards = array('Wireless', 'Atheros'); foreach ($wirelesscards as $wirelesscheck) { - $query = "SELECT COUNT(*) FROM `entPhysical` WHERE `device_id` = '" . $device['device_id'] . "' AND `entPhysicalDescr` LIKE '%" . $wirelesscheck . "%'"; - if (mysql_result(mysql_query($query),0) >= "1") + if (dbFetchCell("SELECT COUNT(*) FROM `entPhysical` WHERE `device_id` = ?AND `entPhysicalDescr` LIKE ?", array($device['device_id'], "%".$wirelesscheck."%")) >= "1") { echo("Checking RouterOS Wireless clients... ");