forked from russellcardullo/go-pingdom
-
Notifications
You must be signed in to change notification settings - Fork 7
/
check_types.go
358 lines (305 loc) · 11.9 KB
/
check_types.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
package pingdom
import (
"fmt"
"sort"
"strconv"
)
// HttpCheck represents a Pingdom HTTP check.
type HttpCheck struct {
Name string `json:"name"`
Hostname string `json:"hostname,omitempty"`
Resolution int `json:"resolution,omitempty"`
Paused bool `json:"paused,omitempty"`
SendNotificationWhenDown int `json:"sendnotificationwhendown,omitempty"`
NotifyAgainEvery int `json:"notifyagainevery,omitempty"`
NotifyWhenBackup bool `json:"notifywhenbackup,omitempty"`
Url string `json:"url,omitempty"`
Encryption bool `json:"encryption,omitempty"`
Port int `json:"port,omitempty"`
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
ShouldContain string `json:"shouldcontain,omitempty"`
ShouldNotContain string `json:"shouldnotcontain,omitempty"`
PostData string `json:"postdata,omitempty"`
RequestHeaders map[string]string `json:"requestheaders,omitempty"`
IntegrationIds []int `json:"integrationids,omitempty"`
ResponseTimeThreshold int `json:"responsetime_threshold,omitempty"`
Tags string `json:"tags,omitempty"`
ProbeFilters string `json:"probe_filters,omitempty"`
UserIds []int `json:"userids,omitempty"`
TeamIds []int `json:"teamids,omitempty"`
VerifyCertificate *bool `json:"verify_certificate,omitempty"`
SSLDownDaysBefore *int `json:"ssl_down_days_before,omitempty"`
}
// PingCheck represents a Pingdom ping check.
type PingCheck struct {
Name string `json:"name"`
Hostname string `json:"hostname,omitempty"`
Resolution int `json:"resolution,omitempty"`
Paused bool `json:"paused,omitempty"`
SendNotificationWhenDown int `json:"sendnotificationwhendown,omitempty"`
NotifyAgainEvery int `json:"notifyagainevery,omitempty"`
NotifyWhenBackup bool `json:"notifywhenbackup,omitempty"`
IntegrationIds []int `json:"integrationids,omitempty"`
Tags string `json:"tags,omitempty"`
ResponseTimeThreshold int `json:"responsetime_threshold,omitempty"`
ProbeFilters string `json:"probe_filters,omitempty"`
UserIds []int `json:"userids,omitempty"`
TeamIds []int `json:"teamids,omitempty"`
}
// TCPCheck represents a Pingdom TCP check.
type TCPCheck struct {
Name string `json:"name"`
Hostname string `json:"hostname,omitempty"`
Resolution int `json:"resolution,omitempty"`
Paused bool `json:"paused,omitempty"`
SendNotificationWhenDown int `json:"sendnotificationwhendown,omitempty"`
NotifyAgainEvery int `json:"notifyagainevery,omitempty"`
NotifyWhenBackup bool `json:"notifywhenbackup,omitempty"`
IntegrationIds []int `json:"integrationids,omitempty"`
Tags string `json:"tags,omitempty"`
ProbeFilters string `json:"probe_filters,omitempty"`
UserIds []int `json:"userids,omitempty"`
TeamIds []int `json:"teamids,omitempty"`
Port int `json:"port"`
StringToSend string `json:"stringtosend,omitempty"`
StringToExpect string `json:"stringtoexpect,omitempty"`
}
// SummaryPerformanceRequest is the API request to Pingdom for a SummaryPerformance.
type SummaryPerformanceRequest struct {
Id int
From int
To int
Resolution string
IncludeUptime bool
Probes string
Order string
}
// PutParams returns a map of parameters for an HttpCheck that can be sent along
// with an HTTP PUT request.
func (ck *HttpCheck) PutParams() map[string]string {
m := map[string]string{
"name": ck.Name,
"host": ck.Hostname,
"resolution": strconv.Itoa(ck.Resolution),
"paused": strconv.FormatBool(ck.Paused),
"notifyagainevery": strconv.Itoa(ck.NotifyAgainEvery),
"notifywhenbackup": strconv.FormatBool(ck.NotifyWhenBackup),
"url": ck.Url,
"encryption": strconv.FormatBool(ck.Encryption),
"postdata": ck.PostData,
"integrationids": intListToCDString(ck.IntegrationIds),
"tags": ck.Tags,
"probe_filters": ck.ProbeFilters,
"userids": intListToCDString(ck.UserIds),
"teamids": intListToCDString(ck.TeamIds),
}
// Ignore zero values
if ck.Port != 0 {
m["port"] = strconv.Itoa(ck.Port)
}
if ck.SendNotificationWhenDown != 0 {
m["sendnotificationwhendown"] = strconv.Itoa(ck.SendNotificationWhenDown)
}
if ck.ResponseTimeThreshold != 0 {
m["responsetime_threshold"] = strconv.Itoa(ck.ResponseTimeThreshold)
}
if ck.VerifyCertificate != nil {
m["verify_certificate"] = strconv.FormatBool(*ck.VerifyCertificate)
}
if ck.SSLDownDaysBefore != nil {
m["ssl_down_days_before"] = strconv.Itoa(*ck.SSLDownDaysBefore)
}
// ShouldContain and ShouldNotContain are mutually exclusive.
// But we must define one so they can be emptied if required.
if ck.ShouldContain != "" {
m["shouldcontain"] = ck.ShouldContain
} else {
m["shouldnotcontain"] = ck.ShouldNotContain
}
// Convert auth
if ck.Username != "" {
m["auth"] = fmt.Sprintf("%s:%s", ck.Username, ck.Password)
}
// Convert headers
var headers []string
for k := range ck.RequestHeaders {
headers = append(headers, k)
}
sort.Strings(headers)
for i, k := range headers {
m[fmt.Sprintf("requestheader%d", i)] = fmt.Sprintf("%s:%s", k, ck.RequestHeaders[k])
}
return m
}
// PostParams returns a map of parameters for an HttpCheck that can be sent along
// with an HTTP POST request. They are the same than the Put params, but
// empty strings cleared out, to avoid Pingdom API reject the request.
func (ck *HttpCheck) PostParams() map[string]string {
params := ck.PutParams()
for k, v := range params {
if v == "" {
delete(params, k)
}
}
params["type"] = "http"
return params
}
// Valid determines whether the HttpCheck contains valid fields. This can be
// used to guard against sending illegal values to the Pingdom API.
func (ck *HttpCheck) Valid() error {
if ck.Name == "" {
return fmt.Errorf("Invalid value for `Name`. Must contain non-empty string")
}
if ck.Hostname == "" {
return fmt.Errorf("Invalid value for `Hostname`. Must contain non-empty string")
}
if ck.Resolution != 1 && ck.Resolution != 5 && ck.Resolution != 15 &&
ck.Resolution != 30 && ck.Resolution != 60 {
return fmt.Errorf("invalid value %v for `Resolution`, allowed values are [1,5,15,30,60]", ck.Resolution)
}
if ck.ShouldContain != "" && ck.ShouldNotContain != "" {
return fmt.Errorf("`ShouldContain` and `ShouldNotContain` must not be declared at the same time")
}
return nil
}
// PutParams returns a map of parameters for a PingCheck that can be sent along
// with an HTTP PUT request.
func (ck *PingCheck) PutParams() map[string]string {
m := map[string]string{
"name": ck.Name,
"host": ck.Hostname,
"resolution": strconv.Itoa(ck.Resolution),
"paused": strconv.FormatBool(ck.Paused),
"notifyagainevery": strconv.Itoa(ck.NotifyAgainEvery),
"notifywhenbackup": strconv.FormatBool(ck.NotifyWhenBackup),
"integrationids": intListToCDString(ck.IntegrationIds),
"probe_filters": ck.ProbeFilters,
"userids": intListToCDString(ck.UserIds),
"teamids": intListToCDString(ck.TeamIds),
}
if ck.SendNotificationWhenDown != 0 {
m["sendnotificationwhendown"] = strconv.Itoa(ck.SendNotificationWhenDown)
}
if ck.ResponseTimeThreshold != 0 {
m["responsetime_threshold"] = strconv.Itoa(ck.ResponseTimeThreshold)
}
return m
}
// PostParams returns a map of parameters for a PingCheck that can be sent along
// with an HTTP POST request. Same as PUT.
func (ck *PingCheck) PostParams() map[string]string {
params := ck.PutParams()
for k, v := range params {
if v == "" {
delete(params, k)
}
}
params["type"] = "ping"
return params
}
// Valid determines whether the PingCheck contains valid fields. This can be
// used to guard against sending illegal values to the Pingdom API.
func (ck *PingCheck) Valid() error {
if ck.Name == "" {
return fmt.Errorf("Invalid value for `Name`. Must contain non-empty string")
}
if ck.Hostname == "" {
return fmt.Errorf("Invalid value for `Hostname`. Must contain non-empty string")
}
if ck.Resolution != 1 && ck.Resolution != 5 && ck.Resolution != 15 &&
ck.Resolution != 30 && ck.Resolution != 60 {
return fmt.Errorf("invalid value %v for `Resolution`, allowed values are [1,5,15,30,60]", ck.Resolution)
}
return nil
}
// PutParams returns a map of parameters for a TCPCheck that can be sent along
// with an HTTP PUT request.
func (ck *TCPCheck) PutParams() map[string]string {
m := map[string]string{
"name": ck.Name,
"host": ck.Hostname,
"resolution": strconv.Itoa(ck.Resolution),
"paused": strconv.FormatBool(ck.Paused),
"notifyagainevery": strconv.Itoa(ck.NotifyAgainEvery),
"notifywhenbackup": strconv.FormatBool(ck.NotifyWhenBackup),
"integrationids": intListToCDString(ck.IntegrationIds),
"probe_filters": ck.ProbeFilters,
"tags": ck.Tags,
"userids": intListToCDString(ck.UserIds),
"teamids": intListToCDString(ck.TeamIds),
"port": strconv.Itoa(ck.Port),
}
if ck.SendNotificationWhenDown != 0 {
m["sendnotificationwhendown"] = strconv.Itoa(ck.SendNotificationWhenDown)
}
if ck.StringToSend != "" {
m["stringtosend"] = ck.StringToSend
}
if ck.StringToExpect != "" {
m["stringtoexpect"] = ck.StringToExpect
}
return m
}
// PostParams returns a map of parameters for a TCPCheck that can be sent along
// with an HTTP POST request. Same as PUT.
func (ck *TCPCheck) PostParams() map[string]string {
params := ck.PutParams()
for k, v := range params {
if v == "" {
delete(params, k)
}
}
params["type"] = "tcp"
return params
}
// Valid determines whether the TCPCheck contains valid fields. This can be
// used to guard against sending illegal values to the Pingdom API.
func (ck *TCPCheck) Valid() error {
if ck.Name == "" {
return fmt.Errorf("invalid value for `Name`, must contain non-empty string")
}
if ck.Hostname == "" {
return fmt.Errorf("invalid value for `Hostname`, must contain non-empty string")
}
if ck.Resolution != 1 && ck.Resolution != 5 && ck.Resolution != 15 &&
ck.Resolution != 30 && ck.Resolution != 60 {
return fmt.Errorf("invalid value %v for `Resolution`, allowed values are [1,5,15,30,60]", ck.Resolution)
}
if ck.Port < 1 {
return fmt.Errorf("Invalid value for `Port`. Must contain an integer >= 1")
}
return nil
}
func intListToCDString(integers []int) string {
var CDString string
for i, item := range integers {
if i == 0 {
CDString = strconv.Itoa(item)
} else {
CDString = fmt.Sprintf("%v,%d", CDString, item)
}
}
return CDString
}
// Valid determines whether a SummaryPerformanceRequest contains valid fields for the Pingdom API.
func (csr SummaryPerformanceRequest) Valid() error {
if csr.Id == 0 {
return ErrMissingId
}
if csr.Resolution != "" && csr.Resolution != "hour" && csr.Resolution != "day" && csr.Resolution != "week" {
return ErrBadResolution
}
return nil
}
// GetParams returns a map of params for a Pingdom SummaryPerformanceRequest.
func (csr SummaryPerformanceRequest) GetParams() (params map[string]string) {
params = make(map[string]string)
if csr.Resolution != "" {
params["resolution"] = csr.Resolution
}
if csr.IncludeUptime {
params["includeuptime"] = "true"
}
return
}