@@ -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}
@@ -163,7 +164,13 @@ inactive = 0`,
163164
164165 sc := result .NewPartialResult ()
165166
166- _ = sc .SetState (rl .GetStatus (cliAlertConfig .StateLabelKey ))
167+ rlStatus := rl .GetStatus (cliAlertConfig .StateLabelKey )
168+ // If the negate flag is set we negate this state
169+ if cliAlertConfig .FlipExitState {
170+ rlStatus = negateStatus (rlStatus )
171+ }
172+
173+ _ = sc .SetState (rlStatus )
167174 sc .Output = rl .GetOutput ()
168175 overall .AddSubcheck (sc )
169176 }
@@ -185,7 +192,13 @@ inactive = 0`,
185192
186193 sc := result .NewPartialResult ()
187194
188- _ = sc .SetState (rl .GetStatus (cliAlertConfig .StateLabelKey ))
195+ rlStatus := rl .GetStatus (cliAlertConfig .StateLabelKey )
196+ // If the negate flag is set we negate this state
197+ if cliAlertConfig .FlipExitState {
198+ rlStatus = negateStatus (rlStatus )
199+ }
200+
201+ _ = sc .SetState (rlStatus )
189202 // Set the alert in the internal Type to generate the output
190203 rl .Alert = alert
191204 sc .Output = rl .GetOutput ()
@@ -257,9 +270,12 @@ func init() {
257270 fs .BoolVarP (& cliAlertConfig .ProblemsOnly , "problems" , "P" , false ,
258271 "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" )
259272
273+ fs .BoolVarP (& cliAlertConfig .FlipExitState , "watchdog" , "W" , false ,
274+ "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" )
275+
260276 fs .StringVarP (& cliAlertConfig .StateLabelKey , "label-key-state" , "S" , "" ,
261277 "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" )
278+ "\n If this flag is set the plugin looks for the strings ' warning/critical/ok' in the provided label key" )
263279}
264280
265281// Function to convert state to integer.
@@ -314,3 +330,19 @@ func matchesLabel(labels model.LabelSet, labelsToMatch []string) bool {
314330
315331 return false
316332}
333+
334+ // negateStatus turns an OK state into critical and a warning/critical state into OK
335+ func negateStatus (state int ) int {
336+ switch state {
337+ case check .OK :
338+ return check .Critical
339+ case check .Critical :
340+ return check .OK
341+ case check .Warning :
342+ return check .OK
343+ case check .Unknown :
344+ return check .Unknown
345+ default :
346+ return check .Unknown
347+ }
348+ }
0 commit comments