Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add monit chef handler #15

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 29 additions & 0 deletions 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
1 change: 1 addition & 0 deletions metadata.rb
Expand Up @@ -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.',
Expand Down
37 changes: 37 additions & 0 deletions 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