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
Tags
No related merge requests found
#!/usr/bin/env php
<?php
/**
* ======================================================================
*
* IML APPMONITOR CLIENT
* AS CLI APP
*
* ---------------------------------------------------------------------
* 2025-03-04 v0.1 Initial version
* ======================================================================
*/
# chdir(__DIR__);
$FLAG_DEBUG = 0;
$VERSION = "0.0.1";
$VERSION = "0.1";
// ---MARK---INCLUDE-CHECKS---START---
if (!file_exists(__DIR__ . "/include_checks.php")) {
......@@ -62,7 +72,6 @@ function _wd($s): void
{
global $FLAG_DEBUG;
if ($FLAG_DEBUG) {
// echo "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.
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] -i <INIFILE>
SYNTAX: $_self [OPTIONS] --ini=<INI-FILE>
OPTIONS:
-h, --help Print this help and exit
-i, --ini Set an INI File to parse
-l, --list list available checks and exit; --ini will be ignored
-m, --modules list available Php modules in this binary
-v, --verbose Verbose output
-l, --list list available checks and exit
-m, --modules list available Php modules in this binary and exit
-v, --verbose Enable verbose output
-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) {
if (isset($ARGS['-v']) || isset($ARGS['--verbose'])) {
$FLAG_DEBUG = 1;
_wd("Verbose mode enabled. Showing debug infos on STDOUT.");
}
_wd("CLI ARGS: " . print_r($ARGS ?? [], 1));
......@@ -134,11 +150,12 @@ if (isset($ARGS['-h']) || isset($ARGS['--help'])) {
_wd("Initializing appmonitor class");
$oMonitor = new appmonitor();
$sPreSpace=" - ";
// show builtin checks
if (isset($ARGS['-l']) || isset($ARGS['--list'])) {
_wd("Showing checks");
echo implode("\n", $oMonitor->listChecks());
echo $sPreSpace . implode("\n$sPreSpace", $oMonitor->listChecks());
exit(0);
}
......@@ -147,32 +164,31 @@ if (isset($ARGS['-m']) || isset($ARGS['--modules'])) {
_wd("Showing php modules");
$aMods = get_loaded_extensions();
sort($aMods);
echo implode("\n", $aMods);
echo $sPreSpace . implode("\n$sPreSpace", $aMods);
exit(0);
}
$inifile = $ARGS["--ini"] ?? ($ARGS["-i"] ?? "");
if (!$inifile) {
echo "ERROR: Set an INI File using -i=<FILE> (or --ini=<FILE>).\n";
exit(1);
exit(3);
}
_wd("Using ini file '$inifile'.");
if (!file_exists($inifile)) {
echo "ERROR: INI File '$inifile' does not exist.\n";
exit(1);
exit(4);
}
$aIni = parse_ini_file($inifile, true);
if (!is_array($aIni)) {
echo "ERROR: INI File '$inifile' could not be parsed.\n";
exit(1);
exit(5);
}
_wd("Parsed INI data: " . print_r($aIni, 1));
// ----------------------------------------------------------------------
// set metadata
......@@ -192,6 +208,8 @@ foreach ($aIni['notifications']['slack'] ?? [] as $sValue) {
_set("addSlackWebhook", $sChannel, $sWebhook);
}
// ----------------------------------------------------------------------
// loop over checks
$aChecks = $aIni;
unset($aChecks["meta"]);
......@@ -205,13 +223,13 @@ foreach ($aChecks as $sKey => $aCheck) {
if (!is_array($aArray)) {
echo "ERROR: key 'params' for check [$sKey] must be JSON.\n";
echo "Value in $inifile: $aCheck[params]\n";
exit(1);
exit(6);
}
}
$aAddCheck = [
"name" => $aCheck['name'] ?? $sKey,
"description" => $aCheck['description'],
"description" => $aCheck['description'] ?? "",
"check" => [
"function" => $aCheck['function'],
"params" => $aArray ?? [],
......@@ -223,24 +241,8 @@ foreach ($aChecks as $sKey => $aCheck) {
}
}
_wd("Execute Check '$sKey': " . print_r($aAddCheck, 1));
$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();
_wd("Send response");
$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