Print monitor features

This commit is contained in:
Matej Kramny
2017-02-04 22:15:24 -08:00
parent a2d8128109
commit edfd4a51e6
4 changed files with 43 additions and 43 deletions

View File

@@ -73,45 +73,23 @@ func main() {
os.Exit(1) 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") logrus.Infof("Pinging cachet")
if err := cfg.API.Ping(); err != nil { 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{} wg := &sync.WaitGroup{}
for _, monitorInterface := range cfg.Monitors { for index, monitor := range cfg.Monitors {
mon := monitorInterface.GetMonitor() logrus.Infof("Starting Monitor #%d:", index)
l := logrus.WithFields(logrus.Fields{ logrus.Infof("Features: \n - %v", strings.Join(monitor.Describe(), "\n - "))
"name": mon.Name,
"interval": mon.Interval,
"target": mon.Target,
"timeout": mon.Timeout,
})
l.Info(" Starting monitor")
// print features // go mon.Start(cfg, wg)
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)
} }
signals := make(chan os.Signal, 1) signals := make(chan os.Signal, 1)
@@ -120,7 +98,7 @@ func main() {
logrus.Warnf("Abort: Waiting monitors to finish") logrus.Warnf("Abort: Waiting monitors to finish")
for _, mon := range cfg.Monitors { for _, mon := range cfg.Monitors {
mon.(*cachet.AbstractMonitor).Stop() mon.GetMonitor().Stop()
} }
wg.Wait() wg.Wait()
@@ -177,17 +155,16 @@ func getConfiguration(path string) (*cachet.CachetMonitor, error) {
cfg.Monitors = make([]cachet.MonitorInterface, len(cfg.RawMonitors)) cfg.Monitors = make([]cachet.MonitorInterface, len(cfg.RawMonitors))
for index, rawMonitor := range cfg.RawMonitors { for index, rawMonitor := range cfg.RawMonitors {
var abstract cachet.AbstractMonitor
var t cachet.MonitorInterface var t cachet.MonitorInterface
var err error var err error
monType := "" monType := cachet.GetMonitorType("")
if t, ok := rawMonitor["type"].(string); ok { if t, ok := rawMonitor["type"].(string); ok {
monType = t monType = cachet.GetMonitorType(t)
} }
switch monType { switch monType {
case "http", "": case "http":
var s cachet.HTTPMonitor var s cachet.HTTPMonitor
err = mapstructure.Decode(rawMonitor, &s) err = mapstructure.Decode(rawMonitor, &s)
t = &s t = &s
@@ -198,10 +175,12 @@ func getConfiguration(path string) (*cachet.CachetMonitor, error) {
case "tcp": case "tcp":
// t = cachet.TCPMonitor // t = cachet.TCPMonitor
default: default:
logrus.Errorf("Invalid monitor type (index: %d) %v", index, abstract.Type) logrus.Errorf("Invalid monitor type (index: %d) %v", index, monType)
continue continue
} }
t.GetMonitor().Type = monType
if err != nil { if err != nil {
logrus.Errorf("Unable to unmarshal monitor to type (index: %d): %v", index, err) logrus.Errorf("Unable to unmarshal monitor to type (index: %d): %v", index, err)
continue continue

View File

@@ -64,3 +64,11 @@ func getHostname() string {
func getMs() int64 { func getMs() int64 {
return time.Now().UnixNano() / int64(time.Millisecond) return time.Now().UnixNano() / int64(time.Millisecond)
} }
func GetMonitorType(t string) string {
if len(t) == 0 {
return "http"
}
return t
}

View File

@@ -134,8 +134,11 @@ func (monitor *HTTPMonitor) Validate() []string {
return errs return errs
} }
func (mon *HTTPMonitor) GetMonitor() AbstractMonitor { func (mon *HTTPMonitor) Describe() []string {
return mon.AbstractMonitor features := mon.AbstractMonitor.Describe()
features = append(features, "Method: "+mon.Method)
return features
} }
// SendMetric sends lag metric point // SendMetric sends lag metric point

View File

@@ -15,7 +15,8 @@ const HistorySize = 10
type MonitorInterface interface { type MonitorInterface interface {
do() bool do() bool
Validate() []string Validate() []string
GetMonitor() AbstractMonitor GetMonitor() *AbstractMonitor
Describe() []string
} }
// AbstractMonitor data model // AbstractMonitor data model
@@ -59,9 +60,18 @@ func (mon *AbstractMonitor) do() bool {
func (mon *AbstractMonitor) Validate() []string { func (mon *AbstractMonitor) Validate() []string {
return []string{} return []string{}
} }
func (mon AbstractMonitor) GetMonitor() AbstractMonitor { func (mon *AbstractMonitor) GetMonitor() *AbstractMonitor {
return mon 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) { func (mon *AbstractMonitor) Start(cfg *CachetMonitor, wg *sync.WaitGroup) {
wg.Add(1) wg.Add(1)