-
Notifications
You must be signed in to change notification settings - Fork 33
/
alerts.go
168 lines (148 loc) · 3.86 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
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
package mackerel
import (
"encoding/json"
"fmt"
"net/http"
"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) findAlertsWithParam(v url.Values) (*AlertsResp, error) {
var d AlertsResp
u := c.urlFor("/api/v0/alerts")
u.RawQuery = v.Encode()
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
return nil, err
}
resp, err := c.Request(req)
defer closeResponse(resp)
if err != nil {
return nil, err
}
err = json.NewDecoder(resp.Body).Decode(&d)
if err != nil {
return nil, err
}
return &d, err
}
// FindAlerts find open alerts
func (c *Client) FindAlerts() (*AlertsResp, error) {
return c.findAlertsWithParam(url.Values{})
}
// FindAlertsByNextID find next open alerts by next id
func (c *Client) FindAlertsByNextID(nextID string) (*AlertsResp, error) {
v := url.Values{}
v.Set("nextId", nextID)
return c.findAlertsWithParam(v)
}
// FindWithClosedAlerts find open and close alerts
func (c *Client) FindWithClosedAlerts() (*AlertsResp, error) {
v := url.Values{}
v.Set("withClosed", "true")
return c.findAlertsWithParam(v)
}
// FindWithClosedAlertsByNextID find open and close alerts by next id
func (c *Client) FindWithClosedAlertsByNextID(nextID string) (*AlertsResp, error) {
v := url.Values{}
v.Set("nextId", nextID)
v.Set("withClosed", "true")
return c.findAlertsWithParam(v)
}
// GetAlert gets Alert
func (c *Client) GetAlert(alertID string) (*Alert, error) {
req, err := http.NewRequest("GET", c.urlFor(fmt.Sprintf("/api/v0/alerts/%s", alertID)).String(), nil)
if err != nil {
return nil, err
}
resp, err := c.Request(req)
defer closeResponse(resp)
if err != nil {
return nil, err
}
var alert *Alert
err = json.NewDecoder(resp.Body).Decode(&alert)
if err != nil {
return nil, err
}
return alert, err
}
// CloseAlert close alert
func (c *Client) CloseAlert(alertID string, reason string) (*Alert, error) {
var reqBody struct {
Reason string `json:"reason"`
}
reqBody.Reason = reason
resp, err := c.PostJSON(fmt.Sprintf("/api/v0/alerts/%s/close", alertID), &reqBody)
defer closeResponse(resp)
if err != nil {
return nil, err
}
var data *Alert
err = json.NewDecoder(resp.Body).Decode(&data)
if err != nil {
return nil, err
}
return data, nil
}
// UpdateAlert updates an Alert
func (c *Client) UpdateAlert(alertID string, param UpdateAlertParam) (*UpdateAlertResponse, error) {
resp, err := c.PutJSON(fmt.Sprintf("/api/v0/alerts/%s", alertID), param)
defer closeResponse(resp)
if err != nil {
return nil, err
}
var data UpdateAlertResponse
err = json.NewDecoder(resp.Body).Decode(&data)
if err != nil {
return nil, err
}
return &data, nil
}