From 267a6cb6b386c4de1a9d785a4a409569c2f1012e Mon Sep 17 00:00:00 2001 From: Matej Kramny Date: Wed, 18 May 2016 23:54:55 +0100 Subject: [PATCH] better packaging - update readme --- Dockerfile | 6 ---- main.go => cli/main.go | 6 ++-- cachet/component.go => component.go | 0 cachet/config.go => config.go | 53 ++++++++++++++++++----------- cachet/incident.go => incident.go | 0 cachet/metrics.go => metrics.go | 0 cachet/monitor.go => monitor.go | 4 +-- readme.md | 42 +++++++---------------- cachet/request.go => request.go | 0 system/config.go | 24 ------------- 10 files changed, 52 insertions(+), 83 deletions(-) delete mode 100644 Dockerfile rename main.go => cli/main.go (81%) rename cachet/component.go => component.go (100%) rename cachet/config.go => config.go (63%) rename cachet/incident.go => incident.go (100%) rename cachet/metrics.go => metrics.go (100%) rename cachet/monitor.go => monitor.go (97%) rename cachet/request.go => request.go (100%) delete mode 100644 system/config.go diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index f163b42..0000000 --- a/Dockerfile +++ /dev/null @@ -1,6 +0,0 @@ -FROM golang - -ADD . /go/src/github.com/castawaylabs/cachet-monitor -RUN go install github.com/castawaylabs/cachet-monitor - -ENTRYPOINT /go/bin/cachet-monitor \ No newline at end of file diff --git a/main.go b/cli/main.go similarity index 81% rename from main.go rename to cli/main.go index d0c672c..fe92dc8 100644 --- a/main.go +++ b/cli/main.go @@ -1,11 +1,13 @@ package main import ( - "github.com/castawaylabs/cachet-monitor/cachet" "time" + + cachet "github.com/castawaylabs/cachet-monitor" ) func main() { + cachet.New() config := cachet.Config log := cachet.Logger @@ -20,7 +22,7 @@ func main() { log.Println() - ticker := time.NewTicker(time.Duration(config.Interval)*time.Second) + ticker := time.NewTicker(time.Duration(config.Interval) * time.Second) for range ticker.C { for _, mon := range config.Monitors { go mon.Run() diff --git a/cachet/component.go b/component.go similarity index 100% rename from cachet/component.go rename to component.go diff --git a/cachet/config.go b/config.go similarity index 63% rename from cachet/config.go rename to config.go index 9e48ff0..68e181c 100644 --- a/cachet/config.go +++ b/config.go @@ -2,16 +2,16 @@ package cachet import ( "encoding/json" + "errors" "flag" "fmt" "io" "io/ioutil" "log" + "net" "net/http" "net/url" "os" - - "github.com/castawaylabs/cachet-monitor/system" ) // Static config @@ -31,7 +31,7 @@ type CachetConfig struct { InsecureAPI bool `json:"insecure_api"` } -func init() { +func New() error { var configPath string var systemName string var logPath string @@ -48,8 +48,7 @@ func init() { // download config response, err := http.Get(configPath) if err != nil { - fmt.Printf("Cannot download network config: %v\n", err) - os.Exit(1) + return errors.New("Cannot download network config: " + err.Error()) } defer response.Body.Close() @@ -59,16 +58,12 @@ func init() { } else { data, err = ioutil.ReadFile(configPath) if err != nil { - fmt.Println("Config file '" + configPath + "' missing!") - os.Exit(1) + return errors.New("Config file '" + configPath + "' missing!") } } - err = json.Unmarshal(data, &Config) - - if err != nil { - fmt.Println("Cannot parse config!") - os.Exit(1) + if err := json.Unmarshal(data, &Config); err != nil { + return errors.New("Cannot parse config!") } if len(systemName) > 0 { @@ -76,7 +71,10 @@ func init() { } if len(Config.SystemName) == 0 { // get hostname - Config.SystemName = system.GetHostname() + Config.SystemName = getHostname() + } + if Config.Interval <= 0 { + Config.Interval = 60 } if len(os.Getenv("CACHET_API")) > 0 { @@ -87,13 +85,11 @@ func init() { } 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) + return errors.New("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") } 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) + return errors.New("No monitors defined!\nSee sample configuration: https://github.com/CastawayLabs/cachet-monitor/blob/master/example.config.json\n") } if len(logPath) > 0 { @@ -105,8 +101,7 @@ func init() { if len(Config.LogPath) > 0 { logWriter, err = os.Create(Config.LogPath) if err != nil { - fmt.Printf("Unable to open file '%v' for logging\n", Config.LogPath) - os.Exit(1) + return errors.New("Unable to open file '" + Config.LogPath + "' for logging\n") } } @@ -116,4 +111,24 @@ func init() { } Logger = log.New(logWriter, "", flags) + + return nil +} + +// getHostname returns id of the current system +func getHostname() string { + hostname, err := os.Hostname() + if err != nil || len(hostname) == 0 { + addrs, err := net.InterfaceAddrs() + + if err != nil { + return "unknown" + } + + for _, addr := range addrs { + return addr.String() + } + } + + return hostname } diff --git a/cachet/incident.go b/incident.go similarity index 100% rename from cachet/incident.go rename to incident.go diff --git a/cachet/metrics.go b/metrics.go similarity index 100% rename from cachet/metrics.go rename to metrics.go diff --git a/cachet/monitor.go b/monitor.go similarity index 97% rename from cachet/monitor.go rename to monitor.go index 6dc687b..8f735c1 100644 --- a/cachet/monitor.go +++ b/monitor.go @@ -115,8 +115,8 @@ func (monitor *Monitor) AnalyseData() { component_id := json.Number(strconv.Itoa(*monitor.ComponentID)) monitor.Incident = &Incident{ - Name: monitor.Incident.Name, - Message: monitor.Name + " check succeeded", + Name: monitor.Incident.Name, + Message: monitor.Name + " check succeeded", ComponentID: &component_id, } diff --git a/readme.md b/readme.md index f4f80a1..26374b1 100644 --- a/readme.md +++ b/readme.md @@ -14,33 +14,20 @@ Features - [x] Updates Component to Major Outage if in Partial Outage - [x] Can be run on multiple servers and geo regions -Docker Quickstart ------------------ - -1. Create a configuration json -2. -``` -docker run -d \ - --name cachet-monitor \ - -h cachet-monitor \ - -v `pwd`/config.json:/etc/cachet-monitor.config.json \ - castawaylabs/cachet-monitor -``` - Configuration ------------- ``` { "api_url": "https://demo.cachethq.io/api/v1", - "api_token": "9yMHsdioQosnyVK4iCVR", + "api_token": "", "interval": 60, "monitors": [ { - "name": "nodegear frontend", - "url": "https://nodegear.io/ping", - "metric_id": 0, - "component_id": 0, + "name": "Name of your monitor", + "url": "Ping URL", + "metric_id": , + "component_id": , "threshold": 80, "expected_status_code": 200, "strict_tls": true @@ -60,20 +47,14 @@ Configuration - `expected_status_code` is a http response code - GET request will be performed on the `url` -How to run ----------- +Installation +------------ -Example: +1. Download binary from release page +2. Create your configuration ([example](https://raw.githubusercontent.com/CastawayLabs/cachet-monitor/master/example.config.json)) +3. `cachet-monitor -c /etc/cachet-monitor.config.json` -1. Set up [Go](https://golang.org) -2. `go get -d github.com/castawaylabs/cachet-monitor` -3. `go install github.com/castawaylabs/cachet-monitor` -4. `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 &` +tip: run in background using `nohup cachet-monitor 2>&1 > /var/log/cachet-monitor.log &` ``` Usage of cachet-monitor: @@ -89,3 +70,4 @@ Environment variables | ------------ | --------------------------- | --------------------------- | | CACHET_API | http://demo.cachethq.io/api | URL endpoint for cachet api | | CACHET_TOKEN | randomvalue | API Authentication token | +| DEVELOPMENT | 1 | Strips logging | diff --git a/cachet/request.go b/request.go similarity index 100% rename from cachet/request.go rename to request.go diff --git a/system/config.go b/system/config.go deleted file mode 100644 index 4c899d9..0000000 --- a/system/config.go +++ /dev/null @@ -1,24 +0,0 @@ -package system - -import ( - "net" - "os" -) - -// GetHostname returns id of the current system -func GetHostname() string { - hostname, err := os.Hostname() - if err != nil || len(hostname) == 0 { - addrs, err := net.InterfaceAddrs() - - if err != nil { - return "unknown" - } - - for _, addr := range addrs { - return addr.String() - } - } - - return hostname -}