This repository was archived by the owner on Jan 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcheck_nsc_web.go
382 lines (337 loc) · 10 KB
/
check_nsc_web.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
package main
import (
"bytes"
"crypto/tls"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"net/http"
"net/http/httputil"
"net/url"
"os"
"strconv"
"strings"
"time"
"github.com/namsral/flag"
)
// TODO
// - strip trailing / from url
// - what about value being int64 in legacy api in PerfLine struct?
const AppVersion = "0.5.3"
var usage = `
check_nsc_web is a REST client for the NSClient++ webserver for querying
and receiving check information over HTTPS.
Copyright 2016 Michael Kraus <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Example:
check_nsc_web -p "password" -u "https://<SERVER_RUNNING_NSCLIENT>:8443" check_cpu
Usage:
check_nsc_web [options] [NSClient query parameters]
check_nsc_web can and should be built with CGO_ENABLED=0
Options:
`
//Query represents the nsclient response, which itself decomposes in lines in which there may be several performance data
type PerfLine struct {
Value *float64 `json:"value,omitempty"`
Unit *string `json:"unit,omitempty"`
Warning *float64 `json:"warning,omitempty"`
Critical *float64 `json:"critical,omitempty"`
Minimum *float64 `json:"minimum,omitempty"`
Maximum *float64 `json:"maximum,omitempty"`
}
type ResultLine struct {
Message string `json:"message"`
Perf map[string]PerfLine `json:"perf"`
}
//Query type depends on API version (v1 or legacy)
type QueryV1 struct {
Command string `json:"command"`
Lines []ResultLine `json:"lines"`
Result int `json:"result"`
}
type QueryLeg struct {
Header struct {
SourceID string `json:"source_id"`
} `json:"header"`
Payload []struct {
Command string `json:"command"`
Lines []struct {
Message string `json:"message"`
Perf []struct {
Alias string `json:"alias"`
IntValue *PerfLine `json:"int_value,omitempty"`
FloatValue *PerfLine `json:"float_value,omitempty"`
} `json:"perf"`
} `json:"lines"`
Result string `json:"result"`
} `json:"payload"`
}
var ReturncodeMap = map[string]int{
"OK": 0,
"WARNING": 1,
"CRITICAL": 2,
"UNKNOWN": 3,
}
func (q QueryLeg) toV1() *QueryV1 {
qV1 := new(QueryV1)
if len(q.Payload) == 0 {
return qV1
}
qV1.Command = q.Payload[0].Command
qV1.Result = ReturncodeMap[q.Payload[0].Result]
qV1.Lines = make([]ResultLine, len(q.Payload[0].Lines))
for i, v := range q.Payload[0].Lines {
qV1.Lines[i].Message = v.Message
qV1.Lines[i].Perf = make(map[string]PerfLine)
for _, p := range v.Perf {
if p.FloatValue != nil {
qV1.Lines[i].Perf[p.Alias] = *p.FloatValue
} else {
qV1.Lines[i].Perf[p.Alias] = *p.IntValue
}
}
}
return qV1
}
func main() {
flag.Usage = func() {
fmt.Fprintln(os.Stderr, "check_nsc_web v"+AppVersion)
fmt.Fprintf(os.Stderr, usage)
flag.PrintDefaults()
}
var flagURL string
var flagLogin string
var flagPassword string
var flagAPIVersion string
var flagTimeout int
var flagVerbose bool
var flagJSON bool
var flagVersion bool
var flagInsecure bool
var flagFloatround int
var flagExtratext string
var flagQuery string
flag.StringVar(&flagURL, "u", "", "NSCLient++ URL, for example https://10.1.2.3:8443.")
flag.StringVar(&flagLogin, "l", "admin", "NSClient++ webserver login.")
flag.StringVar(&flagPassword, "p", "", "NSClient++ webserver password.")
flag.StringVar(&flagAPIVersion, "a", "legacy", "API version of NSClient++ (legacy or 1).")
flag.IntVar(&flagTimeout, "t", 10, "Connection timeout in seconds.")
flag.BoolVar(&flagVerbose, "v", false, "Enable verbose output.")
flag.BoolVar(&flagJSON, "j", false, "Print out JSON response body.")
flag.BoolVar(&flagVersion, "V", false, "Print program version.")
flag.BoolVar(&flagInsecure, "k", false, "Insecure mode - skip TLS verification.")
flag.IntVar(&flagFloatround, "f", -1, "Round performance data float values to this number of digits.")
// These flags support loading config from file using "-config FILENAME"
flag.StringVar(&flagQuery, "query", "", "placeholder for query string from config file")
flag.String(flag.DefaultConfigFlagname, "", "path to config file")
flag.Parse()
if flagVersion {
fmt.Fprintln(os.Stderr, "check_nsc_web v"+AppVersion)
os.Exit(3)
}
seen := make(map[string]bool)
flag.Visit(func(f *flag.Flag) {
seen[f.Name] = true
})
for _, req := range []string{"u", "p"} {
if !seen[req] {
fmt.Fprintf(os.Stderr, "UNKNOWN: Missing required -%s argument\n", req)
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
flag.Usage()
os.Exit(3)
}
}
args := flag.Args()
// Has there a flag "query" been provided in the config file? Transform it into slice and append it to Args()
if seen["query"] {
q := strings.Split(flagQuery, " ")
args = append(args, q...)
}
timeout := time.Second * time.Duration(flagTimeout)
urlStruct, err := url.Parse(flagURL)
if err != nil {
fmt.Println("UNKNOWN: " + err.Error())
os.Exit(3)
}
if len(args) == 0 {
urlStruct.Path += "/"
} else {
if flagAPIVersion == "1" {
urlStruct.Path += "/api/v1/queries/" + args[0] + "/commands/execute"
} else {
urlStruct.Path += "/query/" + args[0]
}
if len(args) > 1 {
var param bytes.Buffer
for i, a := range args {
if i == 0 {
continue
} else if i > 1 {
param.WriteString("&")
}
p := strings.SplitN(a, "=", 2)
if len(p) == 1 {
param.WriteString(url.QueryEscape(p[0]))
} else {
param.WriteString(url.QueryEscape(p[0]) + "=" + url.QueryEscape(p[1]))
}
if err != nil {
fmt.Println("UNKNOWN: " + err.Error())
os.Exit(3)
}
}
urlStruct.RawQuery = param.String()
}
}
var hTransport = &http.Transport{
TLSClientConfig: &tls.Config{
MinVersion: tls.VersionTLS10,
MaxVersion: tls.VersionTLS12,
InsecureSkipVerify: flagInsecure,
},
Dial: (&net.Dialer{
Timeout: timeout,
}).Dial,
ResponseHeaderTimeout: timeout,
TLSHandshakeTimeout: timeout,
IdleConnTimeout: timeout,
}
var hClient = &http.Client{
Timeout: timeout,
Transport: hTransport,
}
req, err := http.NewRequest("GET", urlStruct.String(), nil)
if err != nil {
fmt.Println("UNKNOWN: " + err.Error())
os.Exit(3)
}
if flagAPIVersion == "1" && flagLogin != "" {
req.Header.Add("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(flagLogin+":"+flagPassword)))
} else {
req.Header.Add("password", flagPassword)
}
if flagVerbose {
dumpreq, err := httputil.DumpRequestOut(req, true)
if err != nil {
fmt.Printf("REQUEST-ERROR:\n%s\n", err.Error())
}
fmt.Printf("REQUEST:\n%q\n", dumpreq)
}
res, err := hClient.Do(req)
if err != nil {
fmt.Println("UNKNOWN: " + err.Error())
os.Exit(3)
}
// check http status code
// getting 403 here means we're not allowed on the target (e.g. allowed hosts)
if res.Status == "403" {
fmt.Println("HTTP 403: Forbidden.")
os.Exit(3)
}
if flagVerbose {
dumpres, err := httputil.DumpResponse(res, true)
if err != nil {
fmt.Printf("RESPONSE-ERROR:\n%s\n", err.Error())
}
fmt.Printf("RESPONSE:\n%q\n", dumpres)
}
log.SetOutput(ioutil.Discard)
contents, err := extractHTTPResponse(res)
if err != nil {
fmt.Printf("RESPONSE-ERROR:\n%s\n", err.Error())
}
hClient.CloseIdleConnections()
if len(args) == 0 {
fmt.Println("OK: NSClient API reachable on " + flagURL)
os.Exit(0)
} else {
queryResult := new(QueryV1)
if flagAPIVersion == "1" {
json.Unmarshal(contents, &queryResult)
} else {
queryLeg := new(QueryLeg)
json.Unmarshal(contents, &queryLeg)
if len(queryLeg.Payload) == 0 {
if flagVerbose {
fmt.Printf("QUERY RESULT:\n%+v\n", queryLeg)
}
fmt.Println("UNKNOWN: The resultpayload size is 0")
os.Exit(3)
}
queryResult = queryLeg.toV1()
}
if flagJSON {
jsonStr, _ := json.Marshal(queryResult)
fmt.Println(string(jsonStr))
os.Exit(0)
}
var nagiosMessage string
var nagiosPerfdata bytes.Buffer
// FIXME how to iterate the slice of lines safely ?
for _, l := range queryResult.Lines {
nagiosMessage = strings.TrimSpace(l.Message)
for m, p := range l.Perf {
// FIXME what if crit is set but not warn - there need to be unfilled semicolons
// REFERENCE 'label'=value[UOM];[warn];[crit];[min];[max]
val := ""
uni := ""
war := ""
cri := ""
min := ""
max := ""
if p.Value != nil {
val = strconv.FormatFloat(*(p.Value), 'f', flagFloatround, 64)
} else {
continue
}
if p.Unit != nil {
uni = (*(p.Unit))
}
if p.Warning != nil {
war = strconv.FormatFloat(*(p.Warning), 'f', flagFloatround, 64)
}
if p.Critical != nil {
cri = strconv.FormatFloat(*(p.Critical), 'f', flagFloatround, 64)
}
if p.Minimum != nil {
min = strconv.FormatFloat(*(p.Minimum), 'f', flagFloatround, 64)
}
if p.Maximum != nil {
max = strconv.FormatFloat(*(p.Maximum), 'f', flagFloatround, 64)
}
nagiosPerfdata.WriteString(" '" + m + "'=" + val + uni + ";" + war + ";" + cri + ";" + min + ";" + max)
}
}
if nagiosPerfdata.Len() == 0 {
fmt.Println(nagiosMessage + " " + flagExtratext)
} else {
fmt.Println(nagiosMessage + " " + flagExtratext + "|" + strings.TrimSpace(nagiosPerfdata.String()))
}
os.Exit(queryResult.Result)
}
}
func extractHTTPResponse(response *http.Response) (contents []byte, err error) {
contents, err = ioutil.ReadAll(response.Body)
io.Copy(ioutil.Discard, response.Body)
response.Body.Close()
if err != nil {
return
}
if response.StatusCode != 200 {
err = fmt.Errorf("http request failed: %s", response.Status)
}
return
}