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

v0.6 add meta tags, check count+visual; add _json2array()

parent f832b925
No related branches found
No related tags found
No related merge requests found
...@@ -11,6 +11,8 @@ ...@@ -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-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-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-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/. ...@@ -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 // MAIN
...@@ -256,6 +277,10 @@ if (!is_array($aIni)) { ...@@ -256,6 +277,10 @@ if (!is_array($aIni)) {
_wd("Parsed INI data: " . print_r($aIni, 1)); _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 // set metadata
...@@ -263,6 +288,10 @@ _set("setHost", $aIni['meta']['host'] ?? null); ...@@ -263,6 +288,10 @@ _set("setHost", $aIni['meta']['host'] ?? null);
_set("setWebsite", $aIni['meta']['website'] ?? null); _set("setWebsite", $aIni['meta']['website'] ?? null);
_set("setTtl", $aIni['meta']['ttl'] ?? null); _set("setTtl", $aIni['meta']['ttl'] ?? null);
foreach ($aIni['meta']['tags'] ?? [] as $sValue) {
_set("addTag", $sValue);
}
foreach ($aIni['notifications']['email'] ?? [] as $sValue) { foreach ($aIni['notifications']['email'] ?? [] as $sValue) {
_set("addEmail", $sValue); _set("addEmail", $sValue);
} }
...@@ -279,19 +308,12 @@ unset($aChecks["notifications"]); ...@@ -279,19 +308,12 @@ unset($aChecks["notifications"]);
foreach ($aChecks as $sKey => $aCheck) { foreach ($aChecks as $sKey => $aCheck) {
$aChecks[$sKey]['name'] = $aCheck['name'] ?? $sKey; $aChecks[$sKey]['name'] = $aCheck['name'] ?? $sKey;
$aArray = $aCheck['params'] ?? []; $aParams = _json2array($aCheck['params'] ?? []);
if ($aArray) { if (!is_array($aParams)) {
if (!is_array($aCheck['params'])) { echo "❌ ERROR: key 'params' for check [$sKey] must be \n- a hash or\n- valid JSON\n- not set\n";
$aArray = json_decode($aCheck['params'], 1); echo "Value in $inifile: $aCheck[params]\n";
if (!is_array($aArray)) { echo "Try to use multiple lines 'params[<KEY>]=<VALUE>'.\n";
echo "❌ ERROR: key 'params' for check [$sKey] must be valid JSON.\n"; exit(6);
echo "Value in $inifile: $aCheck[params]\n";
echo "OR: Try to use multiple lines 'params[<KEY>]=<VALUE>' instead.\n";
exit(6);
}
}
} else {
$aArray = [];
} }
$aAddCheck = [ $aAddCheck = [
...@@ -299,10 +321,10 @@ foreach ($aChecks as $sKey => $aCheck) { ...@@ -299,10 +321,10 @@ foreach ($aChecks as $sKey => $aCheck) {
"description" => $aCheck['description'] ?? "", "description" => $aCheck['description'] ?? "",
"check" => [ "check" => [
"function" => $aCheck['function'], "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])) { if (isset($aCheck[$sCustomKey])) {
$aAddCheck[$sCustomKey] = $aCheck[$sCustomKey]; $aAddCheck[$sCustomKey] = $aCheck[$sCustomKey];
} }
......
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
["Apache status"] ["Apache status"]
description="Worker status of Apache httpd" description="Worker status of Apache httpd"
function="ApacheProcesses" function="ApacheProcesses"
params= ; params=
worstresult=0 worstresult=0
; ----------------------------------------------------------------------- ; -----------------------------------------------------------------------
...@@ -19,7 +19,8 @@ ...@@ -19,7 +19,8 @@
host = "www.example.com" host = "www.example.com"
website = "Company website" website = "Company website"
ttl = 300 ttl = 300
tags[]="monitoring" ; tags[]="monitoring"
tags='["monitoring"]'
[notifications] [notifications]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment