You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 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
}