-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
262 lines (224 loc) · 7.15 KB
/
main.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package main
import (
"bufio"
"bytes"
"regexp"
"strings"
"strconv"
"encoding/json"
"text/template"
"time"
"os"
"fmt"
"io/ioutil"
"path/filepath"
"net"
"net/http"
"net/smtp"
"net/url"
"github.com/romana/rlog"
"github.com/ghodss/yaml"
"github.com/antonholmquist/jason"
)
var config string
func MustNoErr(e error, args ...interface{}) {
if e != nil {
if args != nil && args[0] != nil {
rlog.Error(args[0])
}
rlog.Critical(e.Error())
panic(e.Error())
}
}
func LogDebugNoErr(e error, args ...interface{}) {
if e != nil {
if args != nil && args[0] != nil {
rlog.Debug(args[0])
}
rlog.Debug(e.Error())
}
}
func OnlyLogError(e error, args ...interface{}) {
if e != nil {
if args != nil && args[0] != nil {
rlog.Error(args[0])
}
rlog.Error(e.Error())
}
}
func MustString(v string, e error) string {
MustNoErr(e)
return v
}
func MaybeString(v string, e error) string {
return v
}
func MustByteArray(v []byte, e error) []byte {
MustNoErr(e)
return v
}
func LoadConfig() {
config = string(
MustByteArray(yaml.YAMLToJSON(
MustByteArray(ioutil.ReadFile(
MustString(filepath.Abs("./config.yml")))))))
}
func MatchEvent(eventO *jason.Object, notification *jason.Object, regex bool) bool {
var allMatched bool
allMatched = true
var toMatchKey string
toMatchKey = "when"
if regex {
toMatchKey = "when_regex"
}
checkIfRegexMatches, e := notification.GetObject(toMatchKey)
if e != nil {
rlog.Debugf("No %s key to test, returning true", toMatchKey)
return true
}
for eventKey, regexStrings := range checkIfRegexMatches.Map() {
regexStrings, e := regexStrings.Array()
MustNoErr(e)
for _, regexString := range regexStrings {
regexString, e := regexString.String()
MustNoErr(e)
eventKeyAsArray := strings.Split(eventKey, ".")
eventValue, e := eventO.GetString(eventKeyAsArray...)
if e != nil {
return false
}
var matched bool
if regex {
matched, e = regexp.Match(regexString, []byte(eventValue))
MustNoErr(e)
} else {
matched = regexString == eventValue
}
allMatched = allMatched && matched
rlog.Debugf("matched %v [%s] %s == %s \n", matched, eventKey, eventValue, regexString)
}
}
return allMatched
}
func CheckAndNotify(event string) {
rlog.Debugf("Event: %s", event)
eventO, e := jason.NewObjectFromBytes([]byte(event))
LogDebugNoErr(e, "Not valid json event")
if e != nil {
return
}
configO, e := jason.NewObjectFromBytes([]byte(config))
MustNoErr(e, "Not valid config")
notifications, e := configO.GetObjectArray("notifications")
MustNoErr(e, "No notifications defined in config")
for _, notification := range notifications {
title, e := notification.GetString("title")
MustNoErr(e, "No Title defined for notification")
rlog.Debugf("Checking \"%s\"\n", title)
if MatchEvent(eventO, notification, true) && MatchEvent(eventO, notification, false) {
rlog.Debugf("Triggering \"%s\"\n", title)
notify, e := notification.GetStringArray("notify")
MustNoErr(e, "No notify key defined for notification")
PrepareAndSendNotifications(title, event, notify)
}
}
}
func TimeStampFormat (timestamp interface{}, format string) string {
timestampF, _ := strconv.ParseFloat(fmt.Sprintf("%f", timestamp), 64)
return fmt.Sprintf("%v", time.Unix(int64(timestampF), 0).Format(format))
}
func SendNotification(notification string, eventO *map[string]interface{}) {
configO, e := jason.NewObjectFromBytes([]byte(config))
MustNoErr(e, "Not valid config")
var templateFile = MustString(configO.GetString("notifiers", notification, "template"))
var dataEncoding = MaybeString(configO.GetString("notifiers", notification, "data_encoding"))
var tmpl = template.Must(template.New(filepath.Base(templateFile)).
Funcs(template.FuncMap{"TimeStampFormat": TimeStampFormat}).
ParseFiles(MustString(filepath.Abs(templateFile))))
var output bytes.Buffer // will contain output to send
e = tmpl.Execute(&output, &eventO)
MustNoErr(e, fmt.Sprintf("Problems parsing notification template: %s", templateFile))
var notificationUrl = MustString(configO.GetString("notifiers", notification, "url"))
rlog.Debugf("ALERT_TO %v", notificationUrl)
rlog.Debugf("ALERT_TEMPLATE %v", output.String())
urlParsed, e := url.Parse(notificationUrl)
OnlyLogError(e)
rlog.Debug(urlParsed.Scheme)
if urlParsed.Scheme == "smtp" || urlParsed.Scheme == "smtps" {
sendNotificationViaSmtp(urlParsed, output.String(), urlParsed.Scheme == "smtps")
} else {
sendNotificationViaHttp(urlParsed, output.String(), dataEncoding)
}
}
func sendNotificationViaSmtp(notificatonUrl *url.URL, message string, verifyTls bool) {
var auth = smtp.Auth(nil)
password, passSet := notificatonUrl.User.Password()
if passSet != false {
auth = smtp.PlainAuth("", notificatonUrl.User.Username(), password, notificatonUrl.Hostname())
}
smtpQueryString := notificatonUrl.Query()
err := smtp.SendMail(notificatonUrl.Host, auth, smtpQueryString["from"][0], smtpQueryString["to"], []byte(message))
OnlyLogError(err)
}
func sendNotificationViaHttp(notificationUrl *url.URL, message string, dataEncoding string) {
var resp *http.Response
var err error
switch {
case strings.HasPrefix(dataEncoding, "urlencode."):
parts := strings.SplitN(dataEncoding, ".", 2)
if len(parts) != 2 {
rlog.Warnf("Skipping notification > Got unsupported data_encoding ( %v )", dataEncoding)
return
}
payloadKey := parts[1]
values := url.Values{payloadKey: {message}}
resp, err = http.PostForm(notificationUrl.String(), values)
case dataEncoding == "json":
resp, err = http.Post(notificationUrl.String(), "application/json", strings.NewReader(message))
default:
rlog.Warnf("Skipping notification > Got unsupported data_encoding ( %v )", dataEncoding)
return
}
OnlyLogError(err)
if resp != nil && resp.StatusCode != http.StatusOK {
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
OnlyLogError(err, "Error posting notification")
rlog.Warnf("Didn't receive 200 OK when posting notification %v, %v", resp.Status, string(body))
}
}
func PrepareAndSendNotifications (title string, event string, notifications []string) {
rlog.Infof("notify %s %v\n", title, notifications)
var eventO = map[string]interface{}{}
e := json.Unmarshal([]byte(event), &eventO)
MustNoErr(e)
eventO["dockerHostLabel"] = os.Getenv("DOCKER_HOST_LABEL")
eventO["eventJSON"] = string(MustByteArray(json.MarshalIndent(eventO, "", " ")))
eventO["notificationTitle"] = title
for _, notification := range notifications {
SendNotification(notification, &eventO)
}
}
func main() {
// TODO: allow http(s) with tls and all
conn, e := net.Dial("unix", "/var/run/docker.sock")
MustNoErr(e)
LoadConfig()
configO, e := jason.NewObjectFromBytes([]byte(config))
MustNoErr(e)
var filters = ""
filtersO, e := configO.GetObject("filters")
if e == nil {
filters = filtersO.String()
}
fmt.Fprintf(
conn,
"GET /v%s/events?filters=%s HTTP/1.0\r\n\r\n",
os.Getenv("DOCKER_API_VERSION"),
filters)
reader := bufio.NewReader(conn)
for {
CheckAndNotify(string(MustByteArray(reader.ReadBytes('\n'))))
}
rlog.Warnf("Fin - %v", time.Now())
}