Detects hostname/interface ip

- Hostname is monitor's id. Incidents will be created with the monitor id.
This commit is contained in:
Matej Kramny
2015-03-21 11:44:52 +01:00
parent bdb426c232
commit dce1978b51
4 changed files with 50 additions and 12 deletions

View File

@@ -4,6 +4,7 @@ import (
"encoding/json"
"flag"
"fmt"
"github.com/castawaylabs/cachet-monitor/system"
"io/ioutil"
"net/http"
"net/url"
@@ -18,11 +19,14 @@ type CachetConfig struct {
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")
}

View File

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

18
main.go
View File

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

24
system/config.go Normal file
View File

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