diff --git a/cachet/component.go b/cachet/component.go index e8c12fd3d67e89427cd3234229d2274ebdd729bd..bebbad439deb657ca245400c32ec2944f0245966 100644 --- a/cachet/component.go +++ b/cachet/component.go @@ -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"` } diff --git a/cachet/incident.go b/cachet/incident.go index 244a6b11be804e964e7651fb45082d657adf90ae..c8213c0eb5b3536c89505df9a73b424ee03523d0 100644 --- a/cachet/incident.go +++ b/cachet/incident.go @@ -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 diff --git a/cachet/monitor.go b/cachet/monitor.go index 5b4b998b4f70d5caaec14492ebfa143f13f3252d..616ab7400fde588f1e32f4954c8b130da488d11c 100644 --- a/cachet/monitor.go +++ b/cachet/monitor.go @@ -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 }