Log & resolve incidents, add readme
This commit is contained in:
@@ -1,7 +1,12 @@
|
||||
package cachet
|
||||
|
||||
import (
|
||||
"time"
|
||||
"fmt"
|
||||
"bytes"
|
||||
"io/ioutil"
|
||||
"strconv"
|
||||
"net/http"
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
type Incident struct {
|
||||
@@ -12,6 +17,77 @@ type Incident struct {
|
||||
Human_status string `json:"human_status"`
|
||||
Component *Component `json:"component"`
|
||||
Component_id *int `json:"component_id"`
|
||||
Created_at *time.Time `json:"created_at"`
|
||||
Updated_at *time.Time `json:"updated_at"`
|
||||
Created_at int `json:"created_at"`
|
||||
Updated_at int `json:"updated_at"`
|
||||
}
|
||||
|
||||
type IncidentData struct {
|
||||
Incident Incident `json:"data"`
|
||||
}
|
||||
|
||||
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", apiUrl + "/incidents", bytes.NewBuffer(jsonBytes))
|
||||
} else {
|
||||
req, err = http.NewRequest("PUT", apiUrl + "/incidents/" + strconv.Itoa(incident.Id), bytes.NewBuffer(jsonBytes))
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("X-Cachet-Token", apiToken)
|
||||
|
||||
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
|
||||
err = json.Unmarshal(body, &data)
|
||||
if err != nil {
|
||||
fmt.Println("Cannot parse incident body.")
|
||||
panic(err)
|
||||
} else {
|
||||
incident.Id = data.Incident.Id
|
||||
}
|
||||
|
||||
fmt.Println("ID:"+strconv.Itoa(incident.Id))
|
||||
|
||||
if resp.StatusCode != 200 {
|
||||
fmt.Println("Could not create/update incident!")
|
||||
}
|
||||
}
|
||||
|
||||
func (incident *Incident) SetInvestigating() {
|
||||
incident.Status = 1
|
||||
incident.Human_status = "Investigating"
|
||||
}
|
||||
|
||||
func (incident *Incident) SetIdentified() {
|
||||
incident.Status = 2
|
||||
incident.Human_status = "Identified"
|
||||
}
|
||||
|
||||
func (incident *Incident) SetWatching() {
|
||||
incident.Status = 3
|
||||
incident.Human_status = "Watching"
|
||||
}
|
||||
|
||||
func (incident *Incident) SetFixed() {
|
||||
incident.Status = 4
|
||||
incident.Human_status = "Fixed"
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
const timeout = time.Duration(time.Second)
|
||||
|
||||
type Monitor struct {
|
||||
Name string `json:"name"`
|
||||
Url string `json:"url"`
|
||||
MetricId int `json:"metric_id"`
|
||||
Threshold float32 `json:"threshold"`
|
||||
@@ -30,9 +31,8 @@ func (monitor *Monitor) Run() {
|
||||
monitor.History = append(monitor.History, isUp)
|
||||
monitor.AnalyseData()
|
||||
|
||||
if isUp == true {
|
||||
if isUp == true && monitor.MetricId > 0 {
|
||||
SendMetric(monitor.MetricId, lag)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,11 +52,6 @@ func (monitor *Monitor) doRequest() bool {
|
||||
|
||||
func (monitor *Monitor) AnalyseData() {
|
||||
// look at the past few incidents
|
||||
if len(monitor.History) != 10 {
|
||||
// not enough data
|
||||
return
|
||||
}
|
||||
|
||||
numDown := 0
|
||||
for _, wasUp := range monitor.History {
|
||||
if wasUp == false {
|
||||
@@ -65,14 +60,31 @@ func (monitor *Monitor) AnalyseData() {
|
||||
}
|
||||
|
||||
t := (float32(numDown) / float32(len(monitor.History))) * 100
|
||||
fmt.Printf("%s %.2f%% Down. Threshold: %.2f%%\n", monitor.Url, t, monitor.Threshold)
|
||||
fmt.Printf("%s %.2f%% Down at %v. Threshold: %.2f%%\n", monitor.Url, t, time.Now().UnixNano() / int64(time.Second), monitor.Threshold)
|
||||
|
||||
if len(monitor.History) != 10 {
|
||||
// not enough data
|
||||
return
|
||||
}
|
||||
|
||||
if t > monitor.Threshold && monitor.Incident == nil {
|
||||
// is down, create an incident
|
||||
fmt.Println("Creating incident...")
|
||||
monitor.Incident = &Incident{}
|
||||
|
||||
monitor.Incident = &Incident{
|
||||
Name: monitor.Name,
|
||||
Message: monitor.Name + " is unreachable.",
|
||||
}
|
||||
|
||||
monitor.Incident.SetInvestigating()
|
||||
monitor.Incident.Send()
|
||||
} else if t < monitor.Threshold && monitor.Incident != nil {
|
||||
// was down, created an incident, its now ok, make it resolved.
|
||||
fmt.Println("Updating incident to resolved...")
|
||||
|
||||
monitor.Incident.SetFixed()
|
||||
monitor.Incident.Send()
|
||||
|
||||
monitor.Incident = nil
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user