Skip to content

Instantly share code, notes, and snippets.

@artbikes
Created July 23, 2012 18:10
Show Gist options
  • Save artbikes/3165091 to your computer and use it in GitHub Desktop.
Save artbikes/3165091 to your computer and use it in GitHub Desktop.
Export Jenkins Data to Graphite
#!/usr/bin/env ruby
require 'rubygems'
require 'rest-client'
require 'json'
require 'optparse'
require 'ostruct'
require 'pp'
class ParseOpts
def self.parse(args)
options = OpenStruct.new
options.graphiteserver = "graphite.lonecat.com"
options.graphiteport = "2003"
options.jenkinsurl = "https://jenkins.lonecat.com"
options.jenkinsuser = "Jenkins"
options.jenkinspassword = "****"
options.prefix = "jenkins"
opts = OptionParser.new do |opts|
opts.banner = "Usage: #{$PROGRAM_NAME} [options]"
opts.on("-s", "--graphiteserver SERVER", "Graphite server address") do |s|
options.graphiteserver = s
end
opts.on("-p", "--graphiteport PORT", "Graphite server port") do |p|
options.graphiteport = p
end
opts.on("-j", "--jenkinsurl URL", "Base url of your jenkins server (ex http://jenkins.example.com") do |j|
options.jenkinsurl = j
end
opts.on("-U", "--jenkinsuser USER", "User to authenticate with for jenkins") do |u|
options.jenkinsuser = u
end
opts.on("-P", "--jenkinspassword PASS", "Password for authenticating with jenkins") do |w|
options.jenkinspassword = w
end
opts.on("-b", "--jobs", "Jobs view to monitor for success/failure") do |b|
options.jobs = true
end
opts.on("", "--prefix", "Graphite metric prefix") do |r|
options.prefix = r
end
end
opts.parse!(args)
options
end
end
class JenkinsServer
def initialize(base_url, user, password)
@base_url = base_url
@user = user
@password = password
end
def display_details()
puts "URL= #{@base_url}"
end
def get_collection(path)
response = RestClient::Request.new(:method => :get,
:url => @base_url + "/" + path.to_s + "/api/json",
:user => @user,
:password => @password,
:headers => { :accept => :json, :content_type => :json }).execute
results = JSON.parse(response.to_str)
end
end
class GraphiteServer
attr_reader :prefix
attr_accessor :data
def initialize(server, port, prefix)
@server = server
@port = port
@prefix = prefix
@data = {}
end
def add_data(key, value)
@token = prefix + "." + key
data[@token] = value
end
def send
time = Time.new
epoch = time.to_i
begin
s = TCPSocket.open(@server, @port)
rescue Errno::ECONNREFUSED => e
puts "Unable to send to Graphite"
end
data.each do |queue, value|
s.write("#{queue} #{value} #{epoch}\n")
end
end
end
options = ParseOpts.parse(ARGV)
jenkins = JenkinsServer.new(options.jenkinsurl, options.jenkinsuser, options.jenkinspassword)
res = jenkins.get_collection('computer')
queue = jenkins.get_collection('queue')
graphite = GraphiteServer.new(options.graphiteserver, options.graphiteport, options.prefix)
graphite.add_data("queue.size", queue["items"].length)
graphite.add_data("executors.total", res["totalExecutors"])
graphite.add_data("executors.busy", res["busyExecutors"])
graphite.add_data("executors.free", res["totalExecutors"] - res["busyExecutors"])
ok, fail, warn, aborted, disabled = []
if options.jobs
jobs = jenkins.get_collection("view/All")
jobs["jobs"].each {|x|
ok << x if x["color"] == "blue"
fail << x if x["color"] == "red"
warn << x if x["color"] == "yellow"
aborted << x if x["color"] == "aborted"
disabled << x if x["color"] == "disabled"
}
graphite.add_data("jobs.total", jobs["jobs"].length)
graphite.add_data("jobs.ok", ok.length)
graphite.add_data("jobs.fail", fail.length)
graphite.add_data("jobs.warn", warn.length)
graphite.add_data("jobs.aborted", aborted.length)
graphite.add_data("jobs.disabled", disabled.length)
end
graphite.send()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment