Skip to content

Instantly share code, notes, and snippets.

@jbz
Forked from coderanger/chef.rb
Created February 1, 2012 19:25
Show Gist options
  • Save jbz/1718762 to your computer and use it in GitHub Desktop.
Save jbz/1718762 to your computer and use it in GitHub Desktop.
include_recipe "collectd"
class ChefCollectdPluginReportHandler < Chef::Handler
def report
Chef::Log.info('Running collectd hander')
node[:last_success_time] = Time.now.to_f
node[:error_count_since_success] = 0
node.save
end
end
class ChefCollectdPluginErrorHandler < Chef::Handler
def report
node[:last_error_time] = Time.now.to_f
if not node.include? :error_count
node[:error_count] = 0
end
node[:error_count] += 1
if not node.include? :error_count_since_success
node[:error_count_since_success] = 0
end
node[:error_count_since_success] += 1
node.save
end
end
# Remove any previous instances so we don't end up with more than one
# This cannot use is_a? since the class gets recompiled on each run
#Chef::Config.report_handlers.delete_if {|v| v.class.to_s.include? 'ChefCollectdPluginReportHandler'}
Chef::Config.report_handlers.delete_if {|v| v.is_a? ChefCollectdPluginReportHandler}
Chef::Config.report_handlers << ChefCollectdPluginReportHandler.new
Chef::Config.exception_handlers.delete_if {|v| v.class.to_s.include? 'ChefCollectdPluginErrorHandler'}
Chef::Config.exception_handlers << ChefCollectdPluginErrorHandler.new
[Wed, 24 Nov 2010 19:02:29 -0800] INFO: Daemonizing..
[Wed, 24 Nov 2010 19:02:29 -0800] INFO: Forked, in 30315. Priveleges: 0 0
[Wed, 24 Nov 2010 19:02:34 -0800] INFO: Starting Chef Run (Version 0.9.8)
[Wed, 24 Nov 2010 19:02:34 -0800] INFO: Storing updated cookbooks/collectd_plugins/recipes/chef.rb in the cache.
[Wed, 24 Nov 2010 19:02:36 -0800] INFO: Ran execute[apt-get update] successfully
[Wed, 24 Nov 2010 19:02:37 -0800] INFO: Chef Run complete in 3.091286 seconds
[Wed, 24 Nov 2010 19:02:37 -0800] INFO: Running report handlers
[Wed, 24 Nov 2010 19:02:37 -0800] INFO: Running collectd hander
[Wed, 24 Nov 2010 19:02:37 -0800] INFO: Report handlers complete
[Wed, 24 Nov 2010 19:03:43 -0800] INFO: Starting Chef Run (Version 0.9.8)
[Wed, 24 Nov 2010 19:03:45 -0800] INFO: Ran execute[apt-get update] successfully
[Wed, 24 Nov 2010 19:03:46 -0800] INFO: Chef Run complete in 3.656005 seconds
[Wed, 24 Nov 2010 19:03:46 -0800] INFO: Running report handlers
[Wed, 24 Nov 2010 19:03:46 -0800] INFO: Running collectd hander
[Wed, 24 Nov 2010 19:03:47 -0800] INFO: Running collectd hander
[Wed, 24 Nov 2010 19:03:47 -0800] INFO: Report handlers complete
[Wed, 24 Nov 2010 19:05:01 -0800] INFO: Starting Chef Run (Version 0.9.8)
[Wed, 24 Nov 2010 19:05:03 -0800] INFO: Ran execute[apt-get update] successfully
[Wed, 24 Nov 2010 19:05:04 -0800] INFO: Chef Run complete in 3.080184 seconds
[Wed, 24 Nov 2010 19:05:04 -0800] INFO: Running report handlers
[Wed, 24 Nov 2010 19:05:04 -0800] INFO: Running collectd hander
[Wed, 24 Nov 2010 19:05:05 -0800] INFO: Running collectd hander
[Wed, 24 Nov 2010 19:05:05 -0800] INFO: Running collectd hander
[Wed, 24 Nov 2010 19:05:05 -0800] INFO: Report handlers complete
[Wed, 24 Nov 2010 19:06:14 -0800] INFO: Starting Chef Run (Version 0.9.8)
[Wed, 24 Nov 2010 19:06:15 -0800] INFO: Ran execute[apt-get update] successfully
[Wed, 24 Nov 2010 19:06:17 -0800] INFO: Chef Run complete in 3.256612 seconds
[Wed, 24 Nov 2010 19:06:17 -0800] INFO: Running report handlers
[Wed, 24 Nov 2010 19:06:17 -0800] INFO: Running collectd hander
[Wed, 24 Nov 2010 19:06:17 -0800] INFO: Running collectd hander
[Wed, 24 Nov 2010 19:06:17 -0800] INFO: Running collectd hander
[Wed, 24 Nov 2010 19:06:17 -0800] INFO: Running collectd hander
[Wed, 24 Nov 2010 19:06:18 -0800] INFO: Report handlers complete
[Wed, 24 Nov 2010 19:07:39 -0800] INFO: Starting Chef Run (Version 0.9.8)
[Wed, 24 Nov 2010 19:07:41 -0800] INFO: Ran execute[apt-get update] successfully
[Wed, 24 Nov 2010 19:07:50 -0800] INFO: Chef Run complete in 10.38472 seconds
[Wed, 24 Nov 2010 19:07:50 -0800] INFO: Running report handlers
[Wed, 24 Nov 2010 19:07:50 -0800] INFO: Running collectd hander
[Wed, 24 Nov 2010 19:07:50 -0800] INFO: Running collectd hander
[Wed, 24 Nov 2010 19:07:50 -0800] INFO: Running collectd hander
[Wed, 24 Nov 2010 19:07:51 -0800] INFO: Running collectd hander
[Wed, 24 Nov 2010 19:07:51 -0800] INFO: Running collectd hander
[Wed, 24 Nov 2010 19:07:51 -0800] INFO: Report handlers complete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment