4141 emptyQueryStatus check_x.State
4242)
4343
44- func startTimeout () {
45- if timeout != 0 {
46- check_x .StartTimeout (time .Duration (timeout ) * time .Second )
47- }
48- }
49-
5044// This function is intended to be used for single-use cli mode
5145// It will be called from main executable function as it returns int
5246func CheckMain (args []string ) int {
@@ -71,10 +65,10 @@ func GenerateStdout(state check_x.State, msg string, collection *check_x.Perform
7165// It can be used as a library import
7266func Check (args []string ) (check_x.State , string , * check_x.PerformanceDataCollection , error ) {
7367
74- var state check_x.State
75- var msg string
68+ state := check_x .Unknown
69+ msg := "Cli action did not run yet"
7670 var collection = check_x .NewPerformanceDataCollection ()
77- var err error
71+ var err error = nil
7872
7973 cmd := & cli.Command {
8074 Name : "check_prometheus" ,
@@ -118,8 +112,15 @@ func Check(args []string) (check_x.State, string, *check_x.PerformanceDataCollec
118112 Usage : "Returns the build informations" ,
119113 Description : `This check requires that the prometheus server itself is listed as target. Following query will be used: 'prometheus_build_info{job="prometheus"}'` ,
120114 Action : func (ctx context.Context , cmd * cli.Command ) error {
121- startTimeout ()
122- state , msg , err = mode .Ping (address , & collection )
115+ var ctxPing context.Context
116+ var ctxPingCancel context.CancelFunc
117+ if timeout == 0 {
118+ ctxPing = context .WithoutCancel (ctx )
119+ } else {
120+ ctxPing , ctxPingCancel = context .WithTimeout (ctx , time .Duration (timeout )* time .Second )
121+ defer ctxPingCancel ()
122+ }
123+ state , msg , err = mode .Ping (ctxPing , address , & collection )
123124 return err
124125 },
125126 Flags : []cli.Flag {
@@ -193,9 +194,17 @@ func Check(args []string) (check_x.State, string, *check_x.PerformanceDataCollec
193194 --> UNKNOWN - The given States do not contain an State
194195
195196 ` ,
196- Action : func (c context.Context , cmd * cli.Command ) error {
197- startTimeout ()
198- state , msg , err = mode .Query (address , queryDecoded , warning , critical , alias , search , replace , emptyQueryMessage , emptyQueryStatus , & collection )
197+ Action : func (ctx context.Context , cmd * cli.Command ) error {
198+ var ctxQuery context.Context
199+ var ctxQueryCancel context.CancelFunc
200+ if timeout == 0 {
201+ ctxQuery = context .WithoutCancel (ctx )
202+ } else {
203+ ctxQuery , ctxQueryCancel = context .WithTimeout (ctx , time .Duration (timeout )* time .Second )
204+ defer ctxQueryCancel ()
205+ }
206+
207+ state , msg , err = mode .Query (ctxQuery , address , queryDecoded , warning , critical , alias , search , replace , emptyQueryMessage , emptyQueryStatus , & collection )
199208 return err
200209 },
201210 Flags : []cli.Flag {
@@ -360,9 +369,17 @@ func Check(args []string) (check_x.State, string, *check_x.PerformanceDataCollec
360369 HideHelp : false ,
361370 Usage : "Returns the health of the targets" ,
362371 Description : `The warning and critical thresholds are appied on the health_rate. The health_rate is calculted: sum(healthy) / sum(targets).` ,
363- Action : func (c context.Context , cmd * cli.Command ) error {
364- startTimeout ()
365- state , msg , err = mode .TargetsHealth (address , label , warning , critical , & collection )
372+ Action : func (ctx context.Context , cmd * cli.Command ) error {
373+ var ctxTargetsHealth context.Context
374+ var ctxTargetsHealthCancel context.CancelFunc
375+ if timeout == 0 {
376+ ctxTargetsHealth = context .WithoutCancel (ctx )
377+ } else {
378+ ctxTargetsHealth , ctxTargetsHealthCancel = context .WithTimeout (ctx , time .Duration (timeout )* time .Second )
379+ defer ctxTargetsHealthCancel ()
380+ }
381+
382+ state , msg , err = mode .TargetsHealth (ctxTargetsHealth , address , label , warning , critical , & collection )
366383 return err
367384 },
368385 Flags : []cli.Flag {
0 commit comments