-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathnewrelic.coffee
More file actions
69 lines (63 loc) · 2.82 KB
/
newrelic.coffee
File metadata and controls
69 lines (63 loc) · 2.82 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
# Description:
# Display current app performance stats from New Relic
#
# Dependencies:
# "xml2js": "0.2.0"
#
# Configuration:
# HUBOT_NEWRELIC_ACCOUNT_ID
# HUBOT_NEWRELIC_APP_ID
# HUBOT_NEWRELIC_API_KEY
#
# Commands:
# hubot newrelic <appname> - Returns summary application stats from New Relic
#
# Notes:
# How to find these settings:
# After signing into New Relic, select your application
# Given: https://rpm.newrelic.com/accounts/xxx/applications/yyy
# xxx is your Account ID and yyy is your App ID
# Account Settings > API + Web Integrations > API Access > "API key"
#
# Author:
# briandoll
module.exports = (robot) ->
# robot.respond /newrelic me/i, (msg) ->
# accountId = process.env.HUBOT_NEWRELIC_ACCOUNT_ID
# appId = process.env.HUBOT_NEWRELIC_APP_ID
# apiKey = process.env.HUBOT_NEWRELIC_API_KEY
# Parser = require("xml2js").Parser
# msg.http("https://rpm.newrelic.com/accounts/#{accountId}/applications/#{appId}/threshold_values.xml?api_key=#{apiKey}")
# .get() (err, res, body) ->
# if err
# msg.send "New Relic says: #{err}"
# return
# (new Parser).parseString body, (err, json) ->
# threshold_values = json['threshold-values']['threshold_value'] || []
# lines = threshold_values.map (threshold_value) ->
# "#{threshold_value['$']['name']}: #{threshold_value['$']['formatted_metric_value']}"
# msg.send lines.join("\n"), "https://rpm.newrelic.com/accounts/#{accountId}/applications/#{appId}"
robot.respond /newrelic (.*)/i, (msg) ->
accountId = process.env.HUBOT_NEWRELIC_ACCOUNT_ID
appId = process.env.HUBOT_NEWRELIC_APP_ID
apiKey = process.env.HUBOT_NEWRELIC_API_KEY
Parser = require("xml2js").Parser
match = msg.match[1].trim()
msg.http("https://api.newrelic.com/accounts.xml?include=application_health&api_key=#{apiKey}")
.get() (err, res, body) ->
if err
msg.send "New Relic says: #{err}"
return
(new Parser).parseString body, (err, json) ->
accounts = json['accounts']['account']
accounts.forEach (account) ->
applications = account['applications'][0]['application']
applications.forEach (application) ->
lines = []
appName = application['name'][0]
lines.push appName
threshold_values = application['threshold-values'][0]['threshold_value']
threshold_values.forEach (threshold_value) ->
lines.push "#{threshold_value['$']['name']}: #{threshold_value['$']['formatted_metric_value']}"
lines.push "https://rpm.newrelic.com/accounts/#{accountId}/applications/#{application['id'][0]['_']}"
msg.send lines.join("\n") if match.toLowerCase() == appName.toLowerCase() || match.toLowerCase() == 'all'