-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcmd_root.go
173 lines (140 loc) · 5.35 KB
/
cmd_root.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package cmd
import (
"os"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
// "github.com/spf13/cobra/doc"
"github.com/spf13/viper"
"github.com/noerw/osem_notify/utils"
)
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:
healthchecks:
# override default health checks for all boxes
default:
notifications:
transport: email
options:
recipients:
events:
- type: "measurement_age"
target: "all" # all sensors
threshold: "15m" # any duration
- type: "measurement_faulty"
target: "all"
threshold: ""
# set health checks per box
593bcd656ccf3b0011791f5a:
notifications:
options:
recipients:
events:
- type: "measurement_max"
target: "593bcd656ccf3b0011791f5b"
threshold: "40"
# only needed when sending notifications via email
email:
host: smtp.example.com
port: 25
user: foo
pass: bar
from: [email protected]
# only needed when sending notifications via Slack
slack:
webhook: https://hooks.slack.com/services/T030YPW07/xxxxxxx/xxxxxxxxxxxxxxxxxxxxxx
# only needed when sending notifications via XMPP
xmpp:
host: jabber.example.com:5222
user: [email protected]
pass: bar
startls: true
> possible values for healthchecks.*.notifications:
transport | options
----------|-------------------------------------
email | recipients: list of email addresses
slack | -
xmpp | recipients: list of JIDs
> possible values for healthchecks.*.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`,
}
var rootCmd = &cobra.Command{
Use: "osem_notify",
Short: "Root command displaying help",
Long: "Run healthchecks and send notifications for boxes on opensensemap.org",
Version: "1.3.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
api string
)
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "path to config file (default $HOME/.osem_notify.yml)")
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().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 'osem_notify 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 'osem_notify 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() {
// generate documentation
// err := doc.GenMarkdownTree(rootCmd, "./docs")
// if err != nil {
// log.Fatal(err)
// }
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}