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
import (
"github.com/castawaylabs/cachet-monitor/cachet"
"time"
cachet "github.com/castawaylabs/cachet-monitor"
)
func main() {
cachet.New()
config := cachet.Config
log := cachet.Logger

View File

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

View File

@@ -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": "<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": <metric id from cachet>,
"component_id": <component id from cachet>,
"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 |

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
}