basic refactor + new prototype

This commit is contained in:
Matej Kramny
2017-02-04 16:02:22 -08:00
parent aaecc1669a
commit e910807973
6 changed files with 145 additions and 81 deletions

View File

@@ -1,65 +1,58 @@
package cachet
import (
"errors"
"log"
"net"
"os"
"github.com/Sirupsen/logrus"
)
type CachetMonitor struct {
Logger *log.Logger `json:"-"`
APIUrl string `json:"api_url"`
APIToken string `json:"api_token"`
SystemName string `json:"system_name"`
LogPath string `json:"log_path"`
InsecureAPI bool `json:"insecure_api"`
Name string `json:"system_name"`
API CachetAPI `json:"api"`
Monitors []*Monitor `json:"monitors"`
}
func (cfg *CachetMonitor) ValidateConfiguration() error {
if cfg.Logger == nil {
cfg.Logger = log.New(os.Stdout, "", log.Llongfile|log.Ldate|log.Ltime)
}
func (cfg *CachetMonitor) Validate() bool {
valid := true
if len(cfg.SystemName) == 0 {
if len(cfg.Name) == 0 {
// get hostname
cfg.SystemName = getHostname()
cfg.Name = getHostname()
}
if len(cfg.APIToken) == 0 || len(cfg.APIUrl) == 0 {
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(cfg.API.Token) == 0 || len(cfg.API.Url) == 0 {
logrus.Warnf("API URL or API Token missing.\nGet help at https://github.com/castawaylabs/cachet-monitor")
valid = false
}
if len(cfg.Monitors) == 0 {
return errors.New("No monitors defined!\nSee sample configuration: https://github.com/castawaylabs/cachet-monitor/blob/master/example.config.json\n")
logrus.Warnf("No monitors defined!\nSee help for example configuration")
valid = false
}
for _, monitor := range cfg.Monitors {
if err := monitor.ValidateConfiguration(); err != nil {
return err
if err := monitor.Validate(); !valid {
valid = false
}
}
return nil
return valid
}
// 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()
}
if err == nil && len(hostname) > 0 {
return hostname
}
return hostname
addrs, err := net.InterfaceAddrs()
if err != nil {
return "unknown"
}
for _, addr := range addrs {
return addr.String()
}
}