fix type assertion problem from viper, add sensor name to messages

develop
noerw 6 years ago
parent 4106d06493
commit f1cc1bd8d6

@ -19,10 +19,10 @@ const (
type checkType = struct{ description string } type checkType = struct{ description string }
var checkTypes = map[string]checkType{ var checkTypes = map[string]checkType{
eventMeasurementAge: checkType{"No measurement from %s since %s"}, eventMeasurementAge: checkType{"No measurement from %s (%s) since %s"},
eventMeasurementValMin: checkType{"Sensor %s reads low value of %s"}, eventMeasurementValMin: checkType{"Sensor %s (%s) reads low value of %s"},
eventMeasurementValMax: checkType{"Sensor %s reads high value of %s"}, eventMeasurementValMax: checkType{"Sensor %s (%s) reads high value of %s"},
eventMeasurementValFaulty: checkType{"Sensor %s reads presumably faulty value of %s"}, eventMeasurementValFaulty: checkType{"Sensor %s (%s) reads presumably faulty value of %s"},
} }
type FaultyValue struct { type FaultyValue struct {
@ -58,6 +58,7 @@ type Box struct {
Name string `json:"name"` Name string `json:"name"`
Sensors []struct { Sensors []struct {
Id string `json:"_id"` Id string `json:"_id"`
Phenomenon string `json:"title"`
Type string `json:"sensorType"` Type string `json:"sensorType"`
LastMeasurement *struct { LastMeasurement *struct {
Value string `json:"value"` Value string `json:"value"`
@ -79,9 +80,10 @@ func (box Box) RunChecks() ([]CheckResult, error) {
// a validator must set these values // a validator must set these values
var ( var (
status = CheckOk status = CheckOk
target = s.Id target = s.Id
value string targetName = s.Phenomenon
value string
) )
if event.Target == eventTargetAll || event.Target == s.Id { if event.Target == eventTargetAll || event.Target == s.Id {
@ -130,11 +132,12 @@ func (box Box) RunChecks() ([]CheckResult, error) {
} }
results = append(results, CheckResult{ results = append(results, CheckResult{
Threshold: event.Threshold, Threshold: event.Threshold,
Event: event.Type, Event: event.Type,
Target: target, Target: target,
Value: value, TargetName: targetName,
Status: status, Value: value,
Status: status,
}) })
} }
} }

@ -10,11 +10,12 @@ import (
) )
type CheckResult struct { type CheckResult struct {
Status string Status string
Event string Event string
Target string Target string
Value string TargetName string
Threshold string Value string
Threshold string
} }
func (r CheckResult) EventID() string { func (r CheckResult) EventID() string {
@ -26,9 +27,9 @@ func (r CheckResult) EventID() string {
func (r CheckResult) String() string { func (r CheckResult) String() string {
if r.Status == CheckOk { if r.Status == CheckOk {
return fmt.Sprintf("%s %s (on sensor %s with value %s)\n", r.Event, r.Status, r.Target, r.Value) return fmt.Sprintf("%s %s (on sensor %s (%s) with value %s)\n", r.Event, r.Status, r.TargetName, r.Target, r.Value)
} else { } else {
return fmt.Sprintf("%s: "+checkTypes[r.Event].description+"\n", r.Status, r.Target, r.Value) return fmt.Sprintf("%s: "+checkTypes[r.Event].description+"\n", r.Status, r.TargetName, r.Target, r.Value)
} }
} }
@ -69,6 +70,7 @@ func (results BoxCheckResults) Log() {
} }
func (results BoxCheckResults) SendNotifications() error { func (results BoxCheckResults) SendNotifications() error {
// FIXME: don't return on errors, process all boxes first!
results = results.FilterChangedFromCache(false) results = results.FilterChangedFromCache(false)
n := results.Size() n := results.Size()

@ -31,14 +31,33 @@ type EmailNotifier struct {
} }
func (n EmailNotifier) New(config interface{}) (AbstractNotifier, error) { func (n EmailNotifier) New(config interface{}) (AbstractNotifier, error) {
res, ok := config.(EmailNotifier) // assign configuration to the notifier after ensuring the correct type.
// lesson of this project: golang requires us to fuck around with type
// assertions, instead of providing us with proper inheritance.
if !ok || res.Recipients == nil { asserted, ok := config.(EmailNotifier)
return nil, errors.New("Invalid EmailNotifier options") if !ok || asserted.Recipients == nil {
// config did not contain valid options.
// first try fallback: parse result of viper is a map[string]interface{},
// which requires a different assertion change
asserted2, ok := config.(map[string]interface{})
if ok {
asserted3, ok := asserted2["recipients"].([]interface{})
if ok {
asserted = EmailNotifier{Recipients: []string{}}
for _, rec := range asserted3 {
asserted.Recipients = append(asserted.Recipients, rec.(string))
}
}
}
if asserted.Recipients == nil {
return nil, errors.New("Invalid EmailNotifier options")
}
} }
return EmailNotifier{ return EmailNotifier{
Recipients: res.Recipients, Recipients: asserted.Recipients,
}, nil }, nil
} }

Loading…
Cancel
Save