-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathalerts.go
109 lines (95 loc) · 2.96 KB
/
alerts.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
package mackerel
import (
"fmt"
"net/url"
)
/*
{
"alerts": [
{
"id": "2wpLU5fBXbG",
"status": "CRITICAL",
"monitorId": "2cYjfibBkaj",
"type": "connectivity",
"openedAt": 1445399342,
"hostId": "2vJ965ygiXf"
},
{
"id": "2ust8jNxFH3",
"status": "CRITICAL",
"monitorId": "2cYjfibBkaj",
"type": "connectivity",
"openedAt": 1441939801,
"hostId": "2tFrtykgMib"
}
]
}
*/
// Alert information
type Alert struct {
ID string `json:"id,omitempty"`
Status string `json:"status,omitempty"`
MonitorID string `json:"monitorId,omitempty"`
Type string `json:"type,omitempty"`
HostID string `json:"hostId,omitempty"`
Value float64 `json:"value,omitempty"`
Message string `json:"message,omitempty"`
Reason string `json:"reason,omitempty"`
OpenedAt int64 `json:"openedAt,omitempty"`
ClosedAt int64 `json:"closedAt,omitempty"`
Memo string `json:"memo,omitempty"`
}
// AlertsResp includes alert and next id
type AlertsResp struct {
Alerts []*Alert `json:"alerts"`
NextID string `json:"nextId,omitempty"`
}
// UpdateAlertParam is for UpdateAlert
type UpdateAlertParam struct {
Memo string `json:"memo,omitempty"`
}
// UpdateAlertResponse is for UpdateAlert
type UpdateAlertResponse struct {
Memo string `json:"memo,omitempty"`
}
func (c *Client) findAlertsWithParams(params url.Values) (*AlertsResp, error) {
return requestGetWithParams[AlertsResp](c, "/api/v0/alerts", params)
}
// FindAlerts finds open alerts.
func (c *Client) FindAlerts() (*AlertsResp, error) {
return c.findAlertsWithParams(nil)
}
// FindAlertsByNextID finds next open alerts by next id.
func (c *Client) FindAlertsByNextID(nextID string) (*AlertsResp, error) {
params := url.Values{}
params.Set("nextId", nextID)
return c.findAlertsWithParams(params)
}
// FindWithClosedAlerts finds open and close alerts.
func (c *Client) FindWithClosedAlerts() (*AlertsResp, error) {
params := url.Values{}
params.Set("withClosed", "true")
return c.findAlertsWithParams(params)
}
// FindWithClosedAlertsByNextID finds open and close alerts by next id.
func (c *Client) FindWithClosedAlertsByNextID(nextID string) (*AlertsResp, error) {
params := url.Values{}
params.Set("nextId", nextID)
params.Set("withClosed", "true")
return c.findAlertsWithParams(params)
}
// GetAlert gets an alert.
func (c *Client) GetAlert(alertID string) (*Alert, error) {
path := fmt.Sprintf("/api/v0/alerts/%s", alertID)
return requestGet[Alert](c, path)
}
// CloseAlert closes an alert.
func (c *Client) CloseAlert(alertID string, reason string) (*Alert, error) {
path := fmt.Sprintf("/api/v0/alerts/%s/close", alertID)
return requestPost[Alert](c, path, map[string]string{"reason": reason})
}
// UpdateAlert updates an alert.
func (c *Client) UpdateAlert(alertID string, param UpdateAlertParam) (*UpdateAlertResponse, error) {
path := fmt.Sprintf("/api/v0/alerts/%s", alertID)
return requestPut[UpdateAlertResponse](c, path, param)
}