Skip to content
Snippets Groups Projects
Commit 409d7539 authored by Matej Kramny's avatar Matej Kramny
Browse files

Link to incident, resolved message

- Major outage when already in Partial outage
- Resolved at x message
- Link incident to component
parent cceb370e
No related branches found
No related tags found
No related merge requests found
package cachet package cachet
import (
"time"
)
// Component Cachet model // Component Cachet model
type Component struct { type Component struct {
ID int `json:"id"` ID int `json:"id"`
Name string `json:"name"` Name string `json:"name"`
Description string `json:"description"` Description string `json:"description"`
Status int `json:"status"` Status int `json:"status_id"`
Link *string `json:"link"` HumanStatus string `json:"-"`
Order *int `json:"order"` IncidentCount int `json:"-"`
GroupID *int `json:"group_id"` CreatedAt int `json:"created_at"`
CreatedAt *time.Time `json:"created_at"` UpdatedAt int `json:"updated_at"`
UpdatedAt *time.Time `json:"updated_at"` }
DeletedAt *time.Time `json:"deleted_at"`
// ComponentData json response model
type ComponentData struct {
Component Component `json:"data"`
} }
...@@ -57,7 +57,7 @@ func (incident *Incident) Send() { ...@@ -57,7 +57,7 @@ func (incident *Incident) Send() {
requestURL := "/incidents" requestURL := "/incidents"
if incident.ID > 0 { if incident.ID > 0 {
requestType = "PUT" requestType = "PUT"
requestURL = "/incidents/" + strconv.Itoa(incident.ID) requestURL += "/" + strconv.Itoa(incident.ID)
} }
resp, body, err := makeRequest(requestType, requestURL, jsonBytes) resp, body, err := makeRequest(requestType, requestURL, jsonBytes)
...@@ -75,10 +75,9 @@ func (incident *Incident) Send() { ...@@ -75,10 +75,9 @@ func (incident *Incident) Send() {
panic(err) panic(err)
} else { } else {
incident.ID = data.Incident.ID incident.ID = data.Incident.ID
incident.Component = data.Incident.Component
} }
Logger.Println("ID:" + strconv.Itoa(incident.ID))
if resp.StatusCode != 200 { if resp.StatusCode != 200 {
Logger.Println("Could not create/update incident!") Logger.Println("Could not create/update incident!")
} }
...@@ -98,6 +97,59 @@ func (incident *Incident) GetSimilarIncidentID() { ...@@ -98,6 +97,59 @@ func (incident *Incident) GetSimilarIncidentID() {
} }
} }
func (incident *Incident) fetchComponent() error {
_, body, err := makeRequest("GET", "/components/" + strconv.Itoa(*incident.ComponentID), nil)
if err != nil {
return err
}
var data ComponentData
err = json.Unmarshal(body, &data)
if err != nil {
Logger.Println("Cannot parse component body.")
panic(err)
}
incident.Component = &data.Component
return nil
}
func (incident *Incident) UpdateComponent() {
if incident.ComponentID == nil || *incident.ComponentID == 0 {
return
}
if incident.Component == nil {
// fetch component
if err := incident.fetchComponent(); err != nil {
Logger.Printf("Cannot fetch component for incident. %v\n", err)
return
}
}
switch incident.Status {
case 1, 2, 3:
if incident.Component.Status == 3 {
incident.Component.Status = 4
} else {
incident.Component.Status = 3
}
case 4:
incident.Component.Status = 1
}
jsonBytes, _ := json.Marshal(map[string]interface{}{
"status": incident.Component.Status,
})
resp, _, err := makeRequest("PUT", "/components/" + strconv.Itoa(incident.Component.ID), jsonBytes)
if err != nil || resp.StatusCode != 200 {
Logger.Printf("Could not update component: (resp code %d) %v", resp.StatusCode, err)
return
}
}
// SetInvestigating sets status to Investigating // SetInvestigating sets status to Investigating
func (incident *Incident) SetInvestigating() { func (incident *Incident) SetInvestigating() {
incident.Status = 1 incident.Status = 1
......
...@@ -79,6 +79,7 @@ func (monitor *Monitor) AnalyseData() { ...@@ -79,6 +79,7 @@ func (monitor *Monitor) AnalyseData() {
monitor.Incident = &Incident{ monitor.Incident = &Incident{
Name: monitor.Name + " - " + Config.SystemName, Name: monitor.Name + " - " + Config.SystemName,
Message: monitor.Name + " failed", Message: monitor.Name + " failed",
ComponentID: monitor.ComponentID,
} }
if monitor.LastFailReason != nil { if monitor.LastFailReason != nil {
...@@ -93,12 +94,17 @@ func (monitor *Monitor) AnalyseData() { ...@@ -93,12 +94,17 @@ func (monitor *Monitor) AnalyseData() {
// create/update incident // create/update incident
monitor.Incident.Send() monitor.Incident.Send()
monitor.Incident.UpdateComponent()
} 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.
Logger.Println("Updating incident to resolved...") Logger.Println("Updating incident to resolved...")
// Add resolved message
monitor.Incident.Message += "\n\n-\n\nResolved at " + time.Now().String()
monitor.Incident.SetFixed() monitor.Incident.SetFixed()
monitor.Incident.Send() monitor.Incident.Send()
monitor.Incident.UpdateComponent()
monitor.Incident = nil monitor.Incident = nil
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment