Load config from disk|url, create incidents
- Resolve incidents after the monitor is up - Example configuration - Updated readme
This commit is contained in:
@@ -2,7 +2,5 @@ package cachet
|
||||
|
||||
import "os"
|
||||
|
||||
// apiUrl -> https://demo.cachethq.io/api
|
||||
// apiToken -> qwertyuiop
|
||||
var apiUrl = os.Getenv("CACHET_API")
|
||||
var apiToken = os.Getenv("CACHET_TOKEN")
|
||||
var ApiUrl = os.Getenv("CACHET_API")
|
||||
var ApiToken = os.Getenv("CACHET_TOKEN")
|
||||
74
cachet/config.go
Normal file
74
cachet/config.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package cachet
|
||||
|
||||
import (
|
||||
"os"
|
||||
"fmt"
|
||||
"flag"
|
||||
"net/url"
|
||||
"net/http"
|
||||
"io/ioutil"
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
var Config CachetConfig
|
||||
|
||||
type CachetConfig struct {
|
||||
API_Url string `json:"api_url"`
|
||||
API_Token string `json:"api_token"`
|
||||
Monitors []*Monitor `json:"monitors"`
|
||||
}
|
||||
|
||||
func init() {
|
||||
var configPath string
|
||||
flag.StringVar(&configPath, "c", "/etc/cachet-monitor.config.json", "Config path")
|
||||
flag.Parse()
|
||||
|
||||
var data []byte
|
||||
|
||||
// test if its a url
|
||||
_, err := url.ParseRequestURI(configPath)
|
||||
if err == nil {
|
||||
// download config
|
||||
response, err := http.Get(configPath)
|
||||
if err != nil {
|
||||
fmt.Printf("Cannot download network config: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
defer response.Body.Close()
|
||||
|
||||
data, _ = ioutil.ReadAll(response.Body)
|
||||
|
||||
fmt.Println("Downloaded network configuration.")
|
||||
} else {
|
||||
data, err = ioutil.ReadFile(configPath)
|
||||
if err != nil {
|
||||
fmt.Println("Config file '" + configPath + "' missing!")
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
err = json.Unmarshal(data, &Config)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("Cannot parse config!")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if len(os.Getenv("CACHET_API")) > 0 {
|
||||
Config.API_Url = os.Getenv("CACHET_API")
|
||||
}
|
||||
if len(os.Getenv("CACHET_TOKEN")) > 0 {
|
||||
Config.API_Token = os.Getenv("CACHET_TOKEN")
|
||||
}
|
||||
|
||||
if len(Config.API_Token) == 0 || len(Config.API_Url) == 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)
|
||||
}
|
||||
|
||||
if len(Config.Monitors) == 0 {
|
||||
fmt.Printf("No monitors defined!\nSee sample configuration: https://github.com/CastawayLabs/cachet-monitor/blob/master/example.config.json\n")
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
@@ -33,9 +33,9 @@ func (incident *Incident) Send() {
|
||||
|
||||
var req *http.Request
|
||||
if incident.Id == 0 {
|
||||
req, err = http.NewRequest("POST", apiUrl + "/incidents", bytes.NewBuffer(jsonBytes))
|
||||
req, err = http.NewRequest("POST", Config.API_Url + "/incidents", bytes.NewBuffer(jsonBytes))
|
||||
} else {
|
||||
req, err = http.NewRequest("PUT", apiUrl + "/incidents/" + strconv.Itoa(incident.Id), bytes.NewBuffer(jsonBytes))
|
||||
req, err = http.NewRequest("PUT", Config.API_Url + "/incidents/" + strconv.Itoa(incident.Id), bytes.NewBuffer(jsonBytes))
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
@@ -43,7 +43,7 @@ func (incident *Incident) Send() {
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("X-Cachet-Token", apiToken)
|
||||
req.Header.Set("X-Cachet-Token", Config.API_Token)
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
|
||||
@@ -5,38 +5,33 @@ import (
|
||||
"bytes"
|
||||
"strconv"
|
||||
"net/http"
|
||||
"io/ioutil"
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
func SendMetric(metricId int, delay int64) {
|
||||
jsonBytes, err := json.Marshal(&map[string]interface{}{
|
||||
if metricId <= 0 {
|
||||
return
|
||||
}
|
||||
|
||||
jsonBytes, _ := json.Marshal(&map[string]interface{}{
|
||||
"value": delay,
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", apiUrl + "/metrics/" + strconv.Itoa(metricId) + "/points", bytes.NewBuffer(jsonBytes))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("X-Cachet-Token", apiToken)
|
||||
|
||||
client := &http.Client{}
|
||||
req, _ := http.NewRequest("POST", Config.API_Url + "/metrics/" + strconv.Itoa(metricId) + "/points", bytes.NewBuffer(jsonBytes))
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("X-Cachet-Token", Config.API_Token)
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
fmt.Printf("Could not log data point!\n%v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
_, _ = ioutil.ReadAll(resp.Body)
|
||||
// fmt.Println(strconv.Itoa(resp.StatusCode) + " " + string(body))
|
||||
|
||||
if resp.StatusCode != 200 {
|
||||
fmt.Println("Could not log data point!")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -17,6 +17,7 @@ type Monitor struct {
|
||||
ExpectedStatusCode int `json:"expected_status_code"`
|
||||
|
||||
History []bool `json:"-"`
|
||||
LastFailReason *string `json:"-"`
|
||||
Incident *Incident `json:"-"`
|
||||
}
|
||||
|
||||
@@ -42,6 +43,8 @@ func (monitor *Monitor) doRequest() bool {
|
||||
}
|
||||
resp, err := client.Get(monitor.Url)
|
||||
if err != nil {
|
||||
errString := err.Error()
|
||||
monitor.LastFailReason = &errString
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -73,7 +76,11 @@ func (monitor *Monitor) AnalyseData() {
|
||||
|
||||
monitor.Incident = &Incident{
|
||||
Name: monitor.Name,
|
||||
Message: monitor.Name + " is unreachable.",
|
||||
Message: monitor.Name + " failed",
|
||||
}
|
||||
|
||||
if monitor.LastFailReason != nil {
|
||||
monitor.Incident.Message += "\n\n" + *monitor.LastFailReason
|
||||
}
|
||||
|
||||
monitor.Incident.SetInvestigating()
|
||||
@@ -91,4 +98,4 @@ func (monitor *Monitor) AnalyseData() {
|
||||
|
||||
func getMs() int64 {
|
||||
return time.Now().UnixNano() / int64(time.Millisecond)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user