forked from AntoineAugusti/updown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checks.go
189 lines (161 loc) · 5.36 KB
/
checks.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
package updown
import (
"errors"
"fmt"
"net/http"
)
// SSL represents the SSL section of a check
type SSL struct {
TestedAt string `json:"tested_at,omitempty"`
Valid bool `json:"valid,omitempty"`
Error string `json:"error,omitempty"`
}
// Check represents a check performed by Updown on a regular basis
type Check struct {
Token string `json:"token,omitempty"`
URL string `json:"url,omitempty"`
Alias string `json:"alias,omitempty"`
LastStatus int `json:"last_status,omitempty"`
Uptime float64 `json:"uptime,omitempty"`
Down bool `json:"down"`
DownSince string `json:"down_since,omitempty"`
Error string `json:"error,omitempty"`
Period int `json:"period,omitempty"`
Apdex float64 `json:"apdex_t,omitempty"`
Enabled bool `json:"enabled"`
Published bool `json:"published"`
LastCheckAt string `json:"last_check_at,omitempty"`
NextCheckAt string `json:"next_check_at,omitempty"`
FaviconURL string `json:"favicon_url,omitempty"`
SSL SSL `json:"ssl,omitempty"`
StringMatch string `json:"string_match,omitempty"`
MuteUntil string `json:"mute_until,omitempty"`
DisabledLocations []string `json:"disabled_locations,omitempty"`
CustomHeaders map[string]string `json:"custom_headers,omitempty"`
}
// CheckItem represents a new check you want to be performed by Updown
type CheckItem struct {
// The URL you want to monitor
URL string `json:"url,omitempty"`
// Interval in seconds (30, 60, 120, 300 or 600)
Period int `json:"period,omitempty"`
// APDEX threshold in seconds (0.125, 0.25, 0.5 or 1.0)
Apdex float64 `json:"apdex_t,omitempty"`
// Is the check enabled
Enabled bool `json:"enabled"`
// Shall the status page be public
Published bool `json:"published"`
// Human readable name
Alias string `json:"alias,omitempty"`
// Search for this string in the page
StringMatch string `json:"string_match,omitempty"`
// Mute notifications until given time, accepts a time, 'recovery' or 'forever'
MuteUntil string `json:"mute_until,omitempty"`
// Disabled monitoring locations. It's an array of abbreviated location names
DisabledLocations []string `json:"disabled_locations,omitempty"`
// The HTTP headers you want in updown requests
CustomHeaders map[string]string `json:"custom_headers,omitempty"`
}
// CheckService interacts with the checks section of the API
type CheckService struct {
client *Client
cache Cache
}
type removeResponse struct {
Deleted bool `json:"deleted,omitempty"`
}
// ErrTokenNotFound indicates that we cannot find a token for the given name
var ErrTokenNotFound = errors.New("Could not determine a token for the given name")
// TokenForAlias finds the Updown token for a check's alias
func (s *CheckService) TokenForAlias(name string) (string, error) {
// Retrieve from cache
if has, val := s.cache.Get(name); has {
return val, nil
}
// List all checks
checks, _, err := s.List()
if err != nil {
return "", err
}
// And try to find the appropriate name
token, found := "", false
for _, check := range checks {
s.cache.Put(check.Alias, check.Token)
if check.Alias == name {
found, token = true, check.Token
}
}
if found {
return token, nil
}
// Could not find a match
return "", ErrTokenNotFound
}
// List lists all the checks
func (s *CheckService) List() ([]Check, *http.Response, error) {
req, err := s.client.NewRequest("GET", "checks", nil)
if err != nil {
return nil, nil, err
}
var res []Check
resp, err := s.client.Do(req, &res)
if err != nil {
return nil, resp, err
}
return res, resp, err
}
// Get gets a single check by its token
func (s *CheckService) Get(token string) (Check, *http.Response, error) {
req, err := s.client.NewRequest("GET", pathForToken(token), nil)
if err != nil {
return Check{}, nil, err
}
var res Check
resp, err := s.client.Do(req, &res)
if err != nil {
return Check{}, resp, err
}
return res, resp, err
}
// Add adds a new check you want to be performed
func (s *CheckService) Add(data CheckItem) (Check, *http.Response, error) {
req, err := s.client.NewRequest("POST", "checks", data)
if err != nil {
return Check{}, nil, err
}
var res Check
resp, err := s.client.Do(req, &res)
if err != nil {
return Check{}, resp, err
}
return res, resp, err
}
// Update updates a check performed by Updown
func (s *CheckService) Update(token string, data CheckItem) (Check, *http.Response, error) {
req, err := s.client.NewRequest("PUT", pathForToken(token), data)
if err != nil {
return Check{}, nil, err
}
var res Check
resp, err := s.client.Do(req, &res)
if err != nil {
return Check{}, resp, err
}
return res, resp, err
}
// Remove removes a check from Updown by its token
func (s *CheckService) Remove(token string) (bool, *http.Response, error) {
req, err := s.client.NewRequest("DELETE", pathForToken(token), nil)
if err != nil {
return false, nil, err
}
var res removeResponse
resp, err := s.client.Do(req, &res)
if err != nil {
return false, resp, err
}
return res.Deleted, resp, err
}
func pathForToken(token string) string {
return fmt.Sprintf("checks/%s", token)
}