Skip to content

Instantly share code, notes, and snippets.

@rayterrill
Created June 22, 2016 16:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rayterrill/2bf2bb7de4ca60ae4ceb349649c0d37f to your computer and use it in GitHub Desktop.
Save rayterrill/2bf2bb7de4ca60ae4ceb349649c0d37f to your computer and use it in GitHub Desktop.
#BACKSTORY:
#I wanted to get a way to quickly and centrally see how my Chef runs might be going.
#SUMMARY:
#1. I built a quick web service in PHP (details not pertinent here), that takes parameters node, status, and runlist, and saves the results to a database for reporting purposes
#2. I configured Chef to call my PHP web service on each successful and failed chef-client run using Chef.event_handler in my default.rb
#created a helper.rb at ~/POP/cookbooks/MyCookbook/libraries/helper.rb
######################################################################
require 'net/http'
require 'uri'
module MyHandlers
class Helper
def log_run(node_name,run_status,run_list)
url = 'https://mywebserver.mydomain.com/Chef/chefCheckin.php?node=' + node_name + '&status=' + run_status + '&runlist=' + run_list
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)
end
end
end
#modify my cookbook's default.rb to include my new event handler
################################################################
Chef.event_handler do
on :run_completed do
MyHandlers::Helper.new.log_run(Chef.run_context.node.name, '0', Array(Chef.run_context.node.run_list).join(','))
end
on :run_failed do
MyHandlers::Helper.new.log_run(Chef.run_context.node.name, '1', Array(Chef.run_context.node.run_list).join(','))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment