diff --git a/src/amcli.php b/src/amcli.php index f03c04bdcdb2cce46bb7bb04611dd3647dc85e9d..5c43c122f1463817bfc6328d3c48771dce2d1945 100755 --- a/src/amcli.php +++ b/src/amcli.php @@ -11,6 +11,8 @@ * 2025-03-05 v0.2 slack notification as hash from ini; check params can be an hash value or JSON * 2025-03-07 v0.3 set argc and argv from $_SERVER; update help output * 2025-03-09 v0.4 more colors in output of debug and help + * 2025-03-14 v0.5 add build infos + * 2025-03-14 v0.6 add meta tags, check count+visual; add _json2array() * ====================================================================== */ @@ -147,6 +149,25 @@ You find example snippets in the source code of this project in tests/config/. "; } +/** + * JSON helper: + * If a given value is a JSON string then return an array, otherwise return the value + * + * @param mixed $value + * @return mixed + */ +function _json2array(mixed $value=null): mixed { + if (!is_string($value)) { + return $value; + } + $aArray = json_decode($value, 1); + if (is_array($aArray)){ + _wd("JSON found in\n$value\n... and was converted to an array: " . print_r($aArray, 1)); + } + return is_array($aArray) ? $aArray : $value; +} + + // -------------------------------------------------------------------- // // MAIN @@ -256,6 +277,10 @@ if (!is_array($aIni)) { _wd("Parsed INI data: " . print_r($aIni, 1)); +// loop over all values and detect JSON strings to convert +$aIni['meta']['tags']=_json2array($aIni['meta']['tags'] ?? null); +$aIni['notifications']['email']=_json2array($aIni['notifications']['email'] ?? null); + // ---------------------------------------------------------------------- // set metadata @@ -263,6 +288,10 @@ _set("setHost", $aIni['meta']['host'] ?? null); _set("setWebsite", $aIni['meta']['website'] ?? null); _set("setTtl", $aIni['meta']['ttl'] ?? null); +foreach ($aIni['meta']['tags'] ?? [] as $sValue) { + _set("addTag", $sValue); +} + foreach ($aIni['notifications']['email'] ?? [] as $sValue) { _set("addEmail", $sValue); } @@ -279,19 +308,12 @@ unset($aChecks["notifications"]); foreach ($aChecks as $sKey => $aCheck) { $aChecks[$sKey]['name'] = $aCheck['name'] ?? $sKey; - $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 valid JSON.\n"; - echo "Value in $inifile: $aCheck[params]\n"; - echo "OR: Try to use multiple lines 'params[<KEY>]=<VALUE>' instead.\n"; - exit(6); - } - } - } else { - $aArray = []; + $aParams = _json2array($aCheck['params'] ?? []); + if (!is_array($aParams)) { + echo "⌠ERROR: key 'params' for check [$sKey] must be \n- a hash or\n- valid JSON\n- not set\n"; + echo "Value in $inifile: $aCheck[params]\n"; + echo "Try to use multiple lines 'params[<KEY>]=<VALUE>'.\n"; + exit(6); } $aAddCheck = [ @@ -299,10 +321,10 @@ foreach ($aChecks as $sKey => $aCheck) { "description" => $aCheck['description'] ?? "", "check" => [ "function" => $aCheck['function'], - "params" => $aArray ?? [], + "params" => $aParams, ], ]; - foreach (["group", "parent", "worstresult"] as $sCustomKey) { + foreach (["group", "parent", "worstresult", "count", "visual"] as $sCustomKey) { if (isset($aCheck[$sCustomKey])) { $aAddCheck[$sCustomKey] = $aCheck[$sCustomKey]; } diff --git a/tests/configs/apacheprocesses.ini b/tests/configs/apacheprocesses.ini index 22272e9b9195543ad32b5f8af13b339f5d949ff3..5c0b0a9301ca510486d0b2c26d37cb8c0d65fc22 100644 --- a/tests/configs/apacheprocesses.ini +++ b/tests/configs/apacheprocesses.ini @@ -15,7 +15,7 @@ ["Apache status"] description="Worker status of Apache httpd" function="ApacheProcesses" -params= +; params= worstresult=0 ; ----------------------------------------------------------------------- diff --git a/tests/configs/metadata.ini b/tests/configs/metadata.ini index 58d6e0aa736df9fe93471f339ff8f105ff56f990..0c787a430cbf5d48a3b8f0abbf0edcca9229a72d 100644 --- a/tests/configs/metadata.ini +++ b/tests/configs/metadata.ini @@ -19,7 +19,8 @@ host = "www.example.com" website = "Company website" ttl = 300 -tags[]="monitoring" +; tags[]="monitoring" +tags='["monitoring"]' [notifications]