- Added STDOUT logging for PollD_MP if no logfile is configured.

- Added new exception handling and logging for ADODB.
This commit is contained in:
Frank Fegert 2009-07-12 17:12:14 +00:00
parent 8882989945
commit 97afaa73a6
4 changed files with 269 additions and 207 deletions

View File

@ -1143,7 +1143,7 @@ function _adodb_backtrace($printOrArr=true,$levels=9999,$skippy=0,$ishtml=null)
$s .= "\n";
}
if ($html) $s .= '</pre>';
if ($printOrArr) print $s;
// if ($printOrArr) print $s;
return $s;
}

View File

@ -42,6 +42,7 @@ class ADOdb {
var $conn;
var $debug;
var $logfile;
/**
* constructor - establishes a DB connection via ADODB
@ -55,9 +56,10 @@ class ADOdb {
* @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) {
function ADOdb($host, $port = "3306", $user, $pass, $db_name, $db_type, $retr = 20, $debug = FALSE, $logfile = FALSE) {
$this->debug = $debug;
if ($logfile != FALSE) $this->logfile = $logfile;
$try = 0;
$hostport = $host . ":" . $port;
@ -93,6 +95,50 @@ class ADOdb {
/**
* setLogfile - set logfile path and name
*
* @param string $logfile Logfile path and name
*/
function setLogfile($logfile) {
if ($logfile != "") {
$this->logfile = $logfile;
}
}
/**
* 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;
}
}
/**
* closeDB - close an open DB connection
*
@ -115,13 +161,15 @@ class ADOdb {
$this->conn->debug = $this->debug;
$sql = $this->sanitizeSQL($sql);
try {
$recordSet = &$this->conn->Execute($sql);
} catch (exception $e) {
$this->writeMSG(adodb_backtrace($e->gettrace()));
exit;
}
if (($recordSet) || ($this->conn->ErrorNo() == 0)) {
return($recordSet);
} else {
echo "<p style='font-size: 16px; font-weight: bold; color: red;'>Database Error (".$this->conn->ErrorNo().")</p>\n<p>".$this->conn->ErrorMsg()."</p>";
//exit;
return "";
}
}
@ -136,7 +184,6 @@ class ADOdb {
* @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);
@ -145,7 +192,12 @@ class ADOdb {
} 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)) {
if (!$recordSet->EOF) {
@ -158,9 +210,6 @@ class ADOdb {
return($column);
}
} else {
echo "<p style='font-size: 16px; font-weight: bold; color: red;'>Database Error (".$this->conn->ErrorNo().")</p>\n<p>".$this->conn->ErrorMsg()."</p>";
exit;
}
}
@ -173,12 +222,16 @@ class ADOdb {
* @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);
$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) {
@ -187,9 +240,6 @@ class ADOdb {
return($recordFields);
}
} else {
echo "<p style='font-size: 16px; font-weight: bold; color: red;'>Database Error (".$this->conn->ErrorNo().")</p>\n<p>".$this->conn->ErrorMsg()."</p>";
exit;
}
}
@ -202,13 +252,17 @@ class ADOdb {
* @return array All results in an associative array
*/
function fetchArrayDB($sql) {
//$this->conn->debug = true;
$this->conn->debug = $this->debug;
$sql = $this->sanitizeSQL($sql);
$recordArray = array();
$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)) {
while ((!$recordSet->EOF) && ($recordSet)) {
@ -217,9 +271,6 @@ class ADOdb {
}
$recordSet->close();
return($recordArray);
} else {
echo "<p style='font-size: 16px; font-weight: bold; color: red;'>Database Error (".$this->conn->ErrorNo().")</p>\n<p>".$this->conn->ErrorMsg()."</p>";
exit;
}
}
@ -237,9 +288,13 @@ class ADOdb {
* @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;
try {
$this->conn->Replace($table, $cells, $keys, $autoquote);
} catch (exception $e) {
$this->writeMSG(adodb_backtrace($e->gettrace()));
exit;
}
return $this->conn->Insert_ID();
}

View File

@ -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;
}

View File

@ -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,6 +805,7 @@ class PollD_MP {
}
if ($this->debuglevel >= $ilevel) {
if ($this->logfile != "") {
if ($loghandle = fopen($this->logfile, 'a')) {
$starttime = microtime();
do {
@ -818,6 +821,9 @@ class PollD_MP {
echo "ERROR: Cannot open logfile: '".$logfile."' for writing. Falling back to STDOUT.\n";
echo $level.": ".$msg;
}
} else {
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