routeros-scripts/netwatch-notify

176 lines
7 KiB
Text
Raw Normal View History

#!rsc by RouterOS
2020-03-04 14:45:37 +00:00
# RouterOS script: netwatch-notify
2022-01-01 20:38:15 +00:00
# Copyright (c) 2020-2022 Christian Hesse <mail@eworm.de>
# https://git.eworm.de/cgit/routeros-scripts/about/COPYING.md
2020-03-04 14:45:37 +00:00
#
# monitor netwatch and send notifications
2020-03-27 20:48:52 +00:00
# https://git.eworm.de/cgit/routeros-scripts/about/doc/netwatch-notify.md
2020-03-04 14:45:37 +00:00
:local 0 "netwatch-notify";
:global GlobalFunctionsReady;
:while ($GlobalFunctionsReady != true) do={ :delay 500ms; }
2020-03-04 14:45:37 +00:00
:global NetwatchNotify;
:global EitherOr;
:global IfThenElse;
:global IsDNSResolving;
:global LogPrintExit2;
:global ParseKeyValueStore;
2021-08-27 09:20:57 +00:00
:global ScriptLock;
:global SendNotification2;
:global SymbolForNotification;
:local NetwatchNotifyHook do={
:local Name [ :tostr $1 ];
:local Type [ :tostr $2 ];
:local State [ :tostr $3 ];
:local Hook [ :tostr $4 ];
:global LogPrintExit2;
:global ValidateSyntax;
:if ([ $ValidateSyntax $Hook ] = true) do={
:do {
[ :parse $Hook ];
} on-error={
2022-07-06 09:36:27 +00:00
$LogPrintExit2 warning $0 ("The " . $State . "-hook for " . $Type . " '" . $Name . \
"' failed to run.") false;
:return ("The hook failed to run.");
}
} else={
2022-07-06 09:36:27 +00:00
$LogPrintExit2 warning $0 ("The " . $State . "-hook for " . $Type . " '" . $Name . \
"' failed syntax validation.") false;
:return ("The hook failed syntax validation.");
}
2022-07-06 09:36:27 +00:00
$LogPrintExit2 info $0 ("Ran hook on " . $Type . " '" . $Name . "' " . $State . ": " . \
$Hook) false;
:return ("Ran hook:\n" . $Hook);
}
2020-03-04 14:45:37 +00:00
2022-01-02 20:40:40 +00:00
$ScriptLock $0;
:if ([ /system/resource/get uptime ] < 5m) do={
$LogPrintExit2 info $0 ("System just booted, giving netwatch some time to settle.") true;
}
2020-03-04 14:45:37 +00:00
:if ([ :typeof $NetwatchNotify ] = "nothing") do={
:set NetwatchNotify ({});
2020-03-04 14:45:37 +00:00
}
:foreach Host in=[ /tool/netwatch/find where comment~"notify" !disabled ] do={
:local HostVal [ /tool/netwatch/get $Host ];
:local Type [ $IfThenElse ($HostVal->"type" ~ "^(http-get|tcp-conn)\$") "service" "host" ];
:local HostInfo [ $ParseKeyValueStore ($HostVal->"comment") ];
:local HostDetails ($HostVal->"host" . \
[ $IfThenElse ([ :len ($HostInfo->"resolve") ] > 0) (", " . $HostInfo->"resolve") ]);
:if ($HostInfo->"notify" = true && $HostInfo->"disabled" != true) do={
:local Name [ $EitherOr ($HostInfo->"name") ($HostVal->"name") ];
:local Metric { "count"=0; "notified"=false };
:if ([ :typeof ($NetwatchNotify->$Name) ] = "array") do={
:set $Metric ($NetwatchNotify->$Name);
}
:if ([ :typeof ($HostInfo->"resolve") ] = "str") do={
:if ([ $IsDNSResolving ] = true) do={
:do {
:local Resolve [ :resolve ($HostInfo->"resolve") ];
:if ($Resolve != $HostVal->"host") do={
2022-07-06 09:36:27 +00:00
$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-failed") false;
}
} on-error={
:if ($Metric->"resolve-failed" != true) do={
2022-07-06 09:36:27 +00:00
$LogPrintExit2 warning $0 ("Resolving name '" . $HostInfo->"resolve" . [ $IfThenElse \
($HostInfo->"resolve" != $HostInfo->"name") ("' for " . $Type . " '" . \
$HostInfo->"name") "" ] . "' failed.") false;
:set ($Metric->"resolve-failed") true;
}
}
}
}
:if ($HostVal->"status" = "up") do={
2022-09-07 19:54:14 +00:00
:local CountDown ($Metric->"count");
:if ($CountDown > 0) do={
$LogPrintExit2 info $0 \
("The " . $Type . " '" . $Name . "' (" . $HostDetails . ") is up.") false;
:set ($Metric->"count") 0;
}
:if ($Metric->"notified" = true) do={
:local Message ("The " . $Type . " '" . $Name . "' (" . $HostDetails . \
2022-07-06 09:36:27 +00:00
") is up since " . $HostVal->"since" . ".\n" . \
2022-09-07 19:54:14 +00:00
"It was down for " . $CountDown . " checks since " . ($Metric->"since") . ".");
:if ([ :typeof ($HostInfo->"up-hook") ] = "str") do={
2022-07-06 09:36:27 +00:00
:set Message ($Message . "\n\n" . [ $NetwatchNotifyHook $Name $Type "up" \
($HostInfo->"up-hook") ]);
}
$SendNotification2 ({ origin=$0; \
2022-07-06 09:36:27 +00:00
subject=([ $SymbolForNotification "white-heavy-check-mark" ] . "Netwatch Notify: " . \
$Name . " up"); \
message=$Message });
}
:set ($Metric->"notified") false;
:set ($Metric->"parent") ($HostInfo->"parent");
:set ($Metric->"since");
} else={
:set ($Metric->"count") ($Metric->"count" + 1);
:set ($Metric->"parent") ($HostInfo->"parent");
:set ($Metric->"since") ($HostVal->"since");
2022-09-07 19:54:14 +00:00
:local CountDown [ $IfThenElse ([ :tonum ($HostInfo->"count") ] > 0) ($HostInfo->"count") 5 ];
:local Parent ($HostInfo->"parent");
:while ([ :len $Parent ] > 0) do={
2022-09-07 19:54:14 +00:00
:set CountDown ($CountDown + 1);
:set Parent ($NetwatchNotify->$Parent->"parent");
}
:set Parent ($HostInfo->"parent");
:local ParentNotified false;
:while ($ParentNotified = false && [ :len $Parent ] > 0) do={
2022-07-06 09:36:27 +00:00
:set ParentNotified [ $IfThenElse (($NetwatchNotify->$Parent->"notified") = true) \
true false ];
:if ($ParentNotified = false) do={
:set Parent ($NetwatchNotify->$Parent->"parent");
}
}
$LogPrintExit2 [ $IfThenElse ($HostInfo->"no-down-notification" != true) info debug ] $0 \
("The " . $Type . " '" . $Name . "' (" . $HostDetails . ") is down for " . \
2022-07-06 09:36:27 +00:00
$Metric->"count" . " checks, " . [ $IfThenElse ($ParentNotified = false) [ $IfThenElse \
2022-09-07 19:54:14 +00:00
($Metric->"notified" = true) ("already notified.") ($CountDown - $Metric->"count" . \
2022-07-06 09:36:27 +00:00
" to go.") ] ("parent " . $Type . " " . $Parent . " is down.") ]) false;
2022-09-07 19:54:14 +00:00
:if ((($CountDown * 2) - ($Metric->"count" * 3)) / 2 = 0 && \
2022-07-06 09:36:27 +00:00
[ :typeof ($HostInfo->"pre-down-hook") ] = "str") do={
$NetwatchNotifyHook $Name $Type "pre-down" ($HostInfo->"pre-down-hook");
}
2022-09-07 19:54:14 +00:00
:if ($ParentNotified = false && $Metric->"count" >= $CountDown && \
2022-07-06 09:36:27 +00:00
$Metric->"notified" != true) do={
:local Message ("The " . $Type . " '" . $Name . "' (" . $HostDetails . \
2022-07-06 09:36:27 +00:00
") is down since " . $HostVal->"since" . ".");
:if ([ :typeof ($HostInfo->"down-hook") ] = "str") do={
2022-07-06 09:36:27 +00:00
:set Message ($Message . "\n\n" . [ $NetwatchNotifyHook $Name $Type "down" \
($HostInfo->"down-hook") ]);
}
:if ($HostInfo->"no-down-notification" != true) do={
$SendNotification2 ({ origin=$0; \
2022-07-06 09:36:27 +00:00
subject=([ $SymbolForNotification "cross-mark" ] . "Netwatch Notify: " . \
$Name . " down"); \
message=$Message });
}
:set ($Metric->"notified") true;
}
2020-03-04 14:45:37 +00:00
}
:set ($NetwatchNotify->$Name) {
"count"=($Metric->"count");
"notified"=($Metric->"notified");
"parent"=($Metric->"parent");
"resolve-failed"=($Metric->"resolve-failed");
"since"=($Metric->"since") };
2020-03-04 14:45:37 +00:00
}
}