Skip to content

Commit

Permalink
Merge pull request #4 from stephenyeargin/feature/add-panel-selection…
Browse files Browse the repository at this point in the history
…-to-attribute

Add panel selection and 'to' attribute
  • Loading branch information
stephenyeargin committed Jun 24, 2015
2 parents ab41d97 + ee1a821 commit e3a6a24
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 15 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

Query Grafana dashboards.

**Note:** This package requires Grafana 2.x or higher.

## Installation

In hubot project repo, run:
Expand Down Expand Up @@ -33,6 +35,14 @@ export HUBOT_GRAFANA_API_KEY=abcd01234deadbeef01234
## Sample Interaction

```
user1>> hubot grafana dashboard graphite-carbon-metrics
user1>> hubot graf db graphite-carbon-metrics
hubot>> Graphite Carbon Metrics: http://play.grafana.org/render/dashboard/solo/graphite-carbon-metrics/?panelId=1&width=1000&height=500&from=now-6h - http://play.grafana.org/dashboard/db/graphite-carbon-metrics/?panelId=1&fullscreen&from=now-6h
```

## All Commands

- `hubot graf db graphite-carbon-metrics` - Get all panels in the dashboard
- `hubot graf db graphite-carbon-metrics:3` - Get only the third panel of a particular dashboard
- `hubot graf db graphite-carbon-metrics now-12hr` - Get a dashboard with a window of 12 hours ago to now
- `hubot graf db graphite-carbon-metrics now-24hr now-12hr` - Get a dashboard with a window of 24 hours ago to 12 hours ago
- `hubot graf db graphite-carbon-metrics:3 now-8d now-1d` - Get only the third panel of a particular dashboard with a window of 8 days ago to yesterday
73 changes: 60 additions & 13 deletions src/grafana.coffee
Original file line number Diff line number Diff line change
@@ -1,42 +1,89 @@
# Description:
# Query Grafana dashboards
#
# Examples:
# - `hubot graf db graphite-carbon-metrics` - Get all panels in the dashboard
# - `hubot graf db graphite-carbon-metrics:3` - Get only the third panel of a particular dashboard
# - `hubot graf db graphite-carbon-metrics now-12hr` - Get a dashboard with a window of 12 hours ago to now
# - `hubot graf db graphite-carbon-metrics now-24hr now-12hr` - Get a dashboard with a window of 24 hours ago to 12 hours ago
# - `hubot graf db graphite-carbon-metrics:3 now-8d now-1d` - Get only the third panel of a particular dashboard with a window of 8 days ago to yesterday
#
# Configuration:
# HUBOT_GRAFANA_HOST - Host for your Grafana 2.0 install, e.g. 'http://play.grafana.org'
# HUBOT_GRAFANA_API_KEY - API key for a particular user
#
# Commands:
# hubot graph <dashboard slug>[ <from clause>] - obtains all panels in a given dashboard, with optional time clause
# hubot graph db <dashboard slug>[:<panel id>][ <from clause>][ <to clause>]
#

module.exports = (robot) ->

grafana_host = process.env.HUBOT_GRAFANA_HOST
grafana_api_key = process.env.HUBOT_GRAFANA_API_KEY

robot.respond /(?:grafana|graph) (?:dash|dashboard|db) (.*)(| .*)/i, (msg) ->
robot.respond /(?:grafana|graph|graf) (?:dash|dashboard|db) ([A-Za-z0-9\-\:_]+)(| [A-Za-z0-9\-\+]+)?(| [A-Za-z0-9\-\+]+)?/i, (msg) ->

# Get the dashboard slug
slug = msg.match[1]
slug = msg.match[1].trim()
from = msg.match[2] || 'now-6h'
to = msg.match[3] || 'now'
pid = false

# Parse out a specific panel
if /\:/.test slug
parts = slug.split(':')
slug = parts[0]
pid = parseInt parts[1], 10

robot.logger.debug msg.match
robot.logger.debug slug
robot.logger.debug from
robot.logger.debug to
robot.logger.debug pid

# Call the API to get information about this dashboard
getDashboardInformation slug, (dashboard) ->
robot.logger.debug dashboard

# Check dashboard information
if !dashboard
return msg.send "An error ocurred. Check your logs for more details."
return sendError "An error ocurred. Check your logs for more details.", msg
if dashboard.message
return msg.send dashboard.message
return sendError dashboard.message, msg

# Support for templated dashboards
robot.logger.debug dashboard.model.templating.list
if dashboard.model.templating.list
template_map = []
for template in dashboard.model.templating.list
template_map['$' + template.name] = template.current.text

# Return dashboard rows
for row in dashboard.model.rows
for panel in row.panels
imageUrl = "#{grafana_host}/render/dashboard/solo/db/#{slug}/?panelId=#{panel.id}&width=1000&height=500"
link = "#{grafana_host}/dashboard/db/#{slug}/?panelId=#{panel.id}&fullscreen"
if from
imageUrl += "&from=#{from}"
link += "&from=#{from}"
robot.logger.debug panel
msg.send "#{panel.title}: #{imageUrl} - #{link}"

# Skip if panel ID was specified and didn't match
if pid && pid != panel.id
continue

# Clean up attributes
from = from.trim()
to = to.trim()

imageUrl = "#{grafana_host}/render/dashboard/solo/db/#{slug}/?panelId=#{panel.id}&width=1000&height=500&from=#{from}&to=#{to}"
link = "#{grafana_host}/dashboard/db/#{slug}/?panelId=#{panel.id}&fullscreen&from=#{from}&to=#{to}"
msg.send "#{formatTitleWithTemplate(panel.title, template_map)}: #{imageUrl} - #{link}"

sendError = (message, msg) ->
robot.logger.error message
msg.send message

formatTitleWithTemplate = (title, template_map) ->
title.replace /\$\w+/g, (match) ->
if template_map[match]
return template_map[match]
else
return match

getDashboardInformation = (slug, callback) ->
if grafana_api_key
Expand All @@ -50,7 +97,7 @@ module.exports = (robot) ->
}
robot.http("#{grafana_host}/api/dashboards/db/#{slug}").headers(authHeader).get() (err, res, body) ->
if (err)
robot.logger.warning err
robot.logger.error err
return callback(false)
dashboard = JSON.parse(body)
return callback(dashboard)
2 changes: 1 addition & 1 deletion test/grafana-test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ describe 'grafana', ->
require('../src/grafana')(@robot)

it 'registers a dashboard listener', ->
expect(@robot.respond).to.have.been.calledWith(/(?:grafana|graph) (?:dash|dashboard|db) (.*)(| .*)/i)
expect(@robot.respond).to.have.been.calledWith(/(?:grafana|graph|graf) (?:dash|dashboard|db) ([A-Za-z0-9\-\:_]+)(| [A-Za-z0-9\-\+]+)?(| [A-Za-z0-9\-\+]+)?/i)

0 comments on commit e3a6a24

Please sign in to comment.