2020-03-04 15:45:37 +01:00
|
|
|
#!rsc
|
|
|
|
# RouterOS script: netwatch-notify
|
|
|
|
# Copyright (c) 2020 Christian Hesse <mail@eworm.de>
|
2020-06-19 22:17:42 +02:00
|
|
|
# https://git.eworm.de/cgit/routeros-scripts/about/COPYING.md
|
2020-03-04 15:45:37 +01:00
|
|
|
#
|
|
|
|
# monitor netwatch and send notifications
|
2020-03-27 21:48:52 +01:00
|
|
|
# https://git.eworm.de/cgit/routeros-scripts/about/doc/netwatch-notify.md
|
2020-03-04 15:45:37 +01:00
|
|
|
|
|
|
|
:global NetwatchNotify;
|
|
|
|
|
2020-08-24 14:02:40 +02:00
|
|
|
:global IfThenElse;
|
2020-03-05 12:47:42 +01:00
|
|
|
:global LogPrintExit;
|
2020-08-24 14:02:40 +02:00
|
|
|
:global ParseKeyValueStore;
|
2020-03-04 15:45:37 +01:00
|
|
|
:global SendNotification;
|
2020-07-17 08:07:12 +02:00
|
|
|
:global SymbolForNotification;
|
2020-03-04 15:45:37 +01:00
|
|
|
|
|
|
|
:if ([ :typeof $NetwatchNotify ] = "nothing") do={
|
|
|
|
:set NetwatchNotify [ :toarray "" ];
|
|
|
|
}
|
|
|
|
|
2020-07-18 00:01:51 +02:00
|
|
|
:foreach Host in=[ / tool netwatch find where comment~"^notify," disabled=no ] do={
|
2020-03-04 15:45:37 +01:00
|
|
|
:local HostVal [ / tool netwatch get $Host ];
|
2020-07-03 15:19:46 +02:00
|
|
|
:local HostInfo [ $ParseKeyValueStore ($HostVal->"comment") ];
|
|
|
|
:local HostName ($HostInfo->"hostname");
|
2020-03-04 21:01:21 +01:00
|
|
|
|
2020-03-05 12:42:21 +01:00
|
|
|
:local Metric { "count"=0; "notified"=false };
|
|
|
|
:if ([ :typeof ($NetwatchNotify->$HostName) ] = "array") do={
|
|
|
|
:set $Metric ($NetwatchNotify->$HostName);
|
2020-03-04 21:01:21 +01:00
|
|
|
}
|
|
|
|
|
2020-03-04 15:45:37 +01:00
|
|
|
:if ($HostVal->"status" = "up") do={
|
2020-03-05 12:47:42 +01:00
|
|
|
$LogPrintExit debug ("Host " . $HostName . " (" . $HostVal->"host" . ") is up.") false;
|
2020-03-24 12:12:00 +01:00
|
|
|
:local Count ($Metric->"count");
|
2020-03-05 12:42:21 +01:00
|
|
|
:set ($Metric->"count") 0;
|
|
|
|
:if ($Metric->"notified" = true) do={
|
2020-07-17 08:07:12 +02:00
|
|
|
$SendNotification ([ $SymbolForNotification "white-heavy-check-mark" ] . "Netwatch Notify: " . $HostName . " up") \
|
2020-03-24 12:12:00 +01:00
|
|
|
("Host " . $HostName . " (" . $HostVal->"host" . ") is up since " . $HostVal->"since" . ".\n" . \
|
|
|
|
"It was down for " . $Count . " checks.");
|
2020-07-06 15:44:41 +02: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 15:45:37 +01:00
|
|
|
}
|
2020-03-05 12:42:21 +01:00
|
|
|
:set ($Metric->"notified") false;
|
2020-03-04 15:45:37 +01:00
|
|
|
} else={
|
2020-03-05 12:42:21 +01:00
|
|
|
:set ($Metric->"count") ($Metric->"count" + 1);
|
2020-03-05 12:47:42 +01:00
|
|
|
$LogPrintExit info ("Host " . $HostName . " (" . $HostVal->"host" . ") is down for " . \
|
|
|
|
$Metric->"count" . " checks.") false;
|
2020-08-24 14:02:40 +02:00
|
|
|
:if ($Metric->"count" >= [ $IfThenElse ([ :typeof ($HostVal->"count") ] != "nothing") ($HostVal->"count") 5 ] && \
|
|
|
|
$Metric->"notified" != true) do={
|
2020-07-17 08:07:12 +02:00
|
|
|
$SendNotification ([ $SymbolForNotification "cross-mark" ] . "Netwatch Notify: " . $HostName . " down") \
|
2020-03-04 15:45:37 +01:00
|
|
|
("Host " . $HostName . " (" . $HostVal->"host" . ") is down since " . $HostVal->"since" . ".");
|
2020-03-05 12:42:21 +01:00
|
|
|
:set ($Metric->"notified") true;
|
2020-07-03 15:19:46 +02: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 15:45:37 +01:00
|
|
|
}
|
|
|
|
}
|
2020-03-05 12:42:21 +01:00
|
|
|
:set ($NetwatchNotify->$HostName) { "count"=($Metric->"count"); "notified"=($Metric->"notified") };
|
2020-03-04 15:45:37 +01:00
|
|
|
}
|