netwatch-notify: pass script name into local functions

This commit is contained in:
Christian Hesse 2024-03-20 13:59:44 +01:00
parent 81f1d9aa99
commit 6628d9f966

View file

@ -27,10 +27,11 @@
:global SymbolForNotification; :global SymbolForNotification;
:local NetwatchNotifyHook do={ :local NetwatchNotifyHook do={
:local Name [ :tostr $1 ]; :local ScriptName [ :tostr $1 ];
:local Type [ :tostr $2 ]; :local Name [ :tostr $2 ];
:local State [ :tostr $3 ]; :local Type [ :tostr $3 ];
:local Hook [ :tostr $4 ]; :local State [ :tostr $4 ];
:local Hook [ :tostr $5 ];
:global LogPrint; :global LogPrint;
:global ValidateSyntax; :global ValidateSyntax;
@ -39,25 +40,26 @@
:do { :do {
[ :parse $Hook ]; [ :parse $Hook ];
} on-error={ } on-error={
$LogPrint warning $0 ("The " . $State . "-hook for " . $Type . " '" . $Name . "' failed to run."); $LogPrint warning $ScriptName ("The " . $State . "-hook for " . $Type . " '" . $Name . "' failed to run.");
:return ("The hook failed to run."); :return ("The hook failed to run.");
} }
} else={ } else={
$LogPrint warning $0 ("The " . $State . "-hook for " . $Type . " '" . $Name . "' failed syntax validation."); $LogPrint warning $ScriptName ("The " . $State . "-hook for " . $Type . " '" . $Name . "' failed syntax validation.");
:return ("The hook failed syntax validation."); :return ("The hook failed syntax validation.");
} }
$LogPrint info $0 ("Ran hook on " . $Type . " '" . $Name . "' " . $State . ": " . $Hook); $LogPrint info $ScriptName ("Ran hook on " . $Type . " '" . $Name . "' " . $State . ": " . $Hook);
:return ("Ran hook:\n" . $Hook); :return ("Ran hook:\n" . $Hook);
} }
:local ResolveExpected do={ :local ResolveExpected do={
:local Name [ :tostr $1 ]; :local ScriptName [ :tostr $1 ];
:local Expected [ :tostr $2 ]; :local Name [ :tostr $2 ];
:local Expected [ :tostr $3 ];
:global GetRandom20CharAlNum; :global GetRandom20CharAlNum;
:local FwAddrList ($0 . "-" . [ $GetRandom20CharAlNum ]); :local FwAddrList ($ScriptName . "-" . [ $GetRandom20CharAlNum ]);
/ip/firewall/address-list/add address=$Name list=$FwAddrList dynamic=yes timeout=1s; /ip/firewall/address-list/add address=$Name list=$FwAddrList dynamic=yes timeout=1s;
:delay 20ms; :delay 20ms;
:if ([ :len [ /ip/firewall/address-list/find where list=$FwAddrList address=$Expected ] ] > 0) do={ :if ([ :len [ /ip/firewall/address-list/find where list=$FwAddrList address=$Expected ] ] > 0) do={
@ -102,7 +104,7 @@
:do { :do {
:local Resolve [ :resolve ($HostInfo->"resolve") ]; :local Resolve [ :resolve ($HostInfo->"resolve") ];
:if ($Resolve != $HostVal->"host") do={ :if ($Resolve != $HostVal->"host") do={
:if ([ $ResolveExpected ($HostInfo->"resolve") ($HostVal->"host") ] = false) do={ :if ([ $ResolveExpected $ScriptName ($HostInfo->"resolve") ($HostVal->"host") ] = false) do={
$LogPrint info $ScriptName ("Name '" . $HostInfo->"resolve" . [ $IfThenElse \ $LogPrint info $ScriptName ("Name '" . $HostInfo->"resolve" . [ $IfThenElse \
($HostInfo->"resolve" != $HostInfo->"name") ("' for " . $Type . " '" . \ ($HostInfo->"resolve" != $HostInfo->"name") ("' for " . $Type . " '" . \
$HostInfo->"name") "" ] . "' resolves to different address " . $Resolve . \ $HostInfo->"name") "" ] . "' resolves to different address " . $Resolve . \
@ -139,7 +141,7 @@
:set Message ($Message . "\n\nNote:\n" . ($HostInfo->"note")); :set Message ($Message . "\n\nNote:\n" . ($HostInfo->"note"));
} }
:if ([ :typeof ($HostInfo->"up-hook") ] = "str") do={ :if ([ :typeof ($HostInfo->"up-hook") ] = "str") do={
:set Message ($Message . "\n\n" . [ $NetwatchNotifyHook $Name $Type "up" \ :set Message ($Message . "\n\n" . [ $NetwatchNotifyHook $ScriptName $Name $Type "up" \
($HostInfo->"up-hook") ]); ($HostInfo->"up-hook") ]);
} }
$SendNotification2 ({ origin=[ $EitherOr ($HostInfo->"origin") $ScriptName ]; silent=($HostInfo->"silent"); \ $SendNotification2 ({ origin=[ $EitherOr ($HostInfo->"origin") $ScriptName ]; silent=($HostInfo->"silent"); \
@ -183,7 +185,7 @@
} }
:if ((($CountDown * 2) - ($Metric->"count-down" * 3)) / 2 = 0 && \ :if ((($CountDown * 2) - ($Metric->"count-down" * 3)) / 2 = 0 && \
[ :typeof ($HostInfo->"pre-down-hook") ] = "str") do={ [ :typeof ($HostInfo->"pre-down-hook") ] = "str") do={
$NetwatchNotifyHook $Name $Type "pre-down" ($HostInfo->"pre-down-hook"); $NetwatchNotifyHook $ScriptName $Name $Type "pre-down" ($HostInfo->"pre-down-hook");
} }
:if ($ParentNotified = false && $Metric->"count-down" >= $CountDown && \ :if ($ParentNotified = false && $Metric->"count-down" >= $CountDown && \
($ParentUp = false || $ParentUp > 2) && $Metric->"notified" != true) do={ ($ParentUp = false || $ParentUp > 2) && $Metric->"notified" != true) do={
@ -193,7 +195,7 @@
:set Message ($Message . "\n\nNote:\n" . ($HostInfo->"note")); :set Message ($Message . "\n\nNote:\n" . ($HostInfo->"note"));
} }
:if ([ :typeof ($HostInfo->"down-hook") ] = "str") do={ :if ([ :typeof ($HostInfo->"down-hook") ] = "str") do={
:set Message ($Message . "\n\n" . [ $NetwatchNotifyHook $Name $Type "down" \ :set Message ($Message . "\n\n" . [ $NetwatchNotifyHook $ScriptName $Name $Type "down" \
($HostInfo->"down-hook") ]); ($HostInfo->"down-hook") ]);
} }
:if ($HostInfo->"no-down-notification" != true) do={ :if ($HostInfo->"no-down-notification" != true) do={