11package cmd
22
33import (
4- "errors"
54 "fmt"
65 "regexp"
76 "slices"
87 "strings"
98
109 "github.com/NETWAYS/check_prometheus/internal/alert"
1110 "github.com/NETWAYS/go-check"
12- "github.com/NETWAYS/go-check/perfdata"
13- "github.com/NETWAYS/go-check/result"
11+ goresult "github.com/NETWAYS/go-check/result"
1412 "github.com/prometheus/common/model"
1513 "github.com/spf13/cobra"
1614)
@@ -49,7 +47,7 @@ inactive = 0`,
4947 | total=2 firing=1 pending=0 inactive=1` ,
5048 Run : func (_ * cobra.Command , _ []string ) {
5149 // Convert --no-alerts-state to integer and validate input
52- noAlertsState , err := convertStateToInt (cliAlertConfig .NoAlertsState )
50+ noAlertsState , err := check . NewStatusFromString (cliAlertConfig .NoAlertsState )
5351 if err != nil {
5452 check .ExitError (fmt .Errorf ("invalid value for --no-alerts-state: %s" , cliAlertConfig .NoAlertsState ))
5553 }
@@ -88,7 +86,7 @@ inactive = 0`,
8886 // If there are no rules we can exit early
8987 if len (rules ) == 0 {
9088 // Just an empty PerfdataList to have consistent perfdata output
91- pdlist := perfdata .PerfdataList {
89+ pdlist := check .PerfdataList {
9290 {Label : "total" , Value : 0 },
9391 {Label : "firing" , Value : 0 },
9492 {Label : "pending" , Value : 0 },
@@ -98,10 +96,10 @@ inactive = 0`,
9896 // Since the user is expecting the state of a certain alert and
9997 // it that is not present it might be noteworthy.
10098 if cliAlertConfig .AlertName != nil {
101- check .ExitRaw (check .Unknown , "No such alert defined" , "|" , pdlist . String () )
99+ check .ExitWithPerfdata (check .Unknown , pdlist , "No such alert defined" )
102100 }
103101
104- check .ExitRaw (noAlertsState , "No alerts defined" , "|" , pdlist . String () )
102+ check .ExitWithPerfdata (noAlertsState , pdlist , "No alerts defined" )
105103 }
106104
107105 // Set initial capacity to reduce memory allocations
@@ -110,7 +108,7 @@ inactive = 0`,
110108 l *= len (rl .AlertingRule .Alerts )
111109 }
112110
113- var overall result .Overall
111+ var overall goresult .Overall
114112
115113 for _ , rl := range rules {
116114 // If it's not the Alert we're looking for, Skip!
@@ -128,7 +126,7 @@ inactive = 0`,
128126 alertMatchedExclude , regexErr := matches (rl .AlertingRule .Name , cliAlertConfig .ExcludeAlerts )
129127
130128 if regexErr != nil {
131- check .ExitRaw (check .Unknown , "Invalid regular expression provided:" , regexErr .Error ())
129+ check .Exit (check .Unknown , "Invalid regular expression provided:" , regexErr .Error ())
132130 }
133131
134132 if alertMatchedExclude {
@@ -140,7 +138,7 @@ inactive = 0`,
140138 labelsMatchedExclude , regexErr := matchesLabel (rl .AlertingRule .Labels , cliAlertConfig .ExcludeLabels )
141139
142140 if regexErr != nil {
143- check .ExitRaw (check .Unknown , "Invalid regular expression provided:" , regexErr .Error ())
141+ check .Exit (check .Unknown , "Invalid regular expression provided:" , regexErr .Error ())
144142 }
145143
146144 if len (cliAlertConfig .ExcludeLabels ) > 0 && labelsMatchedExclude {
@@ -161,16 +159,16 @@ inactive = 0`,
161159 counterFiring ++
162160 }
163161
164- sc := result .NewPartialResult ()
162+ sc := goresult .NewPartialResult ()
165163
166164 rlStatus := rl .GetStatus (cliAlertConfig .StateLabelKey )
167165 // If the negate flag is set we negate this state
168166 if cliAlertConfig .FlipExitState {
169167 rlStatus = negateStatus (rlStatus )
170168 }
171169
172- _ = sc .SetState (rlStatus )
173- sc .Output = rl .GetOutput ()
170+ sc .SetState (rlStatus )
171+ sc .SetOutput ( rl .GetOutput () )
174172 overall .AddSubcheck (sc )
175173 }
176174
@@ -192,7 +190,7 @@ inactive = 0`,
192190 labelsMatchedInclude , regexErr := matchesLabel (alert .Labels , cliAlertConfig .IncludeLabels )
193191
194192 if regexErr != nil {
195- check .ExitRaw (check .Unknown , "Invalid regular expression provided:" , regexErr .Error ())
193+ check .Exit (check .Unknown , "Invalid regular expression provided:" , regexErr .Error ())
196194 }
197195
198196 if len (cliAlertConfig .IncludeLabels ) > 0 && ! labelsMatchedInclude {
@@ -203,34 +201,34 @@ inactive = 0`,
203201 labelsMatchedExclude , regexErr := matchesLabel (alert .Labels , cliAlertConfig .ExcludeLabels )
204202
205203 if regexErr != nil {
206- check .ExitRaw (check .Unknown , "Invalid regular expression provided:" , regexErr .Error ())
204+ check .Exit (check .Unknown , "Invalid regular expression provided:" , regexErr .Error ())
207205 }
208206
209207 if len (cliAlertConfig .ExcludeLabels ) > 0 && labelsMatchedExclude {
210208 // If the alert labels matches here we can skip it.
211209 continue
212210 }
213211
214- sc := result .NewPartialResult ()
212+ sc := goresult .NewPartialResult ()
215213
216214 rlStatus := rl .GetStatus (cliAlertConfig .StateLabelKey )
217215 // If the negate flag is set we negate this state
218216 if cliAlertConfig .FlipExitState {
219217 rlStatus = negateStatus (rlStatus )
220218 }
221219
222- _ = sc .SetState (rlStatus )
220+ sc .SetState (rlStatus )
223221 // Set the alert in the internal Type to generate the output
224222 rl .Alert = alert
225- sc .Output = rl .GetOutput ()
223+ sc .SetOutput ( rl .GetOutput () )
226224 overall .AddSubcheck (sc )
227225 }
228226 }
229227 }
230228
231229 counterAlert := counterFiring + counterPending + counterInactive
232230
233- perfList := perfdata .PerfdataList {
231+ perfList := check .PerfdataList {
234232 {Label : "total" , Value : counterAlert },
235233 {Label : "firing" , Value : counterFiring },
236234 {Label : "pending" , Value : counterPending },
@@ -239,11 +237,11 @@ inactive = 0`,
239237
240238 // When there are no alerts we add an empty PartialResult just to have consistent output
241239 if len (overall .PartialResults ) == 0 {
242- sc := result .NewPartialResult ()
240+ sc := goresult .NewPartialResult ()
243241 // We already make sure it's valid
244242 //nolint: errcheck
245243 sc .SetDefaultState (noAlertsState )
246- sc .Output = "No alerts retrieved"
244+ sc .SetOutput ( "No alerts retrieved" )
247245 overall .AddSubcheck (sc )
248246 }
249247
@@ -255,7 +253,7 @@ inactive = 0`,
255253 counterPending ,
256254 counterInactive )
257255
258- check .ExitRaw (overall .GetStatus (), overall .GetOutput ())
256+ check .Exit (overall .GetStatus (), overall .GetOutput ())
259257 },
260258}
261259
@@ -299,23 +297,6 @@ func init() {
299297 "\n If this flag is set the plugin looks for the strings 'warning/critical/ok' in the provided label key" )
300298}
301299
302- // Function to convert state to integer.
303- func convertStateToInt (state string ) (int , error ) {
304- state = strings .ToUpper (state )
305- switch state {
306- case "OK" , "0" :
307- return check .OK , nil
308- case "WARNING" , "1" :
309- return check .Warning , nil
310- case "CRITICAL" , "2" :
311- return check .Critical , nil
312- case "UNKNOWN" , "3" :
313- return check .Unknown , nil
314- default :
315- return check .Unknown , errors .New ("invalid state" )
316- }
317- }
318-
319300// Matches a list of regular expressions against a string.
320301func matches (input string , regexToExclude []string ) (bool , error ) {
321302 for _ , regex := range regexToExclude {
@@ -364,7 +345,7 @@ func matchesLabel(labels model.LabelSet, labelsToMatch []string) (bool, error) {
364345}
365346
366347// negateStatus turns an OK state into critical and a warning/critical state into OK
367- func negateStatus (state int ) int {
348+ func negateStatus (state check. Status ) check. Status {
368349 switch state {
369350 case check .OK :
370351 return check .Critical
0 commit comments