routeros-scripts/log-forward
Christian Hesse ee57ddf595 log-forward: fix forwarding first message
Pulling the power cable results in log message on next boot:

dec/16 18:28:28 system,error,critical router rebooted without proper shutdown, probably power outage

This was not forwarded as it had the numeric id 0, which is not greater
than the zero we initialized with. Now initialized with -1 when no log
has been forwarded to fix this.
2021-12-17 11:48:25 +01:00

87 lines
3 KiB
Text

#!rsc by RouterOS
# RouterOS script: log-forward
# Copyright (c) 2020-2021 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 EscapeForRegEx;
:global HexToNum;
:global IfThenElse;
:global LogPrintExit2;
:global QuotedPrintable;
:global ScriptLock;
:global SendNotification2;
:global SymbolForNotification;
:global WaitFullyConnected;
$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;
}
$WaitFullyConnected;
:local Count 0;
:local Duplicates false;
:local Last [ $IfThenElse ([ :len $LogForwardLast ] > 0) [ $HexToNum $LogForwardLast ] -1 ];
:local Messages "";
:local MessageVal;
:local MessageDups [ :toarray "" ];
: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={
:set MessageVal [ / log get $Message ];
:if ($Last < [ $HexToNum ($MessageVal->".id") ]) do={
: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);
:set Count ($Count + 1);
}
}
:if ($Count > 0) do={
$SendNotification2 ({ origin=$0; \
subject=([ $SymbolForNotification "warning-sign" ] . "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.") ] . "\n" . $Messages) });
:set LogForwardRateLimit ($LogForwardRateLimit + 10);
:set LogForwardLast ($MessageVal->".id");
} else={
:if ($LogForwardRateLimit > 0) do={
:set LogForwardRateLimit ($LogForwardRateLimit - 1);
}
}