diff options
author | Christian Hesse <mail@eworm.de> | 2020-07-15 12:22:55 +0200 |
---|---|---|
committer | Christian Hesse <mail@eworm.de> | 2020-07-15 12:41:29 +0200 |
commit | 6bce0a4b6a3d1ee48a1e4b4f40652ae490fb288c (patch) | |
tree | 5cff4a2b93eb6d0d305cd8f824cf6d9d63c3c206 | |
parent | 9740b1f26993bafe09834e19a0275036e2f5c22a (diff) |
add 'log-forward', drop 'early-errors'change-23
-rw-r--r-- | README.md | 2 | ||||
-rw-r--r-- | doc/early-errors.md | 39 | ||||
-rw-r--r-- | doc/log-forward.md | 42 | ||||
-rw-r--r-- | early-errors | 26 | ||||
-rw-r--r-- | global-config | 5 | ||||
-rw-r--r-- | global-config-overlay | 2 | ||||
-rw-r--r-- | global-config.changes | 1 | ||||
-rw-r--r-- | global-functions | 2 | ||||
-rw-r--r-- | log-forward | 47 |
9 files changed, 103 insertions, 63 deletions
@@ -159,7 +159,6 @@ Available Scripts * [Use wireless network with daily psk](doc/daily-psk.md) * [Comment DHCP leases with info from access list](doc/dhcp-lease-comment.md) * [Create DNS records for DHCP leases](doc/dhcp-to-dns.md) -* [Send notification with early errors](doc/early-errors.md) * [Send backup via e-mail](doc/email-backup.md) * [Wait for configuration und functions](doc/global-wait.md) * [Send GPS position to server](doc/gps-track.md) @@ -168,6 +167,7 @@ Available Scripts * [Manage IP addresses with bridge status](doc/ip-addr-bridge.md) * [Run other scripts on DHCP lease](doc/lease-script.md) * [Manage LEDs dark mode](doc/leds-mode.md) +* [Forward log messages via notification](doc/log-forward.md) * [Mode botton with multiple presses](doc/mode-button.md) * [Notify on host up and down](doc/netwatch-notify.md) * [Manage remote logging](doc/netwatch-syslog.md) diff --git a/doc/early-errors.md b/doc/early-errors.md index f6209b7..a16da7d 100644 --- a/doc/early-errors.md +++ b/doc/early-errors.md @@ -3,40 +3,9 @@ Send notification with early errors [◀ Go back to main README](../README.md) -Description ------------ +This script has been replace. Please migrate to +[Forward log messages via notification](log-forward.md). -RouterOS supports sending log messages via e-mail or to a syslog server. -However this does not work early after boot if network connectivity is not -yet established. For example log messages about reboot without proper -shutdown may be missed: - -> router rebooted without proper shutdown, probably power outage - -The script collects log messages with severity `error` and sends a -notification. - -Requirements and installation ------------------------------ - -Just install this script and [global-wait](global-wait.md): - - $ScriptInstallUpdate early-errors,global-wait; - -... and add a scheduler: - - / system scheduler add name=early-erros on-event="/ system script { run global-wait; run early-errors; }" start-time=startup; - -Configuration -------------- - -The notifications just require notification settings for e-mail and telegram. - -See also --------- - -* [Wait for configuration und functions](global-wait.md) - ---- -[◀ Go back to main README](../README.md) +--- +[◀ Go back to main README](../README.md) [▲ Go back to top](#top) diff --git a/doc/log-forward.md b/doc/log-forward.md new file mode 100644 index 0000000..bc7213a --- /dev/null +++ b/doc/log-forward.md @@ -0,0 +1,42 @@ +Forward log messages via notification +===================================== + +[◀ Go back to main README](../README.md) + +Description +----------- + +RouterOS supports sending log messages via e-mail or to a syslog server. +This has some limitation, however: + +* does not work early after boot if network connectivity is not + yet established +* lots of messages generate a flood of mails +* Telegram is not supported + +The script is intended to be run periodically. It collects log messages +and forwards them via notification. + +Requirements and installation +----------------------------- + +Just install the script: + + $ScriptInstallUpdate log-forward; + +... and add a scheduler: + + / system scheduler add interval=1m name=log-forward on-event="/ system script run log-forward;" start-time=startup; + +Configuration +------------- + +The configuration goes to `global-config-overlay`, there is just one parameters: + +* `LogForwardFilter`: define topics *not* to be forwarded + +Also notification settings are required for e-mail and telegram. + +--- +[◀ Go back to main README](../README.md) +[▲ Go back to top](#top) diff --git a/early-errors b/early-errors index a2c5da3..4f411f4 100644 --- a/early-errors +++ b/early-errors @@ -1,28 +1,6 @@ #!rsc # RouterOS script: early-errors -# Copyright (c) 2020 Christian Hesse <mail@eworm.de> -# https://git.eworm.de/cgit/routeros-scripts/about/COPYING.md -# -# send notification with early errors -# https://git.eworm.de/cgit/routeros-scripts/about/doc/early-errors.md -:global Identity; +:global LogPrintExit; -:global SendNotification; -:global WaitFullyConnected; - -$WaitFullyConnected; - -:local Errors [ / log find where (topics~"error" or topics~"critical") \ - !(topics~"certificate") !(topics~"dns") !(topics~"e-mail") ]; -:local ErrCount [ :len $Errors ]; -:if ($ErrCount > 0) do={ - :local Message ("The log on " . $Identity . " contains " . $ErrCount . \ - " errors after " . [ / system resource get uptime ] . " uptime.\n"); - :foreach Log in=$Errors do={ - :local LogVal [ / log get $Log ]; - :set Message ($Message . "\n" . [ :tostr ($LogVal->"topics") ] . \ - " " . ($LogVal->"message")); - } - $SendNotification ("\E2\9A\A0 Early errors") ($Message); -} +$LogPrintExit warning ("This script has been replaced. Please migrate to 'log-forward'.") true; diff --git a/global-config b/global-config index 0e4ae63..87ac2c7 100644 --- a/global-config +++ b/global-config @@ -8,7 +8,7 @@ # Make sure all configuration properties are up to date and this # value is in sync with value in script 'global-functions'! -:global GlobalConfigVersion 22; +:global GlobalConfigVersion 23; # This is used for DNS and backup file. :global Domain "example.com"; @@ -42,6 +42,9 @@ :global BackupUploadUser "mikrotik"; :global BackupUploadPass "v3ry-s3cr3t"; +# This defines a filter on log topics not to be forwarded. +:global LogForwardFilter "(debug|info|script)"; + # Specify an address to enable auto update to version assumed safe. # The configured channel (bugfix, current, release-candidate) is appended. :global SafeUpdateUrl ""; diff --git a/global-config-overlay b/global-config-overlay index 22d3ab5..676db65 100644 --- a/global-config-overlay +++ b/global-config-overlay @@ -9,7 +9,7 @@ # Make sure all configuration properties are up to date and this # value is in sync with value in script 'global-functions'! # Comment or remove to disable change notifications. -:global GlobalConfigVersion 22; +:global GlobalConfigVersion 23; # Copy configuration from global-config here and modify it. diff --git a/global-config.changes b/global-config.changes index f6a0c40..d3ff78f 100644 --- a/global-config.changes +++ b/global-config.changes @@ -26,4 +26,5 @@ 20="Added support for hooks to 'netwatch-notify'"; 21="Added support for installing patch updates automatically by 'check-routeros-update'"; 22="Dropped '\$ScriptUpdatesIgnore' from global configuration, auto-migrating to ignore flag in comment" + 23="Added 'log-forward' with configurable filter, which replaces 'early-errors'"; }; diff --git a/global-functions b/global-functions index 2dc7830..fe51031 100644 --- a/global-functions +++ b/global-functions @@ -8,7 +8,7 @@ # https://git.eworm.de/cgit/routeros-scripts/about/ # expected configuration version -:global ExpectedConfigVersion 22; +:global ExpectedConfigVersion 23; # global variables not to be changed by user :global GlobalFunctionsReady false; diff --git a/log-forward b/log-forward new file mode 100644 index 0000000..8f7bc1f --- /dev/null +++ b/log-forward @@ -0,0 +1,47 @@ +#!rsc +# 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; +:global LogForwardLast; + +:global LogPrintExit; +:global MailServerIsUp; +:global SendNotification; +:global WaitFullyConnected; + +$WaitFullyConnected; + +:if ($MailServerIsUp = false) do={ + $LogPrintExit warning ("Mail server is not up.") true; +} + +:local Count 0; +:local Messages ""; +:local MessageVal; + +:foreach Message in=[ / log find where !(topics~$LogForwardFilter) ] do={ + :set MessageVal [ / log get $Message ]; + + :if ($LogForwardLast = $MessageVal) do={ + :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={ + $SendNotification ("\E2\9A\A0 Log Forwarding") \ + ("The log on " . $Identity . " contains these " . $Count . " messages after " . \ + [ / system resource get uptime ] . " uptime.\n" . $Messages); + + :set LogForwardLast $MessageVal; +} |