Skip to content
Snippets Groups Projects

Update Appmonitor client

Merged Hahn Axel (hahn) requested to merge rollout-plugins into master
16 files
+ 1000
571
Compare changes
  • Side-by-side
  • Inline

Files

@@ -48,8 +48,10 @@ if(!defined('RESULT_OK')){
* 2018-11-05 0.58 axel.hahn@iml.unibe.ch additional flag in http check to show content<br>
* 2019-05-31 0.87 axel.hahn@iml.unibe.ch add timeout as param in connective checks (http, tcp, databases)<br>
* 2019-06-05 0.88 axel.hahn@iml.unibe.ch add plugins<br>
* 2021-10-28 0.93 axel.hahn@iml.unibe.ch add plugins<br>
* 2021-12-14 0.93 axel.hahn@iml.unibe.ch split plugins into single files; added key group in a check<br>
* --------------------------------------------------------------------------------<br>
* @version 0.91
* @version 0.99
* @author Axel Hahn
* @link TODO
* @license GPL
@@ -116,6 +118,7 @@ class appmonitorcheck {
$this->_aData = array(
"name" => $this->_aConfig["name"],
"description" => $this->_aConfig["description"],
"group" => isset($this->_aConfig["group"]) ? $this->_aConfig["group"] : false,
"result" => RESULT_UNKNOWN,
"value" => false,
"type" => false,
@@ -148,7 +151,7 @@ class appmonitorcheck {
* @return boolean
*/
protected function _setCounter($aParams){
if(count($aParams)){
if(is_array($aParams) && count($aParams)){
foreach(array('type', 'count', 'visual') as $sMyKey){
if(isset($aParams[$sMyKey])){
$this->_aData[$sMyKey]=$aParams[$sMyKey];
@@ -182,11 +185,19 @@ class appmonitorcheck {
foreach (explode(",", $sKeyList) as $sKey) {
if (!array_key_exists($sKey, $aConfig)) {
header('HTTP/1.0 503 Service Unavailable');
die('ERROR in ' . __CLASS__ . "<br>array requires the keys [$sKeyList] - but key '$sKey' was not found in config array <pre>" . print_r($aConfig, true));
die('<h1>503 Service Unavailable</h1>'
. '<h2>Details</h2>'
.__METHOD__ . " - array of check parameters requires the keys [$sKeyList] - but key <code>$sKey</code> was not found in config array."
. "<pre>" . print_r($aConfig, true) .'</pre>'
);
}
if (is_null($aConfig[$sKey])) {
header('HTTP/1.0 503 Service Unavailable');
die('ERROR in ' . __CLASS__ . "<br> key '$sKey' is empty in config array <pre>" . print_r($aConfig, true));
die('<h1>503 Service Unavailable</h1>'
. '<h2>Details</h2>'
.__METHOD__ . " - key <code>$sKey</code> is empty in config array"
. "<pre>" . print_r($aConfig, true) .'</pre>'
);
}
}
return true;
@@ -220,36 +231,55 @@ class appmonitorcheck {
$this->_aConfig = $aConfig;
$this->_createDefaultMetadata();
$sCheck = "check" . preg_replace('/[^a-zA-Z0-9]/', '', $this->_aConfig["check"]["function"]);
$sCheck = preg_replace('/[^a-zA-Z0-9]/', '', $this->_aConfig["check"]["function"]);
$aParams = array_key_exists("params", $this->_aConfig["check"]) ? $this->_aConfig["check"]["params"] : array();
if (method_exists($this, $sCheck)) {
// load as internal function
call_user_func(array($this, $sCheck), $aParams);
} else {
// load as plugin
$sPluginFile=$this->_sPluginDir.'/'.$sCheck.'.php';
if (file_exists($sPluginFile)) {
// try to load as plugin from a plugin file
$sPluginFile= strtolower($this->_sPluginDir.'/checks/'.$sCheck.'.php');
// echo "plugin file: $sPluginFile<br>\n";
$sCheckClass = 'check'.$sCheck;
if (!class_exists($sCheckClass)){
if (file_exists($sPluginFile)) {
require_once($sPluginFile);
$oPlogin = new $sCheck;
$aResponse=$oPlogin->run($aParams);
}
}
if (!class_exists($sCheckClass)){
header('HTTP/1.0 503 Service Unavailable');
die('<h1>503 Service Unavailable</h1>'
. '<h2>Details</h2>'
.__METHOD__ . " - check class not found: <code>$sCheckClass</code>"
. "<pre>" . print_r($aConfig, true) .'</pre>'
."<h2>Known checks</h2>\n".print_r($this->listChecks(), 1)
);
}
$oPlugin = new $sCheckClass;
$aResponse=$oPlugin->run($aParams);
if(!is_array($aResponse)){
header('HTTP/1.0 503 Service Unavailable');
die(__CLASS__ . " plugin : $sCheck does not responses an array<pre>" . print_r($aResponse, true));
die('<h1>503 Service Unavailable</h1>'
. '<h2>Details</h2>'
.__METHOD__ . " - plugin : $sCheck does not responses an array"
. "<pre>INPUT " . print_r($aConfig, true) .'</pre>'
. "<pre>RESPONSE " . print_r($aResponse, true) .'</pre>'
);
}
if(count($aResponse)<2){
header('HTTP/1.0 503 Service Unavailable');
die(__CLASS__ . " plugin : $sCheck does not responses the minimum of 2 array values<pre>" . print_r($aResponse, true));
die('<h1>503 Service Unavailable</h1>'
. '<h2>Details</h2>'
.__METHOD__ . " - plugin : $sCheck does not responses the minimum of 2 array values"
. "<pre>INPUT " . print_r($aConfig, true) .'</pre>'
. "<pre>RESPONSE " . print_r($aResponse, true) .'</pre>'
);
}
if(!isset($aResponse[2])){
$aResponse[2]=array();
}
$this->_setReturn($aResponse[0], $aResponse[1], $aResponse[2]);
} else {
header('HTTP/1.0 503 Service Unavailable');
die(__CLASS__ . " check not found: $sCheck <pre>" . print_r($aConfig, true));
}
if (!$this->_aData['group'] && method_exists($oPlugin, "getGroup")){
$this->_aData['group']=$oPlugin->getGroup($aParams);
}
$this->_aData['time'] = number_format((microtime(true) - $this->_iStart) * 1000, 3) . 'ms';
@@ -273,7 +303,7 @@ class appmonitorcheck {
}
}
// return checks from plugins subdir
foreach(glob($this->_sPluginDir.'/check*.php') as $sPluginFile){
foreach(glob($this->_sPluginDir.'/checks/*.php') as $sPluginFile){
$aReturn[str_replace('.php', '', basename($sPluginFile))] = 1;
}
ksort($aReturn);
@@ -329,60 +359,6 @@ class appmonitorcheck {
}
/**
* check SSL certificate
* @param array $aParams
* array(
* "url" optional: url to connect check; default: own protocol + server
* "verify" optional: flag for verification of certificate or check for expiration only; default=true (=verification is on)
* "warning" optional: count of days to warn; default=30
* )
* @return boolean
*/
protected function checkCert($aParams) {
$sUrl = isset($aParams["url"])
? $aParams["url"]
: 'http'. ($_SERVER['HTTPS'] ? 's' : '') . '://' . $_SERVER['SERVER_NAME'] .':' . $_SERVER['SERVER_PORT']
;
$bVerify = isset($aParams["verify"]) ? !!$aParams["verify"] : true;
$iWarn = isset($aParams["warning"]) ? (int)($aParams["warning"]) : 30;
$sMessage="Checked url: $sUrl ... ";
$certinfo=$this->_certGetInfos($sUrl, $bVerify);
if(isset($certinfo['_error'])){
$this->_setReturn(RESULT_ERROR, $certinfo['_error'] . $sMessage);
return true;
}
$sDNS=isset($certinfo['extensions']['subjectAltName']) ? $certinfo['extensions']['subjectAltName'] : false;
$sHost=parse_url($sUrl,PHP_URL_HOST);
if(strstr($sDNS, 'DNS:'.$sHost)===false){
$this->_setReturn(RESULT_ERROR, 'Wrong certificate: '.$sHost.' is not listed as DNS alias in ['.$sDNS.'] ' . $sMessage);
return true;
}
$iDaysleft = round(($certinfo['validTo_time_t'] - date('U')) / 60 / 60 / 24);
$sMessage.= 'Issuer: '. $sIssuer=$certinfo['issuer']['O']
. '; valid from: '. date("Y-m-d H:i", $certinfo['validFrom_time_t'])
. ' to '.date("Y-m-d H:i", $certinfo['validTo_time_t']).' '
. ( $iDaysleft ? "($iDaysleft days left)" : "expired since ".(-$iDaysleft)." days.")
;
if ($iDaysleft<0) {
$this->_setReturn(RESULT_ERROR, 'Expired! ' . $sMessage);
return true;
}
if ($iDaysleft<=$iWarn) {
$this->_setReturn(RESULT_WARNING, 'Expires soon. ' . $sMessage);
return true;
}
// echo '<pre>';
$this->_setReturn(RESULT_OK, 'OK. '
.($bVerify ? 'Certificate is valid. ' : '(Verification is disabled; Check for expiration only.) ' )
. $sMessage);
return true;
}
/**
* get human readable space value
* @param type $size
@@ -412,107 +388,12 @@ class appmonitorcheck {
$power++;
}
header('HTTP/1.0 503 Service Unavailable');
die("ERROR in space value parameter - there is no size unit in [$sValue] - allowed size units are " . implode('|', $this->_units));
return false;
}
/**
* check free disk space on a given directory
* @param array $aParams
* array(
* "directory" directory that must exist
* "warning" space for warning (optional)
* "critical" minimal space
* )
* @return boolean
*/
protected function checkDiskfree($aParams) {
$this->_checkArrayKeys($aParams, "directory", "critical");
$sDirectory = $aParams["directory"];
if(!is_dir($sDirectory)){
$this->_setReturn(RESULT_ERROR, 'directory [' . $sDirectory . '] does not exist. Maybe it is wrong or is not mounted.');
return true;
die('<h1>503 Service Unavailable</h1>'
. '<h2>Details</h2>'
.__METHOD__ . " ERROR in space value parameter - there is no size unit in [$sValue] - allowed size units are " . implode('|', $this->_units)
);
}
$iWarn = isset($aParams["warning"]) ? $this->_getSize($aParams["warning"]) : false;
$iCritical = $this->_getSize($aParams["critical"]);
$iSpaceLeft=disk_free_space($sDirectory);
$sMessage='[' . $sDirectory . '] has '.$this->_getHrSize($iSpaceLeft).' left.';
if($iWarn){
if($iWarn<=$iCritical){
header('HTTP/1.0 503 Service Unavailable');
die("ERROR in a Diskfree check - warning value must be larger than critical.<pre>" . print_r($aParams, true));
}
if ($iWarn<$iSpaceLeft){
$this->_setReturn(RESULT_OK, $sMessage.' Warning level is not reached yet (still '.$this->_getHrSize($iSpaceLeft-$iWarn).' over warning limit).');
return true;
}
if ($iWarn>$iSpaceLeft && $iCritical<$iSpaceLeft){
$this->_setReturn(RESULT_WARNING, $sMessage.' Warning level '.$this->_getHrSize($iWarn).' was reached (space is '.$this->_getHrSize($iWarn-$iSpaceLeft).' below warning limit; still '.$this->_getHrSize($iSpaceLeft-$iCritical).' over critical limit).');
return true;
}
}
// check space
if ($iCritical<$iSpaceLeft){
$this->_setReturn(RESULT_OK, $sMessage .' Minimum is not reached yet (still '.$this->_getHrSize($iSpaceLeft-$iCritical).' over critical limit).');
} else {
$this->_setReturn(RESULT_ERROR, $sMessage);
}
return true;
}
/**
* check a file
* @param array $aParams
* array(
* "filename" directory that must exist
* "exists" "filename" must exist/ must be absent
* "dir" filetype directory
* "file" filetype file
* "link" filetype symbolic link
* "executable" flag executable
* "readable" flag is readable
* "writable" flag is writable
* )
* @return boolean
*/
protected function checkFile($aParams) {
$aOK = array();
$aErrors = array();
$this->_checkArrayKeys($aParams, "filename");
$sFile = $aParams["filename"];
if (isset($aParams['exists'])) {
$sMyflag = 'exists=' . ($aParams['exists'] ? 'yes' : 'no');
if (file_exists($sFile) && $aParams['exists']) {
$aOK[] = $sMyflag;
} else {
$aErrors[] = $sMyflag;
}
}
foreach (array('dir', 'executable', 'file', 'link', 'readable', 'writable') as $sFiletest) {
if (isset($aParams[$sFiletest])) {
$sTestCmd = 'return is_' . $sFiletest . '("' . $sFile . '");';
if (eval($sTestCmd) && $aParams[$sFiletest]) {
$aOK[] = $sFiletest . '=' . ($aParams[$sFiletest] ? 'yes' : 'no');
} else {
$aErrors[] = $sFiletest . '=' . ($aParams[$sFiletest] ? 'yes' : 'no');
}
}
}
$sMessage = (count($aOK) ? ' flags OK: ' . implode('|', $aOK) : '')
. ' ' . (count($aErrors) ? ' flags FAILED: ' . implode('|', $aErrors) : '')
;
if (count($aErrors)) {
$this->_setReturn(RESULT_ERROR, 'file test [' . $sFile . '] ' . $sMessage);
} else {
$this->_setReturn(RESULT_OK, 'file test [' . $sFile . '] ' . $sMessage);
}
return true;
}
/**
@@ -540,394 +421,16 @@ class appmonitorcheck {
return !!(strstr($value, $verifyValue)!==false);
break;
default:
die("FATAL ERROR: a compare function [$sCompare] is not implemented (yet).");
break;
}
return false;
}
/**
* make http request and test response body
* @param array $aParams
* array(
* url string url to fetch
* timeout integer optional timeout in sec; default: 5
* headeronly boolean optional flag to fetch http response herader only; default: false = returns header and body
* follow boolean optional flag to follow a location; default: false = do not follow
* status integer test for an expected http status code; if none is given then test fails on status 400 and greater
* headercontains string test for a string in the http response header; it returns OK if the text was found
* headernotcontains string test for a string in the http response header; it returns OK if the text was not found
* headerregex string test for a regex in the http response header; it returns OK if the regex matches; example: "headerregex"=>"/lowercasematch/i"
* bodycontains string test for a string in the http response body; it returns OK if the text was found
* bodynotcontains string test for a string in the http response body; it returns OK if the text was not found
* bodyregex string test for a regex in the http response body; it returns OK if the regex matches; example: "headerregex"=>"/lowercasematch/i"
* )
*/
protected function checkHttpContent($aParams) {
$this->_checkArrayKeys($aParams, "url");
if (!function_exists("curl_init")) {
header('HTTP/1.0 503 Service Unavailable');
die("ERROR: PHP CURL module is not installed.");
}
$bShowContent = (isset($aParams["content"]) && $aParams["content"]) ? true : false;
$ch = curl_init($aParams["url"]);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_NOBODY, isset($aParams["headeronly"]) && $aParams["headeronly"]);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, isset($aParams["follow"]) && $aParams["follow"]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, (isset($aParams["timeout"]) && (int)$aParams["timeout"]) ? (int)$aParams["timeout"] : $this->_iTimeoutTcp);
$res = curl_exec($ch);
if (!$res) {
$this->_setReturn(RESULT_ERROR, 'ERROR: failed to fetch ' . $aParams["url"] . '.');
return false;
}
$sOut='';
$bError=false;
$aInfos = curl_getinfo($ch);
/*
Array
(
[url] => https://www.iml.unibe.ch/
[content_type] => text/html; charset=utf-8
[http_code] => 200
[header_size] => 926
[request_size] => 55
[filetime] => -1
[ssl_verify_result] => 20
[redirect_count] => 0
[total_time] => 1.812
[namelookup_time] => 0
[connect_time] => 0
[pretransfer_time] => 0.015
[size_upload] => 0
[size_download] => 94654
[speed_download] => 52237
[speed_upload] => 0
[download_content_length] => -1
[upload_content_length] => -1
[starttransfer_time] => 1.812
[redirect_time] => 0
[redirect_url] =>
[primary_ip] => 130.92.30.80
[certinfo] => Array
(
)
[primary_port] => 443
[local_ip] => 10.1.30.49
[local_port] => 63597
)
*/
curl_close($ch);
$aTmp=explode("\r\n\r\n", $res, 2);
$sHttpHeader=$aTmp[0];
$sHttpBody=isset($aTmp[1]) ? $aTmp[1] : false;
// ---------- check functions
// --- http status code
$sOut.="Http status: ".$aInfos['http_code']." - ";
if(isset($aParams["status"])){
if($aInfos['http_code'] === $aParams["status"]){
$sOut.="compare OK<br>";
} else {
$sOut.="compare failed<br>";
$bError=true;
}
} else {
if($aInfos['http_code'] >= 400){
$sOut.="Error page detected<br>";
$bError=true;
} else {
$sOut.="request successful<br>";
}
}
// --- http header
if(isset($aParams["headercontains"]) && $aParams["headercontains"]){
$sOut.="Http header contains &quot;".$aParams["headercontains"]."&quot; - ";
if(!strstr($sHttpHeader, $aParams["headercontains"])===false){
$sOut.="compare OK<br>";
} else {
$sOut.="compare failed<br>";
$bError=true;
}
}
if(isset($aParams["headernotcontains"]) && $aParams["headernotcontains"]){
$sOut.="Http header does not contain &quot;".$aParams["headernotcontains"]."&quot; - ";
if(strstr($sHttpHeader, $aParams["headernotcontains"])===false){
$sOut.="compare OK<br>";
} else {
$sOut.="compare failed<br>";
$bError=true;
}
}
if(isset($aParams["headerregex"]) && $aParams["headerregex"]){
$sOut.="Http header regex test &quot;".$aParams["headerregex"]."&quot; - ";
try{
$bRegex=preg_match($aParams["headerregex"], $sHttpHeader);
if($bRegex){
$sOut.="compare OK<br>";
} else {
$sOut.="compare failed<br>";
$bError=true;
}
}
catch(Exception $e){
$sOut.="Wrong REGEX<br>" . print_r($e, 1).'<br>';
$bError=true;
}
}
// --- http body
if(isset($aParams["bodycontains"]) && $aParams["bodycontains"]){
$sOut.="Http body contains &quot;".$aParams["bodycontains"]."&quot; - ";
if(!strstr($sHttpBody, $aParams["bodycontains"])===false){
$sOut.="compare OK<br>";
} else {
$sOut.="compare failed<br>";
$bError=true;
}
}
if(isset($aParams["bodynotcontains"]) && $aParams["bodynotcontains"]){
$sOut.="Http body does not contain &quot;".$aParams["bodynotcontains"]."&quot; - ";
if(strstr($sHttpBody, $aParams["bodynotcontains"])===false){
$sOut.="compare OK<br>";
} else {
$sOut.="compare failed<br>";
$bError=true;
}
}
if(isset($aParams["bodyregex"]) && $aParams["bodyregex"]){
$sOut.="Http body regex test &quot;".$aParams["bodyregex"]."&quot; - ";
try{
$bRegex=preg_match($aParams["bodyregex"], $sHttpBody);
if($bRegex){
$sOut.="compare OK<br>";
} else {
$sOut.="compare failed<br>";
$bError=true;
}
}
catch(Exception $e){
$sOut.="Wrong REGEX<br>" . print_r($e, 1).'<br>';
$bError=true;
}
}
if (!$bError) {
$this->_setReturn(RESULT_OK,
'OK: http check "' . $aParams["url"] . '".<br>'.$sOut
);
} else {
$this->_setReturn(RESULT_ERROR,
'ERROR: http check "' . $aParams["url"] . '".<br>'.$sOut
die('<h1>503 Service Unavailable</h1>'
. '<h2>Details</h2>'
.__METHOD__ . " - FATAL ERROR: a compare function [$sCompare] is not implemented (yet)."
);
break;
}
/*
echo '<pre>';
echo $sOut."<hr>";
echo "<hr>HEADER: ".htmlentities($sHttpHeader)."<hr>";
print_r($aParams); print_r($aInfos);
// echo htmlentities($sHttpBody);
die();
*/
return true;
}
/**
* check mysql connection to a database using mysqli realconnect
* @param array $aParams
* array(
* server string database hostname / ip address
* user string db user
* password string password for db user
* db string schema / database name
* port integer optional: port
* timeout integer optional timeout in sec; default: 5
* )
*/
protected function checkMysqlConnect($aParams) {
$this->_checkArrayKeys($aParams, "server,user,password,db");
$mysqli=mysqli_init();
if(!$mysqli){
$this->_setReturn(RESULT_ERROR, 'ERROR: mysqli_init failed.');
return false;
}
if (!$mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, (isset($aParams["timeout"]) && (int)$aParams["timeout"]) ? (int)$aParams["timeout"] : $this->_iTimeoutTcp)) {
$this->_setReturn(RESULT_ERROR, 'ERROR: setting mysqli_init failed.');
return false;
}
$db = (isset($aParams["port"]) && $aParams["port"])
? $mysqli->real_connect($aParams["server"], $aParams["user"], $aParams["password"], $aParams["db"], $aParams["port"])
: $mysqli->real_connect($aParams["server"], $aParams["user"], $aParams["password"], $aParams["db"])
;
if ($db) {
$this->_setReturn(RESULT_OK, "OK: Mysql database " . $aParams["db"] . " was connected");
$mysqli->close();
return true;
} else {
$this->_setReturn(RESULT_ERROR, "ERROR: Mysql database " . $aParams["db"] . " was not connected. Error ".mysqli_connect_errno() .": ". mysqli_connect_error());
return false;
}
}
/**
* check connection to a database using pdo
* see http://php.net/manual/en/pdo.drivers.php
*
* @param array $aParams
* array(
* connect string connect string
* user string db user
* password string password for db user
* timeout integer optional timeout in sec; default: 5
* )
*/
protected function checkPdoConnect($aParams) {
$this->_checkArrayKeys($aParams, "connect,user,password");
try{
$db = new PDO(
$aParams['connect'],
$aParams['user'],
$aParams['password'],
array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
// timeout
// Not all drivers support this option; mysqli does
PDO::ATTR_TIMEOUT => (isset($aParams["timeout"]) && (int)$aParams["timeout"]) ? (int)$aParams["timeout"] : $this->_iTimeoutTcp,
// mssql
// PDO::SQLSRV_ATTR_QUERY_TIMEOUT => $this->_iTimeoutTcp,
)
);
$this->_setReturn(RESULT_OK, "OK: Database was connected with PDO " . $aParams['connect']);
$db=null;
return true;
} catch(PDOException $e) {
$this->_setReturn(RESULT_ERROR, "ERROR: Database was not connected " . $aParams['connect'] . " was not connected. Error ".$e->getMessage());
return false;
}
}
/**
* check if system is listening to a given port
* @param array $aParams
* array(
* port integer port
* host string optional hostname to connect; default: 127.0.0.1
* timeout integer optional timeout in sec; default: 5
* )
* @return boolean
*/
protected function checkPortTcp($aParams) {
$this->_checkArrayKeys($aParams, "port");
$sHost = array_key_exists('host', $aParams) ? $aParams['host'] : '127.0.0.1';
$iPort = (int) $aParams['port'];
// from http://php.net/manual/de/sockets.examples.php
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
$this->_setReturn(RESULT_UNKNOWN, "ERROR: $sHost:$iPort was not checked. socket_create() failed: " . socket_strerror(socket_last_error()));
return false;
}
// set socket timeout
socket_set_option(
$socket,
SOL_SOCKET, // socket level
SO_SNDTIMEO, // timeout option
array(
"sec"=>(isset($aParams["timeout"]) && (int)$aParams["timeout"]) ? (int)$aParams["timeout"] : $this->_iTimeoutTcp, // timeout in seconds
"usec"=>0
)
);
$result = socket_connect($socket, $sHost, $iPort);
if ($result === false) {
$this->_setReturn(RESULT_ERROR, "ERROR: $sHost:$iPort failed. " . socket_strerror(socket_last_error($socket)));
socket_close($socket);
return false;
} else {
$this->_setReturn(RESULT_OK, "OK: $sHost:$iPort was connected.");
socket_close($socket);
return true;
}
}
/**
* most simple check: set given values
* Use this function to add a counter
*
* @param array $aParams
* array keys:
* value string description text
* result integer RESULT_nn
*
* brainstorming for a future release
*
* "counter" optioal: array of counter values
* - label string a label
* - value float a number
* - type string one of simple | bar | line
*
*/
protected function checkSimple($aParams) {
$this->_checkArrayKeys($aParams, "result,value");
$this->_setReturn((int) $aParams["result"], $aParams["value"]);
foreach(array('type', 'count', 'visual') as $sMyKey){
if(isset($aParams[$sMyKey])){
$this->_aData[$sMyKey]=$aParams[$sMyKey];
}
}
return true;
}
/**
* check sqlite connection
* @param array $aParams
* array(
* db string full path of sqlite file
* timeout integer optional timeout in sec; default: 5
* )
* @return boolean
*/
protected function checkSqliteConnect($aParams) {
$this->_checkArrayKeys($aParams, "db");
if (!file_exists($aParams["db"])) {
$this->_setReturn(RESULT_ERROR, "ERROR: Sqlite database file " . $aParams["db"] . " does not exist.");
return false;
}
if(!isset($aParams['user'])){
$aParams['user']='';
}
if(!isset($aParams['password'])){
$aParams['password']='';
}
try {
// $db = new SQLite3($sqliteDB);
// $db = new PDO("sqlite:".$sqliteDB);
$o = new PDO("sqlite:" . $aParams["db"],
$aParams['user'],
$aParams['password'],
array(
PDO::ATTR_TIMEOUT => (isset($aParams["timeout"]) && (int)$aParams["timeout"]) ? (int)$aParams["timeout"] : $this->_iTimeoutTcp,
)
);
$this->_setReturn(RESULT_OK, "OK: Sqlite database " . $aParams["db"] . " was connected");
return true;
} catch (Exception $e) {
$this->_setReturn(RESULT_ERROR, "ERROR: Sqlite database " . $aParams["db"] . " was not connected. " . $e->getMessage());
return false;
}
}
}
Loading