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.
145 lines
4.4 KiB
Go
145 lines
4.4 KiB
Go
6 years ago
|
package cmd
|
||
|
|
||
|
import (
|
||
![]()
6 years ago
|
"os"
|
||
|
|
||
|
log "github.com/sirupsen/logrus"
|
||
6 years ago
|
"github.com/spf13/cobra"
|
||
![]()
6 years ago
|
"github.com/spf13/viper"
|
||
![]()
6 years ago
|
|
||
|
"github.com/noerw/osem_notify/utils"
|
||
6 years ago
|
)
|
||
|
|
||
![]()
6 years ago
|
var configHelpCmd = &cobra.Command{
|
||
|
Use: "config",
|
||
|
Short: "How to configure osem_notify",
|
||
|
Long: `osem_notify works out of the box for basic functionality, but uses configuration to
|
||
|
set up notification transports and healthchecks. Additionally, all command line flags can
|
||
|
be set to default values through the configuration.
|
||
|
|
||
|
Configuration can be set either through a YAML file, or through environment variables.
|
||
|
You can use different configuration files per call by settings the --config flag.
|
||
|
|
||
|
|
||
|
> Example configuration:
|
||
|
|
||
|
# override default health checks:
|
||
|
defaultHealthchecks:
|
||
|
notifications:
|
||
|
transport: email
|
||
|
options:
|
||
|
recipients:
|
||
|
- fridolina@example.com
|
||
|
- ruth.less@example.com
|
||
|
events:
|
||
|
- type: "measurement_age"
|
||
|
target: "all" # all sensors
|
||
|
threshold: "15m" # any duration
|
||
|
- type: "measurement_faulty"
|
||
|
target: "all"
|
||
|
threshold: ""
|
||
|
|
||
|
# only needed when sending notifications via email
|
||
|
email:
|
||
|
host: smtp.example.com
|
||
|
port: 25
|
||
|
user: foo
|
||
|
pass: bar
|
||
|
from: hildegunst@example.com
|
||
|
|
||
|
|
||
|
> possible values for defaultHealthchecks.notifications:
|
||
|
|
||
|
transport | options
|
||
|
----------|-------------------------------------
|
||
|
email | recipients: list of email addresses
|
||
|
|
||
|
|
||
|
> possible values for defaultHealthchecks.events[]:
|
||
|
|
||
|
type | description
|
||
|
-------------------|---------------------------------------------------
|
||
|
measurement_age | Alert when sensor target has not submitted measurements within threshold duration.
|
||
|
measurement_faulty | Alert when sensor target's last reading was a presumably faulty value (e.g. broken / disconnected sensor).
|
||
|
measurement_min | Alert when sensor target's last measurement is lower than threshold.
|
||
|
measurement_max | Alert when sensor target's last measurement is higher than threshold.
|
||
|
|
||
|
- target can be either a sensor ID, or "all" to match all sensors of the box.
|
||
|
- threshold must be a string.
|
||
|
|
||
|
> configuration via environment variables
|
||
|
|
||
|
Instead of a YAML file, you may configure the tool through environment variables. Keys are the same as in the YAML, but:
|
||
|
keys are prefixed with "OSEM_NOTIFY_", path separator is not ".", but "_", all upper case
|
||
|
|
||
|
Example: OSEM_NOTIFY_EMAIL_PASS=supersecret osem_notify check boxes`,
|
||
|
}
|
||
|
|
||
6 years ago
|
var rootCmd = &cobra.Command{
|
||
![]()
6 years ago
|
Use: "osem_notify",
|
||
|
Short: "Root command displaying help",
|
||
|
Long: "Run healthchecks and send notifications for boxes on opensensemap.org",
|
||
![]()
6 years ago
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
||
|
// set up logger
|
||
![]()
6 years ago
|
log.SetOutput(os.Stdout)
|
||
![]()
6 years ago
|
if viper.GetBool("debug") {
|
||
![]()
6 years ago
|
log.SetLevel(log.DebugLevel)
|
||
![]()
6 years ago
|
utils.PrintConfig()
|
||
![]()
6 years ago
|
} else {
|
||
![]()
6 years ago
|
log.SetLevel(log.InfoLevel)
|
||
|
}
|
||
![]()
6 years ago
|
switch viper.Get("logformat") {
|
||
![]()
6 years ago
|
case "json":
|
||
|
log.SetFormatter(&log.JSONFormatter{})
|
||
|
}
|
||
![]()
6 years ago
|
|
||
|
return nil
|
||
![]()
6 years ago
|
},
|
||
6 years ago
|
Run: func(cmd *cobra.Command, args []string) {
|
||
|
cmd.Help()
|
||
|
},
|
||
|
}
|
||
|
|
||
![]()
6 years ago
|
// accessed in initConfig(), as it is initialized before config is loaded (sic)
|
||
|
var cfgFile string
|
||
6 years ago
|
|
||
|
func init() {
|
||
![]()
6 years ago
|
var (
|
||
|
shouldNotify bool
|
||
|
debug bool
|
||
|
logFormat string
|
||
![]()
6 years ago
|
api string
|
||
![]()
6 years ago
|
)
|
||
|
|
||
|
cobra.OnInitialize(initConfig)
|
||
|
|
||
|
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "path to config file (default $HOME/.osem_notify.yml)")
|
||
![]()
6 years ago
|
rootCmd.PersistentFlags().StringVarP(&api, "api", "a", "https://api.opensensemap.org", "openSenseMap API to query against")
|
||
![]()
6 years ago
|
rootCmd.PersistentFlags().StringVarP(&logFormat, "logformat", "l", "plain", "log format, can be plain or json")
|
||
|
rootCmd.PersistentFlags().BoolVarP(&debug, "debug", "d", false, "enable verbose logging")
|
||
![]()
6 years ago
|
rootCmd.PersistentFlags().BoolVarP(&shouldNotify, "notify", "n", false, `if set, will send out notifications,
|
||
![]()
6 years ago
|
Otherwise results are printed to stdout only.
|
||
|
You might want to run 'osem_notify debug notifications' first to verify everything works.
|
||
![]()
6 years ago
|
|
||
|
Notifications for failing checks are sent only once,
|
||
|
and then cached until the issue got resolved.
|
||
|
To clear the cache, delete the file ~/.osem_notify_cache.yaml.
|
||
![]()
6 years ago
|
`)
|
||
![]()
6 years ago
|
|
||
|
viper.BindPFlags(rootCmd.PersistentFlags()) // let flags override config
|
||
![]()
6 years ago
|
|
||
|
rootCmd.AddCommand(configHelpCmd)
|
||
6 years ago
|
}
|
||
|
|
||
|
func Execute() {
|
||
![]()
6 years ago
|
// generate documentation
|
||
![]()
6 years ago
|
// err := doc.GenMarkdownTree(rootCmd, "./docs")
|
||
![]()
6 years ago
|
// if err != nil {
|
||
|
// log.Fatal(err)
|
||
|
// }
|
||
|
|
||
6 years ago
|
if err := rootCmd.Execute(); err != nil {
|
||
![]()
6 years ago
|
os.Exit(1)
|
||
6 years ago
|
}
|
||
|
}
|