Skip to content

Instantly share code, notes, and snippets.

@brokenthumbs
Created January 14, 2014 22:36
Show Gist options
  • Save brokenthumbs/8427332 to your computer and use it in GitHub Desktop.
Save brokenthumbs/8427332 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
require 'rubygems'
require 'typhoeus'
require 'json'
jenkins = 'buildmaster.jenkins.com'
user = "jenkins"
process = "ruby"
logfile = "/tmp/zombie_killer.log"
servers = {
"buildslave01.jenkins.com" => "Alpha",
"buildslave02.jenkins.com" => "Bravo",
"buildslave03.jenkins.com" => "Charlie",
"buildslave04.jenkins.com" => "Delta",
}
servers.each do |server, name|
output = "[#{Time.new}] : #{server}"
pid_results = `ssh -T -o StrictHostKeyChecking=no #{user}@#{server} "ps -C #{process} -o pid=" 2>/dev/null`
if !pid_results.empty?
res = Typhoeus::Request.get("#{jenkins}/computer/#{name}/api/json?depth=2", :userpwd => "user:password")
body = JSON.parse(res.body)
executing = false
executors = body["executors"]
executors.each do |executor|
executing = true if !executor["currentExecutable"].nil?
end
if !executing
pids = pid_results.split("\n")
pids.each do |pid|
output += " : #{pid.strip} being killed"
`ssh -tt -o StrictHostKeyChecking=no #{user}@#{server} "kill -9 #{pid.strip}" 2>/dev/null`
end
else
output += " : cannot kill #{process} processes : executor #{name} is busy"
end
else
output += " : No #{process} processes being executed"
end
output += "\n"
File.open(logfile, 'a') {|f| f.write(output) }
end
File.open(logfile, 'a') {|f| f.write("\n") }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment