@@ -7,63 +7,88 @@ import (
7
7
"time"
8
8
"github.com/sirupsen/logrus"
9
9
"strconv"
10
+ "strings"
10
11
)
11
12
12
13
var icGoodState = map [string ]bool {
13
14
"UP" : true ,
15
+ "OK" : true ,
14
16
}
15
17
16
18
var icBadState = map [string ]bool {
17
19
"UNREACHABLE" : true ,
18
20
"DOWN" : true ,
21
+ "CRITICAL" : true ,
19
22
}
20
23
21
- type icingaHostCmd struct {
24
+ var icWarnState = map [string ]bool {
25
+ "WARNING" : true ,
26
+ }
27
+
28
+ type icingaCmd struct {
22
29
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
32
43
// Fields below are unused currently but need to be parsed in order to be compliant
33
44
icDateTime * string
34
45
icHostDisplayname * string
35
46
icUserEmail * string
36
47
icMailFrom * string
37
48
icToSyslog * string
49
+ // Which command are we executing?
50
+ icIsServiceCMD bool
38
51
}
39
52
40
- func (this * icingaHostCmd ) Init (flagSet * flag.FlagSet ) {
53
+ func (this * icingaCmd ) Init (flagSet * flag.FlagSet , isServiceCMD bool ) {
41
54
this .SimpleCFGLocation = flagSet .String ("cfg" , "/etc/sendmsg.yml" , "Path to sendmsg config" )
42
55
this .icHostname = flagSet .String ("l" , "" , "Hostname" )
43
56
this .icHostV4 = flagSet .String ("4" , "" , "Host address (v4)" )
44
57
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" )
50
63
this .icURL = flagSet .String ("i" , "" , "URL of Webinterface" )
51
64
this .icDateTime = flagSet .String ("d" , "" , "Date/Time of event" )
52
65
this .icHostDisplayname = flagSet .String ("n" , "" , "Host display name" )
53
66
this .icUserEmail = flagSet .String ("r" , "" , "User email" )
54
67
this .icMailFrom = flagSet .String ("f" , "" , "Sender email" )
55
68
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
+ }
56
74
}
57
75
58
- func (this * icingaHostCmd ) Parse () Message {
76
+ func (this * icingaCmd ) Parse () Message {
59
77
var msg Message
60
78
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
+ }
62
84
63
85
addFieldToMessage (& msg , true , "IPv4" , this .icHostV4 )
64
86
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
+ }
67
92
timestamp , err := time .Parse ("2006-01-02 15:04:05 -0700" , * this .icDateTime )
68
93
if err != nil {
69
94
logrus .WithFields (logrus.Fields {
@@ -77,29 +102,40 @@ func (this *icingaHostCmd) Parse() Message {
77
102
var buffer bytes.Buffer
78
103
79
104
buffer .WriteString ("```\n " )
80
- buffer .WriteString (* this .icHostCheckOutput )
105
+ buffer .WriteString (* this .icCheckOutput )
81
106
buffer .WriteString ("```\n " )
82
- if * this .icHostNotfnComment != "" {
107
+ if * this .icNotfnComment != "" {
83
108
buffer .WriteString ("Comment: " )
84
- buffer .WriteString (* this .icHostNotfnComment )
109
+ buffer .WriteString (* this .icNotfnComment )
85
110
}
86
111
msg .Body = buffer .String ()
87
112
88
113
if * this .icURL != "" {
89
114
params := url.Values {}
90
115
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
+ }
92
122
}
93
123
94
- if icBadState [* this .icHostState ] {
124
+ if icBadState [* this .icCheckState ] {
95
125
msg .Color = "#ff5566"
96
- } else if icGoodState [* this .icHostState ] {
126
+ } else if icGoodState [* this .icCheckState ] {
97
127
msg .Color = "#44bb77"
128
+ } else if icWarnState [* this .icCheckState ] {
129
+ msg .Color = "#ffaa44"
98
130
} else {
99
131
msg .Color = "#aa44ff"
100
132
}
101
133
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
+ }
103
139
msg .FrontendIconURL = "https://raw.githubusercontent.com/Icinga/icingaweb2/master/public/img/favicon.png"
104
140
105
141
return msg
0 commit comments