6
6
"encoding/base64"
7
7
"encoding/json"
8
8
"fmt"
9
+ "io"
10
+ "io/ioutil"
11
+ "log"
9
12
"net"
10
13
"net/http"
11
14
"net/http/httputil"
@@ -162,7 +165,7 @@ func main() {
162
165
163
166
if flagVersion {
164
167
fmt .Fprintln (os .Stderr , "check_nsc_web v" + AppVersion )
165
- os .Exit (0 )
168
+ os .Exit (3 )
166
169
}
167
170
seen := make (map [string ]bool )
168
171
flag .Visit (func (f * flag.Flag ) {
@@ -265,7 +268,6 @@ func main() {
265
268
fmt .Println ("UNKNOWN: " + err .Error ())
266
269
os .Exit (3 )
267
270
}
268
- defer res .Body .Close ()
269
271
270
272
// check http status code
271
273
// getting 403 here means we're not allowed on the target (e.g. allowed hosts)
@@ -282,16 +284,23 @@ func main() {
282
284
fmt .Printf ("RESPONSE:\n %q\n " , dumpres )
283
285
}
284
286
287
+ log .SetOutput (ioutil .Discard )
288
+ contents , err := extractHTTPResponse (res )
289
+ if err != nil {
290
+ fmt .Printf ("RESPONSE-ERROR:\n %s\n " , err .Error ())
291
+ }
292
+ hClient .CloseIdleConnections ()
293
+
285
294
if len (args ) == 0 {
286
295
fmt .Println ("OK: NSClient API reachable on " + flagURL )
287
296
os .Exit (0 )
288
297
} else {
289
298
queryResult := new (QueryV1 )
290
299
if flagAPIVersion == "1" {
291
- json .NewDecoder ( res . Body ). Decode ( queryResult )
300
+ json .Unmarshal ( contents , & queryResult )
292
301
} else {
293
302
queryLeg := new (QueryLeg )
294
- json .NewDecoder ( res . Body ). Decode ( queryLeg )
303
+ json .Unmarshal ( contents , & queryLeg )
295
304
if len (queryLeg .Payload ) == 0 {
296
305
if flagVerbose {
297
306
fmt .Printf ("QUERY RESULT:\n %+v\n " , queryLeg )
@@ -316,15 +325,15 @@ func main() {
316
325
317
326
nagiosMessage = strings .TrimSpace (l .Message )
318
327
319
- val := ""
320
- uni := ""
321
- war := ""
322
- cri := ""
323
- min := ""
324
- max := ""
325
328
for m , p := range l .Perf {
326
329
// FIXME what if crit is set but not warn - there need to be unfilled semicolons
327
330
// REFERENCE 'label'=value[UOM];[warn];[crit];[min];[max]
331
+ val := ""
332
+ uni := ""
333
+ war := ""
334
+ cri := ""
335
+ min := ""
336
+ max := ""
328
337
if p .Value != nil {
329
338
val = strconv .FormatFloat (* (p .Value ), 'f' , flagFloatround , 64 )
330
339
} else {
@@ -358,3 +367,16 @@ func main() {
358
367
}
359
368
360
369
}
370
+
371
+ func extractHTTPResponse (response * http.Response ) (contents []byte , err error ) {
372
+ contents , err = ioutil .ReadAll (response .Body )
373
+ io .Copy (ioutil .Discard , response .Body )
374
+ response .Body .Close ()
375
+ if err != nil {
376
+ return
377
+ }
378
+ if response .StatusCode != 200 {
379
+ err = fmt .Errorf ("http request failed: %s" , response .Status )
380
+ }
381
+ return
382
+ }
0 commit comments