diff --git a/src/amcli.php b/src/amcli.php index 3d1b850b1d37629675998ade4df3c851ae631a8b..db4bba27305f8b7054e98890861e76d964382a2d 100755 --- a/src/amcli.php +++ b/src/amcli.php @@ -8,6 +8,7 @@ * * --------------------------------------------------------------------- * 2025-03-04 v0.1 Initial version + * 2025-03-05 v0.2 slack notification as hash from ini; check params can be an hash value or JSON * ====================================================================== */ @@ -181,9 +182,14 @@ if (!file_exists($inifile)) { exit(4); } -$aIni = parse_ini_file($inifile, true); -if (!is_array($aIni)) { +try{ + $aIni = parse_ini_file($inifile, true); +} catch (Exception $e) { echo "ERROR: INI File '$inifile' could not be parsed.\n"; + exit(5); +} +if (!is_array($aIni)) { + echo "ERROR: INI File '$inifile' could not be parsed as array.\n"; exit(5); } @@ -199,12 +205,7 @@ _set("setTtl", $aIni['meta']['ttl'] ?? null); foreach ($aIni['notifications']['email'] ?? [] as $sValue) { _set("addEmail", $sValue); } -foreach ($aIni['notifications']['slack'] ?? [] as $sValue) { - - $aArray = json_decode($sValue, 1); - $sChannel = array_keys($aArray)[0]; - $sWebhook = array_values($aArray)[0]; - +foreach ($aIni['notifications']['slack'] ?? [] as $sChannel => $sWebhook) { _set("addSlackWebhook", $sChannel, $sWebhook); } @@ -215,16 +216,20 @@ $aChecks = $aIni; unset($aChecks["meta"]); unset($aChecks["notifications"]); -$aArray = []; 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(6); + $aArray=$aCheck['params']??[]; + if ($aArray) { + if(!is_array($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(6); + } } + } else { + $aArray = []; } $aAddCheck = [ @@ -242,7 +247,6 @@ foreach ($aChecks as $sKey => $aCheck) { } _wd("Execute Check '$sKey': " . print_r($aAddCheck, 1)); $oMonitor->addCheck($aAddCheck); - } diff --git a/src/simple.ini b/src/simple.ini index 56e1efa96ee88aeff33a5951703f30c9fe8aadc0..02653f0b14b049f9ee19783161ec87b57ec54928 100644 --- a/src/simple.ini +++ b/src/simple.ini @@ -27,20 +27,22 @@ 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" -slack[]='{ "#support-channel": "https://hooks.slack.com/services/XXXXXX/YYYYYY/ZZZZZ" }' - +slack["#support-channel"]="https://hooks.slack.com/services/XXXXXX/YYYYYY/ZZZZZ" ; ----------------------------------------------------------------------- ; CHECKS ; ----------------------------------------------------------------------- ["hello plugin"] -description="I sust wann say hello" +description="I just wann say hello" function="hello" -params='{ -"message": "Here I am" -}' + +; JSON example ... variables cannot be expanded +; params='{ +; "message": "Here I am" +; }' +; +; ... but in double quotes the expansion works +params[message]="Here is ${USER}" ; -----------------------------------------------------------------------