2020-09-18 09:00:27 +00:00
|
|
|
#!rsc by RouterOS
|
2020-07-15 10:22:55 +00:00
|
|
|
# RouterOS script: log-forward
|
|
|
|
# Copyright (c) 2020 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
|
|
|
|
|
|
|
|
:global Identity;
|
|
|
|
:global LogForwardFilter;
|
2020-09-25 20:21:59 +00:00
|
|
|
:global LogForwardFilterMessage;
|
2020-07-15 10:22:55 +00:00
|
|
|
:global LogForwardLast;
|
2020-09-28 18:20:19 +00:00
|
|
|
:global LogForwardRateLimit;
|
2020-07-15 10:22:55 +00:00
|
|
|
|
2020-10-18 18:13:58 +00:00
|
|
|
:global IfThenElse;
|
2020-07-15 10:22:55 +00:00
|
|
|
:global LogPrintExit;
|
|
|
|
:global MailServerIsUp;
|
2020-07-17 18:52:52 +00:00
|
|
|
:global ScriptLock;
|
2020-07-15 10:22:55 +00:00
|
|
|
:global SendNotification;
|
2020-07-17 06:07:12 +00:00
|
|
|
:global SymbolForNotification;
|
2020-07-15 10:22:55 +00:00
|
|
|
:global WaitFullyConnected;
|
|
|
|
|
2020-07-17 18:52:52 +00:00
|
|
|
$ScriptLock "log-forward";
|
|
|
|
|
2020-09-28 18:20:19 +00:00
|
|
|
:if ([ :typeof $LogForwardRateLimit ] = "nothing") do={
|
|
|
|
:set LogForwardRateLimit 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
:if ($LogForwardRateLimit > 30) do={
|
|
|
|
:set LogForwardRateLimit ($LogForwardRateLimit - 1);
|
|
|
|
$LogPrintExit info ("Rate limit in action, not forwarding logs!") true;
|
|
|
|
}
|
|
|
|
|
2020-07-15 10:22:55 +00:00
|
|
|
$WaitFullyConnected;
|
|
|
|
|
2020-09-28 17:56:45 +00:00
|
|
|
:if ([ $MailServerIsUp ] = false) do={
|
2020-07-15 10:22:55 +00:00
|
|
|
$LogPrintExit warning ("Mail server is not up.") true;
|
|
|
|
}
|
|
|
|
|
|
|
|
:local Count 0;
|
|
|
|
:local Messages "";
|
|
|
|
:local MessageVal;
|
|
|
|
|
2020-09-25 20:21:59 +00:00
|
|
|
:foreach Message in=[ / log find where !(topics~$LogForwardFilter) !(message~$LogForwardFilterMessage) ] 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 Messages "";
|
|
|
|
:set Count 0;
|
|
|
|
} else={
|
|
|
|
:set Messages ($Messages . "\n" . $MessageVal->"time" . " " . \
|
|
|
|
[ :tostr ($MessageVal->"topics") ] . " " . $MessageVal->"message");
|
|
|
|
:set Count ($Count + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
:if ($Count > 0) do={
|
2020-07-17 06:07:12 +00:00
|
|
|
$SendNotification ([ $SymbolForNotification "warning-sign" ] . "Log Forwarding") \
|
2020-10-18 18:13:58 +00:00
|
|
|
("The log on " . $Identity . " contains " . [ $IfThenElse ($Count = 1) \
|
|
|
|
"this message" ("these " . $Count . " messages") ] . " after " . \
|
|
|
|
[ / system resource get uptime ] . " uptime.\n" . $Messages);
|
2020-07-15 10:22:55 +00:00
|
|
|
|
2020-09-28 18:20:19 +00:00
|
|
|
:set LogForwardRateLimit ($LogForwardRateLimit + 10);
|
2020-07-15 12:43:26 +00:00
|
|
|
:set LogForwardLast ($MessageVal->".id");
|
2020-09-28 18:20:19 +00:00
|
|
|
} else={
|
|
|
|
:if ($LogForwardRateLimit > 0) do={
|
|
|
|
:set LogForwardRateLimit ($LogForwardRateLimit - 1);
|
|
|
|
}
|
2020-07-15 10:22:55 +00:00
|
|
|
}
|