Skip to content
Snippets Groups Projects

update appmonitor client

Merged Hahn Axel (hahn) requested to merge 6421-update-appmonitor-checks into master
20 files
+ 132
122
Compare changes
  • Side-by-side
  • Inline

Files

+ 32
25
@@ -50,8 +50,9 @@ if(!defined('RESULT_OK')){
* 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>
* 2023-06-02 0.125 axel.hahn@unibe.ch replace array_key_exists for better readability
* --------------------------------------------------------------------------------<br>
* @version 0.99
* @version 0.125
* @author Axel Hahn
* @link TODO
* @license GPL
@@ -63,24 +64,30 @@ class appmonitorcheck {
// CONFIG
// ----------------------------------------------------------------------
/**
* starting time using microtime
* @var float|string
*/
protected $_iStart=0;
/**
* config container
* @var array
*/
protected $_aConfig = array();
protected $_aConfig = [];
/**
* data of all checks
* @var array
*/
protected $_aData = array();
protected $_aData = [];
/**
* flat array with units for sizes
* @var array
*/
protected $_units = array( 'B', 'KB', 'MB', 'GB', 'TB');
protected $_units = [ 'B', 'KB', 'MB', 'GB', 'TB' ];
/**
* timeout in sec for tcp socket connections
@@ -115,7 +122,7 @@ class appmonitorcheck {
*/
protected function _createDefaultMetadata() {
$this->_aData = array(
$this->_aData = [
"name" => $this->_aConfig["name"],
"description" => $this->_aConfig["description"],
"group" => isset($this->_aConfig["group"]) ? $this->_aConfig["group"] : false,
@@ -124,7 +131,7 @@ class appmonitorcheck {
"value" => false,
"type" => false,
"time" => false,
);
];
return true;
}
@@ -153,7 +160,7 @@ class appmonitorcheck {
*/
protected function _setCounter($aParams){
if(is_array($aParams) && count($aParams)){
foreach(array('type', 'count', 'visual') as $sMyKey){
foreach([ 'type', 'count', 'visual' ] as $sMyKey){
if(isset($aParams[$sMyKey])){
$this->_aData[$sMyKey]=$aParams[$sMyKey];
}
@@ -169,7 +176,7 @@ class appmonitorcheck {
* @param array $aCounter optional: counter with array keys type, count, visual
* @return boolean
*/
protected function _setReturn($iResult, $s, $aCounter=array()) {
protected function _setReturn($iResult, $s, $aCounter=[]) {
$this->_setResult($iResult);
$this->_setOutput($s);
$this->_setCounter($aCounter);
@@ -184,7 +191,7 @@ class appmonitorcheck {
*/
protected function _checkArrayKeys($aConfig, $sKeyList) {
foreach (explode(",", $sKeyList) as $sKey) {
if (!array_key_exists($sKey, $aConfig)) {
if (!isset($aConfig[$sKey])) {
header('HTTP/1.0 503 Service Unavailable');
die('<h1>503 Service Unavailable</h1>'
. '<h2>Details</h2>'
@@ -212,15 +219,15 @@ class appmonitorcheck {
* perform a check
* @param type $aConfig
* Array
* (
* [
* [name] => Dummy
* [description] => Dummy Test
* [check] => array(
* [check] => [
* [function] => [check function] // i.e. Simple
* [params] => [array] // optional; arguments for Check function
* // its keys depend on the function
* )
* )
* ]
* ]
*
* @return array
*/
@@ -233,7 +240,7 @@ class appmonitorcheck {
$this->_createDefaultMetadata();
$sCheck = preg_replace('/[^a-zA-Z0-9]/', '', $this->_aConfig["check"]["function"]);
$aParams = array_key_exists("params", $this->_aConfig["check"]) ? $this->_aConfig["check"]["params"] : array();
$aParams = $this->_aConfig["check"]["params"] ?? [];
// try to load as plugin from a plugin file
$sPluginFile= strtolower($this->_sPluginDir.'/checks/'.$sCheck.'.php');
@@ -276,7 +283,7 @@ class appmonitorcheck {
);
}
if(!isset($aResponse[2])){
$aResponse[2]=array();
$aResponse[2]=[];
}
$this->_setReturn($aResponse[0], $aResponse[1], $aResponse[2]);
if (!$this->_aData['group'] && method_exists($oPlugin, "getGroup")){
@@ -295,7 +302,7 @@ class appmonitorcheck {
* @return array
*/
public function listChecks() {
$aReturn = array();
$aReturn = [];
// return internal protected fuctions named "check[whatever]"
$class = new ReflectionClass($this);
foreach ($class->getMethods(ReflectionMethod::IS_PROTECTED) as $oReflectionMethod) {
@@ -336,24 +343,24 @@ class appmonitorcheck {
$sHost = isset($aUrldata['host']) ? $aUrldata['host'] : false;
$iPort = isset($aUrldata['port']) ? $aUrldata['port'] : ((isset($aUrldata['scheme']) && $aUrldata['scheme'] === 'https') ? 443 : false);
$aSsl=array('capture_peer_cert' => true);
$aSsl=['capture_peer_cert' => true ];
if($bVerifyCert){
$aSsl['verify_peer']=false;
$aSsl['verify_peer_name']=false;
};
$get = stream_context_create(array('ssl' => $aSsl));
$get = stream_context_create([ 'ssl' => $aSsl ]);
if(!$get){
return array('_error' => 'Error: Cannot create stream_context');
return [ '_error' => 'Error: Cannot create stream_context' ];
}
$errno=-1;
$errstr="stream_socket_client failed.";
$read = stream_socket_client("ssl://$sHost:$iPort", $errno, $errstr, $iTimeout, STREAM_CLIENT_CONNECT, $get);
if(!$read){
return array('_error' => "Error $errno: $errstr; cannot create stream_socket_client with given stream_context to ssl://$sHost:$iPort; you can try to set the flag [verify] to false to check expiration date only.");
return [ '_error' => "Error $errno: $errstr; cannot create stream_socket_client with given stream_context to ssl://$sHost:$iPort; you can try to set the flag [verify] to false to check expiration date only." ];
}
$cert = stream_context_get_params($read);
if(!$cert){
return array('_error' => "Error: socket was connected to ssl://$sHost:$iPort - but I cannot read certificate infos with stream_context_get_params ");
return [ '_error' => "Error: socket was connected to ssl://$sHost:$iPort - but I cannot read certificate infos with stream_context_get_params " ];
}
$certinfo = openssl_x509_parse($cert['options']['ssl']['peer_certificate']);
return $certinfo;
@@ -362,7 +369,7 @@ class appmonitorcheck {
/**
* get human readable space value
* @param type $size
* @param integer $size
* @return string
*/
protected function _getHrSize($size){
@@ -371,7 +378,7 @@ class appmonitorcheck {
}
/**
* get a space in a real value if an integer has added MB|GB|...
* @param type $sValue
* @param string $sValue
* @return integer
*/
protected function _getSize($sValue){
@@ -399,12 +406,12 @@ class appmonitorcheck {
/**
* compare function for 2 values
* @param any $verifyValue search value
* @param string $verifyValue search value
* @param string $sCompare compare function; it is one of
* IS -
* GE - greater or equal
* GT - greater
* @param any $value existing value
* @param string $value existing value
* @return boolean
*/
protected function _compare($value, $sCompare, $verifyValue){
Loading