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 @@
*
* ---------------------------------------------------------------------
* 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);
}
......
......@@ -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}"
; -----------------------------------------------------------------------
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment