forked from bosun-monitor/bosun
-
Notifications
You must be signed in to change notification settings - Fork 1
/
httpunit.go
82 lines (75 loc) · 2.85 KB
/
httpunit.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
package collectors
import (
"fmt"
"time"
"bosun.org/_third_party/github.com/BurntSushi/toml"
"bosun.org/_third_party/github.com/StackExchange/httpunit"
"bosun.org/metadata"
"bosun.org/opentsdb"
)
func HTTPUnitTOML(filename string) error {
var plans httpunit.Plans
if _, err := toml.DecodeFile(filename, &plans); err != nil {
return err
}
HTTPUnitPlans(filename, &plans)
return nil
}
func HTTPUnitHiera(filename string) error {
plans, err := httpunit.ExtractHiera(filename)
if err != nil {
return err
}
HTTPUnitPlans(filename, &httpunit.Plans{
Plans: plans,
})
return nil
}
func HTTPUnitPlans(name string, plans *httpunit.Plans) {
collectors = append(collectors, &IntervalCollector{
F: func() (opentsdb.MultiDataPoint, error) {
return cHTTPUnit(plans)
},
name: fmt.Sprintf("c_httpunit_%s", name),
Interval: time.Minute * 5,
})
}
func cHTTPUnit(plans *httpunit.Plans) (opentsdb.MultiDataPoint, error) {
ch, _, err := plans.Test("", false)
if err != nil {
return nil, err
}
var md opentsdb.MultiDataPoint
for r := range ch {
tags := opentsdb.TagSet{
"protocol": r.Case.URL.Scheme,
"ip": r.Case.IP.String(),
"url_host": r.Case.URL.Host,
"hc_test_case": r.Plan.Label,
}
Add(&md, "hu.error", r.Result.Result != nil, tags, metadata.Gauge, metadata.Bool, descHTTPUnitError)
Add(&md, "hu.socket_connected", r.Result.Connected, tags, metadata.Gauge, metadata.Bool, descHTTPUnitSocketConnected)
switch r.Case.URL.Scheme {
case "http", "https":
Add(&md, "hu.http.got_expected_code", r.Result.GotCode, tags, metadata.Gauge, metadata.Bool, descHTTPUnitExpectedCode)
Add(&md, "hu.http.got_expected_text", r.Result.GotText, tags, metadata.Gauge, metadata.Bool, descHTTPUnitExpectedText)
Add(&md, "hu.http.got_expected_regex", r.Result.GotRegex, tags, metadata.Gauge, metadata.Bool, descHTTPUnitExpectedRegex)
if r.Case.URL.Scheme == "https" {
Add(&md, "hu.cert.valid", !r.Result.InvalidCert, tags, metadata.Gauge, metadata.Bool, descHTTPUnitCertValid)
if resp := r.Result.Resp; resp != nil && resp.TLS != nil && len(resp.TLS.PeerCertificates) > 0 {
Add(&md, "hu.cert.expires", resp.TLS.PeerCertificates[0].NotAfter.Unix(), tags, metadata.Gauge, metadata.Timestamp, descHTTPUnitCertExpires)
}
}
}
}
return md, nil
}
const (
descHTTPUnitError = "1 if any error (no connection, unexpected code or text) occurred, else 0."
descHTTPUnitSocketConnected = "1 if a connection was made, else 0."
descHTTPUnitExpectedCode = "1 if the HTTP status code was expected, else 0."
descHTTPUnitExpectedText = "1 if the response contained expected text, else 0."
descHTTPUnitExpectedRegex = "1 if the response matched expected regex, else 0."
descHTTPUnitCertValid = "1 if the SSL certificate is valid, else 0."
descHTTPUnitCertExpires = "Unix epoch time of the certificate expiration."
)