-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
151 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package mackerel | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
/* | ||
{ | ||
"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 uint64 `json:"value,omitempty"` | ||
Message string `json:"message,omitempty"` | ||
Reason string `json:"reason,omitempty"` | ||
OpenedAt uint64 `json:"openedAt,omitempty"` | ||
ClosedAt uint64 `json:"closedAt,omitempty"` | ||
} | ||
|
||
// FindAlerts find monitors | ||
func (c *Client) FindAlerts() ([]*Alert, error) { | ||
req, err := http.NewRequest("GET", c.urlFor("/api/v0/alerts").String(), nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
resp, err := c.Request(req) | ||
defer closeResponse(resp) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var data struct { | ||
Alerts []*(Alert) `json:"alerts"` | ||
} | ||
err = json.NewDecoder(resp.Body).Decode(&data) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return data.Alerts, err | ||
} | ||
|
||
// CloseAlert update monitor | ||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package mackerel | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
func TestFindAlerts(t *testing.T) { | ||
ts := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { | ||
if req.URL.Path != "/api/v0/alerts" { | ||
t.Error("request URL should be /api/v0/alerts but :", req.URL.Path) | ||
} | ||
|
||
respJSON, _ := json.Marshal(map[string][]map[string]interface{}{ | ||
"alerts": []map[string]interface{}{ | ||
map[string]interface{}{ | ||
"id": "2wpLU5fBXbG", | ||
"status": "CRITICAL", | ||
"monitorId": "2cYjfibBkaj", | ||
"type": "connectivity", | ||
"openedAt": 1445399342, | ||
"hostId": "2vJ965ygiXf", | ||
}, | ||
map[string]interface{}{ | ||
"id": "2ust8jNxFH3", | ||
"status": "CRITICAL", | ||
"monitorId": "2cYjfibBkaj", | ||
"type": "connectivity", | ||
"openedAt": 1441939801, | ||
"hostId": "2tFrtykgMib", | ||
}, | ||
}, | ||
}) | ||
|
||
res.Header()["Content-Type"] = []string{"application/json"} | ||
fmt.Fprint(res, string(respJSON)) | ||
})) | ||
defer ts.Close() | ||
|
||
client, _ := NewClientWithOptions("dummy-key", ts.URL, false) | ||
alerts, err := client.FindAlerts() | ||
|
||
if err != nil { | ||
t.Error("err shoud be nil but: ", err) | ||
} | ||
|
||
if alerts[0].Type != "connectivity" { | ||
t.Error("request sends json including type but: ", alerts[0]) | ||
} | ||
|
||
if alerts[1].Type != "connectivity" { | ||
t.Error("request sends json including type but: ", alerts[1]) | ||
} | ||
|
||
if alerts[1].Status != "CRITICAL" { | ||
t.Error("request sends json including status but: ", alerts[1]) | ||
} | ||
|
||
if alerts[1].OpenedAt != 1441939801 { | ||
t.Error("request sends json including openedAt but: ", alerts[1]) | ||
} | ||
} |