/
loggly-slack.coffee
85 lines (73 loc) · 2.05 KB
/
loggly-slack.coffee
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
# Description:
# Announce Loggly notifications to a slack room.
#
# Dependencies:
# None
#
# Configuration:
# None
#
# Commands:
# None
#
# Notes:
# To use:
# Setup http://hostname/hubot/loggly-slack/%23ROOMNAME as
# your Alert Endpoint in loggly.com. Use GET as the method.
# The POST method will post as text/plain and not be parsed properly.
#
bodyParser = require 'body-parser'
module.exports = (robot) ->
parser = bodyParser.json type: 'text/plain'
removeCharset = (req, res, next) ->
# Remove ISO-8859-1 charset as it is not supported by bodyParser.
req.headers['content-type'] = 'text/plain'
next()
middleware = [removeCharset, parser]
robot.router.post '/hubot/loggly-slack/:room', middleware, (req, res) ->
room = req.params.room
data = req.body
# Example post data
#
# {
# "alert_name": "Name of Alert",
# "edit_alert_link": "https://company.loggly.com/alerts/edit/99999",
# "source_group": "Source Group",
# "start_time": "Aug 29 01:35:52",
# "end_time": "Aug 29 01:40:52",
# "search_link": "https://company.loggly.com/search/?terms=json.level%3Aerror",
# "query": "json.level:error",
# "num_hits": 10,
# "recent_hits": [],
# "owner_username": "company",
# "owner_subdomain": "company",
# "owner_email": "company@example.com"
# }
fields = [
title: data.alert_name
value: data.source_group + ': ' + data.query
short: true
,
title: 'Hits'
value: data.num_hits
short: true
,
title: 'Link'
value: data.search_link
short: false
]
fallback = "#{data.alert_name} : #{data.search_link}"
robot.emit 'slack-attachment',
message:
room: room
username: 'loggly'
icon_emoji: ":warning:"
content:
text: ''
color: "danger"
pretext: ''
fallback: fallback
fields: fields
# Send back an empty response
res.writeHead 204, { 'Content-Length': 0 }
res.end()