Skip to content

Commit 7d11b64

Browse files
committed
Add icinga service command
1 parent b2871df commit 7d11b64

File tree

3 files changed

+78
-31
lines changed

3 files changed

+78
-31
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
build:
2-
go build -i -v -ldflags="-X main.gitversion=$(shell git describe --always --long --dirty)"
2+
go build -i -v -ldflags="-X main.gitversion=$(shell git describe --always --long --dirty)"
33
strip sendmsg

icinga_host.go icinga.go

+63-27
Original file line numberDiff line numberDiff line change
@@ -7,63 +7,88 @@ import (
77
"time"
88
"github.com/sirupsen/logrus"
99
"strconv"
10+
"strings"
1011
)
1112

1213
var icGoodState = map[string]bool {
1314
"UP": true,
15+
"OK": true,
1416
}
1517

1618
var icBadState = map[string]bool {
1719
"UNREACHABLE": true,
1820
"DOWN": true,
21+
"CRITICAL": true,
1922
}
2023

21-
type icingaHostCmd struct {
24+
var icWarnState = map[string]bool {
25+
"WARNING": true,
26+
}
27+
28+
type icingaCmd struct {
2229
SimpleCFGLocation *string
23-
icHostname *string
24-
icHostV4 *string
25-
icHostV6 *string
26-
icHostState *string
27-
icHostNotfnType *string
28-
icHostCheckOutput *string
29-
icHostNotfnUser *string
30-
icHostNotfnComment *string
31-
icURL *string
30+
// Shared fields
31+
icHostname *string
32+
icHostV4 *string
33+
icHostV6 *string
34+
icCheckState *string
35+
icCheckOutput *string
36+
icNotfnType *string
37+
icNotfnUser *string
38+
icNotfnComment *string
39+
icURL *string
40+
// Service-only fields
41+
icServiceName *string
42+
icServiceDisplayName *string
3243
// Fields below are unused currently but need to be parsed in order to be compliant
3344
icDateTime *string
3445
icHostDisplayname *string
3546
icUserEmail *string
3647
icMailFrom *string
3748
icToSyslog *string
49+
// Which command are we executing?
50+
icIsServiceCMD bool
3851
}
3952

40-
func (this *icingaHostCmd) Init(flagSet *flag.FlagSet) {
53+
func (this *icingaCmd) Init(flagSet *flag.FlagSet, isServiceCMD bool) {
4154
this.SimpleCFGLocation = flagSet.String("cfg", "/etc/sendmsg.yml", "Path to sendmsg config")
4255
this.icHostname = flagSet.String("l", "", "Hostname")
4356
this.icHostV4 = flagSet.String("4", "", "Host address (v4)")
4457
this.icHostV6 = flagSet.String("6", "", "Host address (v6)")
45-
this.icHostState = flagSet.String("s", "", "Host state")
46-
this.icHostNotfnType = flagSet.String("t", "", "Notification type")
47-
this.icHostCheckOutput = flagSet.String("o", "", "Check output")
48-
this.icHostNotfnUser = flagSet.String("b", "", "Manual host notification user")
49-
this.icHostNotfnComment = flagSet.String("c", "", "Manual host notification comment")
58+
this.icCheckState = flagSet.String("s", "", "Host state")
59+
this.icNotfnType = flagSet.String("t", "", "Notification type")
60+
this.icCheckOutput = flagSet.String("o", "", "Check output")
61+
this.icNotfnUser = flagSet.String("b", "", "Manual notification user")
62+
this.icNotfnComment = flagSet.String("c", "", "Manual notification comment")
5063
this.icURL = flagSet.String("i", "", "URL of Webinterface")
5164
this.icDateTime = flagSet.String("d", "", "Date/Time of event")
5265
this.icHostDisplayname = flagSet.String("n", "", "Host display name")
5366
this.icUserEmail = flagSet.String("r", "", "User email")
5467
this.icMailFrom = flagSet.String("f", "", "Sender email")
5568
this.icToSyslog = flagSet.String("v", "", "Send to syslog")
69+
this.icIsServiceCMD = isServiceCMD
70+
if isServiceCMD {
71+
this.icServiceName = flagSet.String("e", "", "Service name")
72+
this.icServiceDisplayName = flagSet.String("u", "", "Service display name")
73+
}
5674
}
5775

58-
func (this *icingaHostCmd) Parse() Message {
76+
func (this *icingaCmd) Parse() Message {
5977
var msg Message
6078

61-
msg.Body_title = *this.icHostname + " is " + *this.icHostState
79+
if this.icIsServiceCMD {
80+
msg.Body_title = *this.icServiceName + " is " + *this.icCheckState
81+
} else {
82+
msg.Body_title = *this.icHostname + " is " + *this.icCheckState
83+
}
6284

6385
addFieldToMessage(&msg, true, "IPv4", this.icHostV4)
6486
addFieldToMessage(&msg, true, "IPv6", this.icHostV6)
65-
addFieldToMessage(&msg, true, "Notification type", this.icHostNotfnType)
66-
addFieldToMessage(&msg, true, "Notification user", this.icHostNotfnUser)
87+
addFieldToMessage(&msg, true, "Notification type", this.icNotfnType)
88+
addFieldToMessage(&msg, true, "Notification user", this.icNotfnUser)
89+
if this.icIsServiceCMD {
90+
addFieldToMessage(&msg, true, "Host", this.icHostname)
91+
}
6792
timestamp, err := time.Parse("2006-01-02 15:04:05 -0700", *this.icDateTime)
6893
if err != nil {
6994
logrus.WithFields(logrus.Fields{
@@ -77,29 +102,40 @@ func (this *icingaHostCmd) Parse() Message {
77102
var buffer bytes.Buffer
78103

79104
buffer.WriteString("```\n")
80-
buffer.WriteString(*this.icHostCheckOutput)
105+
buffer.WriteString(*this.icCheckOutput)
81106
buffer.WriteString("```\n")
82-
if *this.icHostNotfnComment != "" {
107+
if *this.icNotfnComment != "" {
83108
buffer.WriteString("Comment: ")
84-
buffer.WriteString(*this.icHostNotfnComment)
109+
buffer.WriteString(*this.icNotfnComment)
85110
}
86111
msg.Body = buffer.String()
87112

88113
if *this.icURL != "" {
89114
params := url.Values{}
90115
params.Add("host", *this.icHostname)
91-
msg.Body_link = *this.icURL + "/monitoring/host/show?"+ params.Encode()
116+
if this.icIsServiceCMD {
117+
params.Add("service", *this.icServiceName)
118+
msg.Body_link = *this.icURL + "/monitoring/service/show?"+ strings.Replace(params.Encode(), "+", "%20", -1)
119+
} else {
120+
msg.Body_link = *this.icURL + "/monitoring/host/show?"+ strings.Replace(params.Encode(), "+", "%20", -1)
121+
}
92122
}
93123

94-
if icBadState[*this.icHostState] {
124+
if icBadState[*this.icCheckState] {
95125
msg.Color = "#ff5566"
96-
} else if icGoodState[*this.icHostState] {
126+
} else if icGoodState[*this.icCheckState] {
97127
msg.Color = "#44bb77"
128+
} else if icWarnState[*this.icCheckState] {
129+
msg.Color = "#ffaa44"
98130
} else {
99131
msg.Color = "#aa44ff"
100132
}
101133

102-
msg.Frontend = "Icinga2 Host Notification"
134+
if this.icIsServiceCMD {
135+
msg.Frontend = "Icinga2 Service Notification"
136+
} else {
137+
msg.Frontend = "Icinga2 Host Notification"
138+
}
103139
msg.FrontendIconURL = "https://raw.githubusercontent.com/Icinga/icingaweb2/master/public/img/favicon.png"
104140

105141
return msg

sendmsg.go

+14-3
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,15 @@ func main() {
2020

2121
// 'Icinga2' frontend - use this as a drop-in replacement for mails
2222
icingaHostCommand := flag.NewFlagSet("icingaHost", flag.ExitOnError)
23-
icingaHostCmd := icingaHostCmd{}
24-
icingaHostCmd.Init(icingaHostCommand)
23+
icingaServiceCommand := flag.NewFlagSet("icingaService", flag.ExitOnError)
24+
icingaHostCmd := icingaCmd{}
25+
icingaHostCmd.Init(icingaHostCommand, false)
26+
icingaServiceCmd := icingaCmd{}
27+
icingaServiceCmd.Init(icingaServiceCommand, true)
2528

2629
if len(os.Args) < 2 {
27-
logrus.Fatal("Please use one of [simple, icingaHost]")
30+
logrus.Fatal("Please specify a command [simple, icingaHost, icingaService]")
31+
flag.PrintDefaults()
2832
}
2933

3034
var cfglocation *string
@@ -37,6 +41,10 @@ func main() {
3741
icingaHostCommand.Parse(os.Args[2:])
3842
cfglocation = icingaHostCmd.SimpleCFGLocation
3943
break
44+
case "icingaService":
45+
icingaServiceCommand.Parse(os.Args[2:])
46+
cfglocation = icingaServiceCmd.SimpleCFGLocation
47+
break
4048
default:
4149
flag.PrintDefaults()
4250
return
@@ -59,6 +67,9 @@ func main() {
5967
if icingaHostCommand.Parsed() {
6068
msg = icingaHostCmd.Parse()
6169
}
70+
if icingaServiceCommand.Parsed() {
71+
msg = icingaServiceCmd.Parse()
72+
}
6273

6374
if msg.Body == "" || msg.Body_title == "" {
6475
logrus.WithFields(logrus.Fields{

0 commit comments

Comments
 (0)