diff --git a/src/amcli.php b/src/amcli.php index bce05785cd077fa8f10d79fb9fd3d533bc4e14bc..a64660aaad43ce3b3f24ef4f569394afa98d7d38 100755 --- a/src/amcli.php +++ b/src/amcli.php @@ -2,7 +2,8 @@ <?php chdir(__DIR__); - +$FLAG_DEBUG=0; +$VERSION="0.0.1"; // ---MARK---INCLUDE-CHECKS---START--- if(! file_exists("include_checks.php")){ @@ -19,11 +20,112 @@ if(!include "include_checks.php"){ // ---MARK---INCLUDE-CHECKS---END -if($argc>1) - parse_str(implode('&',array_slice($argv, 1)), $_GET); +// -------------------------------------------------------------------- +// +// FUNCTIONS +// +// -------------------------------------------------------------------- + + +function _wd($s):void +{ + global $FLAG_DEBUG; + if($FLAG_DEBUG){ + // echo "DEBUG: $s\n"; + fwrite(STDERR, "DEBUG: $s\n"); + } +} + +function _showHelp(): void +{ + global $VERSION; + $_self=basename(__FILE__); + echo "IML Appmonitor as CLI client $VERSION + +This client performs appmonitor checks and puts the results as JSON to stdout. +You can use the compiled binary on non PHP systems. +(c) 2025 Institute for Medical education * University of Bern + + !!! This tool is in alpha stadium !!! + + +SYNTAX: $_self [OPTIONS] + +OPTIONS: + -v, --verbose Verbose output + -h, --help Print this help + -i, --ini INI File + +"; +} + +// -------------------------------------------------------------------- +// +// MAIN +// +// -------------------------------------------------------------------- + +// put params to $ARGS +if($argc>1){ + parse_str(implode('&',array_slice($argv, 1)), $ARGS); +} + +// check params + +if (isset($ARGS['-v']) || isset($ARGS['--verbose'])) { + $FLAG_DEBUG=1; +} +_wd("CLI ARGS: ".print_r($ARGS, 1)); + +if (isset($ARGS['-h']) || isset($ARGS['--help'])) { + _wd("Showing help"); + _showHelp(); + exit(0); +} + + +$inifile=$ARGS["--ini"]??($ARGS["-i"]??"simple.ini"); +_wd("Using ini file '$inifile'."); + +if(!file_exists($inifile)){ + echo "ERROR: INI File '$inifile' does not exist.\n"; + exit(1); +} + +$aIni=parse_ini_file($inifile); +if(!is_array($aIni)){ + echo "ERROR: INI File '$inifile' could not be parsed.\n"; + exit(1); +} +_wd("Parsed INI data: ".print_r($aIni, 1)); + + +// ---------------------------------------------------------------------- + +_wd("Initializing appmonitor class"); + +$oMonitor = new appmonitor(); +$oMonitor->addEmail('sysadmin@example.com'); +$oMonitor->addSlackWebhook("mywebhook", "https://hooks.slack.com/services/(...)"); +$oMonitor->addCheck( + [ + "name" => "hello plugin", + "description" => "Test a plugin ... plugins/checks/hello.php", + "check" => [ + "function" => "Hello", + "params" => [ + "message" => "Here I am", + ], + ], + ] +); +// ---------------------------------------------------------------------- +// send the response -echo "PARAMS: "; -print_r($_GET); +_wd("Setting result"); +$oMonitor->setResult(); +_wd("Send response"); +$oMonitor->render(); diff --git a/src/simple.ini b/src/simple.ini new file mode 100644 index 0000000000000000000000000000000000000000..da29d0ba42e871abd6f194a056a04a8d128184e4 --- /dev/null +++ b/src/simple.ini @@ -0,0 +1,5 @@ +[meta] +host = "www.example.com" +website = "Company website" +ttl = 300 +tags[]="monitoring"