diff --git a/addhost.php b/addhost.php index d2d2819ed..66a545e4c 100755 --- a/addhost.php +++ b/addhost.php @@ -38,7 +38,7 @@ if (isset($argv[1]) && $argv[1]) } list($hostshort) = explode(".", $host); - if (mysql_result(mysql_query("SELECT COUNT(*) FROM `devices` WHERE `hostname` = '".mres($host)."'"), 0) == '0') + if (dbFetchCell("SELECT COUNT(*) FROM `devices` WHERE `hostname` = ?", array($host)) == '0') { if (isDomainResolves($argv[1])) { @@ -69,4 +69,4 @@ if (isset($argv[1]) && $argv[1]) } else { echo("Already got host $host\n"); } } else { echo("Add Host Tool\nUsage: ./addhost.php [community] [v1|v2c] [port] [" . join("|",$config['snmp']['transports']) . "]\n"); } -?> \ No newline at end of file +?> diff --git a/alerts.php b/alerts.php index 07dafc6e1..17e882b77 100755 --- a/alerts.php +++ b/alerts.php @@ -5,8 +5,7 @@ include("includes/defaults.inc.php"); include("config.php"); include("includes/functions.php"); -$alert_query = mysql_query("SELECT *, A.id as id FROM `alerts` as A, `devices` as D where A.device_id = D.device_id AND alerted = '0'"); -while ($alert = mysql_fetch_assoc($alert_query)) +foreach (dbFetchRows("SELECT *, A.id AS id FROM `alerts` AS A, `devices` AS D WHERE A.device_id = D.device_id AND alerted = '0'") as $alert) { $id = $alert['id']; $host = $alert['hostname']; @@ -14,7 +13,8 @@ while ($alert = mysql_fetch_assoc($alert_query)) $msg = $alert['message']; $alert_text .= "$date $host $msg"; - mysql_query("UPDATE `alerts` SET alerted = '1' WHERE `id` = '$id'"); + dbUpdate(array('alerted' => '1'), 'alerts', '`id` = ?' array($id)) + } if ($alert_text) @@ -23,4 +23,4 @@ if ($alert_text) # `echo '$alert_text' | gnokii --sendsms `; } -?> \ No newline at end of file +?> diff --git a/poll-reachability.php b/attic/poll-reachability.php similarity index 100% rename from poll-reachability.php rename to attic/poll-reachability.php diff --git a/check-errors.php b/check-errors.php index be8c88628..6f87249ec 100755 --- a/check-errors.php +++ b/check-errors.php @@ -7,13 +7,11 @@ include("includes/functions.php"); ## Check all of our interface RRD files for errors -if ($argv[1]) { $where = "AND `interface_id` = '$argv[1]'"; } +if ($argv[1]) { $where = "AND `interface_id` = ?"; $params = array($argv[1]); } $i = '0'; -$interface_query = mysql_query("SELECT * FROM `ports` AS I, `devices` AS D WHERE I.device_id = D.device_id $where"); - -while ($interface = mysql_fetch_assoc($interface_query)) +foreach (dbFetchRows("SELECT * FROM `ports` AS I, `devices` AS D WHERE I.device_id = D.device_id $where", $params) as $interface) { $errors = $interface['ifInErrors_delta'] + $interface['ifOutErrors_delta']; if ($errors > '1') diff --git a/check-services.php b/check-services.php index 2f5beac70..3172c170b 100755 --- a/check-services.php +++ b/check-services.php @@ -4,10 +4,7 @@ include("includes/defaults.inc.php"); include("config.php"); include("includes/functions.php"); -$sql = "SELECT * FROM devices AS D, services AS S WHERE S.device_id = D.device_id ORDER by D.device_id DESC"; -$query = mysql_query($sql); - -while ($service = mysql_fetch_assoc($query)) +foreach (dbFetchRows("SELECT * FROM `devices` AS D, `services` AS S WHERE S.device_id = D.device_id ORDER by D.device_id DESC") as $service) { if ($service['status'] = "1") { @@ -27,9 +24,12 @@ while ($service = mysql_fetch_assoc($query)) $check = "Error : Script not found ($checker_script)"; } + $update = array(); + if ($service_status != $status) { - $updated = ", `service_changed` = '" . time() . "' "; + $update['service_changed'] = time(); + if ($service['sysContact']) { $email = $service['sysContact']; } else { $email = $config['email_default']; } if ($status == "1") { @@ -45,8 +45,10 @@ while ($service = mysql_fetch_assoc($query)) } } else { unset($updated); } - $update_sql = "UPDATE `services` SET `service_status` = '$status', `service_message` = '" . addslashes($check) . "', `service_checked` = '" . time() . "' $updated WHERE `service_id` = '" . $service['service_id']. "'"; - mysql_query($update_sql); + $update = array_merge(array('service_status' => $status, 'service_message' => $check, 'service_checked' => time()), $update); + dbUpdate($update, 'services', '`service_id` = ?', array($service['service_id'])); + unset($update); + } else { $status = "0"; } @@ -67,4 +69,4 @@ while ($service = mysql_fetch_assoc($query)) } } # while -?> \ No newline at end of file +?> diff --git a/includes/dbFacile.php b/includes/dbFacile.php index 9ea3dbdab..39717bc11 100644 --- a/includes/dbFacile.php +++ b/includes/dbFacile.php @@ -22,8 +22,9 @@ Usage * Used by the other _query functions. * */ function dbQuery($sql, $parameters = array()) { - global $fullSql; + global $fullSql, $debug; $fullSql = dbMakeQuery($sql, $parameters); + if($debug) { echo(" SQL[".$fullSql."] "); } /* if($this->logFile) $time_start = microtime(true); @@ -111,6 +112,7 @@ function dbUpdate($data, $table, $where = null, $parameters = array()) { if(dbQuery($sql, $data)) { return mysql_affected_rows(); } else { + #echo("$fullSql"); trigger_error('QDB - Update failed.', E_WARNING); return false; } diff --git a/includes/polling/os.inc.php b/includes/polling/os.inc.php index 0d85474f6..5af68fd24 100644 --- a/includes/polling/os.inc.php +++ b/includes/polling/os.inc.php @@ -17,25 +17,25 @@ if ($version && $device['version'] != $version) { - $device['db_update'] .= ", `version` = '".mres($version)."'"; + $update_array['version'] = $version; log_event("OS Version -> ".$version, $device, 'system'); } if ($features != $device['features']) { - $device['db_update'] .= ", `features` = '".mres($features)."'"; + $update_array['features'] = $features; log_event("OS Features -> ".$features, $device, 'system'); } if ($hardware && $hardware != $device['hardware']) { - $device['db_update'] .= ", `hardware` = '".mres($hardware)."'"; + $update_array['hardware'] = $hardware; log_event("Hardware -> ".$hardware, $device, 'system'); } if ($serial && $serial != $device['serial']) { - $device['db_update'] .= ", `serial` = '".mres($serial)."'"; + $update_array['serial'] = $serial; log_event("serial -> ".$serial, $device, 'system'); } diff --git a/includes/polling/system.inc.php b/includes/polling/system.inc.php index 306a73514..21a16bd78 100755 --- a/includes/polling/system.inc.php +++ b/includes/polling/system.inc.php @@ -69,7 +69,7 @@ echo("Uptime: ".formatUptime($uptime)."\n"); - $device['db_update'] .= ", `uptime` = '".mres($uptime)."'"; + $update_array['uptime'] = $uptime; } $poll_device['sysLocation'] = str_replace("\"","", $poll_device['sysLocation']); @@ -77,19 +77,19 @@ if ($poll_device['sysContact'] && $poll_device['sysContact'] != $device['sysContact']) { - $device['db_update'] .= ", `sysContact` = '".mres($poll_device['sysContact'])."'"; + $update_array['sysContact'] = $poll_device['sysContact']; log_event("Contact -> ".$poll_device['sysContact'], $device, 'system'); } if ($poll_device['sysName'] && $poll_device['sysName'] != $device['sysName']) { - $device['db_update'] .= ", `sysName` = '".mres($poll_device['sysName'])."'"; + $update_array['sysName'] = $poll_device['sysName']; log_event("sysName -> ".$poll_device['sysName'], $device, 'system'); } if ($poll_device['sysDescr'] && $poll_device['sysDescr'] != $device['sysDescr']) { - $device['db_update'] .= ", `sysDescr` = '".mres($poll_device['sysDescr'])."'"; + $update_array['sysDescr'] = $poll_device['sysDescr']; log_event("sysDescr -> ".$poll_device['sysDescr'], $device, 'system'); } @@ -97,9 +97,9 @@ { if (!get_dev_attrib($device,'override_sysLocation_bool')) { - $device['db_update'] .= ", `location` = '".mres($poll_device['sysLocation'])."'"; + $update_array['location'] = $poll_device['sysLocation']; + log_event("Location -> ".$poll_device['sysLocation'], $device, 'system'); } - log_event("Location -> ".$poll_device['sysLocation'], $device, 'system'); } ?> diff --git a/poller.php b/poller.php index 16afb7f21..4e9c56f26 100755 --- a/poller.php +++ b/poller.php @@ -105,6 +105,7 @@ function poll_device($device, $options) { global $config; global $device; + global $polled_devices; $attribs = get_dev_attribs($device['device_id']); @@ -120,6 +121,7 @@ function poll_device($device, $options) { echo("\n"); unset($poll_update); unset($poll_update_query); unset($poll_separator); + $poll_update_array = array(); $host_rrd = $config['rrd_dir'] . "/" . $device['hostname']; if (!is_dir($host_rrd)) { mkdir($host_rrd); echo("Created directory : $host_rrd\n"); } @@ -202,29 +204,29 @@ if (!$options['m']) dbInsert(array('device_id' => $device['device_id'], 'graph' => $graph), 'device_graphs'); } } - } $device_end = utime(); $device_run = $device_end - $device_start; $device_time = substr($device_run, 0, 5); - $device['db_update'] = " `last_polled` = NOW() " . $device['db_update']; - $device['db_update'] .= ", `last_polled_timetaken` = '$device_time'"; + + $update_array['last_polled'] = array('NOW()'); + $update_array['last_polled_timetaken'] = $device_time; + #echo("$device_end - $device_start; $device_time $device_run"); echo("Polled in $device_time seconds\n"); - $device['db_update_query'] = "UPDATE `devices` SET "; - $device['db_update_query'] .= $device['db_update']; - $device['db_update_query'] .= " WHERE `device_id` = '" . $device['device_id'] . "'"; - if ($debug) { echo("Updating " . $device['hostname'] . " - ".$device['db_update_query']." \n"); } - if (!mysql_query($device['db_update_query'])) ## FIXME some work to be done to build the update array then dbUpdate() - { - echo("ERROR: " . mysql_error() . "\nSQL: ".$device['db_update_query']."\n"); - } - if (mysql_affected_rows() == "1") { echo("UPDATED!\n"); } else { echo("NOT UPDATED!\n"); } + + + if ($debug) { echo("Updating " . $device['hostname'] . " - ".print_r($update_array)." \n"); } + + $updated = dbUpdate($update_array, 'devices', '`device_id` = ?', array($device['device_id'])); + if($updated) { echo("UPDATED!\n"); } unset($storage_cache); // Clear cache of hrStorage ** MAYBE FIXME? ** unset($cache); // Clear cache (unify all things here?) } + $polled_devices++; + } $poller_end = utime(); $poller_run = $poller_end - $poller_start; $poller_time = substr($poller_run, 0, 5);