Skip to content

Commit e228c73

Browse files
committed
Merge branch 'netwatch-notify' into next
2 parents 8f32887 + 5aff401 commit e228c73

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

netwatch-notify

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ $ScriptLock $0;
6969
:if ($HostInfo->"notify" = true && $HostInfo->"disabled" != true) do={
7070
:local Name [ $EitherOr ($HostInfo->"name") ($HostVal->"name") ];
7171

72-
:local Metric { "count"=0; "notified"=false };
72+
:local Metric { "count-down"=0; "count-up"=0; "notified"=false };
7373
:if ([ :typeof ($NetwatchNotify->$Name) ] = "array") do={
7474
:set $Metric ($NetwatchNotify->$Name);
7575
}
@@ -98,16 +98,17 @@ $ScriptLock $0;
9898
}
9999

100100
:if ($HostVal->"status" = "up") do={
101-
:local Count ($Metric->"count");
102-
:if ($Count > 0) do={
101+
:local CountDown ($Metric->"count-down");
102+
:if ($CountDown > 0) do={
103103
$LogPrintExit2 info $0 \
104104
("The " . $Type . " '" . $Name . "' (" . $HostDetails . ") is up.") false;
105-
:set ($Metric->"count") 0;
105+
:set ($Metric->"count-down") 0;
106106
}
107+
:set ($Metric->"count-up") ($Metric->"count-up" + 1);
107108
:if ($Metric->"notified" = true) do={
108109
:local Message ("The " . $Type . " '" . $Name . "' (" . $HostDetails . \
109110
") is up since " . $HostVal->"since" . ".\n" . \
110-
"It was down for " . $Count . " checks since " . ($Metric->"since") . ".");
111+
"It was down for " . $CountDown . " checks since " . ($Metric->"since") . ".");
111112
:if ([ :typeof ($HostInfo->"up-hook") ] = "str") do={
112113
:set Message ($Message . "\n\n" . [ $NetwatchNotifyHook $Name $Type "up" \
113114
($HostInfo->"up-hook") ]);
@@ -121,35 +122,38 @@ $ScriptLock $0;
121122
:set ($Metric->"parent") ($HostInfo->"parent");
122123
:set ($Metric->"since");
123124
} else={
124-
:set ($Metric->"count") ($Metric->"count" + 1);
125+
:set ($Metric->"count-down") ($Metric->"count-down" + 1);
126+
:set ($Metric->"count-up") 0;
125127
:set ($Metric->"parent") ($HostInfo->"parent");
126128
:set ($Metric->"since") ($HostVal->"since");
127-
:local Count [ $IfThenElse ([ :tonum ($HostInfo->"count") ] > 0) ($HostInfo->"count") 5 ];
129+
:local CountDown [ $IfThenElse ([ :tonum ($HostInfo->"count-down") ] > 0) ($HostInfo->"count-down") 5 ];
128130
:local Parent ($HostInfo->"parent");
131+
:local ParentUp false;
129132
:while ([ :len $Parent ] > 0) do={
130-
:set Count ($Count + 1);
133+
:set CountDown ($CountDown + 1);
131134
:set Parent ($NetwatchNotify->$Parent->"parent");
132135
}
133136
:set Parent ($HostInfo->"parent");
134137
:local ParentNotified false;
135138
:while ($ParentNotified = false && [ :len $Parent ] > 0) do={
136139
:set ParentNotified [ $IfThenElse (($NetwatchNotify->$Parent->"notified") = true) \
137140
true false ];
141+
:set ParentUp ($NetwatchNotify->$Parent->"count-up");
138142
:if ($ParentNotified = false) do={
139143
:set Parent ($NetwatchNotify->$Parent->"parent");
140144
}
141145
}
142146
$LogPrintExit2 [ $IfThenElse ($HostInfo->"no-down-notification" != true) info debug ] $0 \
143147
("The " . $Type . " '" . $Name . "' (" . $HostDetails . ") is down for " . \
144-
$Metric->"count" . " checks, " . [ $IfThenElse ($ParentNotified = false) [ $IfThenElse \
145-
($Metric->"notified" = true) ("already notified.") ($Count - $Metric->"count" . \
148+
$Metric->"count-down" . " checks, " . [ $IfThenElse ($ParentNotified = false) [ $IfThenElse \
149+
($Metric->"notified" = true) ("already notified.") ($CountDown - $Metric->"count-down" . \
146150
" to go.") ] ("parent " . $Type . " " . $Parent . " is down.") ]) false;
147-
:if ((($Count * 2) - ($Metric->"count" * 3)) / 2 = 0 && \
151+
:if ((($CountDown * 2) - ($Metric->"count-down" * 3)) / 2 = 0 && \
148152
[ :typeof ($HostInfo->"pre-down-hook") ] = "str") do={
149153
$NetwatchNotifyHook $Name $Type "pre-down" ($HostInfo->"pre-down-hook");
150154
}
151-
:if ($ParentNotified = false && $Metric->"count" >= $Count && \
152-
$Metric->"notified" != true) do={
155+
:if ($ParentNotified = false && $Metric->"count-down" >= $CountDown && \
156+
($ParentUp = false || $ParentUp > 2) && $Metric->"notified" != true) do={
153157
:local Message ("The " . $Type . " '" . $Name . "' (" . $HostDetails . \
154158
") is down since " . $HostVal->"since" . ".");
155159
:if ([ :typeof ($HostInfo->"down-hook") ] = "str") do={
@@ -166,7 +170,8 @@ $ScriptLock $0;
166170
}
167171
}
168172
:set ($NetwatchNotify->$Name) {
169-
"count"=($Metric->"count");
173+
"count-down"=($Metric->"count-down");
174+
"count-up"=($Metric->"count-up");
170175
"notified"=($Metric->"notified");
171176
"parent"=($Metric->"parent");
172177
"resolve-failed"=($Metric->"resolve-failed");

0 commit comments

Comments
 (0)