Get relevant incident, fix config bug
- Make generic makeRequest fn
This commit is contained in:
@@ -26,8 +26,8 @@ func init() {
|
||||
var data []byte
|
||||
|
||||
// test if its a url
|
||||
_, err := url.ParseRequestURI(configPath)
|
||||
if err == nil {
|
||||
url, err := url.ParseRequestURI(configPath)
|
||||
if err == nil && len(url.Scheme) > 0 {
|
||||
// download config
|
||||
response, err := http.Get(configPath)
|
||||
if err != nil {
|
||||
|
||||
@@ -2,10 +2,7 @@ package cachet
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"bytes"
|
||||
"io/ioutil"
|
||||
"strconv"
|
||||
"net/http"
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
@@ -25,35 +22,44 @@ type IncidentData struct {
|
||||
Incident Incident `json:"data"`
|
||||
}
|
||||
|
||||
type IncidentList struct {
|
||||
Incidents []Incident `json:"data"`
|
||||
}
|
||||
|
||||
func GetIncidents() []Incident {
|
||||
_, body, err := makeRequest("GET", "/incidents", nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
var data IncidentList
|
||||
err = json.Unmarshal(body, &data)
|
||||
if err != nil {
|
||||
fmt.Println("Cannot parse incidents.")
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return data.Incidents
|
||||
}
|
||||
|
||||
func (incident *Incident) Send() {
|
||||
jsonBytes, err := json.Marshal(incident)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
var req *http.Request
|
||||
if incident.Id == 0 {
|
||||
req, err = http.NewRequest("POST", Config.API_Url + "/incidents", bytes.NewBuffer(jsonBytes))
|
||||
} else {
|
||||
req, err = http.NewRequest("PUT", Config.API_Url + "/incidents/" + strconv.Itoa(incident.Id), bytes.NewBuffer(jsonBytes))
|
||||
requestType := "POST"
|
||||
requestUrl := "/incidents"
|
||||
if incident.Id > 0 {
|
||||
requestType = "PUT"
|
||||
requestUrl = "/incidents/" + strconv.Itoa(incident.Id)
|
||||
}
|
||||
|
||||
resp, body, err := makeRequest(requestType, requestUrl, jsonBytes)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("X-Cachet-Token", Config.API_Token)
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, _ := ioutil.ReadAll(resp.Body)
|
||||
fmt.Println(strconv.Itoa(resp.StatusCode) + " " + string(body))
|
||||
|
||||
var data IncidentData
|
||||
@@ -72,6 +78,18 @@ func (incident *Incident) Send() {
|
||||
}
|
||||
}
|
||||
|
||||
func (incident *Incident) GetSimilarIncidentId() {
|
||||
incidents := GetIncidents()
|
||||
|
||||
for _, inc := range incidents {
|
||||
if incident.Name == inc.Name && incident.Message == inc.Message && incident.Status == inc.Status && incident.Human_status == inc.Human_status {
|
||||
incident.Id = inc.Id
|
||||
fmt.Printf("Updated incident id to %v\n", inc.Id)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (incident *Incident) SetInvestigating() {
|
||||
incident.Status = 1
|
||||
incident.Human_status = "Investigating"
|
||||
|
||||
@@ -2,9 +2,7 @@ package cachet
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"bytes"
|
||||
"strconv"
|
||||
"net/http"
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
@@ -17,21 +15,9 @@ func SendMetric(metricId int, delay int64) {
|
||||
"value": delay,
|
||||
})
|
||||
|
||||
client := &http.Client{}
|
||||
req, _ := http.NewRequest("POST", Config.API_Url + "/metrics/" + strconv.Itoa(metricId) + "/points", bytes.NewBuffer(jsonBytes))
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("X-Cachet-Token", Config.API_Token)
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
resp, _, err := makeRequest("POST", "/metrics/" + strconv.Itoa(metricId) + "/points", jsonBytes)
|
||||
if err != nil || resp.StatusCode != 200 {
|
||||
fmt.Printf("Could not log data point!\n%v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != 200 {
|
||||
fmt.Println("Could not log data point!")
|
||||
}
|
||||
}
|
||||
@@ -83,7 +83,13 @@ func (monitor *Monitor) AnalyseData() {
|
||||
monitor.Incident.Message += "\n\n" + *monitor.LastFailReason
|
||||
}
|
||||
|
||||
// set investigating status
|
||||
monitor.Incident.SetInvestigating()
|
||||
|
||||
// lookup relevant incident
|
||||
monitor.Incident.GetSimilarIncidentId()
|
||||
|
||||
// create/update incident
|
||||
monitor.Incident.Send()
|
||||
} else if t < monitor.Threshold && monitor.Incident != nil {
|
||||
// was down, created an incident, its now ok, make it resolved.
|
||||
|
||||
25
cachet/request.go
Normal file
25
cachet/request.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package cachet
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func makeRequest(requestType string, url string, reqBody []byte) (*http.Response, []byte, error) {
|
||||
req, err := http.NewRequest(requestType, Config.API_Url + url, bytes.NewBuffer(reqBody))
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("X-Cachet-Token", Config.API_Token)
|
||||
|
||||
client := &http.Client{}
|
||||
res, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, []byte{}, err
|
||||
}
|
||||
|
||||
defer res.Body.Close()
|
||||
body, _ := ioutil.ReadAll(res.Body)
|
||||
|
||||
return res, body, nil
|
||||
}
|
||||
Reference in New Issue
Block a user