Skip to content

Instantly share code, notes, and snippets.

@fmonserrat
Created October 22, 2014 17:38
Show Gist options
  • Save fmonserrat/7a7c0d957413a464db7b to your computer and use it in GitHub Desktop.
Save fmonserrat/7a7c0d957413a464db7b to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
#Set the environment to maestro
# lists hosts whose chef-client hasn't checked in with the server for a while
# how many seconds before we alert
threshold = 86400
# 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'
require 'pony'
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], item[:domain], item[:ipaddress], "#{delay/3600} hours ago"])
end
if not host_list.empty?
File.new("MissingNodes.txt", "w+")
File.open('MissingNodes.txt', 'w') do |f|
f.write("Chef nodes that have not checked in in more than 24 hours\n")
host_list.each do |item|
f.write("#{item}\n")
end
end
end
Pony.mail(
:to => 'systems-notify@wwwh.com',
:from =>'Chef',
:subject => 'Nodes that didnt call home in the last day',
:html_body => 'Chef Nodes that have not checked-in in more than 24 hours',
:attachments => {"MissingNodes.txt" => File.read("MissingNodes.txt")},
:body_part_header => { content_disposition: "inline" }
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment