routeros-scripts/log-forward

91 lines
3 KiB
Text
Raw Normal View History

#!rsc by RouterOS
2020-07-15 10:22:55 +00:00
# RouterOS script: log-forward
2021-01-01 20:33:52 +00:00
# Copyright (c) 2020-2021 Christian Hesse <mail@eworm.de>
2020-07-15 10:22:55 +00:00
# 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; }
2020-07-15 10:22:55 +00:00
:global Identity;
:global LogForwardFilter;
:global LogForwardFilterMessage;
:global LogForwardInclude;
:global LogForwardIncludeMessage;
2020-07-15 10:22:55 +00:00
:global LogForwardLast;
:global LogForwardRateLimit;
:global NotificationsWithSymbols;
2020-07-15 10:22:55 +00:00
2021-03-17 07:55:33 +00:00
:global EscapeForRegEx;
:global IfThenElse;
:global LogPrintExit2;
:global QuotedPrintable;
:global ScriptLock;
2021-04-27 18:45:51 +00:00
:global SendNotification2;
:global SymbolForNotification;
2020-07-15 10:22:55 +00:00
:global WaitFullyConnected;
2021-02-22 18:52:20 +00:00
$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;
}
2020-07-15 10:22:55 +00:00
$WaitFullyConnected;
:local Count 0;
:local Duplicates false;
2020-07-15 10:22:55 +00:00
:local Messages "";
:local MessageVal;
:local MessageDups [ :toarray "" ];
2020-07-15 10:22:55 +00:00
2021-03-17 07:55:33 +00:00
:local LogForwardFilterLogForwarding ("^" . [ $EscapeForRegEx ("Error sending e-mail <" . \
[ $QuotedPrintable ("[" . $Identity . "] " . [ $SymbolForNotification "warning-sign" ] . \
"Log Forwarding") ] . ">:") ]);
:foreach Message in=[ / log find where (!(message="") and !(message~$LogForwardFilterLogForwarding) and \
!(topics~$LogForwardFilter) and !(message~$LogForwardFilterMessage)) or \
topics~$LogForwardInclude or message~$LogForwardIncludeMessage ] do={
2020-07-15 10:22:55 +00:00
:set MessageVal [ / log get $Message ];
2020-07-15 12:43:26 +00:00
:if ($LogForwardLast = ($MessageVal->".id")) do={
2020-07-15 10:22:55 +00:00
:set Count 0;
:set Duplicates false;
:set Messages "";
:set MessageDups [ :toarray "" ];
2020-07-15 10:22:55 +00:00
} else={
:local DupCount ($MessageDups->($MessageVal->"message"));
: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);
2020-07-15 10:22:55 +00:00
:set Count ($Count + 1);
}
}
:if ($Count > 0) do={
$SendNotification2 ({ origin=$0; \
subject=([ $SymbolForNotification "warning-sign" ] . "Log Forwarding"); \
2021-04-27 18:45:51 +00:00
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.") ] . "\n" . $Messages) });
2020-07-15 10:22:55 +00:00
:set LogForwardRateLimit ($LogForwardRateLimit + 10);
2020-07-15 12:43:26 +00:00
:set LogForwardLast ($MessageVal->".id");
} else={
:if ($LogForwardRateLimit > 0) do={
:set LogForwardRateLimit ($LogForwardRateLimit - 1);
}
2020-07-15 10:22:55 +00:00
}