Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add monit chef handler #15

Open
wants to merge 4 commits into from

2 participants

@xdissent

This PR adds a chef handler for monit that automatically unmonitors all services during a chef-client run, remonitoring them afterward. It was created to prevent monit from restarting, alerting, and just generally freaking out when chef manipulates service resources.

The handler has no effect if http isn't enabled in monitrc or localhost isn't allowed, since that's how the monit command line client communicates with the service. Install the handler by including the monit::handler recipe. Similarly to #14, there are a few assumptions regarding paths that may be platform-specific to Ubuntu.

Bonus: If a monit config for chef-client is found, the chef-client service is never unmonitored. That way, monit will still restart chef-client if it happens to bail mid-run.

@apsoto
Owner

Is this an automatic thing or do users get to opt-in if they want to use it?

@xdissent

Strictly opt-in. If you don't include the monit::handler recipe, there is no change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 67 additions and 0 deletions.
  1. +29 −0 files/default/monit_handler.rb
  2. +1 −0  metadata.rb
  3. +37 −0 recipes/handler.rb
View
29 files/default/monit_handler.rb
@@ -0,0 +1,29 @@
+require "rubygems"
+require "chef"
+require "chef/handler"
+
+class MonitHandler < Chef::Handler
+
+ def initialize(opts = {})
+ @poll_period = opts[:poll_period] || 1
+ end
+
+ def success
+ Chef::Log.info "Monit monitor all succeeded"
+ end
+
+ def failure
+ Chef::Log.warn "Monit monitor all failed"
+ end
+
+ def report
+ if system("ps -e | grep `cat /var/run/monit.pid`")
+ return success if system("monit monitor all")
+ failure
+ Chef::Log.warn "Waiting #{@poll_period} seconds before trying again..."
+ return success if system("sleep #{@poll_period} && monit monitor all")
+ failure
+ end
+ end
+
+end
View
1  metadata.rb
@@ -5,6 +5,7 @@
long_description IO.read(File.join(File.dirname(__FILE__), 'README.rdoc'))
version "0.7"
+depends "chef_handler"
attribute 'monit/notify_email',
:description => 'The email address to send alerts to.',
View
37 recipes/handler.rb
@@ -0,0 +1,37 @@
+# Unmonitor services that might be manipulated by the chef run
+e = execute "monit unmonitor all" do
+ action :nothing
+ only_if "ps -e | grep -q `cat /var/run/monit.pid`"
+ ignore_failure true
+end
+e.run_action(:run)
+
+# Monitor chef-client in case it bails mid-run
+e = execute "monit monitor chef-client" do
+ action :nothing
+ only_if "ps -e | grep -q `cat /var/run/monit.pid` && monit summary | grep -q \"Process 'chef-client'\""
+ ignore_failure true
+end
+e.run_action(:run)
+
+# Install monit chef handler
+include_recipe "chef_handler"
+
+directory node["chef_handler"]["handler_path"] do
+ owner "root"
+ group "root"
+ mode "0755"
+end
+
+handler_path = ::File.join(node["chef_handler"]["handler_path"], "monit_handler.rb")
+cookbook_file handler_path do
+ owner "root"
+ group "root"
+ mode "0644"
+end
+
+chef_handler "MonitHandler" do
+ source handler_path
+ action :enable
+ arguments :poll_period => node["monit"]["poll_period"]
+end
Something went wrong with that request. Please try again.