@@ -22,6 +22,7 @@ type AlertConfig struct {
2222 ExcludeLabels []string
2323 IncludeLabels []string
2424 ProblemsOnly bool
25+ FlipExitState bool
2526 StateLabelKey string
2627 NoAlertsState string
2728}
@@ -99,6 +100,7 @@ inactive = 0`,
99100 if cliAlertConfig .AlertName != nil {
100101 check .ExitRaw (check .Unknown , "No such alert defined" , "|" , pdlist .String ())
101102 }
103+
102104 check .ExitRaw (noAlertsState , "No alerts defined" , "|" , pdlist .String ())
103105 }
104106
@@ -163,7 +165,13 @@ inactive = 0`,
163165
164166 sc := result .NewPartialResult ()
165167
166- _ = sc .SetState (rl .GetStatus (cliAlertConfig .StateLabelKey ))
168+ rlStatus := rl .GetStatus (cliAlertConfig .StateLabelKey )
169+ // If the negate flag is set we negate this state
170+ if cliAlertConfig .FlipExitState {
171+ rlStatus = negateStatus (rlStatus )
172+ }
173+
174+ _ = sc .SetState (rlStatus )
167175 sc .Output = rl .GetOutput ()
168176 overall .AddSubcheck (sc )
169177 }
@@ -185,7 +193,13 @@ inactive = 0`,
185193
186194 sc := result .NewPartialResult ()
187195
188- _ = sc .SetState (rl .GetStatus (cliAlertConfig .StateLabelKey ))
196+ rlStatus := rl .GetStatus (cliAlertConfig .StateLabelKey )
197+ // If the negate flag is set we negate this state
198+ if cliAlertConfig .FlipExitState {
199+ rlStatus = negateStatus (rlStatus )
200+ }
201+
202+ _ = sc .SetState (rlStatus )
189203 // Set the alert in the internal Type to generate the output
190204 rl .Alert = alert
191205 sc .Output = rl .GetOutput ()
@@ -257,9 +271,12 @@ func init() {
257271 fs .BoolVarP (& cliAlertConfig .ProblemsOnly , "problems" , "P" , false ,
258272 "Display only alerts which status is not inactive/OK. Note that in combination with the --name flag this might result in no alerts being displayed" )
259273
274+ fs .BoolVarP (& cliAlertConfig .FlipExitState , "watchdog" , "W" , false ,
275+ "Flip the exit state for firing alerts. When this flag is set firing alerts will be OK and inactive alerts will be CRITICAL. This is intended for handling watchdog alerts" )
276+
260277 fs .StringVarP (& cliAlertConfig .StateLabelKey , "label-key-state" , "S" , "" ,
261278 "Use the given AlertRule label to override the exit state for firing alerts." +
262- "\n If this flag is set the plugin looks for warning/critical/ok in the provided label key" )
279+ "\n If this flag is set the plugin looks for the strings ' warning/critical/ok' in the provided label key" )
263280}
264281
265282// Function to convert state to integer.
@@ -314,3 +331,19 @@ func matchesLabel(labels model.LabelSet, labelsToMatch []string) bool {
314331
315332 return false
316333}
334+
335+ // negateStatus turns an OK state into critical and a warning/critical state into OK
336+ func negateStatus (state int ) int {
337+ switch state {
338+ case check .OK :
339+ return check .Critical
340+ case check .Critical :
341+ return check .OK
342+ case check .Warning :
343+ return check .OK
344+ case check .Unknown :
345+ return check .Unknown
346+ default :
347+ return check .Unknown
348+ }
349+ }
0 commit comments