-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
368 lines (304 loc) · 9.25 KB
/
http.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
package main
import (
"context"
"crypto/tls"
"crypto/x509"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptrace"
"strings"
"time"
"github.com/hako/durafmt"
"github.com/jmespath/go-jmespath"
"github.com/julian7/sensu-base-checks/measurements"
"github.com/julian7/sensu-base-checks/metrics"
"github.com/julian7/sensulib"
"github.com/karrick/tparse"
"github.com/spf13/cobra"
)
type httpConfig struct {
URL string
Timeout string
timeout time.Duration
Headers []string
Insecure bool
Certfile string
CAfile string
Expiry string
expiry time.Time
Method string
Metrics bool
Response uint
Redirect string
UserAgent string
Data string
JSONkey string
JSONval string
certList []*x509.Certificate
tracer *measurements.HTTPTracer
}
func httpCmd() *cobra.Command {
config := &httpConfig{}
cmd := sensulib.NewCommand(
config,
"http",
"HTTP check",
`Checks for HTTP services
This check runs a HTTP query, and inspects return values. Returns
- Unknown on configuration issues,
- Warning on nearing TLS cert expiry or not matching, but non-error HTTP codes,
- Critical on any other cases.
Timeout duration can be provided in short range (eg. ms, s, m, h), cert expiry
can be provided with longer range too (like d, w, mo).
`,
)
flags := cmd.Flags()
flags.StringVarP(&config.URL, "url", "u", "http://127.0.0.1:80/", "Target URL")
flags.StringVarP(&config.Timeout, "timeout", "t", "5s", "Connection timeout")
flags.StringSliceVarP(&config.Headers, "header", "H", []string{}, "HTTP header")
flags.BoolVarP(&config.Insecure, "insecure", "k", false, "Enable insecure connections")
flags.StringVarP(&config.Certfile, "cert", "c", "", "Certificate file")
flags.StringVarP(&config.CAfile, "ca", "C", "", "CA Certificate file")
flags.StringVarP(&config.Expiry, "expiry", "e", "", "Warn EXPIRY before cert expires (duration, like 5d)")
flags.StringVarP(&config.Method, "method", "X", "GET", "HTTP method")
flags.BoolVar(&config.Metrics, "metrics", false, "Output measurements in OpenTSDB format")
flags.StringVarP(&config.UserAgent, "user-agent", "A", "", "User agent")
flags.StringVarP(&config.Data, "body", "d", "", "HTTP body")
flags.StringVarP(&config.JSONkey, "json-key", "K", "", "JSON key selector in JMESPath syntax")
flags.StringVarP(&config.JSONval, "json-val", "V", "", "expected value for JSON key in string form")
flags.UintVarP(&config.Response, "response", "r", 2, "HTTP error code to expect; use 3-digits for exact, "+
"1-digit for first digit check")
flags.StringVarP(&config.Redirect, "redirect", "R", "", "Expect redirection to")
return cmd
}
func (conf *httpConfig) check() error {
var err error
conf.timeout, err = time.ParseDuration(conf.Timeout)
if err != nil {
return fmt.Errorf("cannot parse --timeout: %w", err)
}
if len(conf.Expiry) != 0 {
var err error
//conf.expiry, err = time.ParseDuration(conf.Expiry)
conf.expiry, err = tparse.ParseNow(time.RFC3339, "now+"+conf.Expiry)
if err != nil {
return fmt.Errorf("cannot parse --expiry: %w", err)
}
}
tests := []struct {
opt string
check bool
err string
}{
{
"response",
conf.Response < 1 ||
(conf.Response > 5 && conf.Response < 100) ||
conf.Response > 599,
"should be between 1 and 5, or 100 and 599",
},
{
"response",
len(conf.Redirect) != 0 &&
!(conf.Response == 3 || (conf.Response >= 300 && conf.Response < 400)),
"should expect 3xx if redirect is also expected",
},
}
for _, test := range tests {
if test.check {
return fmt.Errorf("--%s %s", test.opt, test.err)
}
}
return nil
}
func (conf *httpConfig) Run(cmd *cobra.Command, args []string) error {
if err := conf.check(); err != nil {
return sensulib.Unknown(err)
}
req, err := http.NewRequest(conf.Method, conf.URL, strings.NewReader(conf.Data))
if err != nil {
return sensulib.Unknown(fmt.Errorf("cannot assemble HTTP request: %w", err))
}
if len(conf.Headers) > 0 {
for _, item := range conf.Headers {
items := strings.SplitN(item, ":", 2)
req.Header.Set(items[0], strings.Trim(items[1], " \t\r\n"))
}
}
if len(conf.UserAgent) != 0 {
req.Header.Set("User-Agent", conf.UserAgent)
}
client, err := conf.httpClient()
if err != nil {
return err
}
if conf.Metrics {
conf.tracer = measurements.NewHTTPTracer()
req = req.WithContext(httptrace.WithClientTrace(context.Background(), conf.tracer.Trace))
}
resp, err := client.Do(req)
if err != nil {
return sensulib.Crit(err)
}
defer resp.Body.Close()
if conf.Metrics {
return conf.printMetrics(req, resp)
}
if len(conf.Expiry) != 0 {
for _, cert := range conf.certList {
if cert.NotAfter.Before(conf.expiry) {
return sensulib.Warn(fmt.Errorf(
"certificate %s will expire in %s",
cert.Subject,
durafmt.Parse(time.Until(cert.NotAfter)).LimitFirstN(4).String(),
))
}
}
}
if err := conf.checkResponse(resp); err != nil {
return err
}
if err := conf.checkRedirect(resp); err != nil {
return err
}
if conf.JSONkey != "" {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
return conf.checkJSONContent(body)
}
return sensulib.Ok(fmt.Errorf("HTTP request responded successfully with %s", resp.Status))
}
func (conf *httpConfig) httpClient() (*http.Client, error) {
tlsconfig := &tls.Config{}
if conf.Insecure {
tlsconfig.InsecureSkipVerify = true
}
if len(conf.Expiry) != 0 {
tlsconfig.VerifyConnection = func(cs tls.ConnectionState) error {
conf.certList = make([]*x509.Certificate, len(cs.PeerCertificates))
for idx, cert := range cs.PeerCertificates {
conf.certList[idx] = cert
}
return nil
}
}
if len(conf.Certfile) != 0 {
cert, err := tls.LoadX509KeyPair(conf.Certfile, conf.Certfile)
if err != nil {
return nil, sensulib.Unknown(fmt.Errorf("cannot load --certfile: %w", err))
}
tlsconfig.Certificates = []tls.Certificate{cert}
}
if len(conf.CAfile) != 0 {
cacontents, err := ioutil.ReadFile(conf.CAfile)
if err != nil {
return nil, sensulib.Unknown(fmt.Errorf("cannot load --ca: %w", err))
}
certpool := x509.NewCertPool()
certpool.AppendCertsFromPEM(cacontents)
}
return &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
Timeout: conf.timeout,
Transport: &http.Transport{
TLSClientConfig: tlsconfig,
},
}, nil
}
func (conf *httpConfig) checkResponse(resp *http.Response) error {
// response
sfx := ""
response := uint(resp.StatusCode)
if conf.Response < 10 {
response /= 100
sfx = "xx"
}
if conf.Response != response {
err := fmt.Errorf(
"returned with code %s, expected %d%s",
resp.Status,
conf.Response,
sfx,
)
if conf.Response >= 400 {
return sensulib.Crit(err)
}
return sensulib.Warn(err)
}
return nil
}
func (conf *httpConfig) checkRedirect(resp *http.Response) error {
redirect := resp.Header.Get("Location")
switch {
case len(conf.Redirect) != 0 && len(redirect) != 0:
if redirect != conf.Redirect {
return sensulib.Crit(fmt.Errorf(
"redirected to %s, expected to %s",
redirect,
conf.Redirect,
))
}
case len(conf.Redirect) != 0:
return sensulib.Crit(fmt.Errorf("not redirected to %s", conf.Redirect))
case len(redirect) != 0:
if !(conf.Response == 3 || (conf.Response >= 300 && conf.Response < 400)) {
return sensulib.Crit(fmt.Errorf("unexpected redirection to %s", redirect))
}
}
return nil
}
func (conf *httpConfig) checkJSONContent(body []byte) error {
raw, err := conf.checkJSONKey(body)
if err != nil {
return sensulib.Crit(err)
}
if conf.JSONval != "" {
str := fmt.Sprintf("%v", raw)
if str != conf.JSONval {
return sensulib.Crit(fmt.Errorf("key %q has %q, wants %q", conf.JSONkey, str, conf.JSONval))
}
err = fmt.Errorf("HTTP request responded with JSON key %q = %q", conf.JSONkey, conf.JSONval)
} else {
err = fmt.Errorf("HTTP request responded with existing JSON key %q", conf.JSONkey)
}
return sensulib.Ok(err)
}
func (conf *httpConfig) checkJSONKey(body []byte) (interface{}, error) {
var buf interface{}
if err := json.Unmarshal(body, &buf); err != nil {
return nil, fmt.Errorf("parsing response body as JSON: %w", err)
}
raw, err := jmespath.Search(conf.JSONkey, buf)
if err != nil {
return nil, fmt.Errorf("searching for %q in body JSON: %w", conf.JSONkey, err)
}
return raw, nil
}
func (conf *httpConfig) printMetrics(req *http.Request, resp *http.Response) error {
written, err := io.Copy(ioutil.Discard, resp.Body)
conf.tracer.Done()
log := metrics.New("http").With(map[string]string{"url": conf.URL})
transfer_time := conf.tracer.Finished.Sub(conf.tracer.StartResponding)
log.Log("time.total", conf.tracer.Total().Microseconds())
log.Log("time.namelookup", conf.tracer.Namelookup().Microseconds())
log.Log("time.connect", conf.tracer.Connect().Microseconds())
if req.URL.Scheme == "https" {
log.Log("time.pretransfer", conf.tracer.Pretransfer().Microseconds())
}
log.Log("time.starttransfer", conf.tracer.Starttransfer().Microseconds())
log.Log("time.body_transfer", transfer_time.Microseconds())
log.Log("http.http_code", resp.StatusCode)
log.Log("http.body_bytes", written)
log.Log("http.error", err)
if err == nil && transfer_time > 0 && written > 0 {
log.Log("speed.body_transfer", float64(written)/transfer_time.Seconds())
}
return nil
}