restore 2516 dbFacile.php, apparently the code layout changes breaking something somewhere?

git-svn-id: http://www.observium.org/svn/observer/trunk@2525 61d68cd4-352d-0410-923a-c4978735b2b8
This commit is contained in:
Tom Laermans
2011-09-20 15:59:51 +00:00
parent 43f3c547b7
commit 3558ea53a0
4 changed files with 231 additions and 238 deletions

View File

@@ -9,9 +9,9 @@ $config['db_pass'] = "PASSWORD";
$config['db_name'] = "observium"; $config['db_name'] = "observium";
### Locations ### Locations
$config['install_dir'] = "/opt/observium"; $config['install_dir'] = "/opt/observium";
$config['html_dir'] = $config['install_dir'] . "/html"; $config['html_dir'] = $config['install_dir'] . "/html";
$config['rrd_dir'] = $config['install_dir'] . "/rrd"; $config['rrd_dir'] = $config['install_dir'] . "/rrd";
$config['log_file'] = $config['install_dir'] . "/observium.log"; $config['log_file'] = $config['install_dir'] . "/observium.log";
### Thie should *only* be set if you want to *force* a particular hostname/port ### Thie should *only* be set if you want to *force* a particular hostname/port

View File

@@ -161,7 +161,7 @@ if (isset($_GET['format']) && preg_match("/^[a-z]*$/", $_GET['format']))
$maptool = 'dot'; $maptool = 'dot';
} }
if ($where == '') { $maptool = 'neato -Gpack'; } if ($where == '') { $maptool = 'sfdp -Gpack -Gcharset=latin1 -Gsize=200,200'; }
$img = shell_exec("echo \"".addslashes($map)."\" | $maptool -T".$_GET['format'].""); $img = shell_exec("echo \"".addslashes($map)."\" | $maptool -T".$_GET['format']."");
if ($_GET['format'] == "png") if ($_GET['format'] == "png")

View File

@@ -22,30 +22,29 @@ Usage
* Used by the other _query functions. * Used by the other _query functions.
* */ * */
function dbQuery($sql, $parameters = array()) { function dbQuery($sql, $parameters = array()) {
global $fullSql, $debug; global $fullSql, $debug;
$fullSql = dbMakeQuery($sql, $parameters);
if($debug) { echo(" SQL[".$fullSql."] "); }
/*
if($this->logFile)
$time_start = microtime(true);
*/
$fullSql = dbMakeQuery($sql, $parameters); # echo($fullSql);
if ($debug) { echo(" SQL[".$fullSql."] "); }
/*
if ($this->logFile)
$time_start = microtime(true);
*/
# echo($fullSql); $result = mysql_query($fullSql); // sets $this->result
/*
if($this->logFile) {
$time_end = microtime(true);
fwrite($this->logFile, date('Y-m-d H:i:s') . "\n" . $fullSql . "\n" . number_format($time_end - $time_start, 8) . " seconds\n\n");
}
*/
$result = mysql_query($fullSql); // sets $this->result if($result === false && (error_reporting() & 1)) {
/* // aye. this gets triggers on duplicate Contact insert
if ($this->logFile) { //trigger_error('QDB - Error in query: ' . $fullSql . ' : ' . mysql_error(), E_USER_WARNING);
$time_end = microtime(true); }
fwrite($this->logFile, date('Y-m-d H:i:s') . "\n" . $fullSql . "\n" . number_format($time_end - $time_start, 8) . " seconds\n\n"); return $result;
}
*/
if ($result === false && (error_reporting() & 1)) {
// aye. this gets triggers on duplicate Contact insert
//trigger_error('QDB - Error in query: ' . $fullSql . ' : ' . mysql_error(), E_USER_WARNING);
}
return $result;
} }
/* /*
@@ -53,39 +52,37 @@ function dbQuery($sql, $parameters = array()) {
* Check for boolean false to determine whether insert failed * Check for boolean false to determine whether insert failed
* */ * */
function dbInsert($data, $table) { function dbInsert($data, $table) {
global $fullSql; global $fullSql;
global $db_stats; global $db_stats;
// the following block swaps the parameters if they were given in the wrong order.
// it allows the method to work for those that would rather it (or expect it to)
// follow closer with SQL convention:
// insert into the TABLE this DATA
if(is_string($data) && is_array($table)) {
$tmp = $data;
$data = $table;
$table = $tmp;
//trigger_error('QDB - Parameters passed to insert() were in reverse order, but it has been allowed', E_USER_NOTICE);
}
// the following block swaps the parameters if they were given in the wrong order. $sql = 'INSERT INTO `' . $table . '` (`' . implode('`,`', array_keys($data)) . '`) VALUES (' . implode(',', dbPlaceHolders($data)) . ')';
// it allows the method to work for those that would rather it (or expect it to)
// follow closer with SQL convention:
// insert into the TABLE this DATA
if (is_string($data) && is_array($table)) {
$tmp = $data;
$data = $table;
$table = $tmp;
//trigger_error('QDB - Parameters passed to insert() were in reverse order, but it has been allowed', E_USER_NOTICE);
}
$sql = 'INSERT INTO `' . $table . '` (`' . implode('`,`', array_keys($data)) . '`) VALUES (' . implode(',', dbPlaceHolders($data)) . ')';
$time_start = microtime(true); $time_start = microtime(true);
dbBeginTransaction(); dbBeginTransaction();
$result = dbQuery($sql, $data); $result = dbQuery($sql, $data);
if ($result) { if($result) {
$id = mysql_insert_id(); $id = mysql_insert_id();
dbCommitTransaction(); dbCommitTransaction();
#return $id; #return $id;
} else { } else {
if ($table != 'Contact') { if($table != 'Contact') {
trigger_error('QDB - Insert failed.', E_USER_WARNING); trigger_error('QDB - Insert failed.', E_USER_WARNING);
} }
dbRollbackTransaction(); dbRollbackTransaction();
#$id = false; #$id = false;
} }
#logfile($fullSql); #logfile($fullSql);
$time_end = microtime(true); $time_end = microtime(true);
$db_stats['insert_sec'] += number_format($time_end - $time_start, 8); $db_stats['insert_sec'] += number_format($time_end - $time_start, 8);
@@ -100,42 +97,40 @@ function dbInsert($data, $table) {
* Returns the number of affected rows * Returns the number of affected rows
* */ * */
function dbUpdate($data, $table, $where = null, $parameters = array()) { function dbUpdate($data, $table, $where = null, $parameters = array()) {
global $fullSql; global $fullSql;
global $db_stats; global $db_stats;
// the following block swaps the parameters if they were given in the wrong order.
// it allows the method to work for those that would rather it (or expect it to)
// follow closer with SQL convention:
// update the TABLE with this DATA
if(is_string($data) && is_array($table)) {
$tmp = $data;
$data = $table;
$table = $tmp;
//trigger_error('QDB - The first two parameters passed to update() were in reverse order, but it has been allowed', E_USER_NOTICE);
}
// the following block swaps the parameters if they were given in the wrong order. // need field name and placeholder value
// it allows the method to work for those that would rather it (or expect it to) // but how merge these field placeholders with actual $parameters array for the WHERE clause
// follow closer with SQL convention: $sql = 'UPDATE `' . $table . '` set ';
// update the TABLE with this DATA foreach($data as $key => $value) {
if (is_string($data) && is_array($table)) {
$tmp = $data;
$data = $table;
$table = $tmp;
//trigger_error('QDB - The first two parameters passed to update() were in reverse order, but it has been allowed', E_USER_NOTICE);
}
// need field name and placeholder value
// but how merge these field placeholders with actual $parameters array for the WHERE clause
$sql = 'UPDATE `' . $table . '` set ';
foreach($data as $key => $value) {
$sql .= "`".$key."` ". '=:' . $key . ','; $sql .= "`".$key."` ". '=:' . $key . ',';
} }
$sql = substr($sql, 0, -1); // strip off last comma $sql = substr($sql, 0, -1); // strip off last comma
if ($where) { if($where) {
$sql .= ' WHERE ' . $where; $sql .= ' WHERE ' . $where;
$data = array_merge($data, $parameters); $data = array_merge($data, $parameters);
} }
$time_start = microtime(true); $time_start = microtime(true);
if (dbQuery($sql, $data)) { if(dbQuery($sql, $data)) {
$return = mysql_affected_rows(); $return = mysql_affected_rows();
} else { } else {
#echo("$fullSql"); #echo("$fullSql");
trigger_error('QDB - Update failed.', E_USER_WARNING); trigger_error('QDB - Update failed.', E_USER_WARNING);
$return = false; $return = false;
} }
$time_end = microtime(true); $time_end = microtime(true);
$db_stats['update_sec'] += number_format($time_end - $time_start, 8); $db_stats['update_sec'] += number_format($time_end - $time_start, 8);
$db_stats['update']++; $db_stats['update']++;
@@ -145,15 +140,15 @@ function dbUpdate($data, $table, $where = null, $parameters = array()) {
} }
function dbDelete($table, $where = null, $parameters = array()) { function dbDelete($table, $where = null, $parameters = array()) {
$sql = 'DELETE FROM `' . $table.'`'; $sql = 'DELETE FROM `' . $table.'`';
if ($where) { if($where) {
$sql .= ' WHERE ' . $where; $sql .= ' WHERE ' . $where;
} }
if (dbQuery($sql, $parameters)) { if(dbQuery($sql, $parameters)) {
return mysql_affected_rows(); return mysql_affected_rows();
} else { } else {
return false; return false;
} }
} }
/* /*
@@ -164,16 +159,16 @@ function dbFetchRows($sql, $parameters = array()) {
global $db_stats; global $db_stats;
$time_start = microtime(true); $time_start = microtime(true);
$result = dbQuery($sql, $parameters); $result = dbQuery($sql, $parameters);
if (mysql_num_rows($result) > 0) { if(mysql_num_rows($result) > 0) {
$rows = array(); $rows = array();
while ($row = mysql_fetch_assoc($result)) { while($row = mysql_fetch_assoc($result)) {
$rows[] = $row; $rows[] = $row;
} }
mysql_free_result($result); mysql_free_result($result);
return $rows; return $rows;
} }
mysql_free_result($result); mysql_free_result($result);
@@ -181,26 +176,26 @@ function dbFetchRows($sql, $parameters = array()) {
$db_stats['fetchrows_sec'] += number_format($time_end - $time_start, 8); $db_stats['fetchrows_sec'] += number_format($time_end - $time_start, 8);
$db_stats['fetchrows']++; $db_stats['fetchrows']++;
// no records, thus return empty array // no records, thus return empty array
// which should evaluate to false, and will prevent foreach notices/warnings // which should evaluate to false, and will prevent foreach notices/warnings
return array(); return array();
} }
/* /*
* This is intended to be the method used for large result sets. * This is intended to be the method used for large result sets.
* It is intended to return an iterator, and act upon buffered data. * It is intended to return an iterator, and act upon buffered data.
* */ * */
function dbFetch($sql, $parameters = array()) { function dbFetch($sql, $parameters = array()) {
return dbFetchRows($sql, $parameters); return dbFetchRows($sql, $parameters);
/* /*
// for now, don't do the iterator thing // for now, don't do the iterator thing
$result = dbQuery($sql, $parameters); $result = dbQuery($sql, $parameters);
if ($result) { if($result) {
// return new iterator // return new iterator
return new dbIterator($result); return new dbIterator($result);
} else { } else {
return null; // ?? return null; // ??
} }
*/ */
} }
/* /*
@@ -211,19 +206,19 @@ function dbFetchRow($sql = null, $parameters = array()) {
global $db_stats; global $db_stats;
$time_start = microtime(true); $time_start = microtime(true);
$result = dbQuery($sql, $parameters); $result = dbQuery($sql, $parameters);
if ($result) { if($result) {
$row = mysql_fetch_assoc($result); $row = mysql_fetch_assoc($result);
mysql_free_result($result); mysql_free_result($result);
$time_end = microtime(true); $time_end = microtime(true);
$db_stats['fetchrow_sec'] += number_format($time_end - $time_start, 8); $db_stats['fetchrow_sec'] += number_format($time_end - $time_start, 8);
$db_stats['fetchrow']++; $db_stats['fetchrow']++;
return $row; return $row;
} else { } else {
return null; return null;
} }
$time_start = microtime(true); $time_start = microtime(true);
} }
@@ -233,18 +228,17 @@ function dbFetchRow($sql = null, $parameters = array()) {
* */ * */
function dbFetchCell($sql, $parameters = array()) { function dbFetchCell($sql, $parameters = array()) {
global $db_stats; global $db_stats;
$time_start = microtime(true); $time_start = microtime(true);
$row = dbFetchRow($sql, $parameters); $row = dbFetchRow($sql, $parameters);
if ($row) { if($row) {
return array_shift($row); // shift first field off first row return array_shift($row); // shift first field off first row
} }
$time_end = microtime(true); $time_end = microtime(true);
$db_stats['fetchcell_sec'] += number_format($time_end - $time_start, 8); $db_stats['fetchcell_sec'] += number_format($time_end - $time_start, 8);
$db_stats['fetchcell']++; $db_stats['fetchcell']++;
return null; return null;
} }
/* /*
@@ -253,18 +247,17 @@ function dbFetchCell($sql, $parameters = array()) {
* */ * */
function dbFetchColumn($sql, $parameters = array()) { function dbFetchColumn($sql, $parameters = array()) {
global $db_stats; global $db_stats;
$time_start = microtime(true); $time_start = microtime(true);
$cells = array(); $cells = array();
foreach(dbFetch($sql, $parameters) as $row) { foreach(dbFetch($sql, $parameters) as $row) {
$cells[] = array_shift($row); $cells[] = array_shift($row);
} }
$time_end = microtime(true); $time_end = microtime(true);
$db_stats['fetchcol_sec'] += number_format($time_end - $time_start, 8); $db_stats['fetchcol_sec'] += number_format($time_end - $time_start, 8);
$db_stats['fetchcol']++; $db_stats['fetchcol']++;
return $cells; return $cells;
} }
/* /*
@@ -273,18 +266,18 @@ function dbFetchColumn($sql, $parameters = array()) {
* The second the key's value * The second the key's value
*/ */
function dbFetchKeyValue($sql, $parameters = array()) { function dbFetchKeyValue($sql, $parameters = array()) {
$data = array(); $data = array();
foreach(dbFetch($sql, $parameters) as $row) { foreach(dbFetch($sql, $parameters) as $row) {
$key = array_shift($row); $key = array_shift($row);
if (sizeof($row) == 1) { // if there were only 2 fields in the result if(sizeof($row) == 1) { // if there were only 2 fields in the result
// use the second for the value // use the second for the value
$data[ $key ] = array_shift($row); $data[ $key ] = array_shift($row);
} else { // if more than 2 fields were fetched } else { // if more than 2 fields were fetched
// use the array of the rest as the value // use the array of the rest as the value
$data[ $key ] = $row; $data[ $key ] = $row;
} }
} }
return $data; return $data;
} }
/* /*
@@ -293,69 +286,69 @@ function dbFetchKeyValue($sql, $parameters = array()) {
*/ */
function dbMakeQuery($sql, $parameters) { function dbMakeQuery($sql, $parameters) {
// bypass extra logic if we have no parameters // bypass extra logic if we have no parameters
if (sizeof($parameters) == 0) if(sizeof($parameters) == 0)
return $sql; return $sql;
$parameters = dbPrepareData($parameters); $parameters = dbPrepareData($parameters);
// separate the two types of parameters for easier handling // separate the two types of parameters for easier handling
$questionParams = array(); $questionParams = array();
$namedParams = array(); $namedParams = array();
foreach($parameters as $key => $value) { foreach($parameters as $key => $value) {
if (is_numeric($key)) { if(is_numeric($key)) {
$questionParams[] = $value; $questionParams[] = $value;
} else { } else {
$namedParams[ ':' . $key ] = $value; $namedParams[ ':' . $key ] = $value;
} }
} }
// sort namedParams in reverse to stop substring squashing // sort namedParams in reverse to stop substring squashing
krsort($namedParams); krsort($namedParams);
// split on question-mark and named placeholders // split on question-mark and named placeholders
$result = preg_split('/(\?|:[a-zA-Z0-9_-]+)/', $sql, -1, PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE); $result = preg_split('/(\?|:[a-zA-Z0-9_-]+)/', $sql, -1, PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE);
// every-other item in $result will be the placeholder that was found // every-other item in $result will be the placeholder that was found
$query = ''; $query = '';
for($i = 0; $i < sizeof($result); $i+=2) { for($i = 0; $i < sizeof($result); $i+=2) {
$query .= $result[ $i ]; $query .= $result[ $i ];
$j = $i+1; $j = $i+1;
if (array_key_exists($j, $result)) { if(array_key_exists($j, $result)) {
$test = $result[ $j ]; $test = $result[ $j ];
if ($test == '?') { if($test == '?') {
$query .= array_shift($questionParams); $query .= array_shift($questionParams);
} else { } else {
$query .= $namedParams[ $test ]; $query .= $namedParams[ $test ];
} }
} }
} }
return $query; return $query;
} }
function dbPrepareData($data) { function dbPrepareData($data) {
$values = array(); $values = array();
foreach($data as $key=>$value) { foreach($data as $key=>$value) {
$escape = true; $escape = true;
// don't quote or esc if value is an array, we treat it // don't quote or esc if value is an array, we treat it
// as a "decorator" that tells us not to escape the // as a "decorator" that tells us not to escape the
// value contained in the array // value contained in the array
if (is_array($value) && !is_object($value)) { if(is_array($value) && !is_object($value)) {
$escape = false; $escape = false;
$value = array_shift($value); $value = array_shift($value);
} }
// it's not right to worry about invalid fields in this method because we may be operating on fields // it's not right to worry about invalid fields in this method because we may be operating on fields
// that are aliases, or part of other tables through joins // that are aliases, or part of other tables through joins
//if(!in_array($key, $columns)) // skip invalid fields //if(!in_array($key, $columns)) // skip invalid fields
// continue; // continue;
if ($escape) { if($escape) {
$values[$key] = "'" . mysql_real_escape_string($value) . "'"; $values[$key] = "'" . mysql_real_escape_string($value) . "'";
} else } else
$values[$key] = $value; $values[$key] = $value;
} }
return $values; return $values;
} }
/* /*
@@ -363,61 +356,61 @@ function dbPrepareData($data) {
* These may be question marks, or ":email" type * These may be question marks, or ":email" type
*/ */
function dbPlaceHolders($values) { function dbPlaceHolders($values) {
$data = array(); $data = array();
foreach($values as $key => $value) { foreach($values as $key => $value) {
if (is_numeric($key)) if(is_numeric($key))
$data[] = '?'; $data[] = '?';
else else
$data[] = ':' . $key; $data[] = ':' . $key;
} }
return $data; return $data;
} }
function dbBeginTransaction() { function dbBeginTransaction() {
mysql_query('begin'); mysql_query('begin');
} }
function dbCommitTransaction() { function dbCommitTransaction() {
mysql_query('commit'); mysql_query('commit');
} }
function dbRollbackTransaction() { function dbRollbackTransaction() {
mysql_query('rollback'); mysql_query('rollback');
} }
/* /*
class dbIterator implements Iterator { class dbIterator implements Iterator {
private $result; private $result;
private $i; private $i;
public function __construct($r) { public function __construct($r) {
$this->result = $r; $this->result = $r;
$this->i = 0; $this->i = 0;
} }
public function rewind() { public function rewind() {
mysql_data_seek($this->result, 0); mysql_data_seek($this->result, 0);
$this->i = 0; $this->i = 0;
} }
public function current() { public function current() {
$a = mysql_fetch_assoc($this->result); $a = mysql_fetch_assoc($this->result);
return $a; return $a;
} }
public function key() { public function key() {
return $this->i; return $this->i;
} }
public function next() { public function next() {
$this->i++; $this->i++;
$a = mysql_data_seek($this->result, $this->i); $a = mysql_data_seek($this->result, $this->i);
if ($a === false) { if($a === false) {
$this->i = 0; $this->i = 0;
} }
return $a; return $a;
} }
public function valid() { public function valid() {
return ($this->current() !== false); return ($this->current() !== false);
} }
} }
*/ */

0
includes/polling/ipmi.inc.php Normal file → Executable file
View File