Skip to content
Snippets Groups Projects
amcli.php 2.86 KiB
#!/usr/bin/env php
<?php

chdir(__DIR__);
$FLAG_DEBUG=0;
$VERSION="0.0.1";

// ---MARK---INCLUDE-CHECKS---START---
if(! file_exists("include_checks.php")){
  echo "ERROR: File 'include_checks.php' does not exisr yet..\n";
  echo "Run the ../installer.php first!\n";
  exit(1);
}

if(!include "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);
}
// ---MARK---INCLUDE-CHECKS---END


// --------------------------------------------------------------------
//
// 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

_wd("Setting result");
$oMonitor->setResult();

_wd("Send response");
$oMonitor->render();