fix type assertion problem from viper, add sensor name to messages
This commit is contained in:
parent
4106d06493
commit
f1cc1bd8d6
3 changed files with 47 additions and 23 deletions
11
core/Box.go
11
core/Box.go
|
@ -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"`
|
||||
|
@ -81,6 +82,7 @@ func (box Box) RunChecks() ([]CheckResult, error) {
|
|||
var (
|
||||
status = CheckOk
|
||||
target = s.Id
|
||||
targetName = s.Phenomenon
|
||||
value string
|
||||
)
|
||||
|
||||
|
@ -133,6 +135,7 @@ func (box Box) RunChecks() ([]CheckResult, error) {
|
|||
Threshold: event.Threshold,
|
||||
Event: event.Type,
|
||||
Target: target,
|
||||
TargetName: targetName,
|
||||
Value: value,
|
||||
Status: status,
|
||||
})
|
||||
|
|
|
@ -13,6 +13,7 @@ type CheckResult struct {
|
|||
Status string
|
||||
Event string
|
||||
Target string
|
||||
TargetName string
|
||||
Value string
|
||||
Threshold 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 {
|
||||
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…
Add table
Reference in a new issue