Skip to content

Instantly share code, notes, and snippets.

@blovett
Created May 20, 2012 18:57
a graphite node finder.
#!/usr/bin/env ruby
require 'open-uri'
require 'optparse'
require 'json'
@options = {}
opts = OptionParser.new
opts.on("--node S", String, "Node name") do |n|
@options[:node] = n
end
opts.on("--environment S", String, "Environment to search") do |n|
@options[:environment] = n
end
opts.on("--debug", "Turn on debugging") do |n|
@options[:debug] = true
end
opts.parse!
[:node].each {|v| raise "#{v} should be supplied" unless @options[v] }
@graphite_url = ENV['GRAPHITE_URL'] || "http://graphite.example.com"
def get_graphite_data(graphite_base, search_path)
url = graphite_base + "/metrics/find/?query=#{search_path}&format=treejson"
tmpdata = []
open(url) do |f|
f.each_line do |line|
tmpdata << line
end
end
if tmpdata.length > 1
tmpdata
else
tmpdata[0]
end
end
def debug(string)
if @options[:debug]
puts "[debug #{Time.now}] #{string}"
end
end
def main()
region_paths = []
environments = []
if not @options[:environment]
tmp = get_graphite_data(@graphite_url, "servers.*")
JSON.parse(tmp).each {|j| environments << j["id"]}
else
environments << "servers." + @options[:environment]
end
debug "environments is #{environments.class}"
environments.each do |env|
tmp = get_graphite_data(@graphite_url, "#{env}.*")
debug "tmp for environment #{env} is of type #{tmp.class}"
JSON.parse(tmp).each {|j| region_paths << j["id"]}
end
# now we search all region paths
nodes = []
region_paths.each do |region|
tmp = get_graphite_data(@graphite_url, "#{region}.*")
JSON.parse(tmp).each do |j|
if j["text"] == @options[:node]
nodes << j["id"]
end
end
end
if not nodes.empty?
nodes.each {|node| puts "Found at #{node}" }
else
puts "We didn't find #{@options[:node]} anywhere.. Sorry!"
end
end
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment