-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathicinga.go
More file actions
159 lines (142 loc) · 4.67 KB
/
icinga.go
File metadata and controls
159 lines (142 loc) · 4.67 KB
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
package main
import (
"bytes"
"flag"
"github.com/sirupsen/logrus"
"net/url"
"strconv"
"strings"
"time"
)
var icGoodState = map[string]bool{
"UP": true,
"OK": true,
}
var icBadState = map[string]bool{
"UNREACHABLE": true,
"DOWN": true,
"CRITICAL": true,
}
var icWarnState = map[string]bool{
"WARNING": true,
}
type icingaCmd struct {
CFGLocation *string
Debug *bool
// Shared fields
icHostname *string
icHostV4 *string
icHostV6 *string
icCheckState *string
icCheckOutput *string
icNotfnType *string
icNotfnUser *string
icNotfnComment *string
icURL *string
// Service-only fields
icServiceName *string
icServiceDisplayName *string
// Fields below are unused currently but need to be parsed in order to be compliant
icDateTime *string
icHostDisplayname *string
icUserEmail *string
icMailFrom *string
icToSyslog *string
// Which command are we executing?
icIsServiceCMD bool
}
func (this *icingaCmd) Init(flagSet *flag.FlagSet, isServiceCMD bool) {
this.CFGLocation = flagSet.String("cfg", "/etc/sendmsg.yml", "Path to sendmsg config")
this.Debug = flagSet.Bool("debug", false, "Whether to print verbose debug messages")
this.icHostname = flagSet.String("l", "", "Hostname")
this.icHostV4 = flagSet.String("4", "", "Host address (v4)")
this.icHostV6 = flagSet.String("6", "", "Host address (v6)")
this.icCheckState = flagSet.String("s", "", "Host state")
this.icNotfnType = flagSet.String("t", "", "Notification type")
this.icCheckOutput = flagSet.String("o", "", "Check output")
this.icNotfnUser = flagSet.String("b", "", "Manual notification user")
this.icNotfnComment = flagSet.String("c", "", "Manual notification comment")
this.icURL = flagSet.String("i", "", "URL of Webinterface")
this.icDateTime = flagSet.String("d", "", "Date/Time of event")
this.icHostDisplayname = flagSet.String("n", "", "Host display name")
this.icUserEmail = flagSet.String("r", "", "User email")
this.icMailFrom = flagSet.String("f", "", "Sender email")
this.icToSyslog = flagSet.String("v", "", "Send to syslog")
this.icIsServiceCMD = isServiceCMD
if isServiceCMD {
this.icServiceName = flagSet.String("e", "", "Service name")
this.icServiceDisplayName = flagSet.String("u", "", "Service display name")
}
}
func (this *icingaCmd) Parse() Message {
var msg Message
if this.icIsServiceCMD {
msg.Body_title = *this.icServiceName + " is " + *this.icCheckState
} else {
msg.Body_title = *this.icHostname + " is " + *this.icCheckState
}
addFieldToMessage(&msg, true, "IPv4", this.icHostV4)
addFieldToMessage(&msg, true, "IPv6", this.icHostV6)
addFieldToMessage(&msg, true, "Notification type", this.icNotfnType)
addFieldToMessage(&msg, true, "Notification user", this.icNotfnUser)
if this.icIsServiceCMD {
addFieldToMessage(&msg, true, "Host", this.icHostname)
}
timestamp, err := time.Parse("2006-01-02 15:04:05 -0700", *this.icDateTime)
if err != nil {
logrus.WithFields(logrus.Fields{
"error": err,
}).Warn("Couldn't parse supplied timestamp")
} else {
timeStr := convertToSlackDate(timestamp)
addFieldToMessage(&msg, true, "Timestamp", &timeStr)
}
var buffer bytes.Buffer
buffer.WriteString("```\n")
buffer.WriteString(*this.icCheckOutput)
buffer.WriteString("```\n")
if *this.icNotfnComment != "" {
buffer.WriteString("Comment: ")
buffer.WriteString(*this.icNotfnComment)
}
msg.Body = buffer.String()
if *this.icURL != "" {
params := url.Values{}
params.Add("host", *this.icHostname)
if this.icIsServiceCMD {
params.Add("service", *this.icServiceName)
msg.Body_link = *this.icURL + "/monitoring/service/show?" + strings.Replace(params.Encode(), "+", "%20", -1)
} else {
msg.Body_link = *this.icURL + "/monitoring/host/show?" + strings.Replace(params.Encode(), "+", "%20", -1)
}
}
if icBadState[*this.icCheckState] {
msg.Color = "#ff5566"
} else if icGoodState[*this.icCheckState] {
msg.Color = "#44bb77"
} else if icWarnState[*this.icCheckState] {
msg.Color = "#ffaa44"
} else {
msg.Color = "#aa44ff"
}
if this.icIsServiceCMD {
msg.Frontend = "Icinga2 Service Notification"
} else {
msg.Frontend = "Icinga2 Host Notification"
}
msg.FrontendIconURL = "https://raw.githubusercontent.com/Icinga/icingaweb2/master/public/img/favicon.png"
return msg
}
func addFieldToMessage(msg *Message, short bool, header string, field *string) {
if *field != "" {
msg.Fields = append(msg.Fields, Field{
Header: header,
Text: *field,
Short: short,
})
}
}
func convertToSlackDate(timestamp time.Time) string {
return "<!date^" + strconv.FormatInt(timestamp.Unix(), 10) + "^{date_num} {time_secs}|" +
timestamp.Format("Mon Jan 2 15:04:05 -0700 MST 2006") + ">"
}