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

amcli v0.2 slack notification as hash from ini; check params can be an hash value or JSON

parent c460fd11
No related branches found
No related tags found
No related merge requests found
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
* *
* --------------------------------------------------------------------- * ---------------------------------------------------------------------
* 2025-03-04 v0.1 Initial version * 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)) { ...@@ -181,9 +182,14 @@ if (!file_exists($inifile)) {
exit(4); exit(4);
} }
$aIni = parse_ini_file($inifile, true); try{
if (!is_array($aIni)) { $aIni = parse_ini_file($inifile, true);
} catch (Exception $e) {
echo "ERROR: INI File '$inifile' could not be parsed.\n"; 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); exit(5);
} }
...@@ -199,12 +205,7 @@ _set("setTtl", $aIni['meta']['ttl'] ?? null); ...@@ -199,12 +205,7 @@ _set("setTtl", $aIni['meta']['ttl'] ?? null);
foreach ($aIni['notifications']['email'] ?? [] as $sValue) { foreach ($aIni['notifications']['email'] ?? [] as $sValue) {
_set("addEmail", $sValue); _set("addEmail", $sValue);
} }
foreach ($aIni['notifications']['slack'] ?? [] as $sValue) { foreach ($aIni['notifications']['slack'] ?? [] as $sChannel => $sWebhook) {
$aArray = json_decode($sValue, 1);
$sChannel = array_keys($aArray)[0];
$sWebhook = array_values($aArray)[0];
_set("addSlackWebhook", $sChannel, $sWebhook); _set("addSlackWebhook", $sChannel, $sWebhook);
} }
...@@ -215,16 +216,20 @@ $aChecks = $aIni; ...@@ -215,16 +216,20 @@ $aChecks = $aIni;
unset($aChecks["meta"]); unset($aChecks["meta"]);
unset($aChecks["notifications"]); unset($aChecks["notifications"]);
$aArray = [];
foreach ($aChecks as $sKey => $aCheck) { foreach ($aChecks as $sKey => $aCheck) {
$aChecks[$sKey]['name'] = $aCheck['name'] ?? $sKey; $aChecks[$sKey]['name'] = $aCheck['name'] ?? $sKey;
if ($aCheck['params']) { $aArray=$aCheck['params']??[];
$aArray = json_decode($aCheck['params'], 1); if ($aArray) {
if (!is_array($aArray)) { if(!is_array($aCheck['params'])){
echo "ERROR: key 'params' for check [$sKey] must be JSON.\n"; $aArray = json_decode($aCheck['params'], 1);
echo "Value in $inifile: $aCheck[params]\n"; if (!is_array($aArray)) {
exit(6); echo "ERROR: key 'params' for check [$sKey] must be JSON.\n";
echo "Value in $inifile: $aCheck[params]\n";
exit(6);
}
} }
} else {
$aArray = [];
} }
$aAddCheck = [ $aAddCheck = [
...@@ -242,7 +247,6 @@ foreach ($aChecks as $sKey => $aCheck) { ...@@ -242,7 +247,6 @@ foreach ($aChecks as $sKey => $aCheck) {
} }
_wd("Execute Check '$sKey': " . print_r($aAddCheck, 1)); _wd("Execute Check '$sKey': " . print_r($aAddCheck, 1));
$oMonitor->addCheck($aAddCheck); $oMonitor->addCheck($aAddCheck);
} }
......
...@@ -27,20 +27,22 @@ email[]="support@example.com" ...@@ -27,20 +27,22 @@ email[]="support@example.com"
email[]="webmaster@example.com" email[]="webmaster@example.com"
; for slack use the following format ; 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 ; CHECKS
; ----------------------------------------------------------------------- ; -----------------------------------------------------------------------
["hello plugin"] ["hello plugin"]
description="I sust wann say hello" description="I just wann say hello"
function="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}"
; ----------------------------------------------------------------------- ; -----------------------------------------------------------------------
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment