implement Box.runChecks()
parent
295c26237b
commit
778b2ae90f
@ -0,0 +1,65 @@
|
|||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"../core"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
func CheckBoxes(boxIds []string, defaultConf *core.NotifyConfig) ([]core.CheckResult, error) {
|
||||||
|
log.Debug("Checking notifications for ", len(boxIds), " box(es)")
|
||||||
|
|
||||||
|
// TODO: return a map of Box: []Notification instead?
|
||||||
|
results := []core.CheckResult{}
|
||||||
|
for _, boxId := range boxIds {
|
||||||
|
r, err := checkBox(boxId, defaultConf)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if r != nil {
|
||||||
|
results = append(results, r...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return results, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func checkBox(boxId string, defaultConf *core.NotifyConfig) ([]core.CheckResult, error) {
|
||||||
|
boxLogger := log.WithFields(log.Fields{"boxId": boxId})
|
||||||
|
boxLogger.Info("checking box for due notifications")
|
||||||
|
|
||||||
|
// get box data
|
||||||
|
box, err := core.Osem.GetBox(boxId)
|
||||||
|
if err != nil {
|
||||||
|
boxLogger.Error(err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// if box has no notify config, we use the defaultConf
|
||||||
|
if box.NotifyConf == nil {
|
||||||
|
box.NotifyConf = defaultConf
|
||||||
|
}
|
||||||
|
|
||||||
|
// run checks
|
||||||
|
results, err2 := box.RunChecks()
|
||||||
|
if err2 != nil {
|
||||||
|
boxLogger.Error("could not run checks on box: ", err2)
|
||||||
|
return results, err2
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, r := range results {
|
||||||
|
resultLog := boxLogger.WithFields(log.Fields{
|
||||||
|
"status": r.Status,
|
||||||
|
"event": r.Event,
|
||||||
|
"value": r.Value,
|
||||||
|
"target": r.Target,
|
||||||
|
})
|
||||||
|
if r.Status == core.CheckOk {
|
||||||
|
resultLog.Debug(r)
|
||||||
|
} else {
|
||||||
|
resultLog.Warn(r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return results, nil
|
||||||
|
}
|
@ -1,26 +1,53 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
|
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
var rootCmd = &cobra.Command{
|
var rootCmd = &cobra.Command{
|
||||||
Use: "osem_notify",
|
Use: "osem_notify",
|
||||||
Long: "Run healthchecks and send notifications for boxes on opensensemap.org",
|
Long: "Run healthchecks and send notifications for boxes on opensensemap.org",
|
||||||
|
PersistentPreRun: func(cmd *cobra.Command, args []string) {
|
||||||
|
log.SetOutput(os.Stdout)
|
||||||
|
switch logLevel {
|
||||||
|
case "debug":
|
||||||
|
log.SetLevel(log.DebugLevel)
|
||||||
|
case "info":
|
||||||
|
log.SetLevel(log.InfoLevel)
|
||||||
|
case "warn":
|
||||||
|
log.SetLevel(log.WarnLevel)
|
||||||
|
case "error":
|
||||||
|
log.SetLevel(log.ErrorLevel)
|
||||||
|
}
|
||||||
|
switch logFormat {
|
||||||
|
case "json":
|
||||||
|
log.SetFormatter(&log.JSONFormatter{})
|
||||||
|
}
|
||||||
|
},
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
cmd.Help()
|
cmd.Help()
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
var shouldNotify bool
|
var (
|
||||||
var defaultConfig string
|
shouldNotify bool
|
||||||
|
defaultConfig string
|
||||||
|
logLevel string
|
||||||
|
logFormat string
|
||||||
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
rootCmd.PersistentFlags().StringVarP(&logLevel, "log-level", "", "info", "log level, can be one of debug, info, warn, error")
|
||||||
|
rootCmd.PersistentFlags().StringVarP(&logFormat, "log-format", "", "plain", "log format, can be plain or json")
|
||||||
rootCmd.PersistentFlags().BoolVarP(&shouldNotify, "notify", "n", false, "if set, will send out notifications.\nOtherwise results are printed to stdout only")
|
rootCmd.PersistentFlags().BoolVarP(&shouldNotify, "notify", "n", false, "if set, will send out notifications.\nOtherwise results are printed to stdout only")
|
||||||
rootCmd.PersistentFlags().StringVarP(&defaultConfig, "confdefault", "c", "", "default JSON config to use for event checking")
|
rootCmd.PersistentFlags().StringVarP(&defaultConfig, "conf-default", "c", "", "default JSON config to use for event checking")
|
||||||
}
|
}
|
||||||
|
|
||||||
func Execute() {
|
func Execute() {
|
||||||
if err := rootCmd.Execute(); err != nil {
|
if err := rootCmd.Execute(); err != nil {
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,74 +0,0 @@
|
|||||||
package core
|
|
||||||
|
|
||||||
import (
|
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
"os"
|
|
||||||
)
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
log.SetLevel(log.DebugLevel)
|
|
||||||
log.SetOutput(os.Stdout)
|
|
||||||
// log.SetFormatter(&log.JSONFormatter{})
|
|
||||||
}
|
|
||||||
|
|
||||||
func CheckNotifications(boxIds []string, defaultConf *NotifyConfig) ([]Notification, []error) {
|
|
||||||
log.Info("Checking notifications for ", len(boxIds), " box(es)")
|
|
||||||
|
|
||||||
// TODO: return a map of Box: []Notification instead?
|
|
||||||
notifications := []Notification{}
|
|
||||||
errors := []error{}
|
|
||||||
for _, boxId := range boxIds {
|
|
||||||
n, err := checkBox(boxId, defaultConf)
|
|
||||||
if notifications != nil {
|
|
||||||
notifications = append(notifications, n...)
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
errors = append(errors, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(errors) == 0 {
|
|
||||||
errors = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return notifications, errors
|
|
||||||
}
|
|
||||||
|
|
||||||
func checkBox(boxId string, defaultConf *NotifyConfig) ([]Notification, error) {
|
|
||||||
boxLogger := log.WithFields(log.Fields{"boxId": boxId})
|
|
||||||
boxLogger.Debug("checking box for due notifications")
|
|
||||||
|
|
||||||
// get box data
|
|
||||||
box, err := osem.GetBox(boxId)
|
|
||||||
if err != nil {
|
|
||||||
boxLogger.Error(err)
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// if box has no notify config, we use the defaultConf
|
|
||||||
if box.NotifyConf == nil {
|
|
||||||
box.NotifyConf = defaultConf
|
|
||||||
}
|
|
||||||
boxLogger.Debug(box.NotifyConf)
|
|
||||||
|
|
||||||
// run checks
|
|
||||||
notifications, err2 := box.runChecks()
|
|
||||||
if err2 != nil {
|
|
||||||
boxLogger.Error("could not run checks on box: ", err)
|
|
||||||
return notifications, err2
|
|
||||||
}
|
|
||||||
if notifications == nil {
|
|
||||||
boxLogger.Debug("all is fine")
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// store notifications for later submit
|
|
||||||
// notifier, err3 := box.getNotifier()
|
|
||||||
// if err3 != nil {
|
|
||||||
// boxLogger.Error("could not get notifier for box: ", err)
|
|
||||||
// return notifications, err3
|
|
||||||
// }
|
|
||||||
// notifier.AddNotifications(notifications)
|
|
||||||
|
|
||||||
return notifications, nil
|
|
||||||
}
|
|
Loading…
Reference in New Issue