log-forward: move code into function

This commit is contained in:
Christian Hesse 2024-03-04 13:48:01 +01:00
parent 49650d8b14
commit 50e1c45880

View file

@ -8,49 +8,51 @@
# forward log messages via notification # forward log messages via notification
# https://git.eworm.de/cgit/routeros-scripts/about/doc/log-forward.md # https://git.eworm.de/cgit/routeros-scripts/about/doc/log-forward.md
:local 0 [ :jobname ];
:global GlobalFunctionsReady; :global GlobalFunctionsReady;
:while ($GlobalFunctionsReady != true) do={ :delay 500ms; } :while ($GlobalFunctionsReady != true) do={ :delay 500ms; }
:global Identity; :local Main do={
:global LogForwardFilter; :local ScriptName [ :tostr $1 ];
:global LogForwardFilterMessage;
:global LogForwardInclude;
:global LogForwardIncludeMessage;
:global LogForwardLast;
:global LogForwardRateLimit;
:global EitherOr; :global Identity;
:global HexToNum; :global LogForwardFilter;
:global IfThenElse; :global LogForwardFilterMessage;
:global LogForwardFilterLogForwarding; :global LogForwardInclude;
:global LogPrintExit2; :global LogForwardIncludeMessage;
:global MAX; :global LogForwardLast;
:global ScriptLock; :global LogForwardRateLimit;
:global SendNotification2;
:global SymbolForNotification;
$ScriptLock $0; :global EitherOr;
:global HexToNum;
:global IfThenElse;
:global LogForwardFilterLogForwarding;
:global LogPrintExit2;
:global MAX;
:global ScriptLock;
:global SendNotification2;
:global SymbolForNotification;
:if ([ :typeof $LogForwardRateLimit ] = "nothing") do={ $ScriptLock $ScriptName;
:if ([ :typeof $LogForwardRateLimit ] = "nothing") do={
:set LogForwardRateLimit 0; :set LogForwardRateLimit 0;
} }
:if ($LogForwardRateLimit > 30) do={ :if ($LogForwardRateLimit > 30) do={
:set LogForwardRateLimit ($LogForwardRateLimit - 1); :set LogForwardRateLimit ($LogForwardRateLimit - 1);
$LogPrintExit2 info $0 ("Rate limit in action, not forwarding logs, if any!") true; $LogPrintExit2 info $ScriptName ("Rate limit in action, not forwarding logs, if any!") true;
} }
:local Count 0; :local Count 0;
:local Duplicates false; :local Duplicates false;
:local Last [ $IfThenElse ([ :len $LogForwardLast ] > 0) [ $HexToNum $LogForwardLast ] -1 ]; :local Last [ $IfThenElse ([ :len $LogForwardLast ] > 0) [ $HexToNum $LogForwardLast ] -1 ];
:local Messages ""; :local Messages "";
:local Warning false; :local Warning false;
:local MessageVal; :local MessageVal;
:local MessageDups ({}); :local MessageDups ({});
:local LogForwardFilterLogForwardingCached [ $EitherOr [ $LogForwardFilterLogForwarding ] ("\$^") ]; :local LogForwardFilterLogForwardingCached [ $EitherOr [ $LogForwardFilterLogForwarding ] ("\$^") ];
:foreach Message in=[ /log/find where (!(message="") and \ :foreach Message in=[ /log/find where (!(message="") and \
!(message~$LogForwardFilterLogForwardingCached) and \ !(message~$LogForwardFilterLogForwardingCached) and \
!(topics~$LogForwardFilter) and !(message~$LogForwardFilterMessage)) or \ !(topics~$LogForwardFilter) and !(message~$LogForwardFilterMessage)) or \
topics~$LogForwardInclude or message~$LogForwardIncludeMessage ] do={ topics~$LogForwardInclude or message~$LogForwardIncludeMessage ] do={
@ -76,12 +78,12 @@ $ScriptLock $0;
:set ($MessageDups->($MessageVal->"message")) ($DupCount + 1); :set ($MessageDups->($MessageVal->"message")) ($DupCount + 1);
:set Count ($Count + 1); :set Count ($Count + 1);
} }
} }
:if ($Count > 0) do={ :if ($Count > 0) do={
:set LogForwardRateLimit ($LogForwardRateLimit + 10); :set LogForwardRateLimit ($LogForwardRateLimit + 10);
$SendNotification2 ({ origin=$0; \ $SendNotification2 ({ origin=$ScriptName; \
subject=([ $SymbolForNotification [ $IfThenElse ($Warning = true) "warning-sign" "memo" ] ] . \ subject=([ $SymbolForNotification [ $IfThenElse ($Warning = true) "warning-sign" "memo" ] ] . \
"Log Forwarding"); \ "Log Forwarding"); \
message=("The log on " . $Identity . " contains " . [ $IfThenElse ($Count = 1) "this message" \ message=("The log on " . $Identity . " contains " . [ $IfThenElse ($Count = 1) "this message" \
@ -91,6 +93,9 @@ $ScriptLock $0;
"\n" . $Messages) }); "\n" . $Messages) });
:set LogForwardLast ($MessageVal->".id"); :set LogForwardLast ($MessageVal->".id");
} else={ } else={
:set LogForwardRateLimit [ $MAX 0 ($LogForwardRateLimit - 1) ]; :set LogForwardRateLimit [ $MAX 0 ($LogForwardRateLimit - 1) ];
}
} }
$Main [ :jobname ];