diff --git a/cachet/component.go b/cachet/component.go index a6cd677..c194f54 100644 --- a/cachet/component.go +++ b/cachet/component.go @@ -4,15 +4,16 @@ import ( "time" ) +// Cachet component model type Component struct { - Id int `json:"id"` + 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"` + GroupId *int `json:"group_id"` + CreatedAt *time.Time `json:"created_at"` + UpdatedAt *time.Time `json:"updated_at"` + DeletedAt *time.Time `json:"deleted_at"` } \ No newline at end of file diff --git a/cachet/config.go b/cachet/config.go index ef7b543..285df53 100644 --- a/cachet/config.go +++ b/cachet/config.go @@ -10,11 +10,13 @@ import ( "encoding/json" ) +// Static config var Config CachetConfig +// Monitoring tool configuration type CachetConfig struct { - API_Url string `json:"api_url"` - API_Token string `json:"api_token"` + APIUrl string `json:"api_url"` + APIToken string `json:"api_token"` Monitors []*Monitor `json:"monitors"` } @@ -56,13 +58,13 @@ func init() { } 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 { - 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") os.Exit(1) } diff --git a/cachet/incident.go b/cachet/incident.go index ad0a80d..da74e1c 100644 --- a/cachet/incident.go +++ b/cachet/incident.go @@ -6,26 +6,30 @@ import ( "encoding/json" ) +// Cachet Incident data model type Incident struct { - Id int `json:"id"` + ID int `json:"id"` Name string `json:"name"` Message string `json:"message"` Status int `json:"status"`// 4? - Human_status string `json:"human_status"` + HumanStatus string `json:"human_status"` Component *Component `json:"component"` - Component_id *int `json:"component_id"` - Created_at int `json:"created_at"` - Updated_at int `json:"updated_at"` + ComponentId *int `json:"component_id"` + CreatedAt int `json:"created_at"` + UpdatedAt int `json:"updated_at"` } +// Response when creating/updating an incident type IncidentData struct { Incident Incident `json:"data"` } +// from API /incidents type IncidentList struct { Incidents []Incident `json:"data"` } +// Get list of incidents func GetIncidents() []Incident { _, body, err := makeRequest("GET", "/incidents", nil) if err != nil { @@ -36,12 +40,12 @@ func GetIncidents() []Incident { err = json.Unmarshal(body, &data) if err != nil { fmt.Println("Cannot parse incidents.") - panic(err) } return data.Incidents } +// Create or Update incident func (incident *Incident) Send() { jsonBytes, err := json.Marshal(incident) if err != nil { @@ -49,13 +53,13 @@ func (incident *Incident) Send() { } requestType := "POST" - requestUrl := "/incidents" - if incident.Id > 0 { + requestURL := "/incidents" + if incident.ID > 0 { 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 { panic(err) } @@ -68,44 +72,50 @@ func (incident *Incident) Send() { fmt.Println("Cannot parse incident body.") panic(err) } 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 { fmt.Println("Could not create/update incident!") } } +// Get the same incident. +// Updates incident.ID func (incident *Incident) GetSimilarIncidentId() { incidents := GetIncidents() for _, inc := range incidents { - if incident.Name == inc.Name && incident.Message == inc.Message && incident.Status == inc.Status && incident.Human_status == inc.Human_status { - incident.Id = inc.Id - fmt.Printf("Updated incident id to %v\n", inc.Id) + if incident.Name == inc.Name && incident.Message == inc.Message && incident.Status == inc.Status && incident.HumanStatus == inc.HumanStatus { + incident.ID = inc.ID + fmt.Printf("Updated incident id to %v\n", inc.ID) break } } } +// Set status to Investigating func (incident *Incident) SetInvestigating() { incident.Status = 1 - incident.Human_status = "Investigating" + incident.HumanStatus = "Investigating" } +// Set status to Identified func (incident *Incident) SetIdentified() { incident.Status = 2 - incident.Human_status = "Identified" + incident.HumanStatus = "Identified" } +// Set status to Watching func (incident *Incident) SetWatching() { incident.Status = 3 - incident.Human_status = "Watching" + incident.HumanStatus = "Watching" } +// Set status to Fixed func (incident *Incident) SetFixed() { incident.Status = 4 - incident.Human_status = "Fixed" + incident.HumanStatus = "Fixed" } \ No newline at end of file diff --git a/cachet/metrics.go b/cachet/metrics.go index 59eb5ba..44f0142 100644 --- a/cachet/metrics.go +++ b/cachet/metrics.go @@ -6,8 +6,9 @@ import ( "encoding/json" ) -func SendMetric(metricId int, delay int64) { - if metricId <= 0 { +// Send lag metric point +func SendMetric(metricID int, delay int64) { + if metricID <= 0 { return } @@ -15,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 diff --git a/cachet/monitor.go b/cachet/monitor.go index 1935dac..99bc790 100644 --- a/cachet/monitor.go +++ b/cachet/monitor.go @@ -8,12 +8,13 @@ import ( 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"` + URL string `json:"url"` + MetricID int `json:"metric_id"` Threshold float32 `json:"threshold"` - ComponentId *int `json:"component_id"` + ComponentID *int `json:"component_id"` ExpectedStatusCode int `json:"expected_status_code"` History []bool `json:"-"` @@ -21,6 +22,7 @@ type Monitor struct { Incident *Incident `json:"-"` } +// Run loop func (monitor *Monitor) Run() { reqStart := getMs() isUp := monitor.doRequest() @@ -53,6 +55,7 @@ func (monitor *Monitor) doRequest() bool { return resp.StatusCode == monitor.ExpectedStatusCode } +// Decides if the monitor is statistically up or down and creates / resolves an incident func (monitor *Monitor) AnalyseData() { // look at the past few incidents numDown := 0 diff --git a/cachet/request.go b/cachet/request.go index 578cf17..bc912c6 100644 --- a/cachet/request.go +++ b/cachet/request.go @@ -7,10 +7,10 @@ import ( ) 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("X-Cachet-Token", Config.API_Token) + req.Header.Set("X-Cachet-Token", Config.APIToken) client := &http.Client{} res, err := client.Do(req) diff --git a/main.go b/main.go index 2d5ebf2..b419901 100644 --- a/main.go +++ b/main.go @@ -7,7 +7,7 @@ import ( ) 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)) for _, monitor := range cachet.Config.Monitors { fmt.Printf(" %s: GET %s & Expect HTTP %d\n", monitor.Name, monitor.Url, monitor.ExpectedStatusCode)