better packaging

- update readme
This commit is contained in:
Matej Kramny
2016-05-18 23:54:55 +01:00
parent 025d0c5822
commit 267a6cb6b3
10 changed files with 52 additions and 83 deletions

View File

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

View File

@@ -1,11 +1,13 @@
package main package main
import ( import (
"github.com/castawaylabs/cachet-monitor/cachet"
"time" "time"
cachet "github.com/castawaylabs/cachet-monitor"
) )
func main() { func main() {
cachet.New()
config := cachet.Config config := cachet.Config
log := cachet.Logger log := cachet.Logger
@@ -20,7 +22,7 @@ func main() {
log.Println() 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 range ticker.C {
for _, mon := range config.Monitors { for _, mon := range config.Monitors {
go mon.Run() go mon.Run()

View File

@@ -2,16 +2,16 @@ package cachet
import ( import (
"encoding/json" "encoding/json"
"errors"
"flag" "flag"
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"log" "log"
"net"
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
"github.com/castawaylabs/cachet-monitor/system"
) )
// Static config // Static config
@@ -31,7 +31,7 @@ type CachetConfig struct {
InsecureAPI bool `json:"insecure_api"` InsecureAPI bool `json:"insecure_api"`
} }
func init() { func New() error {
var configPath string var configPath string
var systemName string var systemName string
var logPath string var logPath string
@@ -48,8 +48,7 @@ func init() {
// download config // download config
response, err := http.Get(configPath) response, err := http.Get(configPath)
if err != nil { if err != nil {
fmt.Printf("Cannot download network config: %v\n", err) return errors.New("Cannot download network config: " + err.Error())
os.Exit(1)
} }
defer response.Body.Close() defer response.Body.Close()
@@ -59,16 +58,12 @@ func init() {
} else { } else {
data, err = ioutil.ReadFile(configPath) data, err = ioutil.ReadFile(configPath)
if err != nil { if err != nil {
fmt.Println("Config file '" + configPath + "' missing!") return errors.New("Config file '" + configPath + "' missing!")
os.Exit(1)
} }
} }
err = json.Unmarshal(data, &Config) if err := json.Unmarshal(data, &Config); err != nil {
return errors.New("Cannot parse config!")
if err != nil {
fmt.Println("Cannot parse config!")
os.Exit(1)
} }
if len(systemName) > 0 { if len(systemName) > 0 {
@@ -76,7 +71,10 @@ func init() {
} }
if len(Config.SystemName) == 0 { if len(Config.SystemName) == 0 {
// get hostname // get hostname
Config.SystemName = system.GetHostname() Config.SystemName = getHostname()
}
if Config.Interval <= 0 {
Config.Interval = 60
} }
if len(os.Getenv("CACHET_API")) > 0 { if len(os.Getenv("CACHET_API")) > 0 {
@@ -87,13 +85,11 @@ func init() {
} }
if len(Config.APIToken) == 0 || len(Config.APIUrl) == 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") 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")
os.Exit(1)
} }
if len(Config.Monitors) == 0 { 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") return errors.New("No monitors defined!\nSee sample configuration: https://github.com/CastawayLabs/cachet-monitor/blob/master/example.config.json\n")
os.Exit(1)
} }
if len(logPath) > 0 { if len(logPath) > 0 {
@@ -105,8 +101,7 @@ func init() {
if len(Config.LogPath) > 0 { if len(Config.LogPath) > 0 {
logWriter, err = os.Create(Config.LogPath) logWriter, err = os.Create(Config.LogPath)
if err != nil { if err != nil {
fmt.Printf("Unable to open file '%v' for logging\n", Config.LogPath) return errors.New("Unable to open file '" + Config.LogPath + "' for logging\n")
os.Exit(1)
} }
} }
@@ -116,4 +111,24 @@ func init() {
} }
Logger = log.New(logWriter, "", flags) 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
} }

View File

@@ -14,33 +14,20 @@ Features
- [x] Updates Component to Major Outage if in Partial Outage - [x] Updates Component to Major Outage if in Partial Outage
- [x] Can be run on multiple servers and geo regions - [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 Configuration
------------- -------------
``` ```
{ {
"api_url": "https://demo.cachethq.io/api/v1", "api_url": "https://demo.cachethq.io/api/v1",
"api_token": "9yMHsdioQosnyVK4iCVR", "api_token": "<API TOKEN>",
"interval": 60, "interval": 60,
"monitors": [ "monitors": [
{ {
"name": "nodegear frontend", "name": "Name of your monitor",
"url": "https://nodegear.io/ping", "url": "Ping URL",
"metric_id": 0, "metric_id": <metric id from cachet>,
"component_id": 0, "component_id": <component id from cachet>,
"threshold": 80, "threshold": 80,
"expected_status_code": 200, "expected_status_code": 200,
"strict_tls": true "strict_tls": true
@@ -60,20 +47,14 @@ Configuration
- `expected_status_code` is a http response code - `expected_status_code` is a http response code
- GET request will be performed on the `url` - 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) tip: run in background using `nohup cachet-monitor 2>&1 > /var/log/cachet-monitor.log &`
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 &`
``` ```
Usage of cachet-monitor: Usage of cachet-monitor:
@@ -89,3 +70,4 @@ Environment variables
| ------------ | --------------------------- | --------------------------- | | ------------ | --------------------------- | --------------------------- |
| CACHET_API | http://demo.cachethq.io/api | URL endpoint for cachet api | | CACHET_API | http://demo.cachethq.io/api | URL endpoint for cachet api |
| CACHET_TOKEN | randomvalue | API Authentication token | | CACHET_TOKEN | randomvalue | API Authentication token |
| DEVELOPMENT | 1 | Strips logging |

View File

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