netwatch-notify: move code into function

This commit is contained in:
Christian Hesse 2024-03-04 13:48:01 +01:00
parent 458fd1fdcd
commit 3c76738915

View file

@ -8,23 +8,25 @@
# monitor netwatch and send notifications
# https://git.eworm.de/cgit/routeros-scripts/about/doc/netwatch-notify.md
:local 0 [ :jobname ];
:global GlobalFunctionsReady;
:while ($GlobalFunctionsReady != true) do={ :delay 500ms; }
:global NetwatchNotify;
:local Main do={
:local ScriptName [ :tostr $1 ];
:global EitherOr;
:global IfThenElse;
:global IsDNSResolving;
:global LogPrintExit2;
:global ParseKeyValueStore;
:global ScriptFromTerminal;
:global ScriptLock;
:global SendNotification2;
:global SymbolForNotification;
:global NetwatchNotify;
:local NetwatchNotifyHook do={
:global EitherOr;
:global IfThenElse;
:global IsDNSResolving;
:global LogPrintExit2;
:global ParseKeyValueStore;
:global ScriptFromTerminal;
:global ScriptLock;
:global SendNotification2;
:global SymbolForNotification;
:local NetwatchNotifyHook do={
:local Name [ :tostr $1 ];
:local Type [ :tostr $2 ];
:local State [ :tostr $3 ];
@ -50,9 +52,9 @@
$LogPrintExit2 info $0 ("Ran hook on " . $Type . " '" . $Name . "' " . $State . ": " . \
$Hook) false;
:return ("Ran hook:\n" . $Hook);
}
}
:local ResolveExpected do={
:local ResolveExpected do={
:local Name [ :tostr $1 ];
:local Expected [ :tostr $2 ];
@ -71,17 +73,17 @@
}
:return false;
}
}
$ScriptLock $0;
$ScriptLock $ScriptName;
:local ScriptFromTerminalCached [ $ScriptFromTerminal $0 ];
:local ScriptFromTerminalCached [ $ScriptFromTerminal $ScriptName ];
:if ([ :typeof $NetwatchNotify ] = "nothing") do={
:if ([ :typeof $NetwatchNotify ] = "nothing") do={
:set NetwatchNotify ({});
}
}
:foreach Host in=[ /tool/netwatch/find where comment~"\\bnotify\\b" !disabled status!="unknown" ] do={
:foreach Host in=[ /tool/netwatch/find where comment~"\\bnotify\\b" !disabled status!="unknown" ] do={
:local HostVal [ /tool/netwatch/get $Host ];
:local Type [ $IfThenElse ($HostVal->"type" ~ "^(https?-get|tcp-conn)\$") "service" "host" ];
:local HostInfo [ $ParseKeyValueStore ($HostVal->"comment") ];
@ -102,7 +104,7 @@ $ScriptLock $0;
:local Resolve [ :resolve ($HostInfo->"resolve") ];
:if ($Resolve != $HostVal->"host") do={
:if ([ $ResolveExpected ($HostInfo->"resolve") ($HostVal->"host") ] = false) do={
$LogPrintExit2 info $0 ("Name '" . $HostInfo->"resolve" . [ $IfThenElse \
$LogPrintExit2 info $ScriptName ("Name '" . $HostInfo->"resolve" . [ $IfThenElse \
($HostInfo->"resolve" != $HostInfo->"name") ("' for " . $Type . " '" . \
$HostInfo->"name") "" ] . "' resolves to different address " . $Resolve . \
", updating.") false;
@ -114,7 +116,7 @@ $ScriptLock $0;
} on-error={
:set ($Metric->"resolve-failcnt") ($Metric->"resolve-failcnt" + 1);
:if ($Metric->"resolve-failcnt" = 3) do={
$LogPrintExit2 warning $0 ("Resolving name '" . $HostInfo->"resolve" . [ $IfThenElse \
$LogPrintExit2 warning $ScriptName ("Resolving name '" . $HostInfo->"resolve" . [ $IfThenElse \
($HostInfo->"resolve" != $HostInfo->"name") ("' for " . $Type . " '" . \
$HostInfo->"name") "" ] . "' failed.") false;
}
@ -125,7 +127,7 @@ $ScriptLock $0;
:if ($HostVal->"status" = "up") do={
:local CountDown ($Metric->"count-down");
:if ($CountDown > 0) do={
$LogPrintExit2 info $0 \
$LogPrintExit2 info $ScriptName \
("The " . $Type . " '" . $Name . "' (" . $HostDetails . ") is up.") false;
:set ($Metric->"count-down") 0;
}
@ -141,7 +143,7 @@ $ScriptLock $0;
:set Message ($Message . "\n\n" . [ $NetwatchNotifyHook $Name $Type "up" \
($HostInfo->"up-hook") ]);
}
$SendNotification2 ({ origin=[ $EitherOr ($HostInfo->"origin") $0 ]; silent=($HostInfo->"silent"); \
$SendNotification2 ({ origin=[ $EitherOr ($HostInfo->"origin") $ScriptName ]; silent=($HostInfo->"silent"); \
subject=([ $SymbolForNotification "white-heavy-check-mark" ] . "Netwatch Notify: " . $Name . " up"); \
message=$Message });
}
@ -174,7 +176,7 @@ $ScriptLock $0;
}
:if ($Metric->"notified" = false || $Metric->"count-down" % 120 = 0 || \
$ScriptFromTerminalCached = true) do={
$LogPrintExit2 [ $IfThenElse ($HostInfo->"no-down-notification" != true) info debug ] $0 \
$LogPrintExit2 [ $IfThenElse ($HostInfo->"no-down-notification" != true) info debug ] $ScriptName \
("The " . $Type . " '" . $Name . "' (" . $HostDetails . ") is down for " . \
$Metric->"count-down" . " checks, " . [ $IfThenElse ($ParentNotified = false) [ $IfThenElse \
($Metric->"notified" = true) ("already notified.") ($CountDown - $Metric->"count-down" . \
@ -196,7 +198,7 @@ $ScriptLock $0;
($HostInfo->"down-hook") ]);
}
:if ($HostInfo->"no-down-notification" != true) do={
$SendNotification2 ({ origin=[ $EitherOr ($HostInfo->"origin") $0 ]; silent=($HostInfo->"silent"); \
$SendNotification2 ({ origin=[ $EitherOr ($HostInfo->"origin") $ScriptName ]; silent=($HostInfo->"silent"); \
subject=([ $SymbolForNotification "cross-mark" ] . "Netwatch Notify: " . $Name . " down"); \
message=$Message });
}
@ -212,4 +214,7 @@ $ScriptLock $0;
"resolve-failcnt"=($Metric->"resolve-failcnt");
"since"=($Metric->"since") };
}
}
}
$Main [ :jobname ];