Skip to content
Snippets Groups Projects
Commit f68280a1 authored by Marin Jankovski's avatar Marin Jankovski
Browse files

Add monit cookbook.

parent 80a18528
No related branches found
No related tags found
No related merge requests found
The MIT License (MIT)
Copyright © 2011-2013 Phil Cohen <github@phlippers.net>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
# chef-monit [![Build Status](https://secure.travis-ci.org/phlipper/chef-monit.png)](http://travis-ci.org/phlipper/chef-monit) [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/phlipper/chef-monit)
## Description
Installs the `monit` package from (http://mmonit.com/monit/).
## Requirements
### Supported Platforms
The following platforms are supported by this cookbook, meaning that the recipes run on these platforms without error:
* Ubuntu
* Debian
* RedHat
* CentOS
* Scientific
* Fedora
* SUSE
* Amazon
## Recipes
* `monit` - The default recipe. Sets up the service definition and default checks.
## Resources
### `monit_monitrc`
The following will create a monitrc configuration:
```ruby
monit_monitrc "ssh" do
variables({ category: "system" })
end
```
The `name` parameter must match a file located in your templates directory. In the example above, this would be `ssh.monitrc.erb`.
The `variables` option within the block is optional, and can contain a list of key-value pairs to assign within your template.
## Usage
This cookbook installs the monit components if not present, and pulls updates if they are installed on the system.
## Attributes
```ruby
# Delay the start of polling when the service is started
default["monit"]["start_delay"] = 0
# How frequently the monit daemon polls for changes.
default["monit"]["polling_frequency"] = 20
# Use syslog for logging instead of a logfile.
default["monit"]["use_syslog"] = true
# If not using syslog, the log file that monit will use.
default["monit"]["logfile"] = "/var/log/monit.log"
# Enable emails for internal monit alerts
default["monit"]["mail_alerts"] = true
# Email address that will be notified of events.
default["monit"]["alert_email"] = "root@localhost"
# Enable the web interface and define credentials.
default["monit"]["web_interface"] = {
:enable => true,
:port => 2812,
:address => "localhost",
:allow => ["localhost", "admin:b1gbr0th3r"]
}
# Email settings that will be used for notification of events.
default["monit"]["mail"] = {
:hostname => "localhost",
:port => 25,
:username => nil,
:password => nil,
:from => "monit@$HOST",
:subject => "$SERVICE $EVENT at $DATE",
:message => "Monit $ACTION $SERVICE at $DATE on $HOST,\n\n$DESCRIPTION\n\nDutifully,\nMonit",
:security => nil, # 'SSLV2'|'SSLV3'|'TLSV1'
:timeout => 30
}
case node["platform_family"]
when "rhel", "fedora", "suse"
default["monit"]["main_config_path"] = "/etc/monit.conf"
default["monit"]["includes_dir"] = "/etc/monit.d"
else
default["monit"]["main_config_path"] = "/etc/monit/monitrc"
default["monit"]["includes_dir"] = "/etc/monit/conf.d"
end
# The monit::default recipe will load these monit_monitrc resources automatically
# NOTE setting this attribute at the default level will append values to the array
default["monit"]["default_monitrc_configs"] = ["load", "ssh"]
```
## Contributors
Many thanks go to the following [contributors](https://github.com/phlipper/chef-monit/graphs/contributors) who have helped to make this cookbook even better:
* **[@werdan](https://github.com/werdan)**
* add support for redhat-flavored systems
* **[@auser](https://github.com/auser)**
* add missing metadata
* **[@arrowcircle](https://github.com/arrowcircle)**
* update syntax to be Ruby 1.8-compatible
* **[@dwradcliffe](https://github.com/dwradcliffe)**
* typo fix for README
* **[@tjwallace](https://github.com/tjwallace)**
* load default monitrc configs from an attribute
* **[@tomdz](https://github.com/tomdz)**
* Ruby 1.8.x compatibility fix
* add support for templates in different cookbooks
* **[@alexism](https://github.com/alexism)**
* support other security protocols
* **[@fixlr](https://github.com/fixlr)**
* add `mail_alerts` attribute
* **[@darron](https://github.com/darron)**
* add descriptions to email notifications
* **[@claco](https://github.com/claco)**
* add startup delay option to monit daemon config
* restart on default monitrc configs change
* restart monit service if the monit config changes
* fix platform family logic
## Contributing
1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Added some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request
## License
**chef-monit**
* Freely distributable and licensed under the [MIT license](http://phlipper.mit-license.org/2011-2013/license.html).
* Copyright (c) 2011-2013 Phil Cohen (github@phlippers.net) [![endorse](http://api.coderwall.com/phlipper/endorsecount.png)](http://coderwall.com/phlipper)
* http://phlippers.net/
#!/usr/bin/env rake
task :default => :foodcritic
desc "Runs foodcritic linter"
task :foodcritic do
if RUBY_VERSION >= "1.9"
Rake::Task[:prepare_sandbox].execute
sh "bundle exec foodcritic -f any #{sandbox_path}"
end
end
desc "Runs knife cookbook test"
task :knife do
Rake::Task[:prepare_sandbox].execute
sh "bundle exec knife cookbook test cookbook -c test/.chef/knife.rb -o #{sandbox_path}/../"
end
task :prepare_sandbox do
files = %w{*.md *.rb attributes definitions libraries files providers recipes resources templates}
rm_rf sandbox_path
mkdir_p sandbox_path
cp_r Dir.glob("{#{files.join(",")}}"), sandbox_path
end
private
def sandbox_path
File.join(File.dirname(__FILE__), %w[tmp cookbooks cookbook])
end
#
# Cookbook Name:: monit
# Attributes:: default
#
# Author:: Phil Cohen <github@phlippers.net>
#
# Copyright 2011-2012, Phil Cohen
#
# Delay the start of polling when the service is started
default["monit"]["start_delay"] = 0
# How frequently the monit daemon polls for changes.
default["monit"]["polling_frequency"] = 20
# Use syslog for logging instead of a logfile.
default["monit"]["use_syslog"] = true
# If not using syslog, the log file that monit will use.
default["monit"]["logfile"] = "/var/log/monit.log"
# Enable emails for internal monit alerts
default["monit"]["mail_alerts"] = true
# Email address that will be notified of events.
default["monit"]["alert_email"] = "root@localhost"
# Enable the web interface and define credentials.
default["monit"]["web_interface"] = {
:enable => true,
:port => 2812,
:address => "localhost",
:allow => ["localhost", "admin:b1gbr0th3r"]
}
# Email settings that will be used for notification of events.
default["monit"]["mail"] = {
:hostname => "localhost",
:port => 25,
:username => nil,
:password => nil,
:from => "monit@$HOST",
:subject => "$SERVICE $EVENT at $DATE",
:message => "Monit $ACTION $SERVICE at $DATE on $HOST,\n\n$DESCRIPTION\n\nDutifully,\nMonit",
:security => nil, # 'SSLV2'|'SSLV3'|'TLSV1'
:timeout => 30
}
case node["platform_family"]
when "rhel", "fedora", "suse"
default["monit"]["main_config_path"] = "/etc/monit.conf"
default["monit"]["includes_dir"] = "/etc/monit.d"
else
default["monit"]["main_config_path"] = "/etc/monit/monitrc"
default["monit"]["includes_dir"] = "/etc/monit/conf.d"
end
# The monit::default recipe will load these monit_monitrc resources automatically
# NOTE setting this attribute at the default level will append values to the array
default["monit"]["default_monitrc_configs"] = ["load", "ssh"]
name "monit"
maintainer "Phil Cohen"
maintainer_email "github@phlippers.net"
license "MIT"
description "Configures monit"
long_description "Please refer to README.md"
version "1.4.0"
recipe "monit", "Sets up the service definition and default checks."
%w[ubuntu debian redhat centos scientific fedora suse amazon].each do |os|
supports os
end
action :create do
t = template "#{node["monit"]["includes_dir"]}/#{new_resource.name}.monitrc" do
owner "root"
group "root"
mode "0644"
source new_resource.template_source || "#{new_resource.name}.monitrc.erb"
cookbook new_resource.template_cookbook
variables new_resource.variables
action :create
notifies :restart, "service[monit]", :delayed
end
new_resource.updated_by_last_action(t.updated_by_last_action?)
end
action :delete do
f = file "#{node["monit"]["includes_dir"]}/#{new_resource.name}.monitrc" do
action :delete
notifies :restart, "service[monit]", :delayed
end
new_resource.updated_by_last_action(f.updated_by_last_action?)
end
#
# Cookbook Name:: monit
# Recipe:: default
#
package "monit"
# configuration file
template node["monit"]["main_config_path"] do
owner "root"
group "root"
mode "0700"
source "monitrc.erb"
notifies :restart, "service[monit]", :delayed
end
directory "/var/monit" do
owner "root"
group "root"
mode "0700"
end
# enable service startup
execute "enable-monit-startup" do
command "/bin/sed s/START=no/START=yes/ -i /etc/default/monit"
not_if "grep 'START=yes' /etc/default/monit"
only_if { platform_family?("debian") }
end
# system service
service "monit" do
service_name "monit"
case node["platform_family"]
when "rhel", "fedora", "suse"
start_command "/sbin/service monit start"
restart_command "/sbin/service monit restart"
when "debian"
start_command "/usr/sbin/invoke-rc.d monit start"
restart_command "/usr/sbin/invoke-rc.d monit restart"
end
supports value_for_platform(
"debian" => { "4.0" => [ :restart, :start ], "default" => [ :restart, :start ] },
"ubuntu" => { "default" => [ :restart, :start ] },
"redhat" => { "default" => [ :restart, :start ] },
"centos" => { "default" => [ :restart, :start ] },
"fedora" => { "default" => [ :restart, :start ] },
"default" => { "default" => [:restart, :start ] }
)
action :enable
end
# build default monitrc files
node["monit"]["default_monitrc_configs"].each do |conf|
monit_monitrc conf do
variables(:category => "system")
notifies :restart, "service[monit]", :delayed
end
end
actions :create, :delete
default_action :create
attribute :name, :kind_of => String, :name_attribute => true
attribute :variables, :kind_of => Hash
# the cookbook containing the template, defaults to the one invoking this LWRP
attribute :template_source, :kind_of => String
# the template file name, defaults to #{name}.monitrc.erb
attribute :template_cookbook, :kind_of => String
check process sshd with pidfile /var/run/sshd.pid
start program "/sbin/service sshd start"
stop program "/sbin/service sshd stop"
if failed port 22 protocol ssh then restart
if 5 restarts within 5 cycles then timeout
\ No newline at end of file
check system <%= node["fqdn"] %>
if loadavg (1min) > 4 for 2 cycles then alert
if loadavg (5min) > 2 for 2 cycles then alert
if memory usage > 75% for 2 cycles then alert
if cpu usage (user) > 70% for 2 cycles then alert
if cpu usage (system) > 30% for 2 cycles then alert
if cpu usage (wait) > 20% for 2 cycles then alert
# Polling frequency
set daemon <%= node["monit"]["polling_frequency"] %>
<% if node["monit"]["start_delay"].to_i > 0 %>
with start delay <%= node["monit"]["start_delay"].to_i %>
<% end -%>
# Logging
<% if node["monit"]["syslog"] == "true" %>
set logfile syslog facility log_daemon
<% else %>
set logfile <%= node["monit"]["logfile"] %>
<% end %>
<% if node['monit']['mail_alerts'] %>
# Mail alerts
set alert <%= node["monit"]["alert_email"] %>
<% end %>
set mailserver <%= node["monit"]["mail"]["hostname"] %> port <%= node["monit"]["mail"]["port"] %>
<% if node["monit"]["mail"]["username"] -%>
username "<%= node["monit"]["mail"]["username"] %>"
<% end -%>
<% if node["monit"]["mail"]["password"] -%>
password "<%= node["monit"]["mail"]["password"] %>"
<% end -%>
<% if node["monit"]["mail"]["security"] -%>
using <%= node["monit"]["mail"]["security"] %>
<% end -%>
with timeout <%= node["monit"]["mail"]["timeout"] %> seconds
set mail-format {
from: <%= node["monit"]["mail"]["from"] %>
subject: <%= node["monit"]["mail"]["subject"] %>
message: <%= node["monit"]["mail"]["message"] %>
}
<% if node["monit"]["web_interface"]["enable"] %>
# Web interface
set httpd port <%= node["monit"]["web_interface"]["port"] %> and
use address <%= node["monit"]["web_interface"]["address"] %>
<% node["monit"]["web_interface"]["allow"].each do |allow| -%>
allow <%= allow %>
<% end %>
<% end %>
# Include config files
include <%= node["monit"]["includes_dir"] %>/*
check process sshd with pidfile /var/run/sshd.pid
start program "/etc/init.d/ssh start"
stop program "/etc/init.d/ssh stop"
if failed port 22 protocol ssh then restart
if 5 restarts within 5 cycles then timeout
\ No newline at end of file
cache_type "BasicFile"
cache_options(:path => "#{ENV["HOME"]}/.chef/checksums")
source "https://rubygems.org"
gem "rake"
gem "foodcritic", :platform => :ruby_19
gem "chef", ">= 10.12.0"
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment