This commit is contained in:
Matej Kramny
2015-03-20 20:58:56 +01:00
parent cdf51d89f6
commit d421b35e9b
7 changed files with 52 additions and 52 deletions

View File

@@ -6,14 +6,14 @@ import (
// 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"`
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"`
}

View File

@@ -1,13 +1,13 @@
package cachet
import (
"os"
"fmt"
"flag"
"net/url"
"net/http"
"io/ioutil"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
)
// Static config
@@ -15,8 +15,8 @@ var Config CachetConfig
// CachetConfig is the monitoring tool configuration
type CachetConfig struct {
APIUrl string `json:"api_url"`
APIToken string `json:"api_token"`
APIUrl string `json:"api_url"`
APIToken string `json:"api_token"`
Monitors []*Monitor `json:"monitors"`
}

View File

@@ -1,22 +1,22 @@
package cachet
import (
"encoding/json"
"fmt"
"strconv"
"encoding/json"
)
// Incident Cachet data model
type Incident struct {
ID int `json:"id"`
Name string `json:"name"`
Message string `json:"message"`
Status int `json:"status"`// 4?
HumanStatus string `json:"human_status"`
Component *Component `json:"component"`
ComponentID *int `json:"component_id"`
CreatedAt int `json:"created_at"`
UpdatedAt int `json:"updated_at"`
ID int `json:"id"`
Name string `json:"name"`
Message string `json:"message"`
Status int `json:"status"` // 4?
HumanStatus string `json:"human_status"`
Component *Component `json:"component"`
ComponentID *int `json:"component_id"`
CreatedAt int `json:"created_at"`
UpdatedAt int `json:"updated_at"`
}
// IncidentData is a response when creating/updating an incident
@@ -75,7 +75,7 @@ func (incident *Incident) Send() {
incident.ID = data.Incident.ID
}
fmt.Println("ID:"+strconv.Itoa(incident.ID))
fmt.Println("ID:" + strconv.Itoa(incident.ID))
if resp.StatusCode != 200 {
fmt.Println("Could not create/update incident!")

View File

@@ -1,9 +1,9 @@
package cachet
import (
"encoding/json"
"fmt"
"strconv"
"encoding/json"
)
// SendMetric sends lag metric point
@@ -16,7 +16,7 @@ func SendMetric(metricID int, delay int64) {
"value": delay,
})
resp, _, err := makeRequest("POST", "/metrics/" + strconv.Itoa(metricID) + "/points", jsonBytes)
resp, _, err := makeRequest("POST", "/metrics/"+strconv.Itoa(metricID)+"/points", jsonBytes)
if err != nil || resp.StatusCode != 200 {
fmt.Printf("Could not log data point!\n%v\n", err)
return

View File

@@ -2,24 +2,24 @@ package cachet
import (
"fmt"
"time"
"net/http"
"time"
)
const timeout = time.Duration(time.Second)
// Monitor data model
type Monitor struct {
Name string `json:"name"`
URL string `json:"url"`
MetricID int `json:"metric_id"`
Threshold float32 `json:"threshold"`
ComponentID *int `json:"component_id"`
ExpectedStatusCode int `json:"expected_status_code"`
Name string `json:"name"`
URL string `json:"url"`
MetricID int `json:"metric_id"`
Threshold float32 `json:"threshold"`
ComponentID *int `json:"component_id"`
ExpectedStatusCode int `json:"expected_status_code"`
History []bool `json:"-"`
LastFailReason *string `json:"-"`
Incident *Incident `json:"-"`
History []bool `json:"-"`
LastFailReason *string `json:"-"`
Incident *Incident `json:"-"`
}
// Run loop
@@ -66,7 +66,7 @@ func (monitor *Monitor) AnalyseData() {
}
t := (float32(numDown) / float32(len(monitor.History))) * 100
fmt.Printf("%s %.2f%% Down at %v. Threshold: %.2f%%\n", monitor.Url, t, time.Now().UnixNano() / int64(time.Second), 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
@@ -78,7 +78,7 @@ func (monitor *Monitor) AnalyseData() {
fmt.Println("Creating incident...")
monitor.Incident = &Incident{
Name: monitor.Name,
Name: monitor.Name,
Message: monitor.Name + " failed",
}

View File

@@ -7,7 +7,7 @@ import (
)
func makeRequest(requestType string, url string, reqBody []byte) (*http.Response, []byte, error) {
req, err := http.NewRequest(requestType, Config.APIUrl + url, bytes.NewBuffer(reqBody))
req, err := http.NewRequest(requestType, Config.APIUrl+url, bytes.NewBuffer(reqBody))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Cachet-Token", Config.APIToken)

View File

@@ -2,8 +2,8 @@ package main
import (
"fmt"
"time"
"github.com/castawaylabs/cachet-monitor/cachet"
"time"
)
func main() {
@@ -19,7 +19,7 @@ func main() {
fmt.Println()
ticker := time.NewTicker(time.Second)
for _ = range ticker.C {
for range ticker.C {
for _, monitor := range cachet.Config.Monitors {
go monitor.Run()
}