From 7d5c9679952339c8064dc34967c13d0c7779ae87 Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Fri, 14 Jan 2022 07:34:50 +0100 Subject: [PATCH 01/14] global-config: add $ScriptUpdatesUrlSuffix in overlay for RouterOS v6 --- global-config-overlay | 3 +++ 1 file changed, 3 insertions(+) diff --git a/global-config-overlay b/global-config-overlay index 37f78cfb..63034c9c 100644 --- a/global-config-overlay +++ b/global-config-overlay @@ -10,6 +10,9 @@ # Comment or remove to disable news and change notifications. :global GlobalConfigVersion 74; +# Use branch routeros-v6 with RouterOS v6: +:global ScriptUpdatesUrlSuffix "\?h=routeros-v6"; + # Copy configuration from global-config here and modify it. From ee9818e34dbdfeba3104857a54c8ec1bc6aba4ce Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Fri, 14 Jan 2022 08:55:07 +0100 Subject: [PATCH 02/14] global: send notification on 'routeros-v6' This is mainly to keep the version in 'routeros-v6' and 'main' in sync... --- global-config | 2 +- global-config-overlay | 2 +- global-config.changes | 1 + global-functions | 2 +- 4 files changed, 4 insertions(+), 3 deletions(-) diff --git a/global-config b/global-config index 024e52f5..75fbba41 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 74; +:global GlobalConfigVersion 75; # This is used for DNS and backup file. :global Domain "example.com"; diff --git a/global-config-overlay b/global-config-overlay index 63034c9c..c443bead 100644 --- a/global-config-overlay +++ b/global-config-overlay @@ -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'! # Comment or remove to disable news and change notifications. -:global GlobalConfigVersion 74; +:global GlobalConfigVersion 75; # Use branch routeros-v6 with RouterOS v6: :global ScriptUpdatesUrlSuffix "\?h=routeros-v6"; diff --git a/global-config.changes b/global-config.changes index a070ecd2..6f0e2f72 100644 --- a/global-config.changes +++ b/global-config.changes @@ -78,6 +78,7 @@ 72="Introduced new script 'netwatch-dns' to manage DNS and DoH servers from netwatch."; 73="Renamed backup scripts ('cloud-backup' -> 'backup-cloud', 'email-backup' -> 'backup-email', 'upload-backup' -> 'backup-upload')."; 74="Extended 'hotspot-to-wpa', it can now read additional configuration from templates and hotspot users."; + 75="You are using the branch 'routeros-v6', well done."; }; # Migration steps to be applied on script updates diff --git a/global-functions b/global-functions index 625bbe4f..9579f5c7 100644 --- a/global-functions +++ b/global-functions @@ -8,7 +8,7 @@ # https://git.eworm.de/cgit/routeros-scripts/about/ # expected configuration version -:global ExpectedConfigVersion 74; +:global ExpectedConfigVersion 75; # global variables not to be changed by user :global GlobalFunctionsReady false; From 25338ca384ddd41c4b9d3ad2a937228562043235 Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Mon, 17 Jan 2022 20:15:24 +0100 Subject: [PATCH 03/14] global-functions: $DownloadPackage: give url in debug output (cherry picked from commit 51cd11c80380eac0bfde4649cb30747c198881b7) --- global-functions | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/global-functions b/global-functions index 9579f5c7..40cd271a 100644 --- a/global-functions +++ b/global-functions @@ -278,13 +278,13 @@ $LogPrintExit2 error $0 ("Downloading required certificate failed.") true; } + :local Url ("https://upgrade.mikrotik.com/routeros/" . $PkgVer . "/" . $PkgFile); $LogPrintExit2 info $0 ("Downloading package file '" . $PkgName . "'...") false; + $LogPrintExit2 debug $0 ("... from url: " . $Url) false; :local Retry 3; :while ($Retry > 0) do={ :do { - / tool fetch check-certificate=yes-without-crl \ - ("https://upgrade.mikrotik.com/routeros/" . $PkgVer . "/" . $PkgFile) \ - dst-path=$PkgDest; + / tool fetch check-certificate=yes-without-crl $Url dst-path=$PkgDest; $WaitForFile $PkgDest; :if ([ / file get [ find where name=$PkgDest ] type ] = "package") do={ From 5363df356875325420a62273f00df1ea404d2aa4 Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Mon, 17 Jan 2022 20:50:52 +0100 Subject: [PATCH 04/14] global-functions: $DownloadPackage: handle special case with name For RouterOS 6.x bundled package version and architecture are swapped. Closes: #21 (cherry picked from commit 1e6931c8e34d5035cb07baa98b57eccbcd4278ab) --- global-functions | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/global-functions b/global-functions index 40cd271a..c6d311f9 100644 --- a/global-functions +++ b/global-functions @@ -252,6 +252,7 @@ :global CleanFilePath; :global LogPrintExit2; :global MkDir; + :global VersionToNum; :global WaitForFile; :if ([ :len $PkgName ] = 0) do={ :return false; } @@ -259,6 +260,9 @@ :if ([ :len $PkgArch ] = 0) do={ :set PkgArch [ / system resource get architecture-name ]; } :local PkgFile ($PkgName . "-" . $PkgVer . "-" . $PkgArch . ".npk"); + :if ([ $VersionToNum $PkgVer ] < [ $VersionToNum "7.0" ] && $PkgName = "routeros") do={ + :set PkgFile ($PkgName . "-" . $PkgArch . "-" . $PkgVer . ".npk"); + } :if ($PkgArch = "x86_64" || $PkgName ~ "^routeros-") do={ :set PkgFile ($PkgName . "-" . $PkgVer . ".npk"); } From 9024e20c0b576d7d22c28ead3c6461e228bfda49 Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Thu, 20 Jan 2022 22:05:05 +0100 Subject: [PATCH 05/14] global-functions: $ScriptInstallUpdate: give final url in debug output (cherry picked from commit b4a5d824a268fae9f06ccf61fff4f67c760773ed) --- global-functions | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/global-functions b/global-functions index c6d311f9..fc70cc2e 100644 --- a/global-functions +++ b/global-functions @@ -731,15 +731,15 @@ :if ([ :len $SourceNew ] = 0 && $ScriptUpdatesFetch = true) do={ :local Comment [ $ParseKeyValueStore ($ScriptVal->"comment") ]; :if (!($Comment->"ignore" = true)) do={ - $LogPrintExit2 debug $0 ("Fetching script from url: " . $ScriptVal->"name") false; :do { :local BaseUrl $ScriptUpdatesBaseUrl; :local UrlSuffix $ScriptUpdatesUrlSuffix; :if ([ :typeof ($Comment->"base-url") ] = "str") do={ :set BaseUrl ($Comment->"base-url"); } :if ([ :typeof ($Comment->"url-suffix") ] = "str") do={ :set UrlSuffix ($Comment->"url-suffix"); } + :local Url ($BaseUrl . $ScriptVal->"name" . $UrlSuffix); - :local Result [ / tool fetch check-certificate=yes-without-crl \ - ($BaseUrl . $ScriptVal->"name" . $UrlSuffix) output=user as-value ]; + $LogPrintExit2 debug $0 ("Fetching script '" . $ScriptVal->"name" . "' from url: " . $Url) false; + :local Result [ / tool fetch check-certificate=yes-without-crl $Url output=user as-value ]; :if ($Result->"status" = "finished") do={ :set SourceNew ($Result->"data"); } @@ -804,11 +804,10 @@ :global GlobalConfigMigration; :local ChangeLogCode; - $LogPrintExit2 debug $0 ("Fetching news, changes and migration.") false; :do { - :local Result [ / tool fetch check-certificate=yes-without-crl \ - ($ScriptUpdatesBaseUrl . "global-config.changes" . $ScriptUpdatesUrlSuffix) \ - output=user as-value ]; + :local Url ($ScriptUpdatesBaseUrl . "global-config.changes" . $ScriptUpdatesUrlSuffix); + $LogPrintExit2 debug $0 ("Fetching news, changes and migration: " . $Url) false; + :local Result [ / tool fetch check-certificate=yes-without-crl $Url output=user as-value ]; :if ($Result->"status" = "finished") do={ :set ChangeLogCode ($Result->"data"); } From 0a9d0473f37b0091ff115ac7d47d93a521c37c7a Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Thu, 20 Jan 2022 22:11:30 +0100 Subject: [PATCH 06/14] global-functions: $ScriptInstallUpdate: quote names in log output (cherry picked from commit 5e32105e7ea4bd357f9826d1ca24ca6e2c455bdc) --- global-functions | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/global-functions b/global-functions index fc70cc2e..70f10778 100644 --- a/global-functions +++ b/global-functions @@ -723,8 +723,8 @@ :foreach Scheduler in=[ / system scheduler find where on-event~("\\b" . $ScriptVal->"name" . "\\b") ] do={ :local SchedulerVal [ / system scheduler get $Scheduler ]; :if ($ScriptVal->"policy" != $SchedulerVal->"policy") do={ - $LogPrintExit2 warning $0 ("Policies differ for script " . $ScriptVal->"name" . \ - " and its scheduler " . $SchedulerVal->"name" . "!") false; + $LogPrintExit2 warning $0 ("Policies differ for script '" . $ScriptVal->"name" . \ + "' and its scheduler '" . $SchedulerVal->"name" . "'!") false; } } @@ -744,7 +744,7 @@ :set SourceNew ($Result->"data"); } } on-error={ - $LogPrintExit2 warning $0 ("Failed fetching " . $ScriptVal->"name") false; + $LogPrintExit2 warning $0 ("Failed fetching script '" . $ScriptVal->"name" . "'!") false; } } } @@ -765,18 +765,18 @@ :set ReloadGlobalFunctions true; } } else={ - $LogPrintExit2 warning $0 ("Syntax validation for script " . $ScriptVal->"name" . \ - " failed! Ignoring!") false; + $LogPrintExit2 warning $0 ("Syntax validation for script '" . $ScriptVal->"name" . \ + "' failed! Ignoring!") false; } } else={ - $LogPrintExit2 warning $0 ("Looks like new script " . $ScriptVal->"name" . \ - " is not valid (missing shebang). Ignoring!") false; + $LogPrintExit2 warning $0 ("Looks like new script '" . $ScriptVal->"name" . \ + "' is not valid (missing shebang). Ignoring!") false; } } else={ - $LogPrintExit2 debug $0 ("Script " . $ScriptVal->"name" . " did not change.") false; + $LogPrintExit2 debug $0 ("Script '" . $ScriptVal->"name" . "' did not change.") false; } } else={ - $LogPrintExit2 debug $0 ("No update for script " . $ScriptVal->"name" . ".") false; + $LogPrintExit2 debug $0 ("No update for script '" . $ScriptVal->"name" . "'.") false; } } From 221b0409d5198c13c11b0dddb730bd0bf5752efe Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Sun, 30 Jan 2022 22:05:58 +0100 Subject: [PATCH 07/14] check-routeros-update: use correct syntax (cherry picked from commit dfe995be270f1ee9621454e7feada84c33168db0) --- check-routeros-update | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/check-routeros-update b/check-routeros-update index 0f687a5e..8cad07ab 100644 --- a/check-routeros-update +++ b/check-routeros-update @@ -108,7 +108,7 @@ $LogPrintExit2 debug $0 ("Checking for updates...") false; :if ([ $ScriptFromTerminal $0 ] = true) do={ :put ("Do you want to install RouterOS version " . $Update->"latest-version" . "? [y/N]"); - :if (([ :terminal inkey timeout=60 ] % 32) = 25) do={ + :if (([ / terminal inkey timeout=60 ] % 32) = 25) do={ $DoUpdate; } else={ :put "Canceled..."; From 52bd08b750b16b03494af7f7bdd66225fa82995a Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Sun, 30 Jan 2022 22:06:37 +0100 Subject: [PATCH 08/14] packages-update: use correct syntax (cherry picked from commit 27a81bcbca52d513c5b9c10578474a1863bd08af) --- packages-update | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages-update b/packages-update index b6b05246..2e6d8be0 100644 --- a/packages-update +++ b/packages-update @@ -35,7 +35,7 @@ $ScriptLock $0; :if ($NumInstalled > $NumLatest) do={ :if ([ $ScriptFromTerminal $0 ] = true) do={ :put "Latest version is older than installed one. Want to downgrade? [y/N]"; - :if (([ :terminal inkey timeout=60 ] % 32) = 25) do={ + :if (([ / terminal inkey timeout=60 ] % 32) = 25) do={ :set DoDowngrade true; } else={ :put "Canceled..."; @@ -61,7 +61,7 @@ $ScriptLock $0; $LogPrintExit2 warning $0 ("Running backup script " . $ScriptName . " before update failed!") false; :if ([ $ScriptFromTerminal $0 ] = true) do={ :put "Do you want to continue anyway? [y/N]"; - :if (([ :terminal inkey timeout=60 ] % 32) = 25) do={ + :if (([ / terminal inkey timeout=60 ] % 32) = 25) do={ $LogPrintExit2 info $0 ("User requested to continue anyway.") false; } else={ $LogPrintExit2 info $0 ("Canceled update...") true; @@ -80,7 +80,7 @@ $ScriptLock $0; :if ([ $ScriptFromTerminal $0 ] = true) do={ :put "Do you want to (s)chedule reboot or (r)eboot now? [s/R]"; - :if (([ :terminal inkey timeout=60 ] % 32) = 19) do={ + :if (([ / terminal inkey timeout=60 ] % 32) = 19) do={ / system scheduler add name="reboot-for-update" start-time=03:00:00 interval=1d \ on-event=(":global RandomDelay; \$RandomDelay 3600; " . \ "/ system scheduler remove reboot-for-update; / system reboot;"); From b2f45c03501b8f1ff446ec770c05fefcfa36d0ef Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Thu, 3 Feb 2022 14:29:37 +0100 Subject: [PATCH 09/14] doc/packages-update: link backup-cloud (cherry picked from commit dd19aea3628471ce54fefa30913879322f20e63a) --- doc/packages-update.md | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/packages-update.md b/doc/packages-update.md index ac693016..309a3941 100644 --- a/doc/packages-update.md +++ b/doc/packages-update.md @@ -41,6 +41,7 @@ See also -------- * [Notify on RouterOS update](check-routeros-update.md) +* [Upload backup to Mikrotik cloud](backup-cloud.md) * [Send backup via e-mail](backup-email.md) * [Upload backup to server](backup-upload.md) From 8d49babb0114ad55ec3094f7d9cec2f1bfea43be Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Tue, 8 Feb 2022 17:39:53 +0100 Subject: [PATCH 10/14] doc/netwatch-notify: add sections (cherry picked from commit 42c203291a7db814be00f40caddb6980632358fa) --- doc/netwatch-notify.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/doc/netwatch-notify.md b/doc/netwatch-notify.md index 78a0eedf..9b504f7f 100644 --- a/doc/netwatch-notify.md +++ b/doc/netwatch-notify.md @@ -38,6 +38,8 @@ The hosts to be checked have to be added to netwatch with specific comment: / tool netwatch add comment="notify, hostname=example.com" host=[ :resolve "example.com" ]; +### Hooks + It is possible to run an up hook command (`up-hook`) or down hook command (`down-hook`) when a notification is triggered. This has to be added in comment, note that some characters need extra escaping: @@ -48,10 +50,14 @@ Also there is a `pre-down-hook` that fires at two thirds of failed checks required for the notification. The idea is to fix the issue before a notification is sent. +### Count threshould + The count threshould (default is 5 checks) is configurable as well: / tool netwatch add comment="notify, hostname=example.com, count=10" host=104.18.144.11; +### Parents & dependencies + If the host is behind another checked host add a dependency, this will suppress notification if the parent host is down: @@ -61,6 +67,8 @@ suppress notification if the parent host is down: Note that every configured parent in a chain increases the check count threshould by one. +### Update from DNS + The host address can be updated dynamically. Give extra parameter `resolve` with a resolvable name: From c03da561cdf7cf6f47de79772749ef459f6ee495 Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Thu, 3 Feb 2022 13:51:40 +0100 Subject: [PATCH 11/14] netwatch-notify: allow to suppress notification on host down (cherry picked from commit c6e581d4f90c9bea0adf405685aa634e264d1f3b) --- doc/netwatch-notify.md | 10 ++++++++++ global-config | 2 +- global-config-overlay | 2 +- global-config.changes | 1 + global-functions | 2 +- netwatch-notify | 16 +++++++++------- 6 files changed, 23 insertions(+), 10 deletions(-) diff --git a/doc/netwatch-notify.md b/doc/netwatch-notify.md index 9b504f7f..dc107f23 100644 --- a/doc/netwatch-notify.md +++ b/doc/netwatch-notify.md @@ -78,6 +78,16 @@ But be warned: Dynamic updates will probably cause issues if the name has more than one record in dns - a high rate of configuration changes (and flash writes) at least. +### No notification on host down + +Also suppressing the notification on host down is possible with parameter +`no-down-notification`. This may be desired for devices that are usually +powered off, but accessibility is of interest. + + / tool netwatch add comment="notify, hostname=printer, no-down-notification" host=10.0.0.30; + +Go and get your coffee ☕️ before sending the print job. + Also notification settings are required for e-mail, matrix and/or telegram. Tips & Tricks diff --git a/global-config b/global-config index 75fbba41..5f0f4dbf 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 75; +:global GlobalConfigVersion 76; # This is used for DNS and backup file. :global Domain "example.com"; diff --git a/global-config-overlay b/global-config-overlay index c443bead..dfa2b99c 100644 --- a/global-config-overlay +++ b/global-config-overlay @@ -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'! # Comment or remove to disable news and change notifications. -:global GlobalConfigVersion 75; +:global GlobalConfigVersion 76; # Use branch routeros-v6 with RouterOS v6: :global ScriptUpdatesUrlSuffix "\?h=routeros-v6"; diff --git a/global-config.changes b/global-config.changes index 6f0e2f72..37aa0741 100644 --- a/global-config.changes +++ b/global-config.changes @@ -79,6 +79,7 @@ 73="Renamed backup scripts ('cloud-backup' -> 'backup-cloud', 'email-backup' -> 'backup-email', 'upload-backup' -> 'backup-upload')."; 74="Extended 'hotspot-to-wpa', it can now read additional configuration from templates and hotspot users."; 75="You are using the branch 'routeros-v6', well done."; + 76="Added an option to suppress notifications on host down with 'netwatch-notify'."; }; # Migration steps to be applied on script updates diff --git a/global-functions b/global-functions index 70f10778..68d82504 100644 --- a/global-functions +++ b/global-functions @@ -8,7 +8,7 @@ # https://git.eworm.de/cgit/routeros-scripts/about/ # expected configuration version -:global ExpectedConfigVersion 75; +:global ExpectedConfigVersion 76; # global variables not to be changed by user :global GlobalFunctionsReady false; diff --git a/netwatch-notify b/netwatch-notify index ae89cfb2..e82cea69 100644 --- a/netwatch-notify +++ b/netwatch-notify @@ -122,10 +122,10 @@ $ScriptLock $0; :set Parent ($NetwatchNotify->$Parent->"parent"); } } - $LogPrintExit2 info $0 ("Host " . $HostName . " (" . $HostVal->"host" . ") is down for " . \ - $Metric->"count" . " checks, " . [ $IfThenElse ($ParentNotified = false) [ $IfThenElse \ - ($Metric->"notified" = true) ("already notified.") ($Count - $Metric->"count" . " to go.") ] \ - ("parent host " . $Parent . " is down.") ]) false; + $LogPrintExit2 [ $IfThenElse ($HostInfo->"no-down-notification" != true) info debug ] $0 \ + ("Host " . $HostName . " (" . $HostVal->"host" . ") is down for " . $Metric->"count" . " checks, " . \ + [ $IfThenElse ($ParentNotified = false) [ $IfThenElse ($Metric->"notified" = true) ("already notified.") \ + ($Count - $Metric->"count" . " to go.") ] ("parent host " . $Parent . " is down.") ]) false; :if ((($Count * 2) - ($Metric->"count" * 3)) / 2 = 0 && [ :typeof ($HostInfo->"pre-down-hook") ] = "str") do={ $NetwatchNotifyHook $HostName "pre-down" ($HostInfo->"pre-down-hook"); } @@ -134,9 +134,11 @@ $ScriptLock $0; :if ([ :typeof ($HostInfo->"down-hook") ] = "str") do={ :set Message ($Message . "\n\n" . [ $NetwatchNotifyHook $HostName "down" ($HostInfo->"down-hook") ]); } - $SendNotification2 ({ origin=$0; \ - subject=([ $SymbolForNotification "cross-mark" ] . "Netwatch Notify: " . $HostName . " down"); \ - message=$Message }); + :if ($HostInfo->"no-down-notification" != true) do={ + $SendNotification2 ({ origin=$0; \ + subject=([ $SymbolForNotification "cross-mark" ] . "Netwatch Notify: " . $HostName . " down"); \ + message=$Message }); + } :set ($Metric->"notified") true; } } From 0c607a8f9ce7ed9d33df775f03b0292787d9326f Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Mon, 7 Feb 2022 21:46:10 +0100 Subject: [PATCH 12/14] netwatch-notify: nest conditions The logic here was right, but RouterOS runs the checks simultaneously. This caused delays even if no resolving was needed. Nesting the checks fixes this. (cherry picked from commit 0b46c508dc8f76955dd528900882b54c07b62ef3) --- netwatch-notify | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/netwatch-notify b/netwatch-notify index e82cea69..34a9e8fe 100644 --- a/netwatch-notify +++ b/netwatch-notify @@ -66,21 +66,23 @@ $ScriptLock $0; :set $Metric ($NetwatchNotify->$HostName); } - :if ([ :typeof ($HostInfo->"resolve") ] = "str" && [ $DNSIsResolving ] = true) do={ - :do { - :local Resolve [ :resolve ($HostInfo->"resolve") ]; - :if ($Resolve != $HostVal->"host") do={ - $LogPrintExit2 info $0 ("Name '" . $HostInfo->"resolve" . [ $IfThenElse ($HostInfo->"resolve" != \ - $HostInfo->"hostname") ("' for host '" . $HostInfo->"hostname") "" ] . \ - "' resolves to different address " . $Resolve . ", updating.") false; - / tool netwatch set host=$Resolve $Host; - :set ($Metric->"resolve-failed") false; - } - } on-error={ - :if ($Metric->"resolve-failed" != true) do={ - $LogPrintExit2 warning $0 ("Resolving name '" . $HostInfo->"resolve" . [ $IfThenElse ($HostInfo->"resolve" != \ - $HostInfo->"hostname") ("' for host '" . $HostInfo->"hostname") "" ] . "' failed.") false; - :set ($Metric->"resolve-failed") true; + :if ([ :typeof ($HostInfo->"resolve") ] = "str") do={ + :if ([ $DNSIsResolving ] = true) do={ + :do { + :local Resolve [ :resolve ($HostInfo->"resolve") ]; + :if ($Resolve != $HostVal->"host") do={ + $LogPrintExit2 info $0 ("Name '" . $HostInfo->"resolve" . [ $IfThenElse ($HostInfo->"resolve" != \ + $HostInfo->"hostname") ("' for host '" . $HostInfo->"hostname") "" ] . \ + "' resolves to different address " . $Resolve . ", updating.") false; + / tool netwatch set host=$Resolve $Host; + :set ($Metric->"resolve-failed") false; + } + } on-error={ + :if ($Metric->"resolve-failed" != true) do={ + $LogPrintExit2 warning $0 ("Resolving name '" . $HostInfo->"resolve" . [ $IfThenElse ($HostInfo->"resolve" != \ + $HostInfo->"hostname") ("' for host '" . $HostInfo->"hostname") "" ] . "' failed.") false; + :set ($Metric->"resolve-failed") true; + } } } } From c95e8161485ac47aa3c4f65efe99f335cf330d46 Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Mon, 7 Feb 2022 09:58:53 +0100 Subject: [PATCH 13/14] netwatch-dns: flush cache on configuration change (cherry picked from commit a47f5723cc4cdd287d36aaffd7619c17494f9345) --- netwatch-dns | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/netwatch-dns b/netwatch-dns index e731d953..e2fcfa31 100644 --- a/netwatch-dns +++ b/netwatch-dns @@ -43,12 +43,14 @@ $ScriptLock $0; :if ($DnsServers != $DnsCurrent) do={ $LogPrintExit2 info $0 ("Updating DNS servers: " . [ :tostr $DnsServers ]) false; / ip dns set servers=$DnsServers; + / ip dns cache flush; } } else={ :if ([ :len $DnsFallback ] > 0) do={ :if ($DnsFallback != $DnsCurrent) do={ $LogPrintExit2 info $0 ("Updating DNS servers to fallback: " . [ :tostr $DnsFallback ]) false; / ip dns set servers=$DnsFallback; + / ip dns cache flush; } } } @@ -69,10 +71,12 @@ $ScriptLock $0; :if ($DohServer != $DohCurrent) do={ $LogPrintExit2 info $0 ("Updating DoH server: " . $DohServer) false; / ip dns set use-doh-server=$DohServer; + / ip dns cache flush; } } else={ :if ($DohCurrent != "") do={ $LogPrintExit2 info $0 ("DoH server (" . $DohCurrent . ") is down, disabling.") false; / ip dns set use-doh-server=""; + / ip dns cache flush; } } From 32cc55f7d2570e80ab270ae6e5ce97e3c1bc7644 Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Wed, 2 Feb 2022 21:10:10 +0100 Subject: [PATCH 14/14] introduce firmware-upgrade-reboot (cherry picked from commit 3c358980cb416c61419a0aaa384ab1db1dab8d29) --- README.md | 1 + doc/check-routeros-update.md | 1 + doc/firmware-upgrade-reboot.md | 36 ++++++++++++++++++++++++++++++++++ doc/packages-update.md | 1 + firmware-upgrade-reboot | 32 ++++++++++++++++++++++++++++++ global-config | 2 +- global-config-overlay | 2 +- global-config.changes | 1 + global-functions | 2 +- 9 files changed, 75 insertions(+), 3 deletions(-) create mode 100644 doc/firmware-upgrade-reboot.md create mode 100644 firmware-upgrade-reboot diff --git a/README.md b/README.md index 990aef9d..70d3ff5b 100644 --- a/README.md +++ b/README.md @@ -204,6 +204,7 @@ 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) +* [Automatically upgrade firmware and reboot](doc/firmware-upgrade-reboot.md) * [Wait for global functions und modules](doc/global-wait.md) * [Send GPS position to server](doc/gps-track.md) * [Use WPA2 network with hotspot credentials](doc/hotspot-to-wpa.md) diff --git a/doc/check-routeros-update.md b/doc/check-routeros-update.md index 9385341d..d55fa089 100644 --- a/doc/check-routeros-update.md +++ b/doc/check-routeros-update.md @@ -61,6 +61,7 @@ Installing script [packages-update](packages-update.md) gives extra options. See also -------- +* [Automatically upgrade firmware and reboot](firmware-upgrade-reboot.md) * [Manage system update](packages-update.md) --- diff --git a/doc/firmware-upgrade-reboot.md b/doc/firmware-upgrade-reboot.md new file mode 100644 index 00000000..6afc40e3 --- /dev/null +++ b/doc/firmware-upgrade-reboot.md @@ -0,0 +1,36 @@ +Automatically upgrade firmware and reboot +========================================= + +[◀ Go back to main README](../README.md) + +🛈 This script can not be used on its own but requires the base installation. +See [main README](../README.md) for details. + +Description +----------- + +RouterOS and firmware are upgraded separately, activating the latter +requires an extra reboot. This script handles upgrade and reboot. + +⚠️ **Warning**: This *should* be bullet proof, but I can not guarantee. In +worst case it has potential to cause a boot loop, so handle with care! + +Requirements and installation +----------------------------- + +Just install the script and create a scheduler: + + $ScriptInstallUpdate firmware-upgrade-reboot; + / system scheduler add name=firmware-upgrade-reboot on-event="/ system script run firmware-upgrade-reboot;" start-time=startup; + +Enjoy firmware being up to date and in sync with RouterOS. + +See also +-------- + +* [Notify on RouterOS update](check-routeros-update.md) +* [Manage system update](packages-update.md) + +--- +[◀ Go back to main README](../README.md) +[▲ Go back to top](#top) diff --git a/doc/packages-update.md b/doc/packages-update.md index 309a3941..a56f68ef 100644 --- a/doc/packages-update.md +++ b/doc/packages-update.md @@ -44,6 +44,7 @@ See also * [Upload backup to Mikrotik cloud](backup-cloud.md) * [Send backup via e-mail](backup-email.md) * [Upload backup to server](backup-upload.md) +* [Automatically upgrade firmware and reboot](firmware-upgrade-reboot.md) --- [◀ Go back to main README](../README.md) diff --git a/firmware-upgrade-reboot b/firmware-upgrade-reboot new file mode 100644 index 00000000..b3cb6214 --- /dev/null +++ b/firmware-upgrade-reboot @@ -0,0 +1,32 @@ +#!rsc by RouterOS +# RouterOS script: firmware-upgrade-reboot +# Copyright (c) 2022 Christian Hesse +# https://git.eworm.de/cgit/routeros-scripts/about/COPYING.md +# +# install firmware upgrade, and reboot +# https://git.eworm.de/cgit/routeros-scripts/about/doc/firmware-upgrade-reboot.md + +:local 0 "firmware-upgrade-reboot"; +:global GlobalFunctionsReady; +:while ($GlobalFunctionsReady != true) do={ :delay 500ms; } + +:global LogPrintExit2; + +:local RouterBoard [ / system routerboard get ]; +:if ($RouterBoard->"current-firmware" = $RouterBoard->"upgrade-firmware") do={ + $LogPrintExit2 info $0 ("Firmware is already up to date.") true; +} + +:if ([ / system routerboard settings get auto-upgrade ] = false) do={ + $LogPrintExit2 info $0 ("Firmware version " . $RouterBoard->"upgrade-firmware" . \ + " is available, upgrading.") false; + / system routerboard upgrade; +} + +:while ([ :len [ / log find where topics=({"system";"info";"critical"}) \ + message="Firmware upgraded successfully, please reboot for changes to take effect!" ] ] = 0) do={ + :delay 1s; +} + +$LogPrintExit2 info $0 ("Firmware upgrade successful, rebooting.") false; +/ system reboot; diff --git a/global-config b/global-config index 5f0f4dbf..0554cdb7 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 76; +:global GlobalConfigVersion 77; # This is used for DNS and backup file. :global Domain "example.com"; diff --git a/global-config-overlay b/global-config-overlay index dfa2b99c..9075df50 100644 --- a/global-config-overlay +++ b/global-config-overlay @@ -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'! # Comment or remove to disable news and change notifications. -:global GlobalConfigVersion 76; +:global GlobalConfigVersion 77; # Use branch routeros-v6 with RouterOS v6: :global ScriptUpdatesUrlSuffix "\?h=routeros-v6"; diff --git a/global-config.changes b/global-config.changes index 37aa0741..d8cd39d8 100644 --- a/global-config.changes +++ b/global-config.changes @@ -80,6 +80,7 @@ 74="Extended 'hotspot-to-wpa', it can now read additional configuration from templates and hotspot users."; 75="You are using the branch 'routeros-v6', well done."; 76="Added an option to suppress notifications on host down with 'netwatch-notify'."; + 77="Introduced new script 'firmware-upgrade-reboot'. Handle with care!"; }; # Migration steps to be applied on script updates diff --git a/global-functions b/global-functions index 68d82504..0a0b1ffa 100644 --- a/global-functions +++ b/global-functions @@ -8,7 +8,7 @@ # https://git.eworm.de/cgit/routeros-scripts/about/ # expected configuration version -:global ExpectedConfigVersion 76; +:global ExpectedConfigVersion 77; # global variables not to be changed by user :global GlobalFunctionsReady false;