|
| 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 | +# Notes |
| 21 | +# Output format: "[timestamp] message content" |
| 22 | +# |
| 23 | +# Author: |
| 24 | +# spajus |
| 25 | + |
| 26 | +module.exports = (robot) -> |
| 27 | + |
| 28 | + robot.hear /graylog streams$/i, (msg) -> |
| 29 | + graylogStreams msg, (what) -> |
| 30 | + msg.send what |
| 31 | + |
| 32 | + robot.hear /graylog hosts$/i, (msg) -> |
| 33 | + graylogHosts msg, (what) -> |
| 34 | + msg.send what |
| 35 | + |
| 36 | + robot.hear /graylog$/i, (msg) -> |
| 37 | + graylogStreamMessages msg, 'all', 5, (what) -> |
| 38 | + msg.send what |
| 39 | + |
| 40 | + robot.hear /graylog (\d+)$/i, (msg) -> |
| 41 | + count = parseInt(msg.match[1] || '5') |
| 42 | + graylogMessages msg, 'messages.json', (messages) -> |
| 43 | + sayMessages messages, count, (what) -> |
| 44 | + msg.send what |
| 45 | + |
| 46 | + robot.hear /graylog (.+) (\d+)/i, (msg) -> |
| 47 | + stream = msg.match[1] |
| 48 | + count = parseInt(msg.match[2] || '5') |
| 49 | + graylogStreamMessages msg, stream, count, (what) -> |
| 50 | + msg.send what |
| 51 | + |
| 52 | + robot.hear /graylog host (.+) (\d+)/i, (msg) -> |
| 53 | + host = msg.match[1] |
| 54 | + count = parseInt(msg.match[2] || '5') |
| 55 | + graylogHostMessages msg, host, count, (what) -> |
| 56 | + msg.send what |
| 57 | + |
| 58 | +graylogStreamMessages = (msg, stream_title, count, cb) -> |
| 59 | + if stream_title == 'all' |
| 60 | + graylogMessages msg, 'messages.json', (messages) -> |
| 61 | + sayMessages messages, count, cb |
| 62 | + else |
| 63 | + graylogStreamList msg, (streams) -> |
| 64 | + for stream in streams |
| 65 | + if stream.title == stream_title |
| 66 | + url = "streams/#{stream._id}-#{stream.title}/messages.json" |
| 67 | + graylogMessages msg, url, (messages) -> |
| 68 | + sayMessages messages, count, cb |
| 69 | + return |
| 70 | + |
| 71 | +graylogHostMessages = (msg, host_name, count, cb) -> |
| 72 | + url = "hosts/#{host_name}/messages.json" |
| 73 | + graylogMessages msg, url, (messages) -> |
| 74 | + sayMessages messages, count, cb |
| 75 | + |
| 76 | +graylogMessages = (msg, url, cb) -> |
| 77 | + graylogList msg, url, cb |
| 78 | + |
| 79 | +sayMessages = (messages, count, cb) -> |
| 80 | + said = 0 |
| 81 | + for message in messages |
| 82 | + said += 1 |
| 83 | + cb "[#{message.histogram_time}] #{message.message}" |
| 84 | + return if said >= count or said == 20 |
| 85 | + if said < 1 |
| 86 | + cb "No graylog messages" |
| 87 | + |
| 88 | +graylogUrl = (path) -> |
| 89 | + "#{process.env.GRAYLOG_URL}/#{path}?api_key=#{process.env.GRAYLOG_API_TOKEN}" |
| 90 | + |
| 91 | +separator = -> |
| 92 | + process.env.GRAYLOG_SEPARATOR || "\n" |
| 93 | + |
| 94 | +graylogStreams = (msg, cb) -> |
| 95 | + graylogStreamList msg, (streams) -> |
| 96 | + stream_titles = [] |
| 97 | + for stream in streams |
| 98 | + stream_titles.push stream.title |
| 99 | + cb "Graylog streams: #{stream_titles.join(separator())}" |
| 100 | + |
| 101 | +graylogStreamList = (msg, cb) -> |
| 102 | + graylogList msg, 'streams.json', cb |
| 103 | + |
| 104 | +graylogHostList = (msg, cb) -> |
| 105 | + graylogList msg, 'hosts.json', cb |
| 106 | + |
| 107 | +graylogList = (msg, url, cb) -> |
| 108 | + url = graylogUrl url |
| 109 | + msg.robot.http(url).get() (err, res, body) -> |
| 110 | + unless res.statusCode is 200 |
| 111 | + console.log 'Error requesting Graylog url', url, body |
| 112 | + return |
| 113 | + cb JSON.parse body |
| 114 | + |
| 115 | +graylogHosts = (msg, cb) -> |
| 116 | + graylogHostList msg, (hosts) -> |
| 117 | + host_names = [] |
| 118 | + for host in hosts |
| 119 | + host_names.push "#{host.host} (#{host.message_count})" |
| 120 | + cb "Graylog hosts: #{host_names.join(separator())}" |
| 121 | + |
0 commit comments