Skip to content
This repository was archived by the owner on Jun 8, 2023. It is now read-only.

Commit 8ecc8d8

Browse files
committed
Script for displaying Graylog messages via Hubot
1 parent 0bfa699 commit 8ecc8d8

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed

src/scripts/graylog.coffee

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# Description:
2+
# Fetches graylog messages via Hubot
3+
#
4+
# Dependencies:
5+
# None
6+
#
7+
# Configuration
8+
# GRAYLOG_URL (e.g. https://graylog.example.com)
9+
# GRAYLOG_API_TOKEN (e.g. 098f6bcd4621d373cade4e832627b4f6)
10+
# GRAYLOG_SEPARATOR (e.g. ','. Default: "\n")
11+
#
12+
# Commands:
13+
# hubot graylog - output last 5 graylog messages
14+
# hubot graylog <count> - output last <count> graylog messages
15+
# hubot graylog streams - list graylog streams
16+
# hubot graylog hosts - list graylog hosts
17+
# hubot graylog <stream> <count> - output some messages from given stream
18+
# hubot graylog host <host> <count> - output some messages from given host
19+
#
20+
# Author:
21+
# spajus
22+
23+
module.exports = (robot) ->
24+
25+
robot.hear /graylog streams$/i, (msg) ->
26+
graylogStreams msg, (what) ->
27+
msg.send what
28+
29+
robot.hear /graylog hosts$/i, (msg) ->
30+
graylogHosts msg, (what) ->
31+
msg.send what
32+
33+
robot.hear /graylog$/i, (msg) ->
34+
graylogStreamMessages msg, 'all', 5, (what) ->
35+
msg.send what
36+
37+
robot.hear /graylog (\d+)$/i, (msg) ->
38+
count = parseInt(msg.match[1] || '5')
39+
graylogMessages msg, 'messages.json', (messages) ->
40+
sayMessages messages, count, (what) ->
41+
msg.send what
42+
43+
robot.hear /graylog (.+) (\d+)/i, (msg) ->
44+
stream = msg.match[1]
45+
count = parseInt(msg.match[2] || '5')
46+
graylogStreamMessages msg, stream, count, (what) ->
47+
msg.send what
48+
49+
robot.hear /graylog host (.+) (\d+)/i, (msg) ->
50+
host = msg.match[1]
51+
count = parseInt(msg.match[2] || '5')
52+
graylogHostMessages msg, host, count, (what) ->
53+
msg.send what
54+
55+
graylogStreamMessages = (msg, stream_title, count, cb) ->
56+
if stream_title == 'all'
57+
graylogMessages msg, 'messages.json', (messages) ->
58+
sayMessages messages, count, cb
59+
else
60+
graylogStreamList msg, (streams) ->
61+
for stream in streams
62+
if stream.title == stream_title
63+
url = "streams/#{stream._id}-#{stream.title}/messages.json"
64+
graylogMessages msg, url, (messages) ->
65+
sayMessages messages, count, cb
66+
return
67+
68+
graylogHostMessages = (msg, host_name, count, cb) ->
69+
url = "hosts/#{host_name}/messages.json"
70+
graylogMessages msg, url, (messages) ->
71+
sayMessages messages, count, cb
72+
73+
graylogMessages = (msg, url, cb) ->
74+
graylogList msg, url, cb
75+
76+
sayMessages = (messages, count, cb) ->
77+
said = 0
78+
for message in messages
79+
said += 1
80+
cb "[#{message.histogram_time}] #{message.message}"
81+
return if said >= count or said == 20
82+
if said < 1
83+
cb "No graylog messages"
84+
85+
graylogUrl = (path) ->
86+
"#{process.env.GRAYLOG_URL}/#{path}?api_key=#{process.env.GRAYLOG_API_TOKEN}"
87+
88+
separator = ->
89+
process.env.GRAYLOG_SEPARATOR || "\n"
90+
91+
graylogStreams = (msg, cb) ->
92+
graylogStreamList msg, (streams) ->
93+
stream_titles = []
94+
for stream in streams
95+
stream_titles.push stream.title
96+
cb "Graylog streams: #{stream_titles.join(separator())}"
97+
98+
graylogStreamList = (msg, cb) ->
99+
graylogList msg, 'streams.json', cb
100+
101+
graylogHostList = (msg, cb) ->
102+
graylogList msg, 'hosts.json', cb
103+
104+
graylogList = (msg, url, cb) ->
105+
url = graylogUrl url
106+
msg.robot.http(url).get() (err, res, body) ->
107+
unless res.statusCode is 200
108+
console.log 'Error requesting Graylog url', url, body
109+
return
110+
cb JSON.parse body
111+
112+
graylogHosts = (msg, cb) ->
113+
graylogHostList msg, (hosts) ->
114+
host_names = []
115+
for host in hosts
116+
host_names.push "#{host.host} (#{host.message_count})"
117+
cb "Graylog hosts: #{host_names.join(separator())}"
118+

0 commit comments

Comments
 (0)