aboutsummaryrefslogtreecommitdiffstats
path: root/log-forward.rsc
diff options
context:
space:
mode:
authorGravatar Christian Hesse <mail@eworm.de>2023-03-07 10:57:42 +0100
committerGravatar Christian Hesse <mail@eworm.de>2023-03-07 22:26:01 +0100
commita832fd04ef085ef7e85000843a8e8fa59ce36dfb (patch)
treec977ca4b5167d8aa07dbe6bfe15fca8f467db0d9 /log-forward.rsc
parent1e6e0646e256c4acb9f8470853d0699286af010e (diff)
rename scripts and add file extension ".rsc"change-95
No functional change for the user... The migration is done automatically.
Diffstat (limited to 'log-forward.rsc')
-rw-r--r--log-forward.rsc90
1 files changed, 90 insertions, 0 deletions
diff --git a/log-forward.rsc b/log-forward.rsc
new file mode 100644
index 0000000..96cb257
--- /dev/null
+++ b/log-forward.rsc
@@ -0,0 +1,90 @@
+#!rsc by RouterOS
+# RouterOS script: log-forward
+# Copyright (c) 2020-2023 Christian Hesse <mail@eworm.de>
+# https://git.eworm.de/cgit/routeros-scripts/about/COPYING.md
+#
+# forward log messages via notification
+# https://git.eworm.de/cgit/routeros-scripts/about/doc/log-forward.md
+
+:local 0 "log-forward";
+:global GlobalFunctionsReady;
+:while ($GlobalFunctionsReady != true) do={ :delay 500ms; }
+
+:global Identity;
+:global LogForwardFilter;
+:global LogForwardFilterMessage;
+:global LogForwardInclude;
+:global LogForwardIncludeMessage;
+:global LogForwardLast;
+:global LogForwardRateLimit;
+:global NotificationsWithSymbols;
+
+:global EitherOr;
+:global HexToNum;
+:global IfThenElse;
+:global LogForwardFilterLogForwarding;
+:global LogPrintExit2;
+:global ScriptLock;
+:global SendNotification2;
+:global SymbolForNotification;
+
+$ScriptLock $0;
+
+:if ([ :typeof $LogForwardRateLimit ] = "nothing") do={
+ :set LogForwardRateLimit 0;
+}
+
+:if ($LogForwardRateLimit > 30) do={
+ :set LogForwardRateLimit ($LogForwardRateLimit - 1);
+ $LogPrintExit2 info $0 ("Rate limit in action, not forwarding logs, if any!") true;
+}
+
+:local Count 0;
+:local Duplicates false;
+:local Last [ $IfThenElse ([ :len $LogForwardLast ] > 0) [ $HexToNum $LogForwardLast ] -1 ];
+:local Messages "";
+:local Warning false;
+:local MessageVal;
+:local MessageDups ({});
+
+:local LogForwardFilterLogForwardingCached [ $EitherOr [ $LogForwardFilterLogForwarding ] ("\$^") ];
+:foreach Message in=[ /log/find where (!(message="") and \
+ !(message~$LogForwardFilterLogForwardingCached) and \
+ !(topics~$LogForwardFilter) and !(message~$LogForwardFilterMessage)) or \
+ topics~$LogForwardInclude or message~$LogForwardIncludeMessage ] do={
+ :set MessageVal [ /log/get $Message ];
+
+ :if ($Last < [ $HexToNum ($MessageVal->".id") ]) do={
+ :local DupCount ($MessageDups->($MessageVal->"message"));
+ :if ($MessageVal->"topics" ~ "(emergency|alert|critical|error|warning)") do={
+ :set Warning true;
+ }
+ :if ($DupCount < 3) do={
+ :set Messages ($Messages . "\n" . [ $IfThenElse ($NotificationsWithSymbols = true) (" \E2\97\8F ") ] . \
+ $MessageVal->"time" . " " . [ :tostr ($MessageVal->"topics") ] . " " . $MessageVal->"message");
+ } else={
+ :set Duplicates true;
+ }
+ :set ($MessageDups->($MessageVal->"message")) ($DupCount + 1);
+ :set Count ($Count + 1);
+ }
+}
+
+:if ($Count > 0) do={
+ :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={
+ :if ($LogForwardRateLimit > 0) do={
+ :set LogForwardRateLimit ($LogForwardRateLimit - 1);
+ }
+}