diff --git a/public_html/appmonitor/plugins/apps/iml-appmonitor-server.php b/public_html/appmonitor/plugins/apps/iml-appmonitor-server.php index d4e9614be774e3530791c6541d72bc211a854bd2..d85938a912a4729aab293f218f0f3baf105f61a8 100644 --- a/public_html/appmonitor/plugins/apps/iml-appmonitor-server.php +++ b/public_html/appmonitor/plugins/apps/iml-appmonitor-server.php @@ -112,7 +112,7 @@ foreach(array('server/config', 'server/tmp') as $sMyDir){ // ---------------------------------------------------------------------- require_once($sApproot.'/server/classes/appmonitor-server.class.php'); $oServer=new appmonitorserver(); -$iCount=count($oServer->apiGetAppIds()); +$iCount=count($oServer->getAppIds()); $oMonitor->addCheck( array( "name" => "appcounter", diff --git a/public_html/appmonitor/plugins/checks/exec.php b/public_html/appmonitor/plugins/checks/exec.php new file mode 100644 index 0000000000000000000000000000000000000000..a343cd9fcaa323174a1156b6fefcf03838c48df2 --- /dev/null +++ b/public_html/appmonitor/plugins/checks/exec.php @@ -0,0 +1,124 @@ +<?php +/** + * ____________________________________________________________________________ + * + * _____ _____ __ _____ _ _ + * | | | | ___ ___ ___| |___ ___|_| |_ ___ ___ + * |- -| | | | |__ | .'| . | . | | | | . | | | _| . | _| + * |_____|_|_|_|_____| |__,| _| _|_|_|_|___|_|_|_|_| |___|_| + * |_| |_| + * _ _ _ + * ___| |_|___ ___| |_ + * | _| | | -_| | _| + * |___|_|_|___|_|_|_| + * + * ____________________________________________________________________________ + * + * CUSTOM CHECK BASED ON SHELL COMMANDS + * + * Execute a shell command. + * ____________________________________________________________________________ + * + * 2022-09-19 <axel.hahn@iml.unibe.ch> + * + */ +class checkExec extends appmonitorcheck{ + /** + * get default group of this check + * @param array $aParams - see run() method + * @return array + */ + public function getGroup($aParams){ + return 'service'; + } + + /** + * check execution of a command + * @param array $aParams + * array( + * "command" {string} command to execute + * "output" {bool} flag: show output; default: true + * + * "exitOK" {array} array of integegers for ok exitcodes + * "exitWarn" {array} array of integegers for exitcodes with warning + * "exitCritical" {array} array of integegers for exitcodes that result in an error + * + * // TODO ... MAYBE + * "searchOK" {string} search string that must be found in output + * "searchWarn" {string} if search string is found check returns with warning + * "searchCritical" {string} if search string is found check returns with critical + * ) + * @return boolean + */ + public function run($aParams) { + $this->_checkArrayKeys($aParams, "command"); + $_sCmd=$aParams['command']; + $_bShowOutput=isset($aParams['output']) ? !!$aParams['output'] : true; + + $_aRcOK=isset($aParams['exitOK']) ? $aParams['exitOK'] : []; + $_aRcWarning=isset($aParams['exitWarn']) ? $aParams['exitWarn'] : []; + $_aRcCritical=isset($aParams['exitCritical']) ? $aParams['exitCritical'] : []; + + $_sMode='default'; + if(count($_aRcOK) + count($_aRcWarning) + count($_aRcCritical)){ + $_sMode='exitcode'; + } + + exec($_sCmd,$aOutput, $iRc); + $_sOut=$_bShowOutput ? '<br>'.implode("<br>", $aOutput) : ''; + + switch($_sMode){ + // non-zero exitcode is an error + case "default": + if ($iRc) { + return [ + RESULT_ERROR, + 'command failed with exitcode '.$iRc.': [' . $_sCmd . ']'.$_sOut + ]; + } else { + return[ + RESULT_OK, + 'OK [' . $_sCmd . '] ' .$_sOut + ]; + }; + break;; + + // handle given custom exitcodes + case "exitcode": + if (in_array($iRc, $_aRcCritical)){ + return [ + RESULT_ERROR, + 'Critical exitcode '.$iRc.' detected: [' . $_sCmd . ']'.$_sOut + ]; + } + if (in_array($iRc, $_aRcWarning)){ + return [ + RESULT_WARNING, + 'Warning exitcode '.$iRc.' detected: [' . $_sCmd . ']'.$_sOut + ]; + } + if ($iRc == 0 || in_array($iRc, $_aRcOK)){ + return [ + RESULT_OK, + 'OK exitcode '.$iRc.' detected: [' . $_sCmd . ']'.$_sOut + ]; + } + return [ + RESULT_UNKNOWN, + 'UNKNOWN - unhandled exitcode '.$iRc.' detected: [' . $_sCmd . ']'.$_sOut + ]; + case "search": + return[ + RESULT_UNKNOWN, + 'UNKNOWN method [' . $_sMode . '] - is not implemented yet.' + ]; + break;; + default: + return[ + RESULT_UNKNOWN, + 'UNKNOWN mode [' . htmlentities($_sMode) . '].' + ]; + } // switch($_sMode) + } + +} diff --git a/public_html/appmonitor/plugins/checks/ping.php b/public_html/appmonitor/plugins/checks/ping.php new file mode 100644 index 0000000000000000000000000000000000000000..b10397d6a03351c8c3253bcf2ddf3ca298991189 --- /dev/null +++ b/public_html/appmonitor/plugins/checks/ping.php @@ -0,0 +1,110 @@ +<?php +/** + * ____________________________________________________________________________ + * + * _____ _____ __ _____ _ _ + * | | | | ___ ___ ___| |___ ___|_| |_ ___ ___ + * |- -| | | | |__ | .'| . | . | | | | . | | | _| . | _| + * |_____|_|_|_|_____| |__,| _| _|_|_|_|___|_|_|_|_| |___|_| + * |_| |_| + * _ _ _ + * ___| |_|___ ___| |_ + * | _| | | -_| | _| + * |___|_|_|___|_|_|_| + * + * ____________________________________________________________________________ + * + * CHECK PING RESPONSE TIME VIA ICMP + * ____________________________________________________________________________ + * + * 2022-07-05 <axel.hahn@iml.unibe.ch> + * 2022-09-16 <axel.hahn@iml.unibe.ch> read error before closing socket. + * 2022-11-22 <axel.hahn@iml.unibe.ch> Use exec with detecting MS Win for the ping parameter for count of pings + */ +class checkPing extends appmonitorcheck{ + /** + * get default group of this check + * @param array $aParams + * @return array + */ + public function getGroup(){ + return 'network'; + } + + /** + * check ping to a target + * @param array $aParams + * array( + * host string optional hostname to connect; default: 127.0.0.1 + * timeout integer OBSOLET (because using exec): optional timeout in sec; default: 5 + * ) + * @return boolean + */ + public function run($aParams) { + $sHost = array_key_exists('host', $aParams) ? $aParams['host'] : '127.0.0.1'; + + $sParamCount=strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' ? "n" : "c"; + $iRepeat=1; + + $sCommand="ping -$sParamCount $iRepeat $sHost 2>&1"; + exec($sCommand, $aOut, $iRc); + $sOut=implode("\n", $aOut); + + if ($iRc>0){ + return [RESULT_ERROR, "ERROR: ping to $sHost failed.\n".$sOut]; + } + return [RESULT_OK, "OK: ping to $sHost\n".$sOut]; + + /* + Socket functions require root :-/ + + if (!function_exists('socket_create')){ + return [RESULT_UNKNOWN, "UNKNOWN: Unable to perform ping test. The socket module is not enabled in the php installation."]; + } + + // ICMP ping packet with a pre-calculated checksum + $package = "\x08\x00\x7d\x4b\x00\x00\x00\x00PingHost"; + $socket = socket_create(AF_INET, SOCK_RAW, getprotobyname('icmp')); + // TODO + if(!$socket){ + die("ERROR: unable to create socket"); + } + socket_set_option( + $socket, + SOL_SOCKET, + SO_RCVTIMEO, + array( + "sec"=>(isset($aParams["timeout"]) && (int)$aParams["timeout"]) ? (int)$aParams["timeout"] : $this->_iTimeoutTcp, // timeout in seconds + "usec"=>0 + ) + ); + + $start = microtime(true); + socket_connect($socket, $sHost, 0); + $connect = socket_send($socket, $package, strLen($package), 0); + if($connect){ + if (socket_read($socket, 255)){ + $result = microtime(true) - $start; + socket_close($socket); + return [RESULT_OK, + "OK: ping to $sHost", + array( + 'type'=>'counter', + 'count'=>$result, + 'visual'=>'line', + ) + + ]; + } else { + $aResult=[RESULT_ERROR, "ERROR: ping to $sHost failed after connect." . socket_strerror(socket_last_error($socket))]; + socket_close($socket); + return $aResult; + } + } else { + return [RESULT_ERROR, "ERROR: ping to $sHost failed. " . socket_strerror(socket_last_error($socket))]; + } + + */ + } + +} diff --git a/public_html/appmonitor/plugins/checks/porttcp.php b/public_html/appmonitor/plugins/checks/porttcp.php index 79afcd87ff380591f2bacb98515cbf6789f20169..c73e010847e1d9d76125a53f3a4cca873c08129a 100644 --- a/public_html/appmonitor/plugins/checks/porttcp.php +++ b/public_html/appmonitor/plugins/checks/porttcp.php @@ -14,10 +14,12 @@ * * ____________________________________________________________________________ * - * CHECK SWLITE CONNECTION USING PDO + * CHECK TCP CONNECTION TO A GIVEN PORT * ____________________________________________________________________________ * * 2021-10-27 <axel.hahn@iml.unibe.ch> + * 2022-07-05 <axel.hahn@iml.unibe.ch> send unknown if socket module is not activated. + * 2022-09-16 <axel.hahn@iml.unibe.ch> read error before closing socket. * */ class checkPortTcp extends appmonitorcheck{ @@ -46,6 +48,10 @@ class checkPortTcp extends appmonitorcheck{ $sHost = array_key_exists('host', $aParams) ? $aParams['host'] : '127.0.0.1'; $iPort = (int) $aParams['port']; + if (!function_exists('socket_create')){ + return [RESULT_UNKNOWN, "UNKNOWN: Unable to perform tcp test. The socket module is not enabled in the php installation."]; + } + // from http://php.net/manual/de/sockets.examples.php $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); @@ -65,8 +71,9 @@ class checkPortTcp extends appmonitorcheck{ $result = socket_connect($socket, $sHost, $iPort); if ($result === false) { + $aResult=[RESULT_ERROR, "ERROR: $sHost:$iPort failed. " . socket_strerror(socket_last_error($socket))]; socket_close($socket); - return [RESULT_ERROR, "ERROR: $sHost:$iPort failed. " . socket_strerror(socket_last_error($socket))]; + return $aResult; } else { socket_close($socket); return [RESULT_OK, "OK: $sHost:$iPort was connected."];