The smallest watchdog on earth. Tiny, monitoring-plugins compatible monitoring with a status page. https://cloud.docker.com/repository/docker/momar/chihuahua/general
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.
 
 
 
 
 
 

67 lines
2.1 KiB

  1. package chihuahua
  2. import (
  3. "github.com/rs/zerolog/log"
  4. "strings"
  5. )
  6. type verification struct {
  7. previousState CheckResult
  8. times uint
  9. }
  10. var verifications = map[*Check]*verification{}
  11. func (chk *Check) Notify(cfg *Config, previousState CheckResult) {
  12. if previousState.Details == "Waiting for first check..." {
  13. previousState.Status = StatusOk
  14. }
  15. if chk.Verify > 0 {
  16. if _, ok := verifications[chk]; ok {
  17. if verifications[chk].previousState.Status == chk.Result.Status {
  18. // switched back to original state, ignoring
  19. delete(verifications, chk)
  20. return
  21. }
  22. verifications[chk].times++
  23. if verifications[chk].times < chk.Verify {
  24. // not yet verified often enough, waiting for next check
  25. return
  26. }
  27. // verified often enough, will fall through to the notifications
  28. previousState = verifications[chk].previousState
  29. delete(verifications, chk)
  30. } else if previousState.Status != chk.Result.Status {
  31. // first time something changed, waiting for next check
  32. verifications[chk] = &verification{
  33. previousState: previousState,
  34. times: 0,
  35. }
  36. return
  37. } else {
  38. // no reason to worry - there's no verification running, and the check didn't change
  39. return
  40. }
  41. } else if previousState.Status == chk.Result.Status {
  42. // no reason to worry - the check didn't change
  43. return
  44. }
  45. if chk.Notifiers == nil {
  46. log.Info().Str("check", strings.Join(chk.FullID(), "/")).Str("status", chk.Result.Status.String()).Msg("notifying everyone due to status change")
  47. for notifier := range cfg.Notifiers {
  48. cfg.Notifiers[notifier].Notify(cfg, *chk, previousState)
  49. }
  50. return
  51. }
  52. log.Info().Str("check", strings.Join(chk.FullID(), "/")).Interface("notifiers", chk.Notifiers).Str("status", chk.Result.Status.String()).Msg("notifying due to status change")
  53. for _, notifier := range chk.Notifiers {
  54. if _, ok := cfg.Notifiers[notifier]; !ok {
  55. log.Error().Str("notifier", notifier).Msg("notifier doesn't exist")
  56. println("notifier is null")
  57. continue // TODO: we need to check somewhere in the config if the notifiers exist?!
  58. }
  59. cfg.Notifiers[notifier].Notify(cfg, *chk, previousState)
  60. }
  61. }