Skip to content

Instantly share code, notes, and snippets.

@jewzaam
Forked from Ulrhol/README.md
Last active April 25, 2016 15:00
Show Gist options
  • Save jewzaam/efffa657c31e244131e3 to your computer and use it in GitHub Desktop.
Save jewzaam/efffa657c31e244131e3 to your computer and use it in GitHub Desktop.

##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 efffa657c31e244131e3

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

# Set the graphite url
GRAPHITE_URL = 'http://127.0.0.1:8080'
#GRAPHITE_URL = 'https://127.0.0.1:8443'

# 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>
    </li>
    <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>
    </li>

As always, fork and tweak to suit your needs

require 'open-uri'
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_URL = 'https://your.graphite.host'
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(url)
@url = url
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'
print "SOURCE: #{@url}/render?format=json&target=#{statname}&from=#{since}\n"
response = URI.parse("#{@url}/render?format=json&target=#{statname}&from=#{since}").read
result = JSON.parse(response, :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_URL
# 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
@Ulrhol
Copy link

Ulrhol commented Mar 23, 2015

Good updates. You should probably update the readme to reflect your changes GRAPHITE_HOST and
GRAPHITE_PORT are gone it seems. Also it's be nice if you could put an sample widget image in there.

@kassi
Copy link

kassi commented Jul 29, 2015

Yep, and fix the example, i.e. change </ul> into </li> - for those who quickly copy the example code.

@charlesrg
Copy link

Please post a picture

@dontrebootme
Copy link

The dashing install hash still points to Ulrhol's gist, and not this one.

Might want to update:

dashing install 5088efcc94de2fecad5e

To

dashing install efffa657c31e244131e3

@contentfree
Copy link

@jewzaam: It would be better if you updated this gist to fix the couple issues above then have us make yet another fork.

@OliPassey
Copy link

Would this work with hostedgraphite ?

@jewzaam
Copy link
Author

jewzaam commented Apr 25, 2016

All, I didn't notice any of your comments. Sorry! Will look at updating this as I'm picking it back up soon.

@jewzaam
Copy link
Author

jewzaam commented Apr 25, 2016

@OliPassey you're asking about https://www.hostedgraphite.com/ ? I haven't used them, but this will only work if the graphite instance doesn't require authentication.

@jewzaam
Copy link
Author

jewzaam commented Apr 25, 2016

Updated README for fixes recommended.

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