-
Notifications
You must be signed in to change notification settings - Fork 1
/
uptime_monitor.go
367 lines (315 loc) · 11.8 KB
/
uptime_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
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
359
360
361
362
363
364
365
366
367
package ilert
import (
"encoding/json"
"errors"
"fmt"
"net/url"
"strconv"
)
// Legacy API - uptime monitors deprecated and will be removed in the next major version of the sdk
type UptimeMonitor struct {
ID int64 `json:"id,omitempty"`
Name string `json:"name"`
Region string `json:"region"`
CheckType string `json:"checkType"`
CheckParams UptimeMonitorCheckParams `json:"checkParams"`
IntervalSec int `json:"intervalSec,omitempty"` // default: 300
TimeoutMs int `json:"timeoutMs,omitempty"` // default: 30000
CreateIncidentAfterFailedChecks int `json:"createIncidentAfterFailedChecks,omitempty"` // @deprecated
CreateAlertAfterFailedChecks int `json:"createAlertAfterFailedChecks,omitempty"` // default: 1
EscalationPolicy *EscalationPolicy `json:"escalationPolicy,omitempty"`
Paused bool `json:"paused,omitempty"` // default: false
EmbedURL string `json:"embedURL,omitempty"` // read only
ShareURL string `json:"shareURL,omitempty"` // read only
Status string `json:"status,omitempty"`
LastStatusChange string `json:"lastStatusChange,omitempty"` // Date time string in ISO format
}
// UptimeMonitorCheckParams definition
type UptimeMonitorCheckParams struct {
Host string `json:"host,omitempty"`
Port int `json:"port,omitempty"`
URL string `json:"url,omitempty"`
ResponseKeywords []string `json:"responseKeywords,omitempty"` // only for `http`
AlertBeforeSec int `json:"alertBeforeSec,omitempty"` // only for `ssl`
AlertOnFingerprintChange bool `json:"alertOnFingerprintChange"` // only for `ssl`
}
// UptimeMonitorStatuses defines uptime monitor statuses
var UptimeMonitorStatuses = struct {
Up string
Down string
Warning string
Paused string
Unknown string
}{
Up: "up",
Down: "down",
Warning: "warn",
Paused: "paused",
Unknown: "unknown",
}
// UptimeMonitorStatusesAll defines uptime monitor statuses list
var UptimeMonitorStatusesAll = []string{
UptimeMonitorStatuses.Up,
UptimeMonitorStatuses.Down,
UptimeMonitorStatuses.Warning,
UptimeMonitorStatuses.Paused,
UptimeMonitorStatuses.Unknown,
}
// UptimeMonitorRegions defines uptime monitor regions
var UptimeMonitorRegions = struct {
EU string
US string
}{
EU: "EU",
US: "US",
}
// UptimeMonitorRegionsAll defines uptime monitor regions list
var UptimeMonitorRegionsAll = []string{
UptimeMonitorRegions.EU,
UptimeMonitorRegions.US,
}
// UptimeMonitorCheckTypes defines uptime monitor check types
var UptimeMonitorCheckTypes = struct {
HTTP string
Ping string
TCP string
UDP string
SSL string
}{
HTTP: "http",
Ping: "ping",
TCP: "tcp",
UDP: "udp",
SSL: "ssl",
}
// UptimeMonitorCheckTypesAll defines uptime monitor check types list
var UptimeMonitorCheckTypesAll = []string{
UptimeMonitorCheckTypes.HTTP,
UptimeMonitorCheckTypes.Ping,
UptimeMonitorCheckTypes.TCP,
UptimeMonitorCheckTypes.UDP,
UptimeMonitorCheckTypes.SSL,
}
// CreateUptimeMonitorInput represents the input of a CreateUptimeMonitor operation.
type CreateUptimeMonitorInput struct {
_ struct{}
UptimeMonitor *UptimeMonitor
}
// CreateUptimeMonitorOutput represents the output of a CreateUptimeMonitor operation.
type CreateUptimeMonitorOutput struct {
_ struct{}
UptimeMonitor *UptimeMonitor
}
// CreateUptimeMonitor creates a new uptime monitor. https://api.ilert.com/api-docs/#tag/Uptime-Monitors/paths/~1uptime-monitors/post
func (c *Client) CreateUptimeMonitor(input *CreateUptimeMonitorInput) (*CreateUptimeMonitorOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
if input.UptimeMonitor == nil {
return nil, errors.New("uptime monitor input is required")
}
if input.UptimeMonitor.CreateAlertAfterFailedChecks != input.UptimeMonitor.CreateIncidentAfterFailedChecks {
input.UptimeMonitor.CreateAlertAfterFailedChecks = input.UptimeMonitor.CreateIncidentAfterFailedChecks
}
resp, err := c.httpClient.R().SetBody(input.UptimeMonitor).Post(apiRoutes.uptimeMonitors)
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 201); apiErr != nil {
return nil, apiErr
}
uptimeMonitor := &UptimeMonitor{}
err = json.Unmarshal(resp.Body(), uptimeMonitor)
if err != nil {
return nil, err
}
return &CreateUptimeMonitorOutput{UptimeMonitor: uptimeMonitor}, nil
}
// GetUptimeMonitorInput represents the input of a GetUptimeMonitor operation.
type GetUptimeMonitorInput struct {
_ struct{}
UptimeMonitorID *int64
}
// GetUptimeMonitorOutput represents the output of a GetUptimeMonitor operation.
type GetUptimeMonitorOutput struct {
_ struct{}
UptimeMonitor *UptimeMonitor
}
// GetUptimeMonitor gets the uptime monitor with specified id. https://api.ilert.com/api-docs/#tag/Uptime-Monitors/paths/~1uptime-monitors~1{id}/get
func (c *Client) GetUptimeMonitor(input *GetUptimeMonitorInput) (*GetUptimeMonitorOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
if input.UptimeMonitorID == nil {
return nil, errors.New("uptime monitor id is required")
}
resp, err := c.httpClient.R().Get(fmt.Sprintf("%s/%d", apiRoutes.uptimeMonitors, *input.UptimeMonitorID))
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 200); apiErr != nil {
return nil, apiErr
}
uptimeMonitor := &UptimeMonitor{}
err = json.Unmarshal(resp.Body(), uptimeMonitor)
if err != nil {
return nil, err
}
return &GetUptimeMonitorOutput{UptimeMonitor: uptimeMonitor}, nil
}
// GetUptimeMonitorsInput represents the input of a GetUptimeMonitors operation.
type GetUptimeMonitorsInput struct {
_ struct{}
// an integer specifying the starting point (beginning with 0) when paging through a list of entities
StartIndex *int
// the maximum number of results when paging through a list of entities.
// Maximum: 100
MaxResults *int
}
// GetUptimeMonitorsOutput represents the output of a GetUptimeMonitors operation.
type GetUptimeMonitorsOutput struct {
_ struct{}
UptimeMonitors []*UptimeMonitor
}
// GetUptimeMonitors lists existing uptime monitors. https://api.ilert.com/api-docs/#tag/Uptime-Monitors/paths/~1uptime-monitors/get
func (c *Client) GetUptimeMonitors(input *GetUptimeMonitorsInput) (*GetUptimeMonitorsOutput, error) {
q := url.Values{}
if input.StartIndex != nil {
q.Add("start-index", strconv.Itoa(*input.StartIndex))
}
if input.MaxResults != nil {
q.Add("max-results", strconv.Itoa(*input.MaxResults))
}
resp, err := c.httpClient.R().Get(fmt.Sprintf("%s?%s", apiRoutes.uptimeMonitors, q.Encode()))
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 200); apiErr != nil {
return nil, apiErr
}
uptimeMonitors := make([]*UptimeMonitor, 0)
err = json.Unmarshal(resp.Body(), &uptimeMonitors)
if err != nil {
return nil, err
}
return &GetUptimeMonitorsOutput{UptimeMonitors: uptimeMonitors}, nil
}
// SearchUptimeMonitorInput represents the input of a SearchUptimeMonitor operation.
type SearchUptimeMonitorInput struct {
_ struct{}
UptimeMonitorName *string
}
// SearchUptimeMonitorOutput represents the output of a SearchUptimeMonitor operation.
type SearchUptimeMonitorOutput struct {
_ struct{}
UptimeMonitor *UptimeMonitor
}
// SearchUptimeMonitor gets the uptime monitor with specified name.
func (c *Client) SearchUptimeMonitor(input *SearchUptimeMonitorInput) (*SearchUptimeMonitorOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
if input.UptimeMonitorName == nil {
return nil, errors.New("uptime monitor name is required")
}
resp, err := c.httpClient.R().Get(fmt.Sprintf("%s/name/%s", apiRoutes.uptimeMonitors, *input.UptimeMonitorName))
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 200); apiErr != nil {
return nil, apiErr
}
uptimeMonitor := &UptimeMonitor{}
err = json.Unmarshal(resp.Body(), uptimeMonitor)
if err != nil {
return nil, err
}
return &SearchUptimeMonitorOutput{UptimeMonitor: uptimeMonitor}, nil
}
// UpdateUptimeMonitorInput represents the input of a UpdateUptimeMonitor operation.
type UpdateUptimeMonitorInput struct {
_ struct{}
UptimeMonitorID *int64
UptimeMonitor *UptimeMonitor
}
// UpdateUptimeMonitorOutput represents the output of a UpdateUptimeMonitor operation.
type UpdateUptimeMonitorOutput struct {
_ struct{}
UptimeMonitor *UptimeMonitor
}
// UpdateUptimeMonitor updates an existing uptime monitor. https://api.ilert.com/api-docs/#tag/Uptime-Monitors/paths/~1uptime-monitors~1{id}/put
func (c *Client) UpdateUptimeMonitor(input *UpdateUptimeMonitorInput) (*UpdateUptimeMonitorOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
if input.UptimeMonitor == nil {
return nil, errors.New("uptime monitor input is required")
}
if input.UptimeMonitorID == nil {
return nil, errors.New("uptime monitor id is required")
}
resp, err := c.httpClient.R().SetBody(input.UptimeMonitor).Put(fmt.Sprintf("%s/%d", apiRoutes.uptimeMonitors, *input.UptimeMonitorID))
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 200); apiErr != nil {
return nil, apiErr
}
uptimeMonitor := &UptimeMonitor{}
err = json.Unmarshal(resp.Body(), uptimeMonitor)
if err != nil {
return nil, err
}
return &UpdateUptimeMonitorOutput{UptimeMonitor: uptimeMonitor}, nil
}
// DeleteUptimeMonitorInput represents the input of a DeleteUptimeMonitor operation.
type DeleteUptimeMonitorInput struct {
_ struct{}
UptimeMonitorID *int64
}
// DeleteUptimeMonitorOutput represents the output of a DeleteUptimeMonitor operation.
type DeleteUptimeMonitorOutput struct {
_ struct{}
}
// DeleteUptimeMonitor deletes the specified uptime monitor. https://api.ilert.com/api-docs/#tag/Uptime-Monitors/paths/~1uptime-monitors~1{id}/delete
func (c *Client) DeleteUptimeMonitor(input *DeleteUptimeMonitorInput) (*DeleteUptimeMonitorOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
if input.UptimeMonitorID == nil {
return nil, errors.New("uptime monitor id is required")
}
resp, err := c.httpClient.R().Delete(fmt.Sprintf("%s/%d", apiRoutes.uptimeMonitors, *input.UptimeMonitorID))
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 204); apiErr != nil {
return nil, apiErr
}
return &DeleteUptimeMonitorOutput{}, nil
}
// GetUptimeMonitorsCountInput represents the input of a GetUptimeMonitorsCount operation.
type GetUptimeMonitorsCountInput struct {
_ struct{}
}
// GetUptimeMonitorsCountOutput represents the output of a GetUptimeMonitorsCount operation.
type GetUptimeMonitorsCountOutput struct {
_ struct{}
Count int
}
// GetUptimeMonitorsCount gets the count of uptime monitors. https://api.ilert.com/api-docs/#tag/Uptime-Monitors/paths/~1uptime-monitors~1count/get
func (c *Client) GetUptimeMonitorsCount(input *GetUptimeMonitorsCountInput) (*GetUptimeMonitorsCountOutput, error) {
resp, err := c.httpClient.R().Get(fmt.Sprintf("%s/count", apiRoutes.uptimeMonitors))
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 200); apiErr != nil {
return nil, apiErr
}
body := &GenericCountResponse{}
err = json.Unmarshal(resp.Body(), body)
if err != nil {
return nil, err
}
return &GetUptimeMonitorsCountOutput{Count: body.Count}, nil
}