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

1st version of amcli with all checks included

parent a3ad8cbb
Branches
No related tags found
No related merge requests found
#!/usr/bin/env php #!/usr/bin/env php
<?php <?php
/**
* ======================================================================
*
* IML APPMONITOR CLIENT
* AS CLI APP
*
* ---------------------------------------------------------------------
* 2025-03-04 v0.1 Initial version
* ======================================================================
*/
# chdir(__DIR__);
$FLAG_DEBUG = 0; $FLAG_DEBUG = 0;
$VERSION = "0.0.1"; $VERSION = "0.1";
// ---MARK---INCLUDE-CHECKS---START--- // ---MARK---INCLUDE-CHECKS---START---
if (!file_exists(__DIR__."/include_checks.php")) { if (!file_exists(__DIR__ . "/include_checks.php")) {
echo "ERROR: File 'include_checks.php' does not exist yet..\n"; echo "ERROR: File 'include_checks.php' does not exist yet..\n";
echo "Run the ../installer.php first!\n"; echo "Run the ../installer.php first!\n";
exit(1); exit(1);
} }
if (!include __DIR__."/include_checks.php") { if (!include __DIR__ . "/include_checks.php") {
echo "ERROR: Include of generated 'include_checks.php' failed.\n"; echo "ERROR: Include of generated 'include_checks.php' failed.\n";
echo "Check its generation by installer or run the installer again.\n"; echo "Check its generation by installer or run the installer again.\n";
exit(2); exit(2);
...@@ -62,7 +72,6 @@ function _wd($s): void ...@@ -62,7 +72,6 @@ function _wd($s): void
{ {
global $FLAG_DEBUG; global $FLAG_DEBUG;
if ($FLAG_DEBUG) { if ($FLAG_DEBUG) {
// echo "DEBUG: $s\n";
fwrite(STDERR, "DEBUG: $s\n"); fwrite(STDERR, "DEBUG: $s\n");
} }
} }
...@@ -81,19 +90,25 @@ This client performs appmonitor checks and puts the results as JSON to stdout. ...@@ -81,19 +90,25 @@ This client performs appmonitor checks and puts the results as JSON to stdout.
You can use the compiled binary on non PHP systems. You can use the compiled binary on non PHP systems.
(c) 2025 Institute for Medical education * University of Bern (c) 2025 Institute for Medical education * University of Bern
!!! This tool is in alpha stadium !!! SYNTAX: $_self [OPTIONS] --ini=<INI-FILE>
SYNTAX: $_self [OPTIONS] -i <INIFILE>
OPTIONS: OPTIONS:
-h, --help Print this help and exit -h, --help Print this help and exit
-i, --ini Set an INI File to parse -i, --ini Set an INI File to parse
-l, --list list available checks and exit; --ini will be ignored -l, --list list available checks and exit
-m, --modules list available Php modules in this binary -m, --modules list available Php modules in this binary and exit
-v, --verbose Verbose output -v, --verbose Enable verbose output
-V, --version Show version and exit -V, --version Show version and exit
EXAMPLES:
$_self -i=my.ini
$_self --ini=my.ini
Execute checks from INI file 'my.ini'.
$_self --list
List available checks.
"; ";
} }
...@@ -112,6 +127,7 @@ if ($argc > 1) { ...@@ -112,6 +127,7 @@ if ($argc > 1) {
if (isset($ARGS['-v']) || isset($ARGS['--verbose'])) { if (isset($ARGS['-v']) || isset($ARGS['--verbose'])) {
$FLAG_DEBUG = 1; $FLAG_DEBUG = 1;
_wd("Verbose mode enabled. Showing debug infos on STDOUT.");
} }
_wd("CLI ARGS: " . print_r($ARGS ?? [], 1)); _wd("CLI ARGS: " . print_r($ARGS ?? [], 1));
...@@ -134,45 +150,45 @@ if (isset($ARGS['-h']) || isset($ARGS['--help'])) { ...@@ -134,45 +150,45 @@ if (isset($ARGS['-h']) || isset($ARGS['--help'])) {
_wd("Initializing appmonitor class"); _wd("Initializing appmonitor class");
$oMonitor = new appmonitor(); $oMonitor = new appmonitor();
$sPreSpace=" - ";
// show builtin checks // show builtin checks
if (isset($ARGS['-l']) || isset($ARGS['--list'])) { if (isset($ARGS['-l']) || isset($ARGS['--list'])) {
_wd("Showing checks"); _wd("Showing checks");
echo implode("\n", $oMonitor->listChecks()); echo $sPreSpace . implode("\n$sPreSpace", $oMonitor->listChecks());
exit(0); exit(0);
} }
// show builtin modules // show builtin modules
if (isset($ARGS['-m']) || isset($ARGS['--modules'])) { if (isset($ARGS['-m']) || isset($ARGS['--modules'])) {
_wd("Showing php modules"); _wd("Showing php modules");
$aMods=get_loaded_extensions(); $aMods = get_loaded_extensions();
sort($aMods); sort($aMods);
echo implode("\n", $aMods); echo $sPreSpace . implode("\n$sPreSpace", $aMods);
exit(0); exit(0);
} }
$inifile = $ARGS["--ini"] ?? ($ARGS["-i"] ?? ""); $inifile = $ARGS["--ini"] ?? ($ARGS["-i"] ?? "");
if (!$inifile) { if (!$inifile) {
echo "ERROR: Set an INI File using -i=<FILE> (or --ini=<FILE>).\n"; echo "ERROR: Set an INI File using -i=<FILE> (or --ini=<FILE>).\n";
exit(1); exit(3);
} }
_wd("Using ini file '$inifile'."); _wd("Using ini file '$inifile'.");
if (!file_exists($inifile)) { if (!file_exists($inifile)) {
echo "ERROR: INI File '$inifile' does not exist.\n"; echo "ERROR: INI File '$inifile' does not exist.\n";
exit(1); exit(4);
} }
$aIni = parse_ini_file($inifile, true); $aIni = parse_ini_file($inifile, true);
if (!is_array($aIni)) { if (!is_array($aIni)) {
echo "ERROR: INI File '$inifile' could not be parsed.\n"; echo "ERROR: INI File '$inifile' could not be parsed.\n";
exit(1); exit(5);
} }
_wd("Parsed INI data: " . print_r($aIni, 1)); _wd("Parsed INI data: " . print_r($aIni, 1));
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
// set metadata // set metadata
...@@ -192,12 +208,14 @@ foreach ($aIni['notifications']['slack'] ?? [] as $sValue) { ...@@ -192,12 +208,14 @@ foreach ($aIni['notifications']['slack'] ?? [] as $sValue) {
_set("addSlackWebhook", $sChannel, $sWebhook); _set("addSlackWebhook", $sChannel, $sWebhook);
} }
// ----------------------------------------------------------------------
// loop over checks // loop over checks
$aChecks = $aIni; $aChecks = $aIni;
unset($aChecks["meta"]); unset($aChecks["meta"]);
unset($aChecks["notifications"]); unset($aChecks["notifications"]);
$aArray=[]; $aArray = [];
foreach ($aChecks as $sKey => $aCheck) { foreach ($aChecks as $sKey => $aCheck) {
$aChecks[$sKey]['name'] = $aCheck['name'] ?? $sKey; $aChecks[$sKey]['name'] = $aCheck['name'] ?? $sKey;
if ($aCheck['params']) { if ($aCheck['params']) {
...@@ -205,42 +223,26 @@ foreach ($aChecks as $sKey => $aCheck) { ...@@ -205,42 +223,26 @@ foreach ($aChecks as $sKey => $aCheck) {
if (!is_array($aArray)) { if (!is_array($aArray)) {
echo "ERROR: key 'params' for check [$sKey] must be JSON.\n"; echo "ERROR: key 'params' for check [$sKey] must be JSON.\n";
echo "Value in $inifile: $aCheck[params]\n"; echo "Value in $inifile: $aCheck[params]\n";
exit(1); exit(6);
} }
} }
$aAddCheck=[ $aAddCheck = [
"name" => $aCheck['name'] ?? $sKey, "name" => $aCheck['name'] ?? $sKey,
"description" => $aCheck['description'], "description" => $aCheck['description'] ?? "",
"check" => [ "check" => [
"function" => $aCheck['function'], "function" => $aCheck['function'],
"params" => $aArray ?? [], "params" => $aArray ?? [],
], ],
]; ];
foreach(["group", "parent", "worstresult"] as $sCustomKey) { foreach (["group", "parent", "worstresult"] as $sCustomKey) {
if (isset($aCheck[$sCustomKey])) { if (isset($aCheck[$sCustomKey])) {
$aAddCheck[$sCustomKey] = $aCheck[$sCustomKey]; $aAddCheck[$sCustomKey] = $aCheck[$sCustomKey];
} }
} }
_wd("Execute Check '$sKey': " . print_r($aAddCheck, 1)); _wd("Execute Check '$sKey': " . print_r($aAddCheck, 1));
$oMonitor->addCheck($aAddCheck); $oMonitor->addCheck($aAddCheck);
/*
$oMonitor->addCheck(
[
"name" => "hello plugin",
"description" => "Test a plugin ... plugins/checks/hello.php",
"check" => [
"function" => "Hello",
"params" => [
"message" => "Here I am",
],
],
]
);
*/
} }
...@@ -252,3 +254,9 @@ $oMonitor->setResult(); ...@@ -252,3 +254,9 @@ $oMonitor->setResult();
_wd("Send response"); _wd("Send response");
$oMonitor->render(); $oMonitor->render();
$sOut = ob_get_contents();
ob_end_clean();
echo $sOut;
// ----------------------------------------------------------------------
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment