routeros-scripts/netwatch-notify

50 lines
1.9 KiB
Plaintext
Raw Normal View History

2020-03-04 14:45:37 +00:00
#!rsc
# RouterOS script: netwatch-notify
# Copyright (c) 2020 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
:global NetwatchNotify;
:global ParseKeyValueStore;
2020-03-05 11:47:42 +00:00
:global LogPrintExit;
2020-03-04 14:45:37 +00:00
:global SendNotification;
:if ([ :typeof $NetwatchNotify ] = "nothing") do={
:set NetwatchNotify [ :toarray "" ];
}
:foreach Host in=[ / tool netwatch find where comment~"^notify," ] do={
:local HostVal [ / tool netwatch get $Host ];
:local HostName ([ $ParseKeyValueStore ($HostVal->"comment") ]->"hostname");
:local Metric { "count"=0; "notified"=false };
:if ([ :typeof ($NetwatchNotify->$HostName) ] = "array") do={
:set $Metric ($NetwatchNotify->$HostName);
}
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;
:local Count ($Metric->"count");
:set ($Metric->"count") 0;
:if ($Metric->"notified" = true) do={
$SendNotification ("Netwatch Notify: \E2\9C\85 " . $HostName . " up") \
("Host " . $HostName . " (" . $HostVal->"host" . ") is up since " . $HostVal->"since" . ".\n" . \
"It was down for " . $Count . " checks.");
2020-03-04 14:45:37 +00:00
}
:set ($Metric->"notified") false;
2020-03-04 14:45:37 +00:00
} else={
: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;
:if ($Metric->"count" >= 5 && $Metric->"notified" != true) do={
$SendNotification ("Netwatch Notify: \E2\9D\8C " . $HostName . " down") \
2020-03-04 14:45:37 +00:00
("Host " . $HostName . " (" . $HostVal->"host" . ") is down since " . $HostVal->"since" . ".");
:set ($Metric->"notified") true;
2020-03-04 14:45:37 +00:00
}
}
:set ($NetwatchNotify->$HostName) { "count"=($Metric->"count"); "notified"=($Metric->"notified") };
2020-03-04 14:45:37 +00:00
}