statusmon/core/healthchecks.go
Norwin Roosen c4e647b205
work more on stale check notifications
we're doing it kinda wrong, we should mention stale results when
a check switches to OK/FAIL
to do this, we need to track the age of a failed check
2021-03-15 14:35:38 +01:00

71 lines
1.6 KiB
Go

package core
import (
"fmt"
"time"
log "github.com/sirupsen/logrus"
)
const (
CheckOk = "OK"
CheckErr = "FAILED"
eventTargetAll = "all" // if event.Target is this value, all sensors will be checked
)
type checkType struct {
name string // name that is used in config
toString func(result CheckResult) string // error message when check failed
checkFunc func(check Check) (CheckResult, error)
}
var checkers = map[string]checkType{
checkHTTPStatus.name: checkHTTPStatus,
}
type CheckResult struct {
Check
Status string // should be CheckOk | CheckErr
Value string
Stale time.Duration
}
func (r CheckResult) HasStatus(statusToCheck []string) bool {
for _, status := range statusToCheck {
if status == r.Status {
return true
}
}
return false
}
func (r CheckResult) String() string {
if r.Status == CheckOk {
return fmt.Sprintf("%s: %s on %s (%s)\n", r.Status, r.Type, r.Target, r.Value)
} else if r.Stale != 0 {
return fmt.Sprintf("%s: %s (stale since %s)\n", r.Status, checkers[r.Type].toString(r), r.Stale)
} else {
return fmt.Sprintf("%s: %s\n", r.Status, checkers[r.Type].toString(r))
}
}
func (t Target) Check() ([]*CheckResult, error) {
var results = []*CheckResult{}
boxLogger := log.WithField("target", t.ID)
for _, event := range t.Checks {
checker := checkers[event.Type]
if checker.checkFunc == nil {
boxLogger.Warnf("ignoring unknown event type %s", event.Type)
continue
}
result, err := checker.checkFunc(event)
if err != nil {
boxLogger.Errorf("error checking event %s: %v", event.Type, err)
}
results = append(results, &result)
}
return results, nil
}