mirror of
https://git.eworm.de/cgit/routeros-scripts
synced 2025-07-28 06:44:41 +02:00
log-forward: move code into function
This commit is contained in:
parent
49650d8b14
commit
50e1c45880
1 changed files with 77 additions and 72 deletions
149
log-forward.rsc
149
log-forward.rsc
|
@ -8,89 +8,94 @@
|
||||||
# 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;
|
||||||
:set LogForwardRateLimit 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
:if ($LogForwardRateLimit > 30) do={
|
:if ([ :typeof $LogForwardRateLimit ] = "nothing") do={
|
||||||
:set LogForwardRateLimit ($LogForwardRateLimit - 1);
|
:set LogForwardRateLimit 0;
|
||||||
$LogPrintExit2 info $0 ("Rate limit in action, not forwarding logs, if any!") true;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
:local Count 0;
|
:if ($LogForwardRateLimit > 30) do={
|
||||||
:local Duplicates false;
|
:set LogForwardRateLimit ($LogForwardRateLimit - 1);
|
||||||
:local Last [ $IfThenElse ([ :len $LogForwardLast ] > 0) [ $HexToNum $LogForwardLast ] -1 ];
|
$LogPrintExit2 info $ScriptName ("Rate limit in action, not forwarding logs, if any!") true;
|
||||||
:local Messages "";
|
}
|
||||||
:local Warning false;
|
|
||||||
:local MessageVal;
|
|
||||||
:local MessageDups ({});
|
|
||||||
|
|
||||||
:local LogForwardFilterLogForwardingCached [ $EitherOr [ $LogForwardFilterLogForwarding ] ("\$^") ];
|
:local Count 0;
|
||||||
:foreach Message in=[ /log/find where (!(message="") and \
|
:local Duplicates false;
|
||||||
!(message~$LogForwardFilterLogForwardingCached) and \
|
:local Last [ $IfThenElse ([ :len $LogForwardLast ] > 0) [ $HexToNum $LogForwardLast ] -1 ];
|
||||||
!(topics~$LogForwardFilter) and !(message~$LogForwardFilterMessage)) or \
|
:local Messages "";
|
||||||
topics~$LogForwardInclude or message~$LogForwardIncludeMessage ] do={
|
:local Warning false;
|
||||||
:set MessageVal [ /log/get $Message ];
|
:local MessageVal;
|
||||||
:local Bullet "information";
|
:local MessageDups ({});
|
||||||
|
|
||||||
:if ($Last < [ $HexToNum ($MessageVal->".id") ]) do={
|
:local LogForwardFilterLogForwardingCached [ $EitherOr [ $LogForwardFilterLogForwarding ] ("\$^") ];
|
||||||
:local DupCount ($MessageDups->($MessageVal->"message"));
|
:foreach Message in=[ /log/find where (!(message="") and \
|
||||||
:if ($MessageVal->"topics" ~ "(warning)") do={
|
!(message~$LogForwardFilterLogForwardingCached) and \
|
||||||
:set Warning true;
|
!(topics~$LogForwardFilter) and !(message~$LogForwardFilterMessage)) or \
|
||||||
:set Bullet "large-orange-circle";
|
topics~$LogForwardInclude or message~$LogForwardIncludeMessage ] do={
|
||||||
|
:set MessageVal [ /log/get $Message ];
|
||||||
|
:local Bullet "information";
|
||||||
|
|
||||||
|
:if ($Last < [ $HexToNum ($MessageVal->".id") ]) do={
|
||||||
|
:local DupCount ($MessageDups->($MessageVal->"message"));
|
||||||
|
:if ($MessageVal->"topics" ~ "(warning)") do={
|
||||||
|
:set Warning true;
|
||||||
|
:set Bullet "large-orange-circle";
|
||||||
|
}
|
||||||
|
:if ($MessageVal->"topics" ~ "(emergency|alert|critical|error)") do={
|
||||||
|
:set Warning true;
|
||||||
|
:set Bullet "large-red-circle";
|
||||||
|
}
|
||||||
|
:if ($DupCount < 3) do={
|
||||||
|
:set Messages ($Messages . "\n" . [ $SymbolForNotification $Bullet ] . \
|
||||||
|
$MessageVal->"time" . " " . [ :tostr ($MessageVal->"topics") ] . " " . $MessageVal->"message");
|
||||||
|
} else={
|
||||||
|
:set Duplicates true;
|
||||||
|
}
|
||||||
|
:set ($MessageDups->($MessageVal->"message")) ($DupCount + 1);
|
||||||
|
:set Count ($Count + 1);
|
||||||
}
|
}
|
||||||
:if ($MessageVal->"topics" ~ "(emergency|alert|critical|error)") do={
|
}
|
||||||
:set Warning true;
|
|
||||||
:set Bullet "large-red-circle";
|
:if ($Count > 0) do={
|
||||||
}
|
:set LogForwardRateLimit ($LogForwardRateLimit + 10);
|
||||||
:if ($DupCount < 3) do={
|
|
||||||
:set Messages ($Messages . "\n" . [ $SymbolForNotification $Bullet ] . \
|
$SendNotification2 ({ origin=$ScriptName; \
|
||||||
$MessageVal->"time" . " " . [ :tostr ($MessageVal->"topics") ] . " " . $MessageVal->"message");
|
subject=([ $SymbolForNotification [ $IfThenElse ($Warning = true) "warning-sign" "memo" ] ] . \
|
||||||
} else={
|
"Log Forwarding"); \
|
||||||
:set Duplicates true;
|
message=("The log on " . $Identity . " contains " . [ $IfThenElse ($Count = 1) "this message" \
|
||||||
}
|
("these " . $Count . " messages") ] . " after " . [ /system/resource/get uptime ] . " uptime." . \
|
||||||
:set ($MessageDups->($MessageVal->"message")) ($DupCount + 1);
|
[ $IfThenElse ($Duplicates = true) (" Multi-repeated messages have been skipped.") ] . \
|
||||||
:set Count ($Count + 1);
|
[ $IfThenElse ($LogForwardRateLimit > 30) ("\nRate limit in action, delaying forwarding.") ] . \
|
||||||
|
"\n" . $Messages) });
|
||||||
|
|
||||||
|
:set LogForwardLast ($MessageVal->".id");
|
||||||
|
} else={
|
||||||
|
:set LogForwardRateLimit [ $MAX 0 ($LogForwardRateLimit - 1) ];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
:if ($Count > 0) do={
|
$Main [ :jobname ];
|
||||||
:set LogForwardRateLimit ($LogForwardRateLimit + 10);
|
|
||||||
|
|
||||||
$SendNotification2 ({ origin=$0; \
|
|
||||||
subject=([ $SymbolForNotification [ $IfThenElse ($Warning = true) "warning-sign" "memo" ] ] . \
|
|
||||||
"Log Forwarding"); \
|
|
||||||
message=("The log on " . $Identity . " contains " . [ $IfThenElse ($Count = 1) "this message" \
|
|
||||||
("these " . $Count . " messages") ] . " after " . [ /system/resource/get uptime ] . " uptime." . \
|
|
||||||
[ $IfThenElse ($Duplicates = true) (" Multi-repeated messages have been skipped.") ] . \
|
|
||||||
[ $IfThenElse ($LogForwardRateLimit > 30) ("\nRate limit in action, delaying forwarding.") ] . \
|
|
||||||
"\n" . $Messages) });
|
|
||||||
|
|
||||||
:set LogForwardLast ($MessageVal->".id");
|
|
||||||
} else={
|
|
||||||
:set LogForwardRateLimit [ $MAX 0 ($LogForwardRateLimit - 1) ];
|
|
||||||
}
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue