Skip to content
This repository was archived by the owner on Jan 22, 2024. It is now read-only.

Commit 2a17668

Browse files
authored
Merge pull request #11 from ConSol/fix_unsolicited_response
Fix unsolicited response
2 parents 11a421f + be7e079 commit 2a17668

File tree

1 file changed

+32
-10
lines changed

1 file changed

+32
-10
lines changed

check_nsc_web.go

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import (
66
"encoding/base64"
77
"encoding/json"
88
"fmt"
9+
"io"
10+
"io/ioutil"
11+
"log"
912
"net"
1013
"net/http"
1114
"net/http/httputil"
@@ -162,7 +165,7 @@ func main() {
162165

163166
if flagVersion {
164167
fmt.Fprintln(os.Stderr, "check_nsc_web v"+AppVersion)
165-
os.Exit(0)
168+
os.Exit(3)
166169
}
167170
seen := make(map[string]bool)
168171
flag.Visit(func(f *flag.Flag) {
@@ -265,7 +268,6 @@ func main() {
265268
fmt.Println("UNKNOWN: " + err.Error())
266269
os.Exit(3)
267270
}
268-
defer res.Body.Close()
269271

270272
// check http status code
271273
// getting 403 here means we're not allowed on the target (e.g. allowed hosts)
@@ -282,16 +284,23 @@ func main() {
282284
fmt.Printf("RESPONSE:\n%q\n", dumpres)
283285
}
284286

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+
285294
if len(args) == 0 {
286295
fmt.Println("OK: NSClient API reachable on " + flagURL)
287296
os.Exit(0)
288297
} else {
289298
queryResult := new(QueryV1)
290299
if flagAPIVersion == "1" {
291-
json.NewDecoder(res.Body).Decode(queryResult)
300+
json.Unmarshal(contents, &queryResult)
292301
} else {
293302
queryLeg := new(QueryLeg)
294-
json.NewDecoder(res.Body).Decode(queryLeg)
303+
json.Unmarshal(contents, &queryLeg)
295304
if len(queryLeg.Payload) == 0 {
296305
if flagVerbose {
297306
fmt.Printf("QUERY RESULT:\n%+v\n", queryLeg)
@@ -316,15 +325,15 @@ func main() {
316325

317326
nagiosMessage = strings.TrimSpace(l.Message)
318327

319-
val := ""
320-
uni := ""
321-
war := ""
322-
cri := ""
323-
min := ""
324-
max := ""
325328
for m, p := range l.Perf {
326329
// FIXME what if crit is set but not warn - there need to be unfilled semicolons
327330
// REFERENCE 'label'=value[UOM];[warn];[crit];[min];[max]
331+
val := ""
332+
uni := ""
333+
war := ""
334+
cri := ""
335+
min := ""
336+
max := ""
328337
if p.Value != nil {
329338
val = strconv.FormatFloat(*(p.Value), 'f', flagFloatround, 64)
330339
} else {
@@ -358,3 +367,16 @@ func main() {
358367
}
359368

360369
}
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

Comments
 (0)