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

@ -10,11 +10,12 @@ import (
)
type CheckResult struct {
Status string
Event string
Target string
Value string
Threshold string
Status string
Event string
Target string
TargetName string
Value string
Threshold string
}
func (r CheckResult) EventID() string {
@ -26,9 +27,9 @@ func (r CheckResult) EventID() string {
func (r CheckResult) String() string {
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 {
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 {
// FIXME: don't return on errors, process all boxes first!
results = results.FilterChangedFromCache(false)
n := results.Size()

@ -31,14 +31,33 @@ type EmailNotifier struct {
}
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 {
return nil, errors.New("Invalid EmailNotifier options")
asserted, ok := config.(EmailNotifier)
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{
Recipients: res.Recipients,
Recipients: asserted.Recipients,
}, nil
}

Loading…
Cancel
Save