Improve fail reasons, fix api crashes
- Add options about TLS verification - Fix crashes when cachet presents IDs as a string - Improve fail reasons
This commit is contained in:
@@ -1,15 +1,17 @@
|
|||||||
package cachet
|
package cachet
|
||||||
|
|
||||||
|
import "encoding/json"
|
||||||
|
|
||||||
// Component Cachet model
|
// Component Cachet model
|
||||||
type Component struct {
|
type Component struct {
|
||||||
ID int `json:"id"`
|
ID json.Number `json:"id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Description string `json:"description"`
|
Description string `json:"description"`
|
||||||
Status int `json:"status_id"`
|
Status json.Number `json:"status_id"`
|
||||||
HumanStatus string `json:"-"`
|
HumanStatus string `json:"-"`
|
||||||
IncidentCount int `json:"-"`
|
IncidentCount int `json:"-"`
|
||||||
CreatedAt int `json:"created_at"`
|
CreatedAt *string `json:"created_at"`
|
||||||
UpdatedAt int `json:"updated_at"`
|
UpdatedAt *string `json:"updated_at"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// ComponentData json response model
|
// ComponentData json response model
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ type CachetConfig struct {
|
|||||||
Monitors []*Monitor `json:"monitors"`
|
Monitors []*Monitor `json:"monitors"`
|
||||||
SystemName string `json:"system_name"`
|
SystemName string `json:"system_name"`
|
||||||
LogPath string `json:"log_path"`
|
LogPath string `json:"log_path"`
|
||||||
|
InsecureAPI bool `json:"insecure_api"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@@ -107,7 +108,7 @@ func init() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
flags := log.Llongfile|log.Ldate|log.Ltime
|
flags := log.Llongfile | log.Ldate | log.Ltime
|
||||||
if len(os.Getenv("DEVELOPMENT")) > 0 {
|
if len(os.Getenv("DEVELOPMENT")) > 0 {
|
||||||
flags = 0
|
flags = 0
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,15 +7,15 @@ import (
|
|||||||
|
|
||||||
// Incident Cachet data model
|
// Incident Cachet data model
|
||||||
type Incident struct {
|
type Incident struct {
|
||||||
ID int `json:"id"`
|
ID json.Number `json:"id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Message string `json:"message"`
|
Message string `json:"message"`
|
||||||
Status int `json:"status"` // 4?
|
Status json.Number `json:"status"` // 4?
|
||||||
HumanStatus string `json:"human_status"`
|
HumanStatus string `json:"human_status"`
|
||||||
Component *Component `json:"-"`
|
Component *Component `json:"-"`
|
||||||
ComponentID *int `json:"component_id"`
|
ComponentID *json.Number `json:"component_id"`
|
||||||
CreatedAt int `json:"created_at"`
|
CreatedAt *string `json:"created_at"`
|
||||||
UpdatedAt int `json:"updated_at"`
|
UpdatedAt *string `json:"updated_at"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// IncidentData is a response when creating/updating an incident
|
// IncidentData is a response when creating/updating an incident
|
||||||
@@ -40,6 +40,7 @@ func GetIncidents() []Incident {
|
|||||||
err = json.Unmarshal(body, &data)
|
err = json.Unmarshal(body, &data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Logger.Printf("Cannot parse incidents: %v\n", err)
|
Logger.Printf("Cannot parse incidents: %v\n", err)
|
||||||
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return data.Incidents
|
return data.Incidents
|
||||||
@@ -47,17 +48,19 @@ func GetIncidents() []Incident {
|
|||||||
|
|
||||||
// Send - Create or Update incident
|
// Send - Create or Update incident
|
||||||
func (incident *Incident) Send() {
|
func (incident *Incident) Send() {
|
||||||
jsonBytes, err := json.Marshal(incident)
|
jsonBytes, _ := json.Marshal(map[string]interface{}{
|
||||||
if err != nil {
|
"name": incident.Name,
|
||||||
Logger.Printf("Cannot encode incident: %v\n", err)
|
"message": incident.Message,
|
||||||
return
|
"status": incident.Status,
|
||||||
}
|
"component_id": incident.ComponentID,
|
||||||
|
"notify": true,
|
||||||
|
})
|
||||||
|
|
||||||
requestType := "POST"
|
requestType := "POST"
|
||||||
requestURL := "/incidents"
|
requestURL := "/incidents"
|
||||||
if incident.ID > 0 {
|
if len(incident.ID) > 0 {
|
||||||
requestType = "PUT"
|
requestType = "PUT"
|
||||||
requestURL += "/" + strconv.Itoa(incident.ID)
|
requestURL += "/" + string(incident.ID)
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, body, err := makeRequest(requestType, requestURL, jsonBytes)
|
resp, body, err := makeRequest(requestType, requestURL, jsonBytes)
|
||||||
@@ -71,7 +74,7 @@ func (incident *Incident) Send() {
|
|||||||
var data IncidentData
|
var data IncidentData
|
||||||
err = json.Unmarshal(body, &data)
|
err = json.Unmarshal(body, &data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Logger.Println("Cannot parse incident body.")
|
Logger.Println("Cannot parse incident body.", string(body))
|
||||||
panic(err)
|
panic(err)
|
||||||
} else {
|
} else {
|
||||||
incident.ID = data.Incident.ID
|
incident.ID = data.Incident.ID
|
||||||
@@ -89,7 +92,7 @@ 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.HumanStatus == inc.HumanStatus {
|
if incident.Name == inc.Name && incident.Message == inc.Message && incident.Status == inc.Status {
|
||||||
incident.ID = inc.ID
|
incident.ID = inc.ID
|
||||||
Logger.Printf("Updated incident id to %v\n", inc.ID)
|
Logger.Printf("Updated incident id to %v\n", inc.ID)
|
||||||
break
|
break
|
||||||
@@ -98,7 +101,7 @@ func (incident *Incident) GetSimilarIncidentID() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (incident *Incident) fetchComponent() error {
|
func (incident *Incident) fetchComponent() error {
|
||||||
_, body, err := makeRequest("GET", "/components/" + strconv.Itoa(*incident.ComponentID), nil)
|
_, body, err := makeRequest("GET", "/components/"+string(*incident.ComponentID), nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -106,7 +109,7 @@ func (incident *Incident) fetchComponent() error {
|
|||||||
var data ComponentData
|
var data ComponentData
|
||||||
err = json.Unmarshal(body, &data)
|
err = json.Unmarshal(body, &data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Logger.Println("Cannot parse component body.")
|
Logger.Println("Cannot parse component body. %v", string(body))
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -116,7 +119,7 @@ func (incident *Incident) fetchComponent() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (incident *Incident) UpdateComponent() {
|
func (incident *Incident) UpdateComponent() {
|
||||||
if incident.ComponentID == nil || *incident.ComponentID == 0 {
|
if incident.ComponentID == nil || len(*incident.ComponentID) == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -128,22 +131,23 @@ func (incident *Incident) UpdateComponent() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
switch incident.Status {
|
status, _ := strconv.Atoi(string(incident.Status))
|
||||||
|
switch status {
|
||||||
case 1, 2, 3:
|
case 1, 2, 3:
|
||||||
if incident.Component.Status == 3 {
|
if incident.Component.Status == "3" {
|
||||||
incident.Component.Status = 4
|
incident.Component.Status = "4"
|
||||||
} else {
|
} else {
|
||||||
incident.Component.Status = 3
|
incident.Component.Status = "3"
|
||||||
}
|
}
|
||||||
case 4:
|
case 4:
|
||||||
incident.Component.Status = 1
|
incident.Component.Status = "1"
|
||||||
}
|
}
|
||||||
|
|
||||||
jsonBytes, _ := json.Marshal(map[string]interface{}{
|
jsonBytes, _ := json.Marshal(map[string]interface{}{
|
||||||
"status": incident.Component.Status,
|
"status": incident.Component.Status,
|
||||||
})
|
})
|
||||||
|
|
||||||
resp, _, err := makeRequest("PUT", "/components/" + strconv.Itoa(incident.Component.ID), jsonBytes)
|
resp, _, err := makeRequest("PUT", "/components/"+string(incident.Component.ID), jsonBytes)
|
||||||
if err != nil || resp.StatusCode != 200 {
|
if err != nil || resp.StatusCode != 200 {
|
||||||
Logger.Printf("Could not update component: (resp code %d) %v", resp.StatusCode, err)
|
Logger.Printf("Could not update component: (resp code %d) %v", resp.StatusCode, err)
|
||||||
return
|
return
|
||||||
@@ -152,24 +156,24 @@ func (incident *Incident) UpdateComponent() {
|
|||||||
|
|
||||||
// SetInvestigating sets status to Investigating
|
// SetInvestigating sets status to Investigating
|
||||||
func (incident *Incident) SetInvestigating() {
|
func (incident *Incident) SetInvestigating() {
|
||||||
incident.Status = 1
|
incident.Status = "1"
|
||||||
incident.HumanStatus = "Investigating"
|
incident.HumanStatus = "Investigating"
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetIdentified sets status to Identified
|
// SetIdentified sets status to Identified
|
||||||
func (incident *Incident) SetIdentified() {
|
func (incident *Incident) SetIdentified() {
|
||||||
incident.Status = 2
|
incident.Status = "2"
|
||||||
incident.HumanStatus = "Identified"
|
incident.HumanStatus = "Identified"
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetWatching sets status to Watching
|
// SetWatching sets status to Watching
|
||||||
func (incident *Incident) SetWatching() {
|
func (incident *Incident) SetWatching() {
|
||||||
incident.Status = 3
|
incident.Status = "3"
|
||||||
incident.HumanStatus = "Watching"
|
incident.HumanStatus = "Watching"
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetFixed sets status to Fixed
|
// SetFixed sets status to Fixed
|
||||||
func (incident *Incident) SetFixed() {
|
func (incident *Incident) SetFixed() {
|
||||||
incident.Status = 4
|
incident.Status = "4"
|
||||||
incident.HumanStatus = "Fixed"
|
incident.HumanStatus = "Fixed"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,10 @@
|
|||||||
package cachet
|
package cachet
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/tls"
|
||||||
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -15,6 +18,7 @@ type Monitor struct {
|
|||||||
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"`
|
||||||
|
StrictTLS *bool `json:"strict_tls"`
|
||||||
|
|
||||||
History []bool `json:"-"`
|
History []bool `json:"-"`
|
||||||
LastFailReason *string `json:"-"`
|
LastFailReason *string `json:"-"`
|
||||||
@@ -42,6 +46,12 @@ func (monitor *Monitor) doRequest() bool {
|
|||||||
client := &http.Client{
|
client := &http.Client{
|
||||||
Timeout: timeout,
|
Timeout: timeout,
|
||||||
}
|
}
|
||||||
|
if monitor.StrictTLS != nil && *monitor.StrictTLS == false {
|
||||||
|
client.Transport = &http.Transport{
|
||||||
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
resp, err := client.Get(monitor.URL)
|
resp, err := client.Get(monitor.URL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errString := err.Error()
|
errString := err.Error()
|
||||||
@@ -51,7 +61,13 @@ func (monitor *Monitor) doRequest() bool {
|
|||||||
|
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
return resp.StatusCode == monitor.ExpectedStatusCode
|
if resp.StatusCode != monitor.ExpectedStatusCode {
|
||||||
|
failReason := "Unexpected response code: " + strconv.Itoa(resp.StatusCode) + ". Expected " + strconv.Itoa(monitor.ExpectedStatusCode)
|
||||||
|
monitor.LastFailReason = &failReason
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// AnalyseData decides if the monitor is statistically up or down and creates / resolves an incident
|
// AnalyseData decides if the monitor is statistically up or down and creates / resolves an incident
|
||||||
@@ -76,10 +92,11 @@ func (monitor *Monitor) AnalyseData() {
|
|||||||
// is down, create an incident
|
// is down, create an incident
|
||||||
Logger.Println("Creating incident...")
|
Logger.Println("Creating incident...")
|
||||||
|
|
||||||
|
component_id := json.Number(strconv.Itoa(*monitor.ComponentID))
|
||||||
monitor.Incident = &Incident{
|
monitor.Incident = &Incident{
|
||||||
Name: monitor.Name + " - " + Config.SystemName,
|
Name: monitor.Name + " - " + Config.SystemName,
|
||||||
Message: monitor.Name + " failed",
|
Message: monitor.Name + " check failed",
|
||||||
ComponentID: monitor.ComponentID,
|
ComponentID: &component_id,
|
||||||
}
|
}
|
||||||
|
|
||||||
if monitor.LastFailReason != nil {
|
if monitor.LastFailReason != nil {
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package cachet
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"crypto/tls"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
@@ -13,6 +14,12 @@ func makeRequest(requestType string, url string, reqBody []byte) (*http.Response
|
|||||||
req.Header.Set("X-Cachet-Token", Config.APIToken)
|
req.Header.Set("X-Cachet-Token", Config.APIToken)
|
||||||
|
|
||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
|
if Config.InsecureAPI == true {
|
||||||
|
client.Transport = &http.Transport{
|
||||||
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
res, err := client.Do(req)
|
res, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, []byte{}, err
|
return nil, []byte{}, err
|
||||||
|
|||||||
@@ -40,15 +40,19 @@ Configuration
|
|||||||
"component_id": 0,
|
"component_id": 0,
|
||||||
"threshold": 80,
|
"threshold": 80,
|
||||||
"component_id": null,
|
"component_id": null,
|
||||||
"expected_status_code": 200
|
"expected_status_code": 200,
|
||||||
|
"strict_tls": true
|
||||||
}
|
}
|
||||||
]
|
],
|
||||||
|
"insecure_api": false
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
*Notes:*
|
*Notes:*
|
||||||
|
|
||||||
- `metric_id` is optional
|
- `metric_id` is optional
|
||||||
|
- `insecure_api` if true it will ignore HTTPS certificate errors (eg if self-signed)
|
||||||
|
- `strict_tls` if false (true is default) it will ignore HTTPS certificate errors (eg if monitor uses self-signed certificate)
|
||||||
- `component_id` is optional
|
- `component_id` is optional
|
||||||
- `threshold` is a percentage
|
- `threshold` is a percentage
|
||||||
- `expected_status_code` is a http response code
|
- `expected_status_code` is a http response code
|
||||||
|
|||||||
Reference in New Issue
Block a user