Skip to content

Instantly share code, notes, and snippets.

@sixty4k
Created September 16, 2015 21:09
Show Gist options
  • Save sixty4k/da0ebdb5f7f781537058 to your computer and use it in GitHub Desktop.
Save sixty4k/da0ebdb5f7f781537058 to your computer and use it in GitHub Desktop.
datadogstatsd-ruby issue
#!/usr/bin/ruby
# What do we need!
require 'rubygems'
# ruby 1.8 (what's on the nagios server) needs you to pull in rubygems explicitly, before pulling others in
require 'statsd'
# When do we need it?
#
# metric_base => status page url
status_pages = {
"default_batch_queue" => {
"metric_path" => "project.admin.background_tasks",
"url" => "http://online-app1-rr.internal.prod1.core/status.php?queues=default",
"tags" => ["batch_queue:default", "project", "cos:background", "role:batch", "host:batch-app1-1.internal.core"]
},
"transcodes_batch_queue" => {
"metric_path" => "project.admin.background_tasks",
"url" => "http://online-app1-rr.internal.prod1.core/status.php?queues=transcodes",
"tags" => ["batch_queue:transcodes", "project", "cos:background", "role:batch", "host:batch-app1-1.internal.core"]
},
"4core_batch_queue" => {
"metric_path" => "project.admin.background_tasks",
"url" => "http://online-app1-rr.internal.core/admin/scripts/monitoring/status.php?queues=4core",
"tags" => ["batch_queue:4core", "project", "cos:background", "role:batch" , "host:batch-app1-1.internal.core"]
}
}
def main(status_pages)
lockfile('/tmp/dd-poller.lock')
status_pages.each do |status, pairs|
data = scrape_url(pairs["url"])
clean_data = clean_and_sanitize(data)
populate_statsd(pairs["metric_path"],clean_data,pairs["tags"])
end
unlockfile('/tmp/dd-poller.lock')
end
def scrape_url(url)
final = {}
curlout = %x(curl -s "#{url}").downcase
out = curlout.split("\n").grep(/^([a-z].*: .*?)$/)
final = Hash[out.map {|o| o.split ': '}]
return final
end
def clean_and_sanitize(dirtydata)
cleaned = {}
dirtydata.each_pair do |k,v|
case v
when /^ok$/
cleaned[k] = 1
when /^error$/
cleaned[k] = 0
when /online/
cleaned[k] = 1
when /offline/
cleaned[k] = 0
when /[a-z]/
next
when /\d+-\d.-\d. \d.:\d.:\d./
next
when nil
next
else
cleaned[k] = v
end
end
return cleaned
end
def lockfile(lock)
abort("lock file #{lock} found!") if File.exist?(lock)
File.open("#{lock}", "w") {}
end
def unlockfile(lock)
File.delete(lock)
end
def populate_statsd(base,data,tags)
statsd = Statsd.new
data.each_pair do |k,v|
#puts "#{base}.#{k} : #{v} :tags => #{tags.inspect}"
statsd.gauge("#{base}.#{k}", "#{v}", :tags => ["#{tags.inspect}"])
end
end
main(status_pages)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment