aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Christian Hesse <mail@eworm.de>2024-02-29 14:21:46 +0100
committerGravatar Christian Hesse <mail@eworm.de>2024-02-29 14:21:46 +0100
commitb9e0ffac1d5bed0d8243e9ee1467336517f8957a (patch)
tree6aa3ae44b4a5a3fa575446e5ff04eeefa90d9056
parent872abbea7d147a8c6291270ba9389731e998b1e0 (diff)
parenta924de274c482b79777d9c1ca9d2e1d1919155cb (diff)
Merge branch 'netwatch-notify' into next
-rw-r--r--doc/netwatch-notify.md6
-rw-r--r--netwatch-notify.rsc42
2 files changed, 38 insertions, 10 deletions
diff --git a/doc/netwatch-notify.md b/doc/netwatch-notify.md
index b673af8..a7cc2d6 100644
--- a/doc/netwatch-notify.md
+++ b/doc/netwatch-notify.md
@@ -83,9 +83,9 @@ with a resolvable name:
/tool/netwatch/add comment="notify, name=example.com, resolve=example.com";
-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.
+This supports multiple A or AAAA records for a name just fine, even a CNAME
+to those. But be warned: CNAME chains to multiple records will cause a high
+rate of configuration changes (and flash writes)!
### No notification on host down
diff --git a/netwatch-notify.rsc b/netwatch-notify.rsc
index ae9c8a8..cd4fc0d 100644
--- a/netwatch-notify.rsc
+++ b/netwatch-notify.rsc
@@ -52,6 +52,28 @@
:return ("Ran hook:\n" . $Hook);
}
+:local ResolveExpected do={
+ :local Name [ :tostr $1 ];
+ :local Expected [ :tostr $2 ];
+
+ :delay 100ms;
+
+ :if ([ :len [ /ip/dns/cache/find where name=$Name data=$Expected ] ] > 0) do={
+ :return true;
+ }
+
+ :local Cname [ /ip/dns/cache/find where name=$Name type="CNAME" ];
+ :if ([ :len $Cname ] > 0) do={
+ :set Cname [ /ip/dns/cache/get $Cname data ];
+ :set Cname [ :pick $Cname 0 ([ :len $Cname ] - 1) ];
+ :if ([ :len [ /ip/dns/cache/find where name=$Cname data=$Expected ] ] > 0) do={
+ :return true;
+ }
+ }
+
+ :return false;
+}
+
$ScriptLock $0;
:local ScriptFromTerminalCached [ $ScriptFromTerminal $0 ];
@@ -80,12 +102,15 @@ $ScriptLock $0;
:do {
:local Resolve [ :resolve ($HostInfo->"resolve") ];
:if ($Resolve != $HostVal->"host") do={
- $LogPrintExit2 info $0 ("Name '" . $HostInfo->"resolve" . [ $IfThenElse \
- ($HostInfo->"resolve" != $HostInfo->"name") ("' for " . $Type . " '" . \
- $HostInfo->"name") "" ] . "' resolves to different address " . $Resolve . \
- ", updating.") false;
- /tool/netwatch/set host=$Resolve $Host;
- :set ($Metric->"resolve-failcnt") 0;
+ :if ([ $ResolveExpected ($HostInfo->"resolve") ($HostVal->"host") ] = false) do={
+ $LogPrintExit2 info $0 ("Name '" . $HostInfo->"resolve" . [ $IfThenElse \
+ ($HostInfo->"resolve" != $HostInfo->"name") ("' for " . $Type . " '" . \
+ $HostInfo->"name") "" ] . "' resolves to different address " . $Resolve . \
+ ", updating.") false;
+ /tool/netwatch/set host=$Resolve $Host;
+ :set ($Metric->"resolve-failcnt") 0;
+ :set ($HostVal->"status") "unknown";
+ }
}
} on-error={
:set ($Metric->"resolve-failcnt") ($Metric->"resolve-failcnt" + 1);
@@ -124,7 +149,9 @@ $ScriptLock $0;
:set ($Metric->"notified") false;
:set ($Metric->"parent") ($HostInfo->"parent");
:set ($Metric->"since");
- } else={
+ }
+
+ :if ($HostVal->"status" = "down") do={
:set ($Metric->"count-down") ($Metric->"count-down" + 1);
:set ($Metric->"count-up") 0;
:set ($Metric->"parent") ($HostInfo->"parent");
@@ -177,6 +204,7 @@ $ScriptLock $0;
:set ($Metric->"notified") true;
}
}
+
:set ($NetwatchNotify->$Name) {
"count-down"=($Metric->"count-down");
"count-up"=($Metric->"count-up");