From 97afaa73a65fbaf33673fe8c069a4959aecbdbf9 Mon Sep 17 00:00:00 2001 From: Frank Fegert Date: Sun, 12 Jul 2009 17:12:14 +0000 Subject: [PATCH] - Added STDOUT logging for PollD_MP if no logfile is configured. - Added new exception handling and logging for ADODB. --- extlib/adodb5/adodb-lib.inc.php | 4 +- includes/adodb.php | 435 ++++++++++++++++++-------------- includes/global.php | 5 +- includes/polld.php | 32 ++- 4 files changed, 269 insertions(+), 207 deletions(-) diff --git a/extlib/adodb5/adodb-lib.inc.php b/extlib/adodb5/adodb-lib.inc.php index 9b46ded..a7c7010 100644 --- a/extlib/adodb5/adodb-lib.inc.php +++ b/extlib/adodb5/adodb-lib.inc.php @@ -1143,7 +1143,7 @@ function _adodb_backtrace($printOrArr=true,$levels=9999,$skippy=0,$ishtml=null) $s .= "\n"; } if ($html) $s .= ''; - if ($printOrArr) print $s; +// if ($printOrArr) print $s; return $s; } @@ -1190,4 +1190,4 @@ function _adodb_find_from($sql) } */ -?> \ No newline at end of file +?> diff --git a/includes/adodb.php b/includes/adodb.php index bce275e..82ed314 100644 --- a/includes/adodb.php +++ b/includes/adodb.php @@ -40,225 +40,280 @@ class ADOdb { - var $conn; - var $debug; + var $conn; + var $debug; + var $logfile; - /** - * constructor - establishes a DB connection via ADODB - * - * @param string $host the hostname of the DB server - * @param string $port the portnumber for the DB connection - * @param string $user the username for the DB connection - * @param string $pass the password for the DB connection - * @param string $db_name the name of the DB - * @param string $db_type the type of the DB (currently only 'mysql') - * @param string $retr the number attempts for the DB connection before a failure is reported - * @return 0 - */ - function ADOdb($host, $port = "3306", $user, $pass, $db_name, $db_type, $retr = 20, $debug = FALSE) { + /** + * constructor - establishes a DB connection via ADODB + * + * @param string $host the hostname of the DB server + * @param string $port the portnumber for the DB connection + * @param string $user the username for the DB connection + * @param string $pass the password for the DB connection + * @param string $db_name the name of the DB + * @param string $db_type the type of the DB (currently only 'mysql') + * @param string $retr the number attempts for the DB connection before a failure is reported + * @return 0 + */ + function ADOdb($host, $port = "3306", $user, $pass, $db_name, $db_type, $retr = 20, $debug = FALSE, $logfile = FALSE) { - $this->debug = $debug; + $this->debug = $debug; + if ($logfile != FALSE) $this->logfile = $logfile; - $try = 0; - $hostport = $host . ":" . $port; - $this->conn = NewADOConnection($db_type); - while ($try <= $retries) { - if ($this->conn->NConnect($hostport,$user,$pass,$db_name)) { - $this->conn = $this->conn; - return 0; - } - $try++; - usleep(50000); - } + $try = 0; + $hostport = $host . ":" . $port; + $this->conn = NewADOConnection($db_type); + while ($try <= $retries) { + if ($this->conn->NConnect($hostport,$user,$pass,$db_name)) { + $this->conn = $this->conn; + return 0; + } + $try++; + usleep(50000); + } - die("FATAL: Cannot connect to database server on '$host':'$port'. Please make sure you have specified a valid database name in 'includes/config.php'\n"); - return 0; - } + die("FATAL: Cannot connect to database server on '$host':'$port'. Please make sure you have specified a valid database name in 'includes/config.php'\n"); + return 0; + } - /** - * setDebug - enables or disabled debug mode - * - * @param string $debug On or Off - */ - function setDebug($debug) { - if ($debug == "On") { - $this->debug = TRUE; - } else { - $this->debug = FALSE; - } - } + /** + * setDebug - enables or disabled debug mode + * + * @param string $debug On or Off + */ + function setDebug($debug) { + if ($debug == "On") { + $this->debug = TRUE; + } else { + $this->debug = FALSE; + } + } - /** - * closeDB - close an open DB connection - * - * @return string - */ - function closeDB() { - if ($this->conn) { - return $this->conn->Close(); - } - } + /** + * setLogfile - set logfile path and name + * + * @param string $logfile Logfile path and name + */ + function setLogfile($logfile) { + if ($logfile != "") { + $this->logfile = $logfile; + } + } - /** - * execDB - execute a SQL statement against the DB via ADODB - * - * @param string $sql SQL statement to execute - * @return ADORecordSet - */ - function execDB($sql) { - $this->conn->debug = $this->debug; - $sql = $this->sanitizeSQL($sql); - $recordSet = &$this->conn->Execute($sql); - if (($recordSet) || ($this->conn->ErrorNo() == 0)) { - return($recordSet); - } else { - echo "

Database Error (".$this->conn->ErrorNo().")

\n

".$this->conn->ErrorMsg()."

"; - //exit; - return ""; - } - } + /** + * writeMSG + * + * @param mixed $msg + * @access public + * @return void + */ + function writeMSG($msg) { + + if ($this->logfile != "") { + if ($loghandle = fopen($this->logfile, 'a')) { + $starttime = microtime(); + do { + $lock = flock($loghandle, LOCK_EX); + if (!$lock) usleep(round(rand(0, 100) * 1000)); + } while (!$lock && ((microtime() - $starttime) < 1000)); + + if ($lock) { + fwrite($loghandle, $msg); + } + fclose($loghandle); + } else { + echo "ERROR: Cannot open logfile: '".$logfile."' for writing. Falling back to STDOUT.\n"; + echo $msg; + } + } else { + echo $msg; + } + } - /** - * fetchCellDB - execute a SQL query against the DB via ADODB and - * return only the first column of the fist row found - * or a specified column of the fist row found - * - * @param string $sql SQL statement to execute - * @param $column_name Column name to use instead of the first column - * @return string Content of the cell as a single variable - */ - function fetchCellDB($sql, $column_name) { - //$this->conn->debug = true; - $this->conn->debug = $this->debug; - $sql = $this->sanitizeSQL($sql); - - if ($column_name != '') { - $this->conn->SetFetchMode(ADODB_FETCH_ASSOC); - } else { - $this->conn->SetFetchMode(ADODB_FETCH_NUM); - } - $recordSet = $this->conn->Execute($sql); - - if (($recordSet) || ($this->conn->ErrorNo() == 0)) { - if (!$recordSet->EOF) { - if ($column_name != '') { - $column = $recordSet->fields[$column_name]; - }else{ - $column = $recordSet->fields[0]; - } - $recordSet->close(); - - return($column); - } - } else { - echo "

Database Error (".$this->conn->ErrorNo().")

\n

".$this->conn->ErrorMsg()."

"; - exit; - } - } + /** + * closeDB - close an open DB connection + * + * @return string + */ + function closeDB() { + if ($this->conn) { + return $this->conn->Close(); + } + } - /** - * fetchRowDB - execute a SQL query against the DB via ADODB - * and return only the first row found - * - * @param string $sql SQL statement to execute - * @return array First row of results as an associative array - */ - function fetchRowDB($sql) { - //$this->conn->debug = true; - $this->conn->debug = $this->debug; - $sql = $this->sanitizeSQL($sql); + /** + * execDB - execute a SQL statement against the DB via ADODB + * + * @param string $sql SQL statement to execute + * @return ADORecordSet + */ + function execDB($sql) { + $this->conn->debug = $this->debug; + $sql = $this->sanitizeSQL($sql); - $this->conn->SetFetchMode(ADODB_FETCH_ASSOC); - $recordSet = $this->conn->Execute($sql); + try { + $recordSet = &$this->conn->Execute($sql); + } catch (exception $e) { + $this->writeMSG(adodb_backtrace($e->gettrace())); + exit; + } - if (($recordSet) || ($this->conn->ErrorNo() == 0)) { - if (!$recordSet->EOF) { - $recordFields = $recordSet->fields; - $recordSet->close(); - - return($recordFields); - } - } else { - echo "

Database Error (".$this->conn->ErrorNo().")

\n

".$this->conn->ErrorMsg()."

"; - exit; - } - } + if (($recordSet) || ($this->conn->ErrorNo() == 0)) { + return($recordSet); + } + } - /** - * fetchArrayDB - execute a SQL query against the DB via ADODB - * and return results in an associative array. - * - * @param string $sql SQL statement to execute - * @return array All results in an associative array - */ - function fetchArrayDB($sql) { - //$this->conn->debug = true; - $this->conn->debug = $this->debug; - $sql = $this->sanitizeSQL($sql); + /** + * fetchCellDB - execute a SQL query against the DB via ADODB and + * return only the first column of the fist row found + * or a specified column of the fist row found + * + * @param string $sql SQL statement to execute + * @param $column_name Column name to use instead of the first column + * @return string Content of the cell as a single variable + */ + function fetchCellDB($sql, $column_name) { + $this->conn->debug = $this->debug; + $sql = $this->sanitizeSQL($sql); - $recordArray = array(); - $this->conn->SetFetchMode(ADODB_FETCH_ASSOC); - $recordSet = &$this->conn->Execute($sql); + if ($column_name != '') { + $this->conn->SetFetchMode(ADODB_FETCH_ASSOC); + } else { + $this->conn->SetFetchMode(ADODB_FETCH_NUM); + } + try { + $recordSet = $this->conn->Execute($sql); + } catch (exception $e) { + $this->writeMSG(adodb_backtrace($e->gettrace())); + exit; + } - if (($recordSet) || ($this->conn->ErrorNo() == 0)) { - while ((!$recordSet->EOF) && ($recordSet)) { - $recordArray{sizeof($recordArray)} = $recordSet->fields; - $recordSet->MoveNext(); - } - $recordSet->close(); - return($recordArray); - } else { - echo "

Database Error (".$this->conn->ErrorNo().")

\n

".$this->conn->ErrorMsg()."

"; - exit; - } - } + if (($recordSet) || ($this->conn->ErrorNo() == 0)) { + if (!$recordSet->EOF) { + if ($column_name != '') { + $column = $recordSet->fields[$column_name]; + }else{ + $column = $recordSet->fields[0]; + } + $recordSet->close(); + + return($column); + } + } + } - /** - * updateDB - execute a SQL update statement against the DB via ADODB - * to update a record. If the record is not found, an insert - * statement is generated and executed. - * - * @param string $table The name of the table containing the record to be updated - * @param array $cells An array of columnname/value pairs of the record to be updated - * @param string $keys Name of the primary key - * @param boolean $autoquote Use intelligent auto-quoting - * @param ADOConnection $this->conn DB connection ID to run the SQL against - * @return string Auto-increment ID if insert was performed - */ - function updateDB($table, $cells, $keys, $autoquote = TRUE) { - //$this->conn->debug = true; - $this->conn->debug = $this->debug; - $this->conn->Replace($table, $cells, $keys, $autoquote); + /** + * fetchRowDB - execute a SQL query against the DB via ADODB + * and return only the first row found + * + * @param string $sql SQL statement to execute + * @return array First row of results as an associative array + */ + function fetchRowDB($sql) { + $this->conn->debug = $this->debug; + $sql = $this->sanitizeSQL($sql); - return $this->conn->Insert_ID(); - } + $this->conn->SetFetchMode(ADODB_FETCH_ASSOC); + try { + $recordSet = $this->conn->Execute($sql); + } catch (exception $e) { + $this->writeMSG(adodb_backtrace($e->gettrace())); + exit; + } + + if (($recordSet) || ($this->conn->ErrorNo() == 0)) { + if (!$recordSet->EOF) { + $recordFields = $recordSet->fields; + $recordSet->close(); + + return($recordFields); + } + } + } - /** - * sanitizeSQL - removes unwanted chars in values passed for use in - * SQL statements - * - * @param string $sql SQL expression to sanitize - * @return string - */ - function sanitizeSQL($sql) { - $sql = str_replace(";", "\;", $sql); - $sql = str_replace("\n", "", $sql); - $sql = str_replace("\r", "", $sql); - $sql = str_replace("\t", " ", $sql); - return $sql; - } + /** + * fetchArrayDB - execute a SQL query against the DB via ADODB + * and return results in an associative array. + * + * @param string $sql SQL statement to execute + * @return array All results in an associative array + */ + function fetchArrayDB($sql) { + $this->conn->debug = $this->debug; + $sql = $this->sanitizeSQL($sql); + + $recordArray = array(); + $this->conn->SetFetchMode(ADODB_FETCH_ASSOC); + try { + $recordSet = $this->conn->Execute($sql); + } catch (exception $e) { + $this->writeMSG(adodb_backtrace($e->gettrace())); + exit; + } + + if (($recordSet) || ($this->conn->ErrorNo() == 0)) { + while ((!$recordSet->EOF) && ($recordSet)) { + $recordArray{sizeof($recordArray)} = $recordSet->fields; + $recordSet->MoveNext(); + } + $recordSet->close(); + return($recordArray); + } + } + + + /** + * updateDB - execute a SQL update statement against the DB via ADODB + * to update a record. If the record is not found, an insert + * statement is generated and executed. + * + * @param string $table The name of the table containing the record to be updated + * @param array $cells An array of columnname/value pairs of the record to be updated + * @param string $keys Name of the primary key + * @param boolean $autoquote Use intelligent auto-quoting + * @param ADOConnection $this->conn DB connection ID to run the SQL against + * @return string Auto-increment ID if insert was performed + */ + function updateDB($table, $cells, $keys, $autoquote = TRUE) { + $this->conn->debug = $this->debug; + try { + $this->conn->Replace($table, $cells, $keys, $autoquote); + } catch (exception $e) { + $this->writeMSG(adodb_backtrace($e->gettrace())); + exit; + } + + return $this->conn->Insert_ID(); + } + + + /** + * sanitizeSQL - removes unwanted chars in values passed for use in + * SQL statements + * + * @param string $sql SQL expression to sanitize + * @return string + */ + function sanitizeSQL($sql) { + $sql = str_replace(";", "\;", $sql); + $sql = str_replace("\n", "", $sql); + $sql = str_replace("\r", "", $sql); + $sql = str_replace("\t", " ", $sql); + return $sql; + } } diff --git a/includes/global.php b/includes/global.php index 90a2db2..877b275 100644 --- a/includes/global.php +++ b/includes/global.php @@ -85,6 +85,7 @@ header("Pragma: no-cache"); //error_reporting(E_ALL); // ** Include generic code and external libraries ** // +include ($config["library_path"] . "/adodb5/adodb-exceptions.inc.php"); include ($config["library_path"] . "/adodb5/adodb.inc.php"); include_once($config["include_path"] . "/adodb.php"); include_once($config["include_path"] . "/tsmmonitor.php"); @@ -94,11 +95,11 @@ include_once($config["include_path"] . "/polld.php"); $adodb = new ADOdb($config["db_host"], $config["db_port"], $config["db_user"], $config["db_password"], $config["db_name"], $config["db_type"]); // ** instantiate TSMMonitor Class ** // -$tsmmonitor = new TSMMonitor($adodb); +if (isset($_SERVER['HTTP_USER_AGENT'])) $tsmmonitor = new TSMMonitor($adodb); // check to see if this is a new installation $version = $adodb->fetchCellDB("SELECT confval FROM cfg_config WHERE confkey='version'", ''); -if ($version != $config["tsm_monitor_version"] && basename($_SERVER['REQUEST_URI']) != 'install.php') { +if (isset($_SERVER['HTTP_USER_AGENT']) && $version != $config["tsm_monitor_version"] && basename($_SERVER['REQUEST_URI']) != 'install.php') { header("Location: install.php"); exit; } diff --git a/includes/polld.php b/includes/polld.php index 95ca213..a9c6a2c 100644 --- a/includes/polld.php +++ b/includes/polld.php @@ -80,6 +80,8 @@ class PollD { $this->loghandle = fopen($logfile[0]["confval"], 'at'); if (!$this->loghandle) { echo "ERROR: Cannot open logfile: '".$logfile[0]["confval"]."' for writing. Falling back to STDOUT.\n"; + } else { + $this->adodb->setLogfile($logfile[0]["confval"]); } } $sql = "select confval from cfg_config WHERE `confkey`='path_dsmadmc'"; @@ -732,7 +734,7 @@ class PollD_MP { // Check if PHP has pcntl_fork() enabled. foreach ($funcs as $func) { - if (!function_exists($func)){ + if (!function_exists($func)) { $func_ena = false; array_push($func_miss, $func); } @@ -803,19 +805,23 @@ class PollD_MP { } if ($this->debuglevel >= $ilevel) { - if ($loghandle = fopen($this->logfile, 'a')) { - $starttime = microtime(); - do { - $lock = flock($loghandle, LOCK_EX); - if (!$lock) usleep(round(rand(0, 100) * 1000)); - } while (!$lock && ((microtime() - $starttime) < 1000)); + if ($this->logfile != "") { + if ($loghandle = fopen($this->logfile, 'a')) { + $starttime = microtime(); + do { + $lock = flock($loghandle, LOCK_EX); + if (!$lock) usleep(round(rand(0, 100) * 1000)); + } while (!$lock && ((microtime() - $starttime) < 1000)); - if ($lock) { - fwrite($loghandle, $level.": ".$msg); + if ($lock) { + fwrite($loghandle, $level.": ".$msg); + } + fclose($loghandle); + } else { + echo "ERROR: Cannot open logfile: '".$logfile."' for writing. Falling back to STDOUT.\n"; + echo $level.": ".$msg; } - fclose($loghandle); } else { - echo "ERROR: Cannot open logfile: '".$logfile."' for writing. Falling back to STDOUT.\n"; echo $level.": ".$msg; } } @@ -1302,7 +1308,7 @@ class PollD_MP { if ($this->isEnabled()) { // Main loop for dispatcher while(true) { - $this->adodb = new ADOdb($this->cfg["db_host"], $this->cfg["db_port"], $this->cfg["db_user"], $this->cfg["db_password"], $this->cfg["db_name"], $this->cfg["db_type"]); + $this->adodb = new ADOdb($this->cfg["db_host"], $this->cfg["db_port"], $this->cfg["db_user"], $this->cfg["db_password"], $this->cfg["db_name"], $this->cfg["db_type"], "", "", $this->logfile); $this->updateJoblist($this->adodb); $query = "SELECT * FROM job_list"; $jobs = $this->adodb->fetchArrayDB($query); @@ -1352,7 +1358,7 @@ class PollD_MP { $this->child_pid = posix_getpid(); $timestamp = time(); $this->writeMSG("Worker(".$this->child_pid.") ".sprintf('%-16s', $server)." Timestamp for this run is $timestamp.\n", INFO); - $this->adodb = new ADOdb($this->cfg["db_host"], $this->cfg["db_port"], $this->cfg["db_user"], $this->cfg["db_password"], $this->cfg["db_name"], $this->cfg["db_type"]); + $this->adodb = new ADOdb($this->cfg["db_host"], $this->cfg["db_port"], $this->cfg["db_user"], $this->cfg["db_password"], $this->cfg["db_name"], $this->cfg["db_type"], "", "", $this->logfile); $this->setPollDStatus($server, "running", $this->child_pid, $timestamp, ""); // Process job