Refactor based on goreportcard.com
This commit is contained in:
@@ -4,15 +4,16 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Cachet component 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"`
|
||||||
Group_id *int `json:"group_id"`
|
GroupId *int `json:"group_id"`
|
||||||
Created_at *time.Time `json:"created_at"`
|
CreatedAt *time.Time `json:"created_at"`
|
||||||
Updated_at *time.Time `json:"updated_at"`
|
UpdatedAt *time.Time `json:"updated_at"`
|
||||||
Deleted_at *time.Time `json:"deleted_at"`
|
DeletedAt *time.Time `json:"deleted_at"`
|
||||||
}
|
}
|
||||||
@@ -10,11 +10,13 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Static config
|
||||||
var Config CachetConfig
|
var Config CachetConfig
|
||||||
|
|
||||||
|
// Monitoring tool configuration
|
||||||
type CachetConfig struct {
|
type CachetConfig struct {
|
||||||
API_Url string `json:"api_url"`
|
APIUrl string `json:"api_url"`
|
||||||
API_Token string `json:"api_token"`
|
APIToken string `json:"api_token"`
|
||||||
Monitors []*Monitor `json:"monitors"`
|
Monitors []*Monitor `json:"monitors"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -56,13 +58,13 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(os.Getenv("CACHET_API")) > 0 {
|
if len(os.Getenv("CACHET_API")) > 0 {
|
||||||
Config.API_Url = os.Getenv("CACHET_API")
|
Config.APIUrl = os.Getenv("CACHET_API")
|
||||||
}
|
}
|
||||||
if len(os.Getenv("CACHET_TOKEN")) > 0 {
|
if len(os.Getenv("CACHET_TOKEN")) > 0 {
|
||||||
Config.API_Token = os.Getenv("CACHET_TOKEN")
|
Config.APIToken = os.Getenv("CACHET_TOKEN")
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(Config.API_Token) == 0 || len(Config.API_Url) == 0 {
|
if len(Config.APIToken) == 0 || len(Config.APIUrl) == 0 {
|
||||||
fmt.Printf("API URL or API Token not set. cachet-monitor won't be able to report incidents.\n\nPlease set:\n CACHET_API and CACHET_TOKEN environment variable to override settings.\n\nGet help at https://github.com/CastawayLabs/cachet-monitor\n")
|
fmt.Printf("API URL or API Token not set. cachet-monitor won't be able to report incidents.\n\nPlease set:\n CACHET_API and CACHET_TOKEN environment variable to override settings.\n\nGet help at https://github.com/CastawayLabs/cachet-monitor\n")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,26 +6,30 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Cachet Incident 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?
|
||||||
Human_status string `json:"human_status"`
|
HumanStatus string `json:"human_status"`
|
||||||
Component *Component `json:"component"`
|
Component *Component `json:"component"`
|
||||||
Component_id *int `json:"component_id"`
|
ComponentId *int `json:"component_id"`
|
||||||
Created_at int `json:"created_at"`
|
CreatedAt int `json:"created_at"`
|
||||||
Updated_at int `json:"updated_at"`
|
UpdatedAt int `json:"updated_at"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Response when creating/updating an incident
|
||||||
type IncidentData struct {
|
type IncidentData struct {
|
||||||
Incident Incident `json:"data"`
|
Incident Incident `json:"data"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// from API /incidents
|
||||||
type IncidentList struct {
|
type IncidentList struct {
|
||||||
Incidents []Incident `json:"data"`
|
Incidents []Incident `json:"data"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get list of incidents
|
||||||
func GetIncidents() []Incident {
|
func GetIncidents() []Incident {
|
||||||
_, body, err := makeRequest("GET", "/incidents", nil)
|
_, body, err := makeRequest("GET", "/incidents", nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -36,12 +40,12 @@ func GetIncidents() []Incident {
|
|||||||
err = json.Unmarshal(body, &data)
|
err = json.Unmarshal(body, &data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Cannot parse incidents.")
|
fmt.Println("Cannot parse incidents.")
|
||||||
panic(err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return data.Incidents
|
return data.Incidents
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create or Update incident
|
||||||
func (incident *Incident) Send() {
|
func (incident *Incident) Send() {
|
||||||
jsonBytes, err := json.Marshal(incident)
|
jsonBytes, err := json.Marshal(incident)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -49,13 +53,13 @@ func (incident *Incident) Send() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
requestType := "POST"
|
requestType := "POST"
|
||||||
requestUrl := "/incidents"
|
requestURL := "/incidents"
|
||||||
if incident.Id > 0 {
|
if incident.ID > 0 {
|
||||||
requestType = "PUT"
|
requestType = "PUT"
|
||||||
requestUrl = "/incidents/" + strconv.Itoa(incident.Id)
|
requestURL = "/incidents/" + strconv.Itoa(incident.ID)
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, body, err := makeRequest(requestType, requestUrl, jsonBytes)
|
resp, body, err := makeRequest(requestType, requestURL, jsonBytes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
@@ -68,44 +72,50 @@ func (incident *Incident) Send() {
|
|||||||
fmt.Println("Cannot parse incident body.")
|
fmt.Println("Cannot parse incident body.")
|
||||||
panic(err)
|
panic(err)
|
||||||
} else {
|
} else {
|
||||||
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!")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get the same incident.
|
||||||
|
// Updates incident.ID
|
||||||
func (incident *Incident) GetSimilarIncidentId() {
|
func (incident *Incident) GetSimilarIncidentId() {
|
||||||
incidents := GetIncidents()
|
incidents := GetIncidents()
|
||||||
|
|
||||||
for _, inc := range incidents {
|
for _, inc := range incidents {
|
||||||
if incident.Name == inc.Name && incident.Message == inc.Message && incident.Status == inc.Status && incident.Human_status == inc.Human_status {
|
if incident.Name == inc.Name && incident.Message == inc.Message && incident.Status == inc.Status && incident.HumanStatus == inc.HumanStatus {
|
||||||
incident.Id = inc.Id
|
incident.ID = inc.ID
|
||||||
fmt.Printf("Updated incident id to %v\n", inc.Id)
|
fmt.Printf("Updated incident id to %v\n", inc.ID)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set status to Investigating
|
||||||
func (incident *Incident) SetInvestigating() {
|
func (incident *Incident) SetInvestigating() {
|
||||||
incident.Status = 1
|
incident.Status = 1
|
||||||
incident.Human_status = "Investigating"
|
incident.HumanStatus = "Investigating"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set status to Identified
|
||||||
func (incident *Incident) SetIdentified() {
|
func (incident *Incident) SetIdentified() {
|
||||||
incident.Status = 2
|
incident.Status = 2
|
||||||
incident.Human_status = "Identified"
|
incident.HumanStatus = "Identified"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set status to Watching
|
||||||
func (incident *Incident) SetWatching() {
|
func (incident *Incident) SetWatching() {
|
||||||
incident.Status = 3
|
incident.Status = 3
|
||||||
incident.Human_status = "Watching"
|
incident.HumanStatus = "Watching"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set status to Fixed
|
||||||
func (incident *Incident) SetFixed() {
|
func (incident *Incident) SetFixed() {
|
||||||
incident.Status = 4
|
incident.Status = 4
|
||||||
incident.Human_status = "Fixed"
|
incident.HumanStatus = "Fixed"
|
||||||
}
|
}
|
||||||
@@ -6,8 +6,9 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
)
|
)
|
||||||
|
|
||||||
func SendMetric(metricId int, delay int64) {
|
// Send lag metric point
|
||||||
if metricId <= 0 {
|
func SendMetric(metricID int, delay int64) {
|
||||||
|
if metricID <= 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -15,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
|
||||||
|
|||||||
@@ -8,12 +8,13 @@ import (
|
|||||||
|
|
||||||
const timeout = time.Duration(time.Second)
|
const timeout = time.Duration(time.Second)
|
||||||
|
|
||||||
|
// 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:"-"`
|
||||||
@@ -21,6 +22,7 @@ type Monitor struct {
|
|||||||
Incident *Incident `json:"-"`
|
Incident *Incident `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Run loop
|
||||||
func (monitor *Monitor) Run() {
|
func (monitor *Monitor) Run() {
|
||||||
reqStart := getMs()
|
reqStart := getMs()
|
||||||
isUp := monitor.doRequest()
|
isUp := monitor.doRequest()
|
||||||
@@ -53,6 +55,7 @@ func (monitor *Monitor) doRequest() bool {
|
|||||||
return resp.StatusCode == monitor.ExpectedStatusCode
|
return resp.StatusCode == monitor.ExpectedStatusCode
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Decides if the monitor is statistically up or down and creates / resolves an incident
|
||||||
func (monitor *Monitor) AnalyseData() {
|
func (monitor *Monitor) AnalyseData() {
|
||||||
// look at the past few incidents
|
// look at the past few incidents
|
||||||
numDown := 0
|
numDown := 0
|
||||||
|
|||||||
@@ -7,10 +7,10 @@ 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.API_Url + 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.API_Token)
|
req.Header.Set("X-Cachet-Token", Config.APIToken)
|
||||||
|
|
||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
res, err := client.Do(req)
|
res, err := client.Do(req)
|
||||||
|
|||||||
2
main.go
2
main.go
@@ -7,7 +7,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
fmt.Printf("API: %s\n", cachet.Config.API_Url)
|
fmt.Printf("API: %s\n", cachet.Config.APIUrl)
|
||||||
fmt.Printf("Starting %d monitors:\n", len(cachet.Config.Monitors))
|
fmt.Printf("Starting %d monitors:\n", len(cachet.Config.Monitors))
|
||||||
for _, monitor := range cachet.Config.Monitors {
|
for _, monitor := range cachet.Config.Monitors {
|
||||||
fmt.Printf(" %s: GET %s & Expect HTTP %d\n", monitor.Name, monitor.Url, monitor.ExpectedStatusCode)
|
fmt.Printf(" %s: GET %s & Expect HTTP %d\n", monitor.Name, monitor.Url, monitor.ExpectedStatusCode)
|
||||||
|
|||||||
Reference in New Issue
Block a user