Skip to content

Instantly share code, notes, and snippets.

@Ulrhol
Last active August 29, 2015 14:05
Show Gist options
  • Save Ulrhol/01ade12ae650a77fc8d5 to your computer and use it in GitHub Desktop.
Save Ulrhol/01ade12ae650a77fc8d5 to your computer and use it in GitHub Desktop.
OpenTSDB support for Dashing (http://dashing.io)

##OpenTSDB Support for Dashing Pull data from OpenTSDB and present in Dashing. Support both graph and number widget. Examples for how to write OpenTSDB Sub Queries can be found at http://opentsdb.net/docs/build/html/api_http/query/index.html

This is the gist for easy installing. For the full project please check: https://github.com/Ulrhol/dashing_opentsdb

##Usage

Install manually by following the instructions below or install from the gist: dashing install 01ade12ae650a77fc8d5

  1. Copy opentsdb.rb to the jobs directory

  2. Make sure the required gems are installed

gem install 'net/http'
gem install 'json'
gem install 'date'
  1. Configure the jobs/opentsdb.rb to pull the required stats:
OPENTSDB_HOST = 'localhost' 
OPENTSDB_PORT = 4242 

job_mapping = {
   'web-server-load'   => 'sum:proc.loadavg.5min{host=web.server.net}',
   'other-server-load' => 'sum:proc.loadavg.5min{host=other.server.net}'
}


If you wish to set a different timespan to chart do it in the SCHEDULER. Use the relative timespan as described in the OpenTSDB docs, <amount><time unit>-ago . Examples: 1h-ago, 4h-ago, 1m-ago, 1w-ago

    # get the current points and value. Timespan is static atm
    points, current = q.points "#{statname}", "1h-ago"
  1. Configure dashboard to use your data. Add to your dashboard file, for instance dashboards/sample.erb
    <li data-row="1" data-col="1" data-sizex="1" data-sizey="1">
        <div data-id="web-server-load" data-view="Graph" data-title="Web Server Load" data-moreinfo="Last 1h"></div>
    </ul>
    <li data-row="1" data-col="1" data-sizex="1" data-sizey="1">
        <div data-id="other-server-load" data-view="Graph" data-title="Other Server Load" data-moreinfo="Last 1h"></div>
    </ul>
require 'net/http'
require 'json'
require 'date'
# Pull data from OpenTSDB and make available to Dashing Widgets
# Tested with v2.0.0 from http://opentsdb.net/
# Set the host and port
OPENTSDB_HOST = '127.0.0.1' # specifiy the base url of your OpenTSDB
OPENTSDB_PORT = 4242 # specify the port of the server here
# Job mappings. Define a name and set the Sub Query
# More examples can be found in OpenTSDB API documentation
# http://opentsdb.net/docs/build/html/api_http/query/index.html
job_mapping = {
'proc-stat-idle-foo' => 'sum:rate:proc.stat.cpu{host=foo,type=idle}',
'proc-stat-wait-bar' => 'sum:rate:proc.stat.cpu{host=bar,type=wait}'
}
# Too many decimals makes messy widgets
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 OpenTSDB
# Initialize the class
def initialize(host, port)
@host = host
@port = port
end
# Use OpenTSDB 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("/api/query?start=#{since}&m=#{statname}"))
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)
stats = query name, since
dps = stats[:dps]
points = []
last = 0
count = 1
(dps.select { |el| not el.nil? }).each do|item|
points << { x: count, y: item[1].sigfig_to_s(2) }
count += 1
last = item[1].sigfig_to_s(2)
end
return points, last
end
end
job_mapping.each do |title, statname|
SCHEDULER.every '30s', :first_in => 0 do
# Create an instance of our opentsdb class
q = OpenTSDB.new OPENTSDB_HOST, OPENTSDB_PORT
# get the current points and value. Timespan is static atm
points, current = q.points "#{statname}", "1h-ago"
# send to dashboard, so the number the meter and the graph widget can understand it
send_event "#{title}", { current: current, value: current, points: points }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment