netwatch-notify: better match notify-entries

This commit is contained in:
Christian Hesse 2022-01-02 20:47:12 +01:00
parent 45ffb7e552
commit 7f0a8cdfcd

View file

@ -50,94 +50,97 @@
$ScriptLock $0; $ScriptLock $0;
:foreach Host in=[ / tool netwatch find where comment~"^notify," disabled=no ] do={ :foreach Host in=[ / tool netwatch find where comment~"notify" disabled=no ] do={
:local HostVal [ / tool netwatch get $Host ]; :local HostVal [ / tool netwatch get $Host ];
:local HostInfo [ $ParseKeyValueStore ($HostVal->"comment") ]; :local HostInfo [ $ParseKeyValueStore ($HostVal->"comment") ];
:local HostName ($HostInfo->"hostname");
:local Metric { "count"=0; "notified"=false }; :if ($HostInfo->"notify" = true && $HostInfo->"disabled" != true) do={
:if ([ :typeof ($NetwatchNotify->$HostName) ] = "array") do={ :local HostName ($HostInfo->"hostname");
:set $Metric ($NetwatchNotify->$HostName);
}
:if ([ :typeof ($HostInfo->"resolve") ] = "str" && [ $DNSIsResolving ] = true) do={ :local Metric { "count"=0; "notified"=false };
:do { :if ([ :typeof ($NetwatchNotify->$HostName) ] = "array") do={
:local Resolve [ :resolve ($HostInfo->"resolve") ]; :set $Metric ($NetwatchNotify->$HostName);
:if ($Resolve != $HostVal->"host") do={ }
$LogPrintExit2 info $0 ("Name '" . $HostInfo->"resolve" . [ $IfThenElse ($HostInfo->"resolve" != \
$HostInfo->"hostname") ("' for host '" . $HostInfo->"hostname") "" ] . \ :if ([ :typeof ($HostInfo->"resolve") ] = "str" && [ $DNSIsResolving ] = true) do={
"' resolves to different address " . $Resolve . ", updating.") false; :do {
/ tool netwatch set host=$Resolve $Host; :local Resolve [ :resolve ($HostInfo->"resolve") ];
:set ($Metric->"resolve-failed") false; :if ($Resolve != $HostVal->"host") do={
} $LogPrintExit2 info $0 ("Name '" . $HostInfo->"resolve" . [ $IfThenElse ($HostInfo->"resolve" != \
} on-error={ $HostInfo->"hostname") ("' for host '" . $HostInfo->"hostname") "" ] . \
:if ($Metric->"resolve-failed" != true) do={ "' resolves to different address " . $Resolve . ", updating.") false;
$LogPrintExit2 warning $0 ("Resolving name '" . $HostInfo->"resolve" . [ $IfThenElse ($HostInfo->"resolve" != \ / tool netwatch set host=$Resolve $Host;
$HostInfo->"hostname") ("' for host '" . $HostInfo->"hostname") "" ] . "' failed.") false; :set ($Metric->"resolve-failed") false;
:set ($Metric->"resolve-failed") true; }
} 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 ($HostVal->"status" = "up") do={ :if ($HostVal->"status" = "up") do={
:local Count ($Metric->"count"); :local Count ($Metric->"count");
:if ($Count > 0) do={ :if ($Count > 0) do={
$LogPrintExit2 info $0 ("Host " . $HostName . " (" . $HostVal->"host" . ") is up.") false; $LogPrintExit2 info $0 ("Host " . $HostName . " (" . $HostVal->"host" . ") is up.") false;
:set ($Metric->"count") 0; :set ($Metric->"count") 0;
}
:if ($Metric->"notified" = true) do={
:local Message ("Host " . $HostName . " (" . $HostVal->"host" . ") is up since " . $HostVal->"since" . ".\n" . \
"It was down for " . $Count . " checks since " . ($Metric->"since") . ".");
:if ([ :typeof ($HostInfo->"up-hook") ] = "str") do={
:set Message ($Message . "\n\n" . [ $NetwatchNotifyHook $HostName "up" ($HostInfo->"up-hook") ]);
} }
$SendNotification2 ({ origin=$0; \ :if ($Metric->"notified" = true) do={
subject=([ $SymbolForNotification "white-heavy-check-mark" ] . "Netwatch Notify: " . $HostName . " up"); \ :local Message ("Host " . $HostName . " (" . $HostVal->"host" . ") is up since " . $HostVal->"since" . ".\n" . \
message=$Message }); "It was down for " . $Count . " checks since " . ($Metric->"since") . ".");
} :if ([ :typeof ($HostInfo->"up-hook") ] = "str") do={
:set ($Metric->"notified") false; :set Message ($Message . "\n\n" . [ $NetwatchNotifyHook $HostName "up" ($HostInfo->"up-hook") ]);
:set ($Metric->"parent") ($HostInfo->"parent"); }
:set ($Metric->"since"); $SendNotification2 ({ origin=$0; \
} else={ subject=([ $SymbolForNotification "white-heavy-check-mark" ] . "Netwatch Notify: " . $HostName . " up"); \
:set ($Metric->"count") ($Metric->"count" + 1); message=$Message });
:set ($Metric->"parent") ($HostInfo->"parent"); }
:set ($Metric->"since") ($HostVal->"since"); :set ($Metric->"notified") false;
:local Count [ $IfThenElse ([ :tonum ($HostInfo->"count") ] > 0) ($HostInfo->"count") 5 ]; :set ($Metric->"parent") ($HostInfo->"parent");
:local Parent ($HostInfo->"parent"); :set ($Metric->"since");
:while ([ :len $Parent ] > 0) do={ } else={
:set Count ($Count + 1); :set ($Metric->"count") ($Metric->"count" + 1);
:set Parent ($NetwatchNotify->$Parent->"parent"); :set ($Metric->"parent") ($HostInfo->"parent");
} :set ($Metric->"since") ($HostVal->"since");
:set Parent ($HostInfo->"parent"); :local Count [ $IfThenElse ([ :tonum ($HostInfo->"count") ] > 0) ($HostInfo->"count") 5 ];
:local ParentNotified false; :local Parent ($HostInfo->"parent");
:while ($ParentNotified = false && [ :len $Parent ] > 0) do={ :while ([ :len $Parent ] > 0) do={
:set ParentNotified [ $IfThenElse (($NetwatchNotify->$Parent->"notified") = true) true false ]; :set Count ($Count + 1);
:if ($ParentNotified = false) do={
:set Parent ($NetwatchNotify->$Parent->"parent"); :set Parent ($NetwatchNotify->$Parent->"parent");
} }
} :set Parent ($HostInfo->"parent");
$LogPrintExit2 info $0 ("Host " . $HostName . " (" . $HostVal->"host" . ") is down for " . \ :local ParentNotified false;
$Metric->"count" . " checks, " . [ $IfThenElse ($ParentNotified = false) [ $IfThenElse \ :while ($ParentNotified = false && [ :len $Parent ] > 0) do={
($Metric->"notified" = true) ("already notified.") ($Count - $Metric->"count" . " to go.") ] \ :set ParentNotified [ $IfThenElse (($NetwatchNotify->$Parent->"notified") = true) true false ];
("parent host " . $Parent . " is down.") ]) false; :if ($ParentNotified = false) do={
:if ((($Count * 2) - ($Metric->"count" * 3)) / 2 = 0 && [ :typeof ($HostInfo->"pre-down-hook") ] = "str") do={ :set Parent ($NetwatchNotify->$Parent->"parent");
$NetwatchNotifyHook $HostName "pre-down" ($HostInfo->"pre-down-hook"); }
} }
:if ($ParentNotified = false && $Metric->"count" >= $Count && $Metric->"notified" != true) do={ $LogPrintExit2 info $0 ("Host " . $HostName . " (" . $HostVal->"host" . ") is down for " . \
:local Message ("Host " . $HostName . " (" . $HostVal->"host" . ") is down since " . $HostVal->"since" . "."); $Metric->"count" . " checks, " . [ $IfThenElse ($ParentNotified = false) [ $IfThenElse \
:if ([ :typeof ($HostInfo->"down-hook") ] = "str") do={ ($Metric->"notified" = true) ("already notified.") ($Count - $Metric->"count" . " to go.") ] \
:set Message ($Message . "\n\n" . [ $NetwatchNotifyHook $HostName "down" ($HostInfo->"down-hook") ]); ("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={
:set Message ($Message . "\n\n" . [ $NetwatchNotifyHook $HostName "down" ($HostInfo->"down-hook") ]);
}
$SendNotification2 ({ origin=$0; \
subject=([ $SymbolForNotification "cross-mark" ] . "Netwatch Notify: " . $HostName . " down"); \
message=$Message });
:set ($Metric->"notified") true;
} }
$SendNotification2 ({ origin=$0; \
subject=([ $SymbolForNotification "cross-mark" ] . "Netwatch Notify: " . $HostName . " down"); \
message=$Message });
:set ($Metric->"notified") true;
} }
:set ($NetwatchNotify->$HostName) {
"count"=($Metric->"count");
"notified"=($Metric->"notified");
"parent"=($Metric->"parent");
"resolve-failed"=($Metric->"resolve-failed");
"since"=($Metric->"since") };
} }
:set ($NetwatchNotify->$HostName) {
"count"=($Metric->"count");
"notified"=($Metric->"notified");
"parent"=($Metric->"parent");
"resolve-failed"=($Metric->"resolve-failed");
"since"=($Metric->"since") };
} }