Skip to content

Instantly share code, notes, and snippets.

@paulchoi
Created November 8, 2010 22:25
Show Gist options
  • Save paulchoi/668386 to your computer and use it in GitHub Desktop.
Save paulchoi/668386 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
# lists hosts whose chef-client hasn't checked in with the server for a while
# how many seconds before we alert
threshold = 3600
# requires that your user account is set up for Chef's "knife" utility
me = ENV["USER"]
if not File.exists?(File.expand_path("~#{me}/.chef/knife.rb"))
puts "Please set up knife first."
exit 1
end
require 'rubygems'
require 'chef/rest'
require 'chef/config'
require 'chef/search/query'
require 'chef/node'
config = Chef::Config.from_file('.chef/knife.rb')
q = Chef::Search::Query.new
time_now = Time.now.to_i
time_threshold = Time.now.to_i - threshold
host_list = Array.new
# use Solr query as much as possible
q.search(:node, "ohai_time:[0 TO #{time_threshold}]") do |item|
delay = Time.now.to_i - item[:ohai_time].to_i
host_list.push([ item[:hostname], delay ])
end
if not host_list.empty?
print "Chef clients that have not checked in in the last ", threshold, "s\n"
print "\n"
host_list.each do |item|
print item[0], ": ", item[1], " s\n"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment