netwatch-notify: implement pre-down hook

This commit is contained in:
Christian Hesse 2021-07-09 11:27:26 +02:00
parent 12d34e4a7c
commit bccb7c3452
6 changed files with 11 additions and 3 deletions

View file

@ -44,6 +44,10 @@ comment:
/ tool netwatch add comment="notify, hostname=poe-device, down-hook=/ interface ethernet poe power-cycle en21;" host=10.0.0.20;
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.
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;

View file

@ -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 59;
:global GlobalConfigVersion 60;
# This is used for DNS and backup file.
:global Domain "example.com";

View file

@ -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 59;
:global GlobalConfigVersion 60;
# Copy configuration from global-config here and modify it.

View file

@ -63,6 +63,7 @@
57="Celebrating the 1.000th commit - Hooray!";
58="Added a cleanup script for 'hotspot-to-wpa' to purge old access list entries.";
59="Updating CAP with 'check-routeros-update' is now possible with opt-in.";
60="Implemented a pre-down hook in 'netwatch-notify' that fires at two thirds of failed checks.";
};
# Migration steps to be applied on script updates

View file

@ -8,7 +8,7 @@
# https://git.eworm.de/cgit/routeros-scripts/about/
# expected configuration version
:global ExpectedConfigVersion 59;
:global ExpectedConfigVersion 60;
# global variables not to be changed by user
:global GlobalFunctionsReady false;

View file

@ -117,6 +117,9 @@
$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");
}
:if ($ParentNotified = false && $Metric->"count" >= $Count && $Metric->"notified" != true) do={
:local Message ("Host " . $HostName . " (" . $HostVal->"host" . ") is down since " . $HostVal->"since" . ".");
:if ([ :typeof ($HostInfo->"down-hook") ] = "str") do={