forked from yteraoka/check_http_go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_http_go.go
244 lines (212 loc) · 7.08 KB
/
check_http_go.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
package main
import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"github.com/icza/dyno"
flags "github.com/jessevdk/go-flags"
"golang.org/x/net/http2"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"
)
// https://golang.org/pkg/net/http/
// https://godoc.org/github.com/jessevdk/go-flags
// https://qiita.com/t-mochizuki/items/4ffc478fedae7b776805
type Options struct {
Verbose bool `short:"v" long:"verbose" description:"Show verbose debug information"`
Vhost string `short:"H" long:"vhost" description:"Host header"`
Ipaddr string `short:"I" long:"ipaddr" description:"IP address"`
Port int `short:"p" long:"port" description:"TCP Port" default:"0"`
Warn float64 `short:"w" long:"warn" description:"Warning time in second" default:"5.0"`
Crit float64 `short:"c" long:"crit" description:"Critical time in second" default:"10.0"`
Headers []string `short:"k" long:"header" description:"additional headers, acceptable multiple times"`
Timeout int `short:"t" long:"timeout" description:"Timeout in second" default:"10"`
Uri string `short:"u" long:"uri" description:"URI" default:"/"`
Ssl bool `short:"S" long:"ssl" description:"Enable TLS"`
Expect string `short:"e" long:"expect" description:"Expected status codes (csv)" default:""`
JsonKey string `long:"json-key" description:"JSON key "`
JsonValue string `long:"json-value" description:"Expected json value"`
Method string `short:"j" long:"method" description:"HTTP METHOD (GET, HEAD, POST)" default:"GET"`
UserAgent string `short:"A" long:"useragent" description:"User-Agent header" default:"check_http_go"`
ClientCertFile string `short:"J" long:"client-cert" description:"Client Certificate File"`
PrivateKeyFile string `short:"K" long:"private-key" description:"Private Key File"`
Version bool `long:"version" description:"Print version"`
}
const (
NagiosOk = 0
NagiosWarning = 1
NagiosCritical = 2
NagiosUnknown = 3
Version = "0.2"
)
func genTlsConfig(opts Options) *tls.Config {
conf := &tls.Config{}
conf.InsecureSkipVerify = true
if opts.ClientCertFile != "" && opts.PrivateKeyFile != "" {
cert, err := tls.LoadX509KeyPair(opts.ClientCertFile, opts.PrivateKeyFile)
if err != nil {
fmt.Printf("HTTP UNKNOWN - %s\n", err)
os.Exit(NagiosUnknown)
}
conf.Certificates = []tls.Certificate{cert}
}
return conf
}
func prettyPrintJSON(b []byte) ([]byte, error) {
var out bytes.Buffer
err := json.Indent(&out, b, "", " ")
return out.Bytes(), err
}
func main() {
var opts Options
var result_message string
var host_header string
var additional_out []byte
scheme := "http"
_, err := flags.Parse(&opts)
if err != nil {
os.Exit(NagiosUnknown)
}
if opts.Version {
fmt.Printf("check_http_go: %s\n", Version)
os.Exit(0)
}
if opts.Ipaddr == "" && opts.Vhost != "" {
opts.Ipaddr = opts.Vhost
}
if opts.Ipaddr == "" {
os.Exit(NagiosUnknown)
}
host_header = opts.Ipaddr
if opts.Vhost != "" {
host_header = opts.Vhost
}
if opts.Port == 0 {
if opts.Ssl {
scheme = "https"
opts.Port = 443
} else {
opts.Port = 80
}
}
if opts.Ssl {
scheme = "https"
}
// https://golang.org/pkg/crypto/tls/#Config
tr := &http.Transport{
TLSClientConfig: genTlsConfig(opts),
}
// https://github.com/golang/go/issues/17051
// https://qiita.com/catatsuy/items/ee4fc094c6b9c39ee08f
if err := http2.ConfigureTransport(tr); err != nil {
log.Fatalf("Failed to configure h2 transport: %s", err)
}
c := &http.Client{
Timeout: time.Duration(opts.Timeout) * time.Second,
// https://jonathanmh.com/tracing-preventing-http-redirects-golang/
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
Transport: tr,
}
url_str := scheme + "://" + opts.Ipaddr + ":" + strconv.Itoa(opts.Port) + opts.Uri
values := url.Values{}
req, err := http.NewRequest(opts.Method, url_str, strings.NewReader(values.Encode()))
if err != nil {
fmt.Printf("HTTP UNKNOWN - %s\n", err)
os.Exit(NagiosUnknown)
}
req.Host = host_header
req.Header.Set("User-Agent", opts.UserAgent)
for _, header := range opts.Headers {
hdr := strings.SplitN(header, ": ", 2)
req.Header.Set(hdr[0], hdr[1])
}
t1 := time.Now()
resp, err := c.Do(req)
if err != nil {
fmt.Printf("HTTP CRITICAL - %s\n", err)
os.Exit(NagiosCritical)
}
defer resp.Body.Close()
buf, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Printf("HTTP CRITICAL - %s\n", err)
os.Exit(NagiosCritical)
}
t2 := time.Now()
diff := t2.Sub(t1)
status_text := strconv.Itoa(resp.StatusCode)
size := len(buf)
if opts.Verbose {
fmt.Print(string(buf))
}
nagios_status := NagiosOk
if opts.Expect == "" {
if resp.StatusCode >= 500 {
nagios_status = NagiosCritical
result_message = fmt.Sprintf("Unexpected http status code: %d", resp.StatusCode)
} else if resp.StatusCode >= 400 {
nagios_status = NagiosWarning
result_message = fmt.Sprintf("Unexpected http status code: %d", resp.StatusCode)
}
} else {
nagios_status = NagiosWarning
for _, expect := range strings.Split(opts.Expect, ",") {
if status_text == expect {
nagios_status = NagiosOk
}
}
if nagios_status == NagiosWarning {
result_message = fmt.Sprintf("Unexpected http status code: %d", resp.StatusCode)
}
}
if opts.JsonKey != "" && opts.JsonValue != "" {
// https://stackoverflow.com/questions/27689058/convert-string-to-interface
t := strings.Split(opts.JsonKey, ".")
s := make([]interface{}, len(t))
for i, v := range t {
s[i] = v
}
// https://reformatcode.com/code/json/taking-a-json-string-unmarshaling-it-into-a-mapstringinterface-editing-and-marshaling-it-into-a-byte-seems-more-complicated-then-it-should-be
var d map[string]interface{}
json.Unmarshal(buf, &d)
// https://qiita.com/hnakamur/items/c3560a4b780487ef6065
v, _ := dyno.Get(d, s...)
if fmt.Sprintf("%v", v) != opts.JsonValue {
nagios_status = NagiosCritical
result_message = fmt.Sprintf("`%s` is not `%s`", opts.JsonKey, opts.JsonValue)
}
additional_out, err = prettyPrintJSON(buf)
}
if nagios_status == NagiosOk {
if diff.Seconds() > opts.Crit {
nagios_status = NagiosCritical
result_message = fmt.Sprintf("response time %3.fs exceeded critical threshold %.3fs", diff.Seconds(), opts.Crit)
} else if diff.Seconds() > opts.Warn {
nagios_status = NagiosWarning
result_message = fmt.Sprintf("response time %3.fs exceeded warning threshold %.3fs", diff.Seconds(), opts.Warn)
}
}
result_str := "OK"
if nagios_status == NagiosWarning {
result_str = "WARNING"
} else if nagios_status == NagiosCritical {
result_str = "CRITICAL"
}
fmt.Printf("HTTP %s: %s %s - %d bytes in %.3f second response time |time=%.6fs;;;%.6f size=%dB;;;0\n", result_str, resp.Proto, resp.Status, size, diff.Seconds(), diff.Seconds(), 0.0, size)
if result_message != "" {
fmt.Println(result_message)
}
if len(additional_out) > 0 {
fmt.Printf("\n%s", additional_out)
}
os.Exit(nagios_status)
}