From edfd4a51e69d25a584636fa7a34e2b6e0baf8baf Mon Sep 17 00:00:00 2001 From: Matej Kramny Date: Sat, 4 Feb 2017 22:15:24 -0800 Subject: [PATCH] Print monitor features --- cli/main.go | 57 +++++++++++++++++------------------------------------ config.go | 8 ++++++++ http.go | 7 +++++-- monitor.go | 14 +++++++++++-- 4 files changed, 43 insertions(+), 43 deletions(-) diff --git a/cli/main.go b/cli/main.go index 54e0b3e..03d0aff 100644 --- a/cli/main.go +++ b/cli/main.go @@ -73,45 +73,23 @@ func main() { os.Exit(1) } - logrus.Infof("System: %s\nAPI: %s\nMonitors: %d\n\n", cfg.SystemName, cfg.API.URL, len(cfg.Monitors)) + logrus.Infof("System: %s", cfg.SystemName) + logrus.Infof("API: %s", cfg.API.URL) + logrus.Infof("Monitors: %d\n", len(cfg.Monitors)) + logrus.Infof("Pinging cachet") if err := cfg.API.Ping(); err != nil { - logrus.Warnf("Cannot ping cachet!\n%v", err) + logrus.Errorf("Cannot ping cachet!\n%v", err) + os.Exit(1) } + logrus.Infof("Ping OK") wg := &sync.WaitGroup{} - for _, monitorInterface := range cfg.Monitors { - mon := monitorInterface.GetMonitor() - l := logrus.WithFields(logrus.Fields{ - "name": mon.Name, - "interval": mon.Interval, - "target": mon.Target, - "timeout": mon.Timeout, - }) - l.Info(" Starting monitor") + for index, monitor := range cfg.Monitors { + logrus.Infof("Starting Monitor #%d:", index) + logrus.Infof("Features: \n - %v", strings.Join(monitor.Describe(), "\n - ")) - // print features - if mon.Type == "http" { - httpMonitor := monitorInterface.(*cachet.HTTPMonitor) - - for k, v := range httpMonitor.Headers { - logrus.Infof(" - HTTP-Header '%s' '%s'", k, v) - } - if httpMonitor.ExpectedStatusCode > 0 { - l.Infof(" - Expect HTTP %d", httpMonitor.ExpectedStatusCode) - } - if len(httpMonitor.ExpectedBody) > 0 { - l.Infof(" - Expect Body to match \"%v\"", httpMonitor.ExpectedBody) - } - } - if mon.MetricID > 0 { - l.Infof(" - Log lag to metric id %d\n", mon.MetricID) - } - if mon.ComponentID > 0 { - l.Infof(" - Update component id %d\n\n", mon.ComponentID) - } - - go mon.Start(cfg, wg) + // go mon.Start(cfg, wg) } signals := make(chan os.Signal, 1) @@ -120,7 +98,7 @@ func main() { logrus.Warnf("Abort: Waiting monitors to finish") for _, mon := range cfg.Monitors { - mon.(*cachet.AbstractMonitor).Stop() + mon.GetMonitor().Stop() } wg.Wait() @@ -177,17 +155,16 @@ func getConfiguration(path string) (*cachet.CachetMonitor, error) { cfg.Monitors = make([]cachet.MonitorInterface, len(cfg.RawMonitors)) for index, rawMonitor := range cfg.RawMonitors { - var abstract cachet.AbstractMonitor var t cachet.MonitorInterface var err error - monType := "" + monType := cachet.GetMonitorType("") if t, ok := rawMonitor["type"].(string); ok { - monType = t + monType = cachet.GetMonitorType(t) } switch monType { - case "http", "": + case "http": var s cachet.HTTPMonitor err = mapstructure.Decode(rawMonitor, &s) t = &s @@ -198,10 +175,12 @@ func getConfiguration(path string) (*cachet.CachetMonitor, error) { case "tcp": // t = cachet.TCPMonitor default: - logrus.Errorf("Invalid monitor type (index: %d) %v", index, abstract.Type) + logrus.Errorf("Invalid monitor type (index: %d) %v", index, monType) continue } + t.GetMonitor().Type = monType + if err != nil { logrus.Errorf("Unable to unmarshal monitor to type (index: %d): %v", index, err) continue diff --git a/config.go b/config.go index 9d92bac..8579469 100644 --- a/config.go +++ b/config.go @@ -64,3 +64,11 @@ func getHostname() string { func getMs() int64 { return time.Now().UnixNano() / int64(time.Millisecond) } + +func GetMonitorType(t string) string { + if len(t) == 0 { + return "http" + } + + return t +} diff --git a/http.go b/http.go index 736b971..12d1d36 100644 --- a/http.go +++ b/http.go @@ -134,8 +134,11 @@ func (monitor *HTTPMonitor) Validate() []string { return errs } -func (mon *HTTPMonitor) GetMonitor() AbstractMonitor { - return mon.AbstractMonitor +func (mon *HTTPMonitor) Describe() []string { + features := mon.AbstractMonitor.Describe() + features = append(features, "Method: "+mon.Method) + + return features } // SendMetric sends lag metric point diff --git a/monitor.go b/monitor.go index 663c013..520e3ab 100644 --- a/monitor.go +++ b/monitor.go @@ -15,7 +15,8 @@ const HistorySize = 10 type MonitorInterface interface { do() bool Validate() []string - GetMonitor() AbstractMonitor + GetMonitor() *AbstractMonitor + Describe() []string } // AbstractMonitor data model @@ -59,9 +60,18 @@ func (mon *AbstractMonitor) do() bool { func (mon *AbstractMonitor) Validate() []string { return []string{} } -func (mon AbstractMonitor) GetMonitor() AbstractMonitor { +func (mon *AbstractMonitor) GetMonitor() *AbstractMonitor { return mon } +func (mon AbstractMonitor) Describe() []string { + features := []string{"Type: " + mon.Type} + + if len(mon.Name) > 0 { + features = append(features, "Name: "+mon.Name) + } + + return features +} func (mon *AbstractMonitor) Start(cfg *CachetMonitor, wg *sync.WaitGroup) { wg.Add(1)