Skip to content

Instantly share code, notes, and snippets.

@dalehamel
Last active August 29, 2015 14:13
Show Gist options
  • Save dalehamel/629f41bdcb8c0627f985 to your computer and use it in GitHub Desktop.
Save dalehamel/629f41bdcb8c0627f985 to your computer and use it in GitHub Desktop.
Hubot Datadog WIP
# Description:
# Query Datadog using Hubot.
#
# Configuration:
# HUBOT_DATADOG_APIKEY - Your Datadog API key
#  HUBOT_DATADOG_APPKEY - Your Datadog app Key
#
# Commands:
# opsbot (datadog|dd|dog|graph) [dashboard] [graph] - snapshot a specific graph from a dashboard that you've already told hubot about.
# opsbot (datadog|dd|dog) graphs - Show the available graphs
# opsbot (datadog|dd|dog) graph me <amount><unit> <metric query> - Queries for a graph snapshot
# opsbot (datadog|dd|dog) metric search <metric query> - Queries for a list of matching metrics
#
# Author
# tombell
#
# Notes:
# * Built using https://www.npmjs.com/package/dogapi
# * Forked from https://github.com/zestia/hubot-datadog
dog = require 'dogapi'
moment = require 'moment'
# Add graphs here, grouped by dashboard. You must include the dashboard ID.
# The keys here will be what you use to query for the graph,
# The title must be the title of the graph from the datadog dashboard.
graphs =
"nginx"
dashboard: DASHBOARD_ID_FROM_URL_SLUG # adme
graphs:
"requests":
title: "Current # of requests"
"traffic":
title: "Traffic by Nginx LB In/Out"
"connections":
title: "Connections Per Server"
"load":
title: "Avg Load by %"
"200":
title: "RPM 2xx Responses by host"
"300":
title: "RPM 3xx Responses by host"
"400":
title: "RPM 4xx Responses by host"
"500":
title: "RPM 5xx Responses by host"
module.exports = (robot) ->
unless process.env.HUBOT_DATADOG_APIKEY?
return robot.logger.error "HUBOT_DATADOG_APIKEY env var is not set"
unless process.env.HUBOT_DATADOG_APPKEY?
return robot.logger.error "HUBOT_DATADOG_APPKEY env var is not set"
client = new dog {
api_key: process.env.HUBOT_DATADOG_APIKEY
app_key: process.env.HUBOT_DATADOG_APPKEY
}
robot.respond /(dd|datadog|dog)+\s+graphs/i, (msg) ->
message = "I know about these graphs:\n"
for dash,obj of graphs
for graph,data of obj['graphs']
message += "#{dash} #{graph} (#{data['title']})\n"
message += 'You can use "opsbot dd [dashboard] [graph]" to view them.'
msg.send message
robot.respond /(dd|datadog|dog|graph)+\s+(\S+)\s+(\S+)/i, (msg) ->
dash = msg.match[2]
graph = msg.match[3]
return msg.send "Sorry, no one told me how about the #{dash} dashboard yet :(" unless dash of graphs
return msg.send "Sorry, no one told me how to get the graph for #{graph} yet :(" unless graph of graphs[dash]['graphs']
title = graphs[dash]['graphs'][graph]['title']
dashboard = graphs[dash]['dashboard']
client.get_dashboard dashboard, (err, result, status) ->
return msg.send "Could not get the graph dashboard #{err}" if err?
definitions = ( definition for definition in result['dash']['graphs'] when definition['title'] == title )
return msg.send "Uh oh, I couldn't find a graph for #{title} on dashboard #{dashboard}, maybe it's been renamed?" unless definitions.length > 0
graph_def = JSON.stringify definitions[0]["definition"]
now = moment()
end = now.unix()
start = now.subtract(1,'h').unix()
snapshot = {
graph_def: graph_def
start: start
end: end
}
# datadog posts back the URL before it's actually ready.
# We must poll befor putting it into the chat.
client.add_snapshot_from_def snapshot, (err, result, status) ->
check_status = (url) ->
client.snapshot_status result['snapshot_url'], (err,result,status) ->
robot.logger.debug JSON.stringify result
check_status url unless result['status_code'] == 200
if result['status_code'] == 200
end = moment()
elapsed = end - start
msg.send "Here's your graph for #{dash} #{graph}, #{msg.message.user.name}: #{url} (took #{elapsed/1000}s)"
start = moment()
check_status result['snapshot_url']
robot.respond /(dd|datadog|dog)+\s+graph(\s*me)?\s+(\d+)([smhdwMy])\s+(.*)/i, (msg) ->
time = msg.match[2]
unit = msg.match[3]
metric = msg.match[4]
now = moment()
end = now.unix()
start = now.subtract(unit, time).unix()
snapshot = {
metric_query: metric
start: start
end: end
}
client.add_snapshot snapshot, (err, result, status) ->
return msg.send "Could not generate the graph snapshot: #{err}" if err?
setTimeout ->
msg.send result['snapshot_url']
, 3000
robot.respond /(dd|datadog|dog)+\s+metric(s)?\s+search\s+(.*)/i, (msg) ->
metric = msg.match[2]
client.search metric, (err, result, status) ->
msg.send "Could not fetch search results: #{err}" if err?
metrics = result['results']['metrics']
msg.send "I found the following results:", metrics.join("\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment