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

amcli: set metadata, execute check, list available checks

parent d82d6c89
No related branches found
No related tags found
No related merge requests found
...@@ -7,7 +7,7 @@ $VERSION="0.0.1"; ...@@ -7,7 +7,7 @@ $VERSION="0.0.1";
// ---MARK---INCLUDE-CHECKS---START--- // ---MARK---INCLUDE-CHECKS---START---
if (!file_exists("include_checks.php")) { if (!file_exists("include_checks.php")) {
echo "ERROR: File 'include_checks.php' does not exisr 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);
} }
...@@ -27,6 +27,37 @@ if(!include "include_checks.php"){ ...@@ -27,6 +27,37 @@ if(!include "include_checks.php"){
// -------------------------------------------------------------------- // --------------------------------------------------------------------
/**
* Call $oMonitor-><METHODNAME> with 1 or 2 params.
* This function was introduced to shorten the code.
*
* @param string $sMethod method name of appmonitor class
* @param mixed $value
* @param mixed $value2
* @return void
*/
function _set(string $sMethod, mixed $value = null, mixed $value2 = null)
{
global $oMonitor;
if (!isset($value)) {
_wd("SKIP \$oMonitor->$sMethod(<no_value>)");
} else {
if (!isset($value2)) {
_wd("calling \$oMonitor->$sMethod('$value')");
$oMonitor->$sMethod($value);
} else {
_wd("calling \$oMonitor->$sMethod('$value', '$value2')");
$oMonitor->$sMethod($value, $value2);
}
}
}
/**
* Write debug output if FLAG_DEBUG is true (use -v or --verbose on command line)
* @param string $s message to show; a prefix "DEBUG:" will be added in front
* @return void
*/
function _wd($s): void function _wd($s): void
{ {
global $FLAG_DEBUG; global $FLAG_DEBUG;
...@@ -36,6 +67,10 @@ function _wd($s):void ...@@ -36,6 +67,10 @@ function _wd($s):void
} }
} }
/**
* Show help text
* @return void
*/
function _showHelp(): void function _showHelp(): void
{ {
global $VERSION; global $VERSION;
...@@ -52,9 +87,11 @@ You can use the compiled binary on non PHP systems. ...@@ -52,9 +87,11 @@ You can use the compiled binary on non PHP systems.
SYNTAX: $_self [OPTIONS] SYNTAX: $_self [OPTIONS]
OPTIONS: 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
-v, --verbose Verbose output -v, --verbose Verbose output
-h, --help Print this help -V, --version Show version and exit
-i, --ini INI File
"; ";
} }
...@@ -75,14 +112,34 @@ if($argc>1){ ...@@ -75,14 +112,34 @@ if($argc>1){
if (isset($ARGS['-v']) || isset($ARGS['--verbose'])) { if (isset($ARGS['-v']) || isset($ARGS['--verbose'])) {
$FLAG_DEBUG = 1; $FLAG_DEBUG = 1;
} }
_wd("CLI ARGS: ".print_r($ARGS, 1)); _wd("CLI ARGS: " . print_r($ARGS ?? [], 1));
// show version
if (isset($ARGS['-V']) || isset($ARGS['--version'])) {
_wd("Showing version");
echo "$VERSION\n";
exit(0);
}
// show help
if (isset($ARGS['-h']) || isset($ARGS['--help'])) { if (isset($ARGS['-h']) || isset($ARGS['--help'])) {
_wd("Showing help"); _wd("Showing help");
_showHelp(); _showHelp();
exit(0); exit(0);
} }
// ----------------------------------------------------------------------
_wd("Initializing appmonitor class");
$oMonitor = new appmonitor();
// show builtin checks
if (isset($ARGS['-l']) || isset($ARGS['--list'])) {
echo implode("\n", $oMonitor->listChecks());
exit(0);
}
$inifile = $ARGS["--ini"] ?? ($ARGS["-i"] ?? "simple.ini"); $inifile = $ARGS["--ini"] ?? ($ARGS["-i"] ?? "simple.ini");
_wd("Using ini file '$inifile'."); _wd("Using ini file '$inifile'.");
...@@ -92,22 +149,59 @@ if(!file_exists($inifile)){ ...@@ -92,22 +149,59 @@ if(!file_exists($inifile)){
exit(1); exit(1);
} }
$aIni=parse_ini_file($inifile); $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(1);
} }
_wd("Parsed INI data: " . print_r($aIni, 1)); _wd("Parsed INI data: " . print_r($aIni, 1));
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
_wd("Initializing appmonitor class"); // set metadata
_set("setHost", $aIni['meta']['host'] ?? null);
_set("setWebsite", $aIni['meta']['website'] ?? null);
_set("setTtl", $aIni['meta']['ttl'] ?? null);
$oMonitor = new appmonitor(); foreach ($aIni['notification']['email'] ?? [] as $sValue) {
$oMonitor->addEmail('sysadmin@example.com'); _set("addEmail", $sValue);
$oMonitor->addSlackWebhook("mywebhook", "https://hooks.slack.com/services/(...)"); }
foreach ($aIni['notification']['slack'] ?? [] as $sValue) {
$sChannel = preg_filter('/,.*/', '', $sValue);
$sWebhook = preg_filter('/^.*,/', '', $sValue);
_set("addSlackWebhook", $sChannel, $sWebhook);
}
// loop over checks
$aChecks = $aIni;
unset($aChecks["meta"]);
unset($aChecks["notification"]);
foreach ($aChecks as $sKey => $aCheck) {
$aChecks[$sKey]['name'] = $aCheck['name'] ?? $sKey;
if ($aCheck['params']) {
$aArray = json_decode($aCheck['params'], 1);
if (!is_array($aArray)) {
echo "ERROR: key 'params' for check [$sKey] must be JSON.\n";
echo "Value in $inifile: $aCheck[params]\n";
exit(1);
}
}
_wd("Execute Check '$sKey'");
$oMonitor->addCheck([
"name" => $aCheck['name'] ?? $sKey,
"description" => $aCheck['description'],
"check" => [
"function" => $aCheck['function'],
"params" => $aArray,
],
]);
/*
$oMonitor->addCheck( $oMonitor->addCheck(
[ [
"name" => "hello plugin", "name" => "hello plugin",
...@@ -120,6 +214,10 @@ $oMonitor->addCheck( ...@@ -120,6 +214,10 @@ $oMonitor->addCheck(
], ],
] ]
); );
*/
}
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
// send the response // send the response
......
; =======================================================================
;
; APPMONITOR CLI CLIENT
;
; to understand the entries of metadata see JSON in
; <https://os-docs.iml.unibe.ch/appmonitor/Client/Description_of_response.html>
;
; for the checks see its parameters
; <https://os-docs.iml.unibe.ch/appmonitor/PHP_client/Plugins/Checks/index.html>
;
; =======================================================================
; -----------------------------------------------------------------------
; METADATA
; -----------------------------------------------------------------------
[meta] [meta]
host = "www.example.com" host = "www.example.com"
website = "Company website" website = "Company website"
ttl = 300 ttl = 300
tags[]="monitoring" tags[]="monitoring"
[notification]
email[]="support@example.com"
email[]="webmaster@example.com"
; for slack use the following format
; <channelname> + comma + <webhook url>
slack[]="#support-channel,https://hooks.slack.com/services/XXXXXX/YYYYYY/ZZZZZ"
; -----------------------------------------------------------------------
; CHECKS
; -----------------------------------------------------------------------
["hello plugin"]
description="I sust wann say hello"
function="hello"
params='{
"message": "Here I am"
}'
; -----------------------------------------------------------------------
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment