-
Notifications
You must be signed in to change notification settings - Fork 1
/
http_monitor.go
126 lines (104 loc) · 2.89 KB
/
http_monitor.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
package gerty
import (
"io"
"net/http"
"strings"
"time"
)
// ensure we always implement Monitor
var _ Monitor = (*HttpMonitor)(nil)
type SuccessChecker func(*http.Response) bool
type HttpMonitor struct {
*BaseMonitor
url string
buffer CircularBuffer
opts *HttpMonitorOptions
}
type HttpMonitorOptions struct {
Checks int
Method string
Cookies []http.Cookie
Header http.Header
Timeout time.Duration
Successful SuccessChecker
Body string
}
var DefaultHttpMonitorOptions = HttpMonitorOptions{
Checks: 5,
Method: "GET",
Cookies: []http.Cookie{},
Header: http.Header{},
Timeout: 10 * time.Second,
Successful: defaultSuccessChecker,
Body: "",
}
func mergeHttpOpts(given *HttpMonitorOptions) *HttpMonitorOptions {
if given == nil {
return &DefaultHttpMonitorOptions
}
if given.Checks <= 0 {
given.Checks = DefaultHttpMonitorOptions.Checks
}
if len(given.Method) <= 0 {
given.Method = DefaultHttpMonitorOptions.Method
}
if given.Timeout <= 0 {
given.Timeout = DefaultHttpMonitorOptions.Timeout
}
if given.Successful == nil {
given.Successful = DefaultHttpMonitorOptions.Successful
}
return given
}
func NewHttpMonitorWithOptions(title, description, url string, _opts *HttpMonitorOptions) *HttpMonitor {
opts := mergeHttpOpts(_opts)
buffer := NewCircularBuffer(opts.Checks)
return &HttpMonitor{NewBaseMonitor(title, description), url, buffer, opts}
}
func NewHttpMonitor(title, description, url string) *HttpMonitor {
return NewHttpMonitorWithOptions(title, description, url, nil)
}
func defaultSuccessChecker(response *http.Response) bool {
return response.StatusCode >= 200 && response.StatusCode < 400
}
func addHeader(request *http.Request, header *http.Header) {
for key, value := range *header {
request.Header.Add(key, value[0])
}
}
func addCookies(request *http.Request, cookies *[]http.Cookie) {
for _, c := range *cookies {
request.AddCookie(&c)
}
}
func (monitor *HttpMonitor) Check() Result {
logger.Printf("checking monitor %s", monitor.Name())
client := http.Client{Timeout: monitor.opts.Timeout}
var body io.Reader = nil
if len(monitor.opts.Body) > 0 {
body = strings.NewReader(monitor.opts.Body)
}
req, err := http.NewRequest(monitor.opts.Method, monitor.url, body)
if err != nil {
logger.Fatalf("can't ping malformed URL: %s", monitor.url)
}
addHeader(req, &monitor.opts.Header)
addCookies(req, &monitor.opts.Cookies)
resp, err := client.Do(req)
if err != nil {
logger.Printf("%s monitor check failed with error: %v", monitor.Name(), err)
monitor.buffer.Append(NOK)
return NOK
}
if monitor.opts.Successful(resp) {
monitor.buffer.Append(OK)
return OK
} else {
logger.Printf("%s monitor check failed, status = %d", monitor.Name(), resp.StatusCode)
monitor.buffer.Append(NOK)
return NOK
}
}
func (monitor *HttpMonitor) Values() []ValueWithTimestamp {
return monitor.buffer.GetValues()
}