From 59664048432aab3e7c84c787a8ba433a4695b958 Mon Sep 17 00:00:00 2001
From: "Hahn Axel (hahn)" <axel.hahn@unibe.ch>
Date: Tue, 4 Mar 2025 16:47:25 +0100
Subject: [PATCH] 1st version of amcli with all checks included

---
 src/amcli.php | 88 ++++++++++++++++++++++++++++-----------------------
 1 file changed, 48 insertions(+), 40 deletions(-)

diff --git a/src/amcli.php b/src/amcli.php
index be44ad2..e565719 100755
--- a/src/amcli.php
+++ b/src/amcli.php
@@ -1,18 +1,28 @@
 #!/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")) {
+if (!file_exists(__DIR__ . "/include_checks.php")) {
     echo "ERROR: File 'include_checks.php' does not exist yet..\n";
     echo "Run the ../installer.php first!\n";
     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 "Check its generation by installer or run the installer again.\n";
     exit(2);
@@ -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,45 +150,45 @@ 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);
 }
 
 // show builtin modules
 if (isset($ARGS['-m']) || isset($ARGS['--modules'])) {
     _wd("Showing php modules");
-    $aMods=get_loaded_extensions();
+    $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,12 +208,14 @@ foreach ($aIni['notifications']['slack'] ?? [] as $sValue) {
     _set("addSlackWebhook", $sChannel, $sWebhook);
 }
 
+// ----------------------------------------------------------------------
+
 // loop over checks
 $aChecks = $aIni;
 unset($aChecks["meta"]);
 unset($aChecks["notifications"]);
 
-$aArray=[];
+$aArray = [];
 foreach ($aChecks as $sKey => $aCheck) {
     $aChecks[$sKey]['name'] = $aCheck['name'] ?? $sKey;
     if ($aCheck['params']) {
@@ -205,42 +223,26 @@ 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=[
+    $aAddCheck = [
         "name" => $aCheck['name'] ?? $sKey,
-        "description" => $aCheck['description'],
+        "description" => $aCheck['description'] ?? "",
         "check" => [
             "function" => $aCheck['function'],
             "params" => $aArray ?? [],
         ],
     ];
-    foreach(["group", "parent", "worstresult"] as $sCustomKey) {
+    foreach (["group", "parent", "worstresult"] as $sCustomKey) {
         if (isset($aCheck[$sCustomKey])) {
             $aAddCheck[$sCustomKey] = $aCheck[$sCustomKey];
         }
     }
     _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;
+
+// ----------------------------------------------------------------------
-- 
GitLab