refactoring
This commit is contained in:
110
cli/main.go
110
cli/main.go
@@ -1,31 +1,105 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"time"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
|
||||
cachet "github.com/castawaylabs/cachet-monitor"
|
||||
)
|
||||
|
||||
var configPath string
|
||||
var systemName string
|
||||
var logPath string
|
||||
|
||||
func main() {
|
||||
cachet.New()
|
||||
config := cachet.Config
|
||||
log := cachet.Logger
|
||||
flag.StringVar(&configPath, "c", "/etc/cachet-monitor.config.json", "Config path")
|
||||
flag.StringVar(&systemName, "name", "", "System Name")
|
||||
flag.StringVar(&logPath, "log", "", "Log path")
|
||||
flag.Parse()
|
||||
|
||||
log.Printf("System: %s, Interval: %d second(s), API: %s\n", config.SystemName, config.Interval, config.APIUrl)
|
||||
log.Printf("Starting %d monitors:\n", len(config.Monitors))
|
||||
for _, mon := range config.Monitors {
|
||||
log.Printf(" %s: GET %s & Expect HTTP %d\n", mon.Name, mon.URL, mon.ExpectedStatusCode)
|
||||
if mon.MetricID > 0 {
|
||||
log.Printf(" - Logs lag to metric id: %d\n", mon.MetricID)
|
||||
}
|
||||
cfg, err := getConfiguration(configPath)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
log.Println()
|
||||
|
||||
ticker := time.NewTicker(time.Duration(config.Interval) * time.Second)
|
||||
for range ticker.C {
|
||||
for _, mon := range config.Monitors {
|
||||
go mon.Run()
|
||||
}
|
||||
if len(systemName) > 0 {
|
||||
cfg.SystemName = systemName
|
||||
}
|
||||
if len(logPath) > 0 {
|
||||
cfg.LogPath = logPath
|
||||
}
|
||||
|
||||
if len(os.Getenv("CACHET_API")) > 0 {
|
||||
cfg.APIUrl = os.Getenv("CACHET_API")
|
||||
}
|
||||
if len(os.Getenv("CACHET_TOKEN")) > 0 {
|
||||
cfg.APIToken = os.Getenv("CACHET_TOKEN")
|
||||
}
|
||||
|
||||
if err := cfg.ValidateConfiguration(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
cfg.Run()
|
||||
}
|
||||
|
||||
func getLogger(logPath string) *log.Logger {
|
||||
var logWriter = os.Stdout
|
||||
var err error
|
||||
|
||||
if len(logPath) > 0 {
|
||||
logWriter, err = os.Create(logPath)
|
||||
if err != nil {
|
||||
fmt.Printf("Unable to open file '%v' for logging\n", logPath)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
flags := log.Llongfile | log.Ldate | log.Ltime
|
||||
if len(os.Getenv("CACHET_DEV")) > 0 {
|
||||
flags = 0
|
||||
}
|
||||
|
||||
return log.New(logWriter, "", flags)
|
||||
}
|
||||
|
||||
func getConfiguration(path string) (*cachet.CachetMonitor, error) {
|
||||
var cfg cachet.CachetMonitor
|
||||
var data []byte
|
||||
|
||||
// test if its a url
|
||||
url, err := url.ParseRequestURI(path)
|
||||
if err == nil && len(url.Scheme) > 0 {
|
||||
// download config
|
||||
response, err := http.Get(path)
|
||||
if err != nil {
|
||||
return nil, errors.New("Cannot download network config: " + err.Error())
|
||||
}
|
||||
|
||||
defer response.Body.Close()
|
||||
data, _ = ioutil.ReadAll(response.Body)
|
||||
|
||||
fmt.Println("Downloaded network configuration.")
|
||||
} else {
|
||||
data, err = ioutil.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil, errors.New("Config file '" + path + "' missing!")
|
||||
}
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(data, &cfg); err != nil {
|
||||
fmt.Println(err)
|
||||
return nil, errors.New("Cannot parse config!")
|
||||
}
|
||||
|
||||
cfg.Logger = getLogger(cfg.LogPath)
|
||||
|
||||
return &cfg, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user