112 lines
2.7 KiB
Go
112 lines
2.7 KiB
Go
package core
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
type TargetCheckResults map[*Target][]*CheckResult
|
|
|
|
func (results TargetCheckResults) Size(statusToCheck []string) int {
|
|
size := 0
|
|
for _, boxResults := range results {
|
|
for _, result := range boxResults {
|
|
if result.HasStatus(statusToCheck) {
|
|
size++
|
|
}
|
|
}
|
|
}
|
|
return size
|
|
}
|
|
|
|
func (results TargetCheckResults) Log() {
|
|
// collect statistics for summary print
|
|
targetsSkipped := 0
|
|
targetsWithIssues := 0
|
|
targetsWithoutIssues := 0
|
|
failedChecks := 0
|
|
errorsByEvent := map[string]int{}
|
|
for event, _ := range checkers {
|
|
errorsByEvent[event] = 0
|
|
}
|
|
|
|
for target, boxResults := range results {
|
|
tLog := log.WithFields(log.Fields{
|
|
"target": target.ID,
|
|
})
|
|
countErr := 0
|
|
for _, r := range boxResults {
|
|
resultLog := tLog.WithFields(log.Fields{
|
|
"status": r.Status,
|
|
"type": r.Type,
|
|
"value": r.Value,
|
|
"target": r.Target,
|
|
})
|
|
if r.Status == CheckOk {
|
|
resultLog.Debugf("%s: %s", target, r)
|
|
} else {
|
|
resultLog.Warnf("%s: %s", target, r)
|
|
countErr++
|
|
errorsByEvent[r.Type]++
|
|
}
|
|
}
|
|
|
|
if len(boxResults) == 0 {
|
|
tLog.Infof("%s: no checks defined", target.ID)
|
|
targetsSkipped++
|
|
} else if countErr == 0 {
|
|
tLog.Infof("%s: all is fine!", target.ID)
|
|
targetsWithoutIssues++
|
|
} else {
|
|
// we logged the error(s) already
|
|
targetsWithIssues++
|
|
failedChecks += countErr
|
|
}
|
|
}
|
|
|
|
// print summary
|
|
targetsChecked := targetsWithIssues + targetsWithoutIssues
|
|
if targetsChecked > 1 {
|
|
summaryLog := log.WithFields(log.Fields{
|
|
"targetsChecked": targetsChecked,
|
|
"targetsSkipped": targetsSkipped, // boxes are also skipped when they never submitted any measurements before!
|
|
"targetsOk": targetsWithoutIssues,
|
|
"targetsErr": targetsWithIssues,
|
|
"failedChecks": failedChecks,
|
|
"errorsByCheck": errorsByEvent,
|
|
})
|
|
summaryLog.Infof(
|
|
"summary: %v of %v checked targets are fine. (%v had no checks)",
|
|
targetsWithoutIssues,
|
|
targetsChecked,
|
|
targetsSkipped)
|
|
}
|
|
}
|
|
|
|
func DoChecks(targets []*Target) (TargetCheckResults, error) {
|
|
log.Info("Checking notifications for ", len(targets), " target(s)")
|
|
|
|
results := TargetCheckResults{}
|
|
errs := []string{}
|
|
|
|
// @TODO: check boxes in parallel, capped at 5 at once. and/or rate limit?
|
|
for _, target := range targets {
|
|
boxLogger := log.WithField("target", target.ID)
|
|
boxLogger.Debug("checking target for events")
|
|
|
|
res, err := target.Check()
|
|
if err != nil {
|
|
boxLogger.Errorf("could not run checks for %s: %s", target.ID, err)
|
|
errs = append(errs, err.Error())
|
|
continue
|
|
}
|
|
results[target] = res
|
|
}
|
|
|
|
if len(errs) != 0 {
|
|
return results, fmt.Errorf(strings.Join(errs, "\n"))
|
|
}
|
|
return results, nil
|
|
}
|