Beginning of incident reporting
This commit is contained in:
18
cachet/component.go
Normal file
18
cachet/component.go
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package cachet
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
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"`
|
||||||
|
Group_id *int `json:"group_id"`
|
||||||
|
Created_at *time.Time `json:"created_at"`
|
||||||
|
Updated_at *time.Time `json:"updated_at"`
|
||||||
|
Deleted_at *time.Time `json:"deleted_at"`
|
||||||
|
}
|
||||||
17
cachet/incident.go
Normal file
17
cachet/incident.go
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package cachet
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Incident struct {
|
||||||
|
Id int `json:"id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Message string `json:"message"`
|
||||||
|
Status int `json:"status"`// 4?
|
||||||
|
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"`
|
||||||
|
}
|
||||||
@@ -33,8 +33,8 @@ func SendMetric(metricId int, delay int64) {
|
|||||||
|
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
body, _ := ioutil.ReadAll(resp.Body)
|
_, _ = ioutil.ReadAll(resp.Body)
|
||||||
fmt.Println(strconv.Itoa(resp.StatusCode) + " " + string(body))
|
// fmt.Println(strconv.Itoa(resp.StatusCode) + " " + string(body))
|
||||||
|
|
||||||
if resp.StatusCode != 200 {
|
if resp.StatusCode != 200 {
|
||||||
fmt.Println("Could not log data point!")
|
fmt.Println("Could not log data point!")
|
||||||
|
|||||||
@@ -11,37 +11,70 @@ const timeout = time.Duration(time.Second)
|
|||||||
type Monitor struct {
|
type Monitor struct {
|
||||||
Url string `json:"url"`
|
Url string `json:"url"`
|
||||||
MetricId int `json:"metric_id"`
|
MetricId int `json:"metric_id"`
|
||||||
|
Threshold float32 `json:"threshold"`
|
||||||
|
ComponentId *int `json:"component_id"`
|
||||||
|
ExpectedStatusCode int `json:"expected_status_code"`
|
||||||
|
|
||||||
|
History []bool `json:"-"`
|
||||||
|
Incident *Incident `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (monitor *Monitor) Run() {
|
func (monitor *Monitor) Run() {
|
||||||
reqStart := getMs()
|
reqStart := getMs()
|
||||||
err := monitor.doRequest()
|
isUp := monitor.doRequest()
|
||||||
lag := getMs() - reqStart
|
lag := getMs() - reqStart
|
||||||
|
|
||||||
failed := false
|
if len(monitor.History) >= 10 {
|
||||||
if err != nil {
|
monitor.History = monitor.History[len(monitor.History)-9:]
|
||||||
failed = true
|
}
|
||||||
|
monitor.History = append(monitor.History, isUp)
|
||||||
|
monitor.AnalyseData()
|
||||||
|
|
||||||
|
if isUp == true {
|
||||||
|
SendMetric(monitor.MetricId, lag)
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if failed == true {
|
func (monitor *Monitor) doRequest() bool {
|
||||||
fmt.Println("Req failed")
|
|
||||||
}
|
|
||||||
|
|
||||||
SendMetric(1, lag)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (monitor *Monitor) doRequest() error {
|
|
||||||
client := &http.Client{
|
client := &http.Client{
|
||||||
Timeout: timeout,
|
Timeout: timeout,
|
||||||
}
|
}
|
||||||
resp, err := client.Get(monitor.Url) // http://127.0.0.1:1337
|
resp, err := client.Get(monitor.Url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
return nil
|
return resp.StatusCode == monitor.ExpectedStatusCode
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
||||||
|
numDown++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
t := (float32(numDown) / float32(len(monitor.History))) * 100
|
||||||
|
fmt.Printf("%s %.2f%% Down. Threshold: %.2f%%\n", monitor.Url, t, monitor.Threshold)
|
||||||
|
if t > monitor.Threshold && monitor.Incident == nil {
|
||||||
|
// is down, create an incident
|
||||||
|
fmt.Println("Creating incident...")
|
||||||
|
monitor.Incident = &Incident{}
|
||||||
|
} 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 = nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getMs() int64 {
|
func getMs() int64 {
|
||||||
|
|||||||
11
main.go
11
main.go
@@ -6,10 +6,17 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
monitors := []cachet.Monitor{
|
monitors := []*cachet.Monitor{
|
||||||
cachet.Monitor{
|
/*&cachet.Monitor{
|
||||||
Url: "https://nodegear.io/ping",
|
Url: "https://nodegear.io/ping",
|
||||||
MetricId: 1,
|
MetricId: 1,
|
||||||
|
Threshold: 80.0,
|
||||||
|
},*/
|
||||||
|
&cachet.Monitor{
|
||||||
|
Url: "http://localhost:1337",
|
||||||
|
MetricId: 1,
|
||||||
|
Threshold: 80.0,
|
||||||
|
ExpectedStatusCode: 200,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user