Link to incident, resolved message
- Major outage when already in Partial outage - Resolved at x message - Link incident to component
This commit is contained in:
@@ -1,19 +1,18 @@
|
||||
package cachet
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// Component Cachet model
|
||||
type Component struct {
|
||||
ID int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
Status int `json:"status"`
|
||||
Link *string `json:"link"`
|
||||
Order *int `json:"order"`
|
||||
GroupID *int `json:"group_id"`
|
||||
CreatedAt *time.Time `json:"created_at"`
|
||||
UpdatedAt *time.Time `json:"updated_at"`
|
||||
DeletedAt *time.Time `json:"deleted_at"`
|
||||
ID int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
Status int `json:"status_id"`
|
||||
HumanStatus string `json:"-"`
|
||||
IncidentCount int `json:"-"`
|
||||
CreatedAt int `json:"created_at"`
|
||||
UpdatedAt int `json:"updated_at"`
|
||||
}
|
||||
|
||||
// ComponentData json response model
|
||||
type ComponentData struct {
|
||||
Component Component `json:"data"`
|
||||
}
|
||||
|
||||
@@ -57,7 +57,7 @@ func (incident *Incident) Send() {
|
||||
requestURL := "/incidents"
|
||||
if incident.ID > 0 {
|
||||
requestType = "PUT"
|
||||
requestURL = "/incidents/" + strconv.Itoa(incident.ID)
|
||||
requestURL += "/" + strconv.Itoa(incident.ID)
|
||||
}
|
||||
|
||||
resp, body, err := makeRequest(requestType, requestURL, jsonBytes)
|
||||
@@ -75,10 +75,9 @@ func (incident *Incident) Send() {
|
||||
panic(err)
|
||||
} else {
|
||||
incident.ID = data.Incident.ID
|
||||
incident.Component = data.Incident.Component
|
||||
}
|
||||
|
||||
Logger.Println("ID:" + strconv.Itoa(incident.ID))
|
||||
|
||||
if resp.StatusCode != 200 {
|
||||
Logger.Println("Could not create/update incident!")
|
||||
}
|
||||
@@ -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
|
||||
func (incident *Incident) SetInvestigating() {
|
||||
incident.Status = 1
|
||||
|
||||
@@ -77,8 +77,9 @@ func (monitor *Monitor) AnalyseData() {
|
||||
Logger.Println("Creating incident...")
|
||||
|
||||
monitor.Incident = &Incident{
|
||||
Name: monitor.Name + " - " + Config.SystemName,
|
||||
Message: monitor.Name + " failed",
|
||||
Name: monitor.Name + " - " + Config.SystemName,
|
||||
Message: monitor.Name + " failed",
|
||||
ComponentID: monitor.ComponentID,
|
||||
}
|
||||
|
||||
if monitor.LastFailReason != nil {
|
||||
@@ -93,12 +94,17 @@ func (monitor *Monitor) AnalyseData() {
|
||||
|
||||
// create/update incident
|
||||
monitor.Incident.Send()
|
||||
monitor.Incident.UpdateComponent()
|
||||
} else if t < monitor.Threshold && monitor.Incident != nil {
|
||||
// was down, created an incident, its now ok, make it 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.Send()
|
||||
monitor.Incident.UpdateComponent()
|
||||
|
||||
monitor.Incident = nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user