Get relevant incident, fix config bug

- Make generic makeRequest fn
This commit is contained in:
Matej Kramny
2015-03-20 19:10:29 +01:00
parent f5826d7832
commit 8e17ebceef
5 changed files with 73 additions and 38 deletions

View File

@@ -26,8 +26,8 @@ func init() {
var data []byte var data []byte
// test if its a url // test if its a url
_, err := url.ParseRequestURI(configPath) url, err := url.ParseRequestURI(configPath)
if err == nil { if err == nil && len(url.Scheme) > 0 {
// download config // download config
response, err := http.Get(configPath) response, err := http.Get(configPath)
if err != nil { if err != nil {

View File

@@ -2,10 +2,7 @@ package cachet
import ( import (
"fmt" "fmt"
"bytes"
"io/ioutil"
"strconv" "strconv"
"net/http"
"encoding/json" "encoding/json"
) )
@@ -25,35 +22,44 @@ type IncidentData struct {
Incident Incident `json:"data"` 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() { func (incident *Incident) Send() {
jsonBytes, err := json.Marshal(incident) jsonBytes, err := json.Marshal(incident)
if err != nil { if err != nil {
panic(err) panic(err)
} }
var req *http.Request requestType := "POST"
if incident.Id == 0 { requestUrl := "/incidents"
req, err = http.NewRequest("POST", Config.API_Url + "/incidents", bytes.NewBuffer(jsonBytes)) if incident.Id > 0 {
} else { requestType = "PUT"
req, err = http.NewRequest("PUT", Config.API_Url + "/incidents/" + strconv.Itoa(incident.Id), bytes.NewBuffer(jsonBytes)) requestUrl = "/incidents/" + strconv.Itoa(incident.Id)
} }
resp, body, err := makeRequest(requestType, requestUrl, jsonBytes)
if err != nil { if err != nil {
panic(err) 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)) fmt.Println(strconv.Itoa(resp.StatusCode) + " " + string(body))
var data IncidentData 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() { func (incident *Incident) SetInvestigating() {
incident.Status = 1 incident.Status = 1
incident.Human_status = "Investigating" incident.Human_status = "Investigating"

View File

@@ -2,9 +2,7 @@ package cachet
import ( import (
"fmt" "fmt"
"bytes"
"strconv" "strconv"
"net/http"
"encoding/json" "encoding/json"
) )
@@ -17,21 +15,9 @@ func SendMetric(metricId int, delay int64) {
"value": delay, "value": delay,
}) })
client := &http.Client{} resp, _, err := makeRequest("POST", "/metrics/" + strconv.Itoa(metricId) + "/points", jsonBytes)
req, _ := http.NewRequest("POST", Config.API_Url + "/metrics/" + strconv.Itoa(metricId) + "/points", bytes.NewBuffer(jsonBytes)) if err != nil || resp.StatusCode != 200 {
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Cachet-Token", Config.API_Token)
resp, err := client.Do(req)
if err != nil {
fmt.Printf("Could not log data point!\n%v\n", err) fmt.Printf("Could not log data point!\n%v\n", err)
return return
} }
defer resp.Body.Close()
if resp.StatusCode != 200 {
fmt.Println("Could not log data point!")
}
} }

View File

@@ -83,7 +83,13 @@ func (monitor *Monitor) AnalyseData() {
monitor.Incident.Message += "\n\n" + *monitor.LastFailReason monitor.Incident.Message += "\n\n" + *monitor.LastFailReason
} }
// set investigating status
monitor.Incident.SetInvestigating() monitor.Incident.SetInvestigating()
// lookup relevant incident
monitor.Incident.GetSimilarIncidentId()
// create/update incident
monitor.Incident.Send() monitor.Incident.Send()
} else if t < monitor.Threshold && monitor.Incident != nil { } else if t < monitor.Threshold && monitor.Incident != nil {
// was down, created an incident, its now ok, make it resolved. // was down, created an incident, its now ok, make it resolved.

25
cachet/request.go Normal file
View 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
}