From dce1978b510d36a8a2368c92f9a7a082fbc75e79 Mon Sep 17 00:00:00 2001 From: Matej Kramny Date: Sat, 21 Mar 2015 11:44:52 +0100 Subject: [PATCH] Detects hostname/interface ip - Hostname is monitor's id. Incidents will be created with the monitor id. --- cachet/config.go | 18 +++++++++++++++--- cachet/monitor.go | 2 +- main.go | 18 ++++++++++-------- system/config.go | 24 ++++++++++++++++++++++++ 4 files changed, 50 insertions(+), 12 deletions(-) create mode 100644 system/config.go diff --git a/cachet/config.go b/cachet/config.go index b8aa8b5..b3ca870 100644 --- a/cachet/config.go +++ b/cachet/config.go @@ -4,6 +4,7 @@ import ( "encoding/json" "flag" "fmt" + "github.com/castawaylabs/cachet-monitor/system" "io/ioutil" "net/http" "net/url" @@ -15,14 +16,17 @@ var Config CachetConfig // CachetConfig is the monitoring tool configuration type CachetConfig struct { - APIUrl string `json:"api_url"` - APIToken string `json:"api_token"` - Monitors []*Monitor `json:"monitors"` + APIUrl string `json:"api_url"` + APIToken string `json:"api_token"` + Monitors []*Monitor `json:"monitors"` + SystemName string `json:"system_name"` } func init() { var configPath string + var systemName string flag.StringVar(&configPath, "c", "/etc/cachet-monitor.config.json", "Config path") + flag.StringVar(&systemName, "name", "", "System Name") flag.Parse() var data []byte @@ -57,6 +61,14 @@ func init() { os.Exit(1) } + if len(systemName) > 0 { + Config.SystemName = systemName + } + if len(Config.SystemName) == 0 { + // get hostname + Config.SystemName = system.GetHostname() + } + if len(os.Getenv("CACHET_API")) > 0 { Config.APIUrl = os.Getenv("CACHET_API") } diff --git a/cachet/monitor.go b/cachet/monitor.go index 2a922d7..6997de2 100644 --- a/cachet/monitor.go +++ b/cachet/monitor.go @@ -78,7 +78,7 @@ func (monitor *Monitor) AnalyseData() { fmt.Println("Creating incident...") monitor.Incident = &Incident{ - Name: monitor.Name, + Name: monitor.Name + " - " + Config.SystemName, Message: monitor.Name + " failed", } diff --git a/main.go b/main.go index cf79c43..2cce06f 100644 --- a/main.go +++ b/main.go @@ -7,12 +7,14 @@ import ( ) func main() { - fmt.Printf("API: %s\n", cachet.Config.APIUrl) - fmt.Printf("Starting %d monitors:\n", len(cachet.Config.Monitors)) - for _, monitor := range cachet.Config.Monitors { - fmt.Printf(" %s: GET %s & Expect HTTP %d\n", monitor.Name, monitor.URL, monitor.ExpectedStatusCode) - if monitor.MetricID > 0 { - fmt.Printf(" - Logs lag to metric id: %d\n", monitor.MetricID) + config := cachet.Config + + fmt.Printf("System: %s, API: %s\n", config.SystemName, config.APIUrl) + fmt.Printf("Starting %d monitors:\n", len(config.Monitors)) + for _, mon := range config.Monitors { + fmt.Printf(" %s: GET %s & Expect HTTP %d\n", mon.Name, mon.URL, mon.ExpectedStatusCode) + if mon.MetricID > 0 { + fmt.Printf(" - Logs lag to metric id: %d\n", mon.MetricID) } } @@ -20,8 +22,8 @@ func main() { ticker := time.NewTicker(time.Second) for range ticker.C { - for _, monitor := range cachet.Config.Monitors { - go monitor.Run() + for _, mon := range config.Monitors { + go mon.Run() } } } diff --git a/system/config.go b/system/config.go new file mode 100644 index 0000000..4c899d9 --- /dev/null +++ b/system/config.go @@ -0,0 +1,24 @@ +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 +}