2020-03-04 14:45:37 +00:00
|
|
|
#!rsc
|
|
|
|
# RouterOS script: netwatch-notify
|
|
|
|
# Copyright (c) 2020 Christian Hesse <mail@eworm.de>
|
2020-06-19 20:17:42 +00:00
|
|
|
# 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
|
|
|
|
|
|
|
:global NetwatchNotify;
|
|
|
|
|
|
|
|
:global ParseKeyValueStore;
|
2020-03-05 11:47:42 +00:00
|
|
|
:global LogPrintExit;
|
2020-03-04 14:45:37 +00:00
|
|
|
:global SendNotification;
|
2020-07-17 06:07:12 +00:00
|
|
|
:global SymbolForNotification;
|
2020-03-04 14:45:37 +00:00
|
|
|
|
|
|
|
:if ([ :typeof $NetwatchNotify ] = "nothing") do={
|
|
|
|
:set NetwatchNotify [ :toarray "" ];
|
|
|
|
}
|
|
|
|
|
|
|
|
:foreach Host in=[ / tool netwatch find where comment~"^notify," ] do={
|
|
|
|
:local HostVal [ / tool netwatch get $Host ];
|
2020-07-03 13:19:46 +00:00
|
|
|
:local HostInfo [ $ParseKeyValueStore ($HostVal->"comment") ];
|
|
|
|
:local HostName ($HostInfo->"hostname");
|
2020-03-04 20:01:21 +00:00
|
|
|
|
2020-03-05 11:42:21 +00:00
|
|
|
:local Metric { "count"=0; "notified"=false };
|
|
|
|
:if ([ :typeof ($NetwatchNotify->$HostName) ] = "array") do={
|
|
|
|
:set $Metric ($NetwatchNotify->$HostName);
|
2020-03-04 20:01:21 +00:00
|
|
|
}
|
|
|
|
|
2020-03-04 14:45:37 +00:00
|
|
|
:if ($HostVal->"status" = "up") do={
|
2020-03-05 11:47:42 +00:00
|
|
|
$LogPrintExit debug ("Host " . $HostName . " (" . $HostVal->"host" . ") is up.") false;
|
2020-03-24 11:12:00 +00:00
|
|
|
:local Count ($Metric->"count");
|
2020-03-05 11:42:21 +00:00
|
|
|
:set ($Metric->"count") 0;
|
|
|
|
:if ($Metric->"notified" = true) do={
|
2020-07-17 06:07:12 +00:00
|
|
|
$SendNotification ([ $SymbolForNotification "white-heavy-check-mark" ] . "Netwatch Notify: " . $HostName . " up") \
|
2020-03-24 11:12:00 +00:00
|
|
|
("Host " . $HostName . " (" . $HostVal->"host" . ") is up since " . $HostVal->"since" . ".\n" . \
|
|
|
|
"It was down for " . $Count . " checks.");
|
2020-07-06 13:44:41 +00:00
|
|
|
:if ([ :typeof ($HostInfo->"up-hook") ] = "str") do={
|
|
|
|
$LogPrintExit info ("Running hook on host " . $HostName . " up: " . ($HostInfo->"up-hook")) false;
|
|
|
|
[ :parse ($HostInfo->"up-hook") ];
|
|
|
|
}
|
2020-03-04 14:45:37 +00:00
|
|
|
}
|
2020-03-05 11:42:21 +00:00
|
|
|
:set ($Metric->"notified") false;
|
2020-03-04 14:45:37 +00:00
|
|
|
} else={
|
2020-03-05 11:42:21 +00:00
|
|
|
:set ($Metric->"count") ($Metric->"count" + 1);
|
2020-03-05 11:47:42 +00:00
|
|
|
$LogPrintExit info ("Host " . $HostName . " (" . $HostVal->"host" . ") is down for " . \
|
|
|
|
$Metric->"count" . " checks.") false;
|
2020-03-05 11:42:21 +00:00
|
|
|
:if ($Metric->"count" >= 5 && $Metric->"notified" != true) do={
|
2020-07-17 06:07:12 +00:00
|
|
|
$SendNotification ([ $SymbolForNotification "cross-mark" ] . "Netwatch Notify: " . $HostName . " down") \
|
2020-03-04 14:45:37 +00:00
|
|
|
("Host " . $HostName . " (" . $HostVal->"host" . ") is down since " . $HostVal->"since" . ".");
|
2020-03-05 11:42:21 +00:00
|
|
|
:set ($Metric->"notified") true;
|
2020-07-03 13:19:46 +00:00
|
|
|
:if ([ :typeof ($HostInfo->"down-hook") ] = "str") do={
|
|
|
|
$LogPrintExit info ("Running hook on host " . $HostName . " down: " . ($HostInfo->"down-hook")) false;
|
|
|
|
[ :parse ($HostInfo->"down-hook") ];
|
|
|
|
}
|
2020-03-04 14:45:37 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-05 11:42:21 +00:00
|
|
|
:set ($NetwatchNotify->$HostName) { "count"=($Metric->"count"); "notified"=($Metric->"notified") };
|
2020-03-04 14:45:37 +00:00
|
|
|
}
|