-
-
Save apsoto/668421 to your computer and use it in GitHub Desktop.
Script that notifies what chef nodes have not 'checked in' within a certain time period
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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