Skip to content

Commit af7f352

Browse files
committed
Add debug flag
1 parent f5b08f4 commit af7f352

File tree

4 files changed

+34
-16
lines changed

4 files changed

+34
-16
lines changed

icinga.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ var icWarnState = map[string]bool{
2626
}
2727

2828
type icingaCmd struct {
29-
SimpleCFGLocation *string
29+
CFGLocation *string
30+
Debug *bool
3031
// Shared fields
3132
icHostname *string
3233
icHostV4 *string
@@ -51,7 +52,8 @@ type icingaCmd struct {
5152
}
5253

5354
func (this *icingaCmd) Init(flagSet *flag.FlagSet, isServiceCMD bool) {
54-
this.SimpleCFGLocation = flagSet.String("cfg", "/etc/sendmsg.yml", "Path to sendmsg config")
55+
this.CFGLocation = flagSet.String("cfg", "/etc/sendmsg.yml", "Path to sendmsg config")
56+
this.Debug = flagSet.Bool("debug", false, "Whether to print verbose debug messages")
5557
this.icHostname = flagSet.String("l", "", "Hostname")
5658
this.icHostV4 = flagSet.String("4", "", "Host address (v4)")
5759
this.icHostV6 = flagSet.String("6", "", "Host address (v6)")

sendmsg.go

+19-3
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,32 @@ func main() {
3232
}
3333

3434
var cfglocation *string
35+
var debug *bool
3536
switch os.Args[1] {
3637
case "simple":
3738
simpleCommand.Parse(os.Args[2:])
38-
cfglocation = simpleFrontendCmd.SimpleCFGLocation
39+
cfglocation = simpleFrontendCmd.CFGLocation
40+
debug = simpleFrontendCmd.Debug
3941
break
4042
case "icingaHost":
4143
icingaHostCommand.Parse(os.Args[2:])
42-
cfglocation = icingaHostCmd.SimpleCFGLocation
44+
cfglocation = icingaHostCmd.CFGLocation
45+
debug = icingaHostCmd.Debug
4346
break
4447
case "icingaService":
4548
icingaServiceCommand.Parse(os.Args[2:])
46-
cfglocation = icingaServiceCmd.SimpleCFGLocation
49+
cfglocation = icingaServiceCmd.CFGLocation
50+
debug = icingaServiceCmd.Debug
4751
break
4852
default:
4953
flag.PrintDefaults()
5054
return
5155
}
5256

57+
if *debug {
58+
logrus.SetLevel(logrus.DebugLevel)
59+
}
60+
5361
cfg := Config{}
5462
if contents, err := ioutil.ReadFile(*cfglocation); err == nil {
5563
err = yaml.UnmarshalStrict(contents, &cfg)
@@ -60,6 +68,12 @@ func main() {
6068
logrus.WithError(err).Fatal("Couldn't read the configuration file!")
6169
}
6270

71+
logrus.WithFields(logrus.Fields{
72+
"version": gitversion,
73+
"frontend": os.Args[1],
74+
"backend": cfg.Backend,
75+
}).Info("Starting sendmsg")
76+
6377
var msg Message
6478
if simpleCommand.Parsed() {
6579
msg = simpleFrontendCmd.Parse()
@@ -81,5 +95,7 @@ func main() {
8195
switch cfg.Backend {
8296
case "slack":
8397
send_with_slack(msg, cfg)
98+
default:
99+
logrus.WithField("backend", cfg.Backend).Fatal("Unkown backend!")
84100
}
85101
}

simple.go

+10-8
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,25 @@ package main
33
import "flag"
44

55
type simpleCmdData struct {
6-
SimpleCFGLocation *string
7-
head *string
8-
title *string
9-
title_url *string
10-
body *string
11-
color *string
12-
fields fieldList
6+
CFGLocation *string
7+
head *string
8+
title *string
9+
title_url *string
10+
body *string
11+
color *string
12+
fields fieldList
13+
Debug *bool
1314
}
1415

1516
func (this *simpleCmdData) Init(flagSet *flag.FlagSet) {
16-
this.SimpleCFGLocation = flagSet.String("cfg", "/etc/sendmsg.yml", "Path to sendmsg config")
17+
this.CFGLocation = flagSet.String("cfg", "/etc/sendmsg.yml", "Path to sendmsg config")
1718
this.head = flagSet.String("head", "", "The header of the message to send (required)")
1819
this.title = flagSet.String("title", "", "The title of the message to send (required)")
1920
this.title_url = flagSet.String("title_url", "", "The url of the title of the message to send")
2021
this.body = flagSet.String("body", "", "The body of the message to send")
2122
this.color = flagSet.String("color", "", "The color of the message to send")
2223
flagSet.Var(&this.fields, "fields", "A comma seperated list of fields (name:text) to be added")
24+
this.Debug = flagSet.Bool("debug", false, "Whether to print verbose debug messages")
2325
}
2426

2527
func (this *simpleCmdData) Parse() Message {

slack.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,6 @@ func send_with_slack(msg Message, cfg Config) {
7676
bodyString := string(bodyBytes)
7777

7878
if bodyString != "ok" {
79-
logrus.WithFields(logrus.Fields{
80-
"rc": bodyString,
81-
}).Warnf("Slack didn't return 'OK'")
79+
logrus.WithField("rc", bodyString).Warnf("Slack didn't return 'OK'")
8280
}
8381
}

0 commit comments

Comments
 (0)