CLI to run health checks against sensor stations on openSenseMap.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

healthchecks.go 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package core
  2. import (
  3. "crypto/sha256"
  4. "encoding/hex"
  5. "fmt"
  6. log "github.com/sirupsen/logrus"
  7. )
  8. const (
  9. CheckOk = "OK"
  10. CheckErr = "FAILED"
  11. eventTargetAll = "all" // if event.Target is this value, all sensors will be checked
  12. )
  13. type checkType struct {
  14. name string // name that is used in config
  15. toString func(result CheckResult) string // error message when check failed
  16. checkFunc func(event NotifyEvent, sensor Sensor, context Box) (CheckResult, error)
  17. }
  18. var checkers = map[string]checkType{
  19. checkMeasurementAge.name: checkMeasurementAge,
  20. checkMeasurementMin.name: checkMeasurementMin,
  21. checkMeasurementMax.name: checkMeasurementMax,
  22. checkMeasurementFaulty.name: checkMeasurementFaulty,
  23. }
  24. type CheckResult struct {
  25. Status string // should be CheckOk | CheckErr
  26. TargetName string
  27. Value string
  28. Target string
  29. Event string // these should be copied from the NotifyEvent
  30. Threshold string
  31. }
  32. func (r CheckResult) HasStatus(statusToCheck []string) bool {
  33. for _, status := range statusToCheck {
  34. if status == r.Status {
  35. return true
  36. }
  37. }
  38. return false
  39. }
  40. func (r CheckResult) EventID() string {
  41. s := fmt.Sprintf("%s%s%s", r.Event, r.Target, r.Threshold)
  42. hasher := sha256.New()
  43. hasher.Write([]byte(s))
  44. return hex.EncodeToString(hasher.Sum(nil))
  45. }
  46. func (r CheckResult) String() string {
  47. if r.Status == CheckOk {
  48. return fmt.Sprintf("%s: %s (on sensor %s (%s) with value %s)\n", r.Status, r.Event, r.TargetName, r.Target, r.Value)
  49. } else {
  50. return fmt.Sprintf("%s: %s\n", r.Status, checkers[r.Event].toString(r))
  51. }
  52. }
  53. func (box Box) RunChecks() ([]CheckResult, error) {
  54. var results = []CheckResult{}
  55. boxLogger := log.WithField("box", box.Id)
  56. for _, event := range box.NotifyConf.Events {
  57. for _, s := range box.Sensors {
  58. // if a sensor never measured anything, thats ok. checks would fail anyway
  59. if s.LastMeasurement == nil {
  60. continue
  61. }
  62. if event.Target != s.Id && event.Target != eventTargetAll {
  63. continue
  64. }
  65. checker := checkers[event.Type]
  66. if checker.checkFunc == nil {
  67. boxLogger.Warnf("ignoring unknown event type %s", event.Type)
  68. continue
  69. }
  70. result, err := checker.checkFunc(event, s, box)
  71. if err != nil {
  72. boxLogger.Errorf("error checking event %s: %v", event.Type, err)
  73. }
  74. results = append(results, result)
  75. }
  76. }
  77. return results, nil
  78. }