Skip to content

Instantly share code, notes, and snippets.

@Ulrhol
Last active September 2, 2016 08:14
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Ulrhol/5088efcc94de2fecad5e to your computer and use it in GitHub Desktop.
Save Ulrhol/5088efcc94de2fecad5e to your computer and use it in GitHub Desktop.
Graphite support for Dashing

##Yet another gist to provide Graphite support for Dashing Pull data from Graphite and present in Dashing. Support both graph and number widget.

Quick install:

dashing install 5088efcc94de2fecad5e

Set the graphite host/port and names of metrics in jobs/graphite.rb

# Set the graphite host and port (ip or hostname)
GRAPHITE_HOST = '127.0.0.1'
GRAPHITE_PORT = '8080'

# Job mappings. Define a name and set the metrics name from graphite
job_mapping = {
    'host1-load-1min' => '*.*.host1.load.onemin',
    'host2-load-1min' => '*.*.host1.load.onemin'
}

Configure your dashboard to use your data. Example dashboards/sample.erb:

    <li data-row="1" data-col="1" data-sizex="1" data-sizey="1">
        <div data-id="host1-load-1min" data-view="Graph" data-title="Host1 Load 1min" data-moreinfo="Last 4h"></div>
    </ul>
    <li data-row="1" data-col="1" data-sizex="1" data-sizey="1">
        <div data-id="host2-load-1min" data-view="Meter" data-title="Host2 Load 1min" data-min="0" data-max="100" data-moreinfo="Last 4h"></div>
    </ul>

As always, fork and tweak to suit your needs

require 'net/http'
require 'json'
require 'date'
# Pull data from Graphite and make available to Dashing Widgets
# Heavily inspired from Thomas Van Machelen's "Bling dashboard article"
# Set the graphite host and port (ip or hostname)
GRAPHITE_HOST = '127.0.0.1'
GRAPHITE_PORT = '8080'
INTERVAL = '30s'
# Job mappings. Define a name and set the metrics name from graphite
job_mapping = {
'host1-load-1min' => '*.*.host1.load.onemin',
'host2-load-1min' => '*.*.host1.load.onemin'
}
# Extend the float to allow better rounding. Too many digits makes a messy dashboard
class Float
def sigfig_to_s(digits)
f = sprintf("%.#{digits - 1}e", self).to_f
i = f.to_i
(i == f ? i : f)
end
end
class Graphite
# Initialize the class
def initialize(host, port)
@host = host
@port = port
end
# Use Graphite api to query for the stats, parse the returned JSON and return the result
def query(statname, since=nil)
since ||= '1h-ago'
http = Net::HTTP.new(@host, @port)
response = http.request(Net::HTTP::Get.new("/render?format=json&target=#{statname}&from=#{since}"))
result = JSON.parse(response.body, :symbolize_names => true)
return result.first
end
# Gather the datapoints and turn into Dashing graph widget format
def points(name, since=nil)
since ||= '-1min'
stats = query name, since
datapoints = stats[:datapoints]
points = []
count = 1
(datapoints.select { |el| not el[0].nil? }).each do|item|
points << { x: count, y: get_value(item)}
count += 1
end
value = (datapoints.select { |el| not el[0].nil? }).last[0].sigfig_to_s(2)
return points, value
end
def get_value(datapoint)
value = datapoint[0] || 0
return value.round(2)
end
def value(name, since=nil)
since ||= '-10min'
stats = query name, since
last = (stats[:datapoints].select { |el| not el[0].nil? }).last[0].sigfig_to_s(2)
return last
end
end
job_mapping.each do |title, statname|
SCHEDULER.every INTERVAL, :first_in => 0 do
# Create an instance of our Graphite class
q = Graphite.new GRAPHITE_HOST, GRAPHITE_PORT
# Get the current points and value. Timespan is static atm
points, current = q.points "#{statname}", "-1hour"
# Send to dashboard, tested supports for number, meter and graph widgets
send_event "#{title}", { current: current, value: current, points: points }
end
end
@jewzaam
Copy link

jewzaam commented Mar 4, 2015

This was very helpful, thank you! But it doesn't support https without some tweaking. I changed this to use open-uri and refactored to take a URL instead of HOST and PORT.

https://gist.github.com/jewzaam/ef7b47b8b580263082f3

@jewzaam
Copy link

jewzaam commented Mar 4, 2015

Forked and updated. New to gist so didn't noticed I could do that. Don't see any way to request a merge.

@Ulrhol
Copy link
Author

Ulrhol commented Mar 23, 2015

No need to merge. If your works I'm ok to sunset this and leave you to maintain the graphite widget. I'm running my OpenTSDB widget and made the graphite one just for recreational purposes.

@charlesrg
Copy link

Paste a picture please.

@AlexAznar
Copy link

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment