statusmon/cmd/cmd_root.go

154 lines
4.3 KiB
Go

package cmd
import (
"os"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
// "github.com/spf13/cobra/doc"
"github.com/spf13/viper"
"statusmon/utils"
)
var configHelpCmd = &cobra.Command{
Use: "config",
Short: "How to configure statusmon",
Long: `statusmon configuration of healthchecks, notification transports and default flags
can be set either through a YAML file (default ~/.statusmon.yml), or through environment variables.
> configuration via environment variables
If configured via environment, config keys are the same as in the YAML, but prefixed with "STATUSMON_",
the path separator is not ".", but "_", all upper case
Example: STATUSMON_EMAIL_PASS=supersecret statusmon check
> Example configuration:
healthchecks:
# override default health checks for all boxes
default:
notifications:
transport: email
options:
recipients:
- fridolina@example.com
example:
events:
- type: http-status
target: https://example.com/health
notifications:
options:
recipients:
- ruth.less@example.com
# only needed when sending notifications via email
email:
host: smtp.example.com
port: 25
user: foo
pass: bar
from: hildegunst@example.com
# only needed when sending notifications via XMPP
xmpp:
host: jabber.example.com:5222
user: foo@jabber.example.com
pass: bar
starttls: true
# optionally specify default webhooks for slack & discord
slack:
webhooks: []
discord:
webhooks: []
> possible values for healthchecks.*.notifications:
transport | options
----------|-------------------------------------
email | recipients: list of email addresses
slack | recipients: list of webhook urls
discord | recipients: list of webhook urls
xmpp | recipients: list of JIDs
> possible values for healthchecks.*.events[]:
type | description
-------------------|---------------------------------------------------
http-status | Alert when HTTP GET to target results in status code larger than threshold, default 400`,
}
var rootCmd = &cobra.Command{
Use: "statusmon",
Short: "Root command displaying help",
Long: "Run healthchecks and send notifications for your webservices",
Version: "0.1.0",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
// set up logger
log.SetOutput(os.Stdout)
if viper.GetBool("debug") {
log.SetLevel(log.DebugLevel)
utils.PrintConfig()
} else {
log.SetLevel(log.InfoLevel)
}
switch viper.Get("logformat") {
case "json":
log.SetFormatter(&log.JSONFormatter{})
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
}
// accessed in initConfig(), as it is initialized before config is loaded (sic)
var cfgFile string
func init() {
var (
debug bool
noCache bool
shouldNotify string
logFormat string
)
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "path to config file (default $HOME/.statusmon.yml)")
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().StringVarP(&shouldNotify, "notify", "n", "", `If set, will send out notifications for the specified type of check result,
otherwise results are printed to stdout only.
Allowed values are "all", "error", "ok".
You might want to run 'statusmon debug notifications' first to verify everything works.
Notifications for failing checks are sent only once, and then cached until the issue got
resolved, unless --no-cache is set.
To clear the cache, run 'statusmon debug cache --clear'.
`)
rootCmd.PersistentFlags().BoolVarP(&noCache, "no-cache", "", false, "send all notifications, ignoring results from previous runs. also don't update the cache.")
viper.BindPFlags(rootCmd.PersistentFlags()) // let flags override config
rootCmd.AddCommand(configHelpCmd)
}
func Execute(version string) {
// generate documentation
// err := doc.GenMarkdownTree(rootCmd, "./docs")
// if err != nil {
// log.Fatal(err)
// }
rootCmd.Version = version
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}