gofmt -w
This commit is contained in:
@@ -6,14 +6,14 @@ import (
|
|||||||
|
|
||||||
// Component Cachet model
|
// Component Cachet model
|
||||||
type Component struct {
|
type Component struct {
|
||||||
ID int `json:"id"`
|
ID int `json:"id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Description string `json:"description"`
|
Description string `json:"description"`
|
||||||
Status int `json:"status"`
|
Status int `json:"status"`
|
||||||
Link *string `json:"link"`
|
Link *string `json:"link"`
|
||||||
Order *int `json:"order"`
|
Order *int `json:"order"`
|
||||||
GroupID *int `json:"group_id"`
|
GroupID *int `json:"group_id"`
|
||||||
CreatedAt *time.Time `json:"created_at"`
|
CreatedAt *time.Time `json:"created_at"`
|
||||||
UpdatedAt *time.Time `json:"updated_at"`
|
UpdatedAt *time.Time `json:"updated_at"`
|
||||||
DeletedAt *time.Time `json:"deleted_at"`
|
DeletedAt *time.Time `json:"deleted_at"`
|
||||||
}
|
}
|
||||||
@@ -1,13 +1,13 @@
|
|||||||
package cachet
|
package cachet
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
|
||||||
"fmt"
|
|
||||||
"flag"
|
|
||||||
"net/url"
|
|
||||||
"net/http"
|
|
||||||
"io/ioutil"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Static config
|
// Static config
|
||||||
@@ -15,8 +15,8 @@ var Config CachetConfig
|
|||||||
|
|
||||||
// CachetConfig is the monitoring tool configuration
|
// CachetConfig is the monitoring tool configuration
|
||||||
type CachetConfig struct {
|
type CachetConfig struct {
|
||||||
APIUrl string `json:"api_url"`
|
APIUrl string `json:"api_url"`
|
||||||
APIToken string `json:"api_token"`
|
APIToken string `json:"api_token"`
|
||||||
Monitors []*Monitor `json:"monitors"`
|
Monitors []*Monitor `json:"monitors"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,22 +1,22 @@
|
|||||||
package cachet
|
package cachet
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
"encoding/json"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Incident Cachet data model
|
// Incident Cachet data model
|
||||||
type Incident struct {
|
type Incident struct {
|
||||||
ID int `json:"id"`
|
ID int `json:"id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Message string `json:"message"`
|
Message string `json:"message"`
|
||||||
Status int `json:"status"`// 4?
|
Status int `json:"status"` // 4?
|
||||||
HumanStatus string `json:"human_status"`
|
HumanStatus string `json:"human_status"`
|
||||||
Component *Component `json:"component"`
|
Component *Component `json:"component"`
|
||||||
ComponentID *int `json:"component_id"`
|
ComponentID *int `json:"component_id"`
|
||||||
CreatedAt int `json:"created_at"`
|
CreatedAt int `json:"created_at"`
|
||||||
UpdatedAt int `json:"updated_at"`
|
UpdatedAt int `json:"updated_at"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// IncidentData is a response when creating/updating an incident
|
// IncidentData is a response when creating/updating an incident
|
||||||
@@ -75,7 +75,7 @@ func (incident *Incident) Send() {
|
|||||||
incident.ID = data.Incident.ID
|
incident.ID = data.Incident.ID
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("ID:"+strconv.Itoa(incident.ID))
|
fmt.Println("ID:" + strconv.Itoa(incident.ID))
|
||||||
|
|
||||||
if resp.StatusCode != 200 {
|
if resp.StatusCode != 200 {
|
||||||
fmt.Println("Could not create/update incident!")
|
fmt.Println("Could not create/update incident!")
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
package cachet
|
package cachet
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
"encoding/json"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// SendMetric sends lag metric point
|
// SendMetric sends lag metric point
|
||||||
@@ -16,7 +16,7 @@ func SendMetric(metricID int, delay int64) {
|
|||||||
"value": delay,
|
"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 {
|
if err != nil || resp.StatusCode != 200 {
|
||||||
fmt.Printf("Could not log data point!\n%v\n", err)
|
fmt.Printf("Could not log data point!\n%v\n", err)
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -2,24 +2,24 @@ package cachet
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
const timeout = time.Duration(time.Second)
|
const timeout = time.Duration(time.Second)
|
||||||
|
|
||||||
// Monitor data model
|
// Monitor data model
|
||||||
type Monitor struct {
|
type Monitor struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
URL string `json:"url"`
|
URL string `json:"url"`
|
||||||
MetricID int `json:"metric_id"`
|
MetricID int `json:"metric_id"`
|
||||||
Threshold float32 `json:"threshold"`
|
Threshold float32 `json:"threshold"`
|
||||||
ComponentID *int `json:"component_id"`
|
ComponentID *int `json:"component_id"`
|
||||||
ExpectedStatusCode int `json:"expected_status_code"`
|
ExpectedStatusCode int `json:"expected_status_code"`
|
||||||
|
|
||||||
History []bool `json:"-"`
|
History []bool `json:"-"`
|
||||||
LastFailReason *string `json:"-"`
|
LastFailReason *string `json:"-"`
|
||||||
Incident *Incident `json:"-"`
|
Incident *Incident `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run loop
|
// Run loop
|
||||||
@@ -66,7 +66,7 @@ func (monitor *Monitor) AnalyseData() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
t := (float32(numDown) / float32(len(monitor.History))) * 100
|
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 {
|
if len(monitor.History) != 10 {
|
||||||
// not enough data
|
// not enough data
|
||||||
@@ -78,7 +78,7 @@ func (monitor *Monitor) AnalyseData() {
|
|||||||
fmt.Println("Creating incident...")
|
fmt.Println("Creating incident...")
|
||||||
|
|
||||||
monitor.Incident = &Incident{
|
monitor.Incident = &Incident{
|
||||||
Name: monitor.Name,
|
Name: monitor.Name,
|
||||||
Message: monitor.Name + " failed",
|
Message: monitor.Name + " failed",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func makeRequest(requestType string, url string, reqBody []byte) (*http.Response, []byte, error) {
|
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("Content-Type", "application/json")
|
||||||
req.Header.Set("X-Cachet-Token", Config.APIToken)
|
req.Header.Set("X-Cachet-Token", Config.APIToken)
|
||||||
|
|||||||
4
main.go
4
main.go
@@ -2,8 +2,8 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
|
||||||
"github.com/castawaylabs/cachet-monitor/cachet"
|
"github.com/castawaylabs/cachet-monitor/cachet"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -19,7 +19,7 @@ func main() {
|
|||||||
fmt.Println()
|
fmt.Println()
|
||||||
|
|
||||||
ticker := time.NewTicker(time.Second)
|
ticker := time.NewTicker(time.Second)
|
||||||
for _ = range ticker.C {
|
for range ticker.C {
|
||||||
for _, monitor := range cachet.Config.Monitors {
|
for _, monitor := range cachet.Config.Monitors {
|
||||||
go monitor.Run()
|
go monitor.Run()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user