allow to specify which check result types to notify for

develop
Norwin 6 years ago
parent 8df4f6c753
commit dc7a2019e7

@ -116,8 +116,8 @@ var cfgFile string
func init() { func init() {
var ( var (
shouldNotify bool
debug bool debug bool
shouldNotify string
logFormat string logFormat string
api string api string
) )
@ -128,8 +128,9 @@ func init() {
rootCmd.PersistentFlags().StringVarP(&api, "api", "a", "https://api.opensensemap.org", "openSenseMap API to query against") rootCmd.PersistentFlags().StringVarP(&api, "api", "a", "https://api.opensensemap.org", "openSenseMap API to query against")
rootCmd.PersistentFlags().StringVarP(&logFormat, "logformat", "l", "plain", "log format, can be plain or json") rootCmd.PersistentFlags().StringVarP(&logFormat, "logformat", "l", "plain", "log format, can be plain or json")
rootCmd.PersistentFlags().BoolVarP(&debug, "debug", "d", false, "enable verbose logging") rootCmd.PersistentFlags().BoolVarP(&debug, "debug", "d", false, "enable verbose logging")
rootCmd.PersistentFlags().BoolVarP(&shouldNotify, "notify", "n", false, `if set, will send out notifications, rootCmd.PersistentFlags().StringVarP(&shouldNotify, "notify", "n", "", `if set, will send out notifications for the specified type of check result,
Otherwise results are printed to stdout only. Otherwise results are printed to stdout only.
Allowed values are "all", "error", "ok".
You might want to run 'osem_notify debug notifications' first to verify everything works. You might want to run 'osem_notify debug notifications' first to verify everything works.
Notifications for failing checks are sent only once, Notifications for failing checks are sent only once,

@ -3,6 +3,7 @@ package cmd
import ( import (
"fmt" "fmt"
"regexp" "regexp"
"strings"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
@ -49,8 +50,21 @@ func checkAndNotify(boxIds []string) error {
results.Log() results.Log()
if viper.GetBool("notify") { notify := strings.ToLower(viper.GetString("notify"))
return results.SendNotifications() if notify != "" {
types := []string{}
switch notify {
case "all":
types = []string{core.CheckErr, core.CheckOk}
case "error", "err":
types = []string{core.CheckErr}
case "ok":
types = []string{core.CheckOk}
default:
return fmt.Errorf("invalid value %s for \"notify\"", notify)
}
return results.SendNotifications(types)
} }
return nil return nil
} }

@ -10,11 +10,11 @@ import (
type BoxCheckResults map[*Box][]CheckResult type BoxCheckResults map[*Box][]CheckResult
func (results BoxCheckResults) Size(status string) int { func (results BoxCheckResults) Size(statusToCheck []string) int {
size := 0 size := 0
for _, boxResults := range results { for _, boxResults := range results {
for _, result := range boxResults { for _, result := range boxResults {
if status == result.Status || status == "" { if result.HasStatus(statusToCheck) {
size++ size++
} }
} }

@ -37,6 +37,15 @@ type CheckResult struct {
Threshold string Threshold string
} }
func (r CheckResult) HasStatus(statusToCheck []string) bool {
for _, status := range statusToCheck {
if status == r.Status {
return true
}
}
return false
}
func (r CheckResult) EventID() string { func (r CheckResult) EventID() string {
s := fmt.Sprintf("%s%s%s", r.Event, r.Target, r.Threshold) s := fmt.Sprintf("%s%s%s", r.Event, r.Target, r.Threshold)
hasher := sha256.New() hasher := sha256.New()

@ -39,25 +39,23 @@ func (box Box) GetNotifier() (AbstractNotifier, error) {
return notifier.New(box.NotifyConf.Notifications.Options) return notifier.New(box.NotifyConf.Notifications.Options)
} }
func (results BoxCheckResults) SendNotifications() error { func (results BoxCheckResults) SendNotifications(notifyTypes []string) error {
// TODO: expose flags to not use cache, and to notify for checks turned CheckOk as well // TODO: expose flag to not use cache
results = results.filterChangedFromCache() results = results.filterChangedFromCache()
nErr := results.Size(CheckErr) toCheck := results.Size(notifyTypes)
if nErr == 0 { if toCheck == 0 {
log.Info("No notifications due.") log.Info("No notifications due.")
} else { } else {
log.Infof("Notifying for %v checks turned bad in total...", nErr) log.Infof("Notifying for %v checks changing state to %v...", toCheck, notifyTypes)
} }
log.Debugf("%v checks turned OK!", results.Size(CheckOk))
errs := []string{} errs := []string{}
for box, resultsBox := range results { for box, resultsBox := range results {
// only submit results which are errors // only submit results which are errors
resultsDue := []CheckResult{} resultsDue := []CheckResult{}
for _, result := range resultsBox { for _, result := range resultsBox {
if result.Status != CheckOk { if result.HasStatus(notifyTypes) {
resultsDue = append(resultsDue, result) resultsDue = append(resultsDue, result)
} }
} }
@ -91,7 +89,7 @@ func (results BoxCheckResults) SendNotifications() error {
} }
} }
// update cache (also with CheckOk results to reset status) // update cache (with /all/ changed results to reset status)
notifyLog.Debug("updating cache") notifyLog.Debug("updating cache")
cacheError := updateCache(box, resultsBox) cacheError := updateCache(box, resultsBox)
if cacheError != nil { if cacheError != nil {
@ -100,7 +98,7 @@ func (results BoxCheckResults) SendNotifications() error {
} }
if len(resultsDue) != 0 { if len(resultsDue) != 0 {
notifyLog.Infof("Sent notification for %s via %s with %v new issues", box.Name, transport, len(resultsDue)) notifyLog.Infof("Sent notification for %s via %s with %v updated issues", box.Name, transport, len(resultsDue))
} }
} }

Loading…
Cancel
Save