Load config from disk|url, create incidents

- Resolve incidents after the monitor is up
- Example configuration
- Updated readme
This commit is contained in:
Matej Kramny
2015-03-18 22:58:45 +01:00
parent 44a9cf905f
commit 42237e9c86
9 changed files with 130 additions and 49 deletions

1
.gitignore vendored
View File

@@ -1 +1,2 @@
gin-bin
example.config.local.json

View File

@@ -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
View 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)
}
}

View File

@@ -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)

View File

@@ -5,37 +5,32 @@ 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!")
}

View File

@@ -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()

14
example.config.json Normal file
View File

@@ -0,0 +1,14 @@
{
"api_url": "https://demo.cachethq.io/api",
"api_token": "9yMHsdioQosnyVK4iCVR",
"monitors": [
{
"name": "nodegear frontend",
"url": "https://nodegear.io/ping",
"metric_id": 1,
"threshold": 80,
"component_id": null,
"expected_status_code": 200
}
]
}

23
main.go
View File

@@ -7,24 +7,9 @@ import (
)
func main() {
monitors := []*cachet.Monitor{
/*&cachet.Monitor{
Name: "nodegear frontend",
Url: "https://nodegear.io/ping",
MetricId: 1,
Threshold: 80.0,
ExpectedStatusCode: 200,
},*/
&cachet.Monitor{
Name: "local test server",
Url: "http://localhost:1337",
Threshold: 80.0,
ExpectedStatusCode: 200,
},
}
fmt.Printf("Starting %d monitors:\n", len(monitors))
for _, monitor := range monitors {
fmt.Printf("API: %s\n", cachet.Config.API_Url)
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)
if monitor.MetricId > 0 {
fmt.Printf(" - Logs lag to metric id: %d\n", monitor.MetricId)
@@ -35,7 +20,7 @@ func main() {
ticker := time.NewTicker(time.Second)
for _ = range ticker.C {
for _, monitor := range monitors {
for _, monitor := range cachet.Config.Monitors {
go monitor.Run()
}
}

View File

@@ -6,9 +6,16 @@ This is a monitoring plugin for CachetHQ.
How to run:
-----------
Example:
1. Set up [Go](https://golang.org)
2. `go install github.com/castawaylabs/cachet-monitor`
3. `cachet-monitor`
3. `cachet-monitor -c https://raw.githubusercontent.com/CastawayLabs/cachet-monitor/master/example.config.json`
Production:
1. Download the example config and save to `/etc/cachet-monitor.config.json`
2. Run in background: `nohup cachet-monitor 2>&1 > /var/log/cachet-monitor.log &`
Environment variables:
----------------------