Skip to content
Snippets Groups Projects
Commit f58ddc38 authored by Hahn Axel (hahn)'s avatar Hahn Axel (hahn)
Browse files

Merge branch '5950-socket-error-in-appmonitor-check' into 'master'

update appmonitor

See merge request !37
parents b524ab3f cd68d544
No related branches found
No related tags found
1 merge request!37update appmonitor
Pipeline #1082 passed
...@@ -112,7 +112,7 @@ foreach(array('server/config', 'server/tmp') as $sMyDir){ ...@@ -112,7 +112,7 @@ foreach(array('server/config', 'server/tmp') as $sMyDir){
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
require_once($sApproot.'/server/classes/appmonitor-server.class.php'); require_once($sApproot.'/server/classes/appmonitor-server.class.php');
$oServer=new appmonitorserver(); $oServer=new appmonitorserver();
$iCount=count($oServer->apiGetAppIds()); $iCount=count($oServer->getAppIds());
$oMonitor->addCheck( $oMonitor->addCheck(
array( array(
"name" => "appcounter", "name" => "appcounter",
......
<?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)
}
}
<?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))];
}
*/
}
}
...@@ -14,10 +14,12 @@ ...@@ -14,10 +14,12 @@
* *
* ____________________________________________________________________________ * ____________________________________________________________________________
* *
* CHECK SWLITE CONNECTION USING PDO * CHECK TCP CONNECTION TO A GIVEN PORT
* ____________________________________________________________________________ * ____________________________________________________________________________
* *
* 2021-10-27 <axel.hahn@iml.unibe.ch> * 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{ class checkPortTcp extends appmonitorcheck{
...@@ -46,6 +48,10 @@ class checkPortTcp extends appmonitorcheck{ ...@@ -46,6 +48,10 @@ class checkPortTcp extends appmonitorcheck{
$sHost = array_key_exists('host', $aParams) ? $aParams['host'] : '127.0.0.1'; $sHost = array_key_exists('host', $aParams) ? $aParams['host'] : '127.0.0.1';
$iPort = (int) $aParams['port']; $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 // from http://php.net/manual/de/sockets.examples.php
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
...@@ -65,8 +71,9 @@ class checkPortTcp extends appmonitorcheck{ ...@@ -65,8 +71,9 @@ class checkPortTcp extends appmonitorcheck{
$result = socket_connect($socket, $sHost, $iPort); $result = socket_connect($socket, $sHost, $iPort);
if ($result === false) { if ($result === false) {
$aResult=[RESULT_ERROR, "ERROR: $sHost:$iPort failed. " . socket_strerror(socket_last_error($socket))];
socket_close($socket); socket_close($socket);
return [RESULT_ERROR, "ERROR: $sHost:$iPort failed. " . socket_strerror(socket_last_error($socket))]; return $aResult;
} else { } else {
socket_close($socket); socket_close($socket);
return [RESULT_OK, "OK: $sHost:$iPort was connected."]; return [RESULT_OK, "OK: $sHost:$iPort was connected."];
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment