├── LICENSE ├── files └── ubuntu │ └── monit.default ├── metadata.rb ├── templates └── default │ ├── services.conf.erb │ └── monitrc.erb ├── recipes ├── services.rb └── default.rb ├── attributes └── default.rb └── README.md /LICENSE: -------------------------------------------------------------------------------- 1 | Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php 2 | -------------------------------------------------------------------------------- /files/ubuntu/monit.default: -------------------------------------------------------------------------------- 1 | # Defaults for monit initscript 2 | # sourced by /etc/init.d/monit 3 | # installed at /etc/default/monit by chef 4 | 5 | # You must set this variable to for monit to start 6 | startup=1 7 | 8 | # To change the intervals which monit should run, 9 | # edit the configuration file /etc/monit/monitrc 10 | # It can no longer be configured here. 11 | 12 | -------------------------------------------------------------------------------- /metadata.rb: -------------------------------------------------------------------------------- 1 | maintainer "Brad Montgomery" 2 | maintainer_email "bmontgomery@coroutine.com" 3 | license "MIT" 4 | description "Configures monit. Originally based off the 37 Signals Cookbook." 5 | long_description IO.read(File.join(File.dirname(__FILE__), 'README.md')) 6 | version "0.8.5" 7 | 8 | recipe "monit::default", "install/configure monit" 9 | recipe "monit::services", "configure services for monit to monitor" 10 | 11 | %w{ ubuntu debian }.each do |os| 12 | supports os 13 | end 14 | -------------------------------------------------------------------------------- /templates/default/services.conf.erb: -------------------------------------------------------------------------------- 1 | CHECK PROCESS <%= @conf["process_name"] %> WITH PIDFILE <%= @conf["pidfile"] %> 2 | START PROGRAM "<%= @conf["start_command"] %>" 3 | STOP PROGRAM "<%= @conf["stop_command"] %>" 4 | IF FAILED <% if @conf["check_port"] %>PORT <%= @conf["check_port"] %><% elsif @conf["check_socket"] %>unixsocket <%= @conf["check_socket"] %><% end %><% if @conf["check_protocol"] %> PROTOCOL <%= @conf["check_protocol"] %><% end %><% if @conf["check_times"] && @conf["within_cycles"] %> <%= @conf["check_times"] %> TIMES WITHIN <%= @conf["within_cycles"] %> CYCLES<% end %><% if @conf["then_action"] %> THEN <%= @conf["then_action"] %><% end %> 5 | -------------------------------------------------------------------------------- /recipes/services.rb: -------------------------------------------------------------------------------- 1 | include_recipe "monit::default" 2 | 3 | # 4 | # Pulls the appropriate configs from the "monit" data bag. 5 | # These should be specified in a role. 6 | # 7 | 8 | node['monit']['services'].each do |item_name| 9 | search(:monit, "id:#{item_name}") do |services| 10 | 11 | services.each do |service, service_config| 12 | 13 | # Don't use the monitrc definition. Instead, 14 | # just write a new template. 15 | if service != "id" 16 | template "/etc/monit/conf.d/#{service}.conf" do 17 | owner "root" 18 | group "root" 19 | mode 0644 20 | source "services.conf.erb" 21 | variables( 22 | "service" => service, 23 | "conf" => service_config 24 | ) 25 | notifies :restart, resources(:service => "monit"), :delayed 26 | action :create 27 | end 28 | end 29 | end 30 | end 31 | end 32 | -------------------------------------------------------------------------------- /recipes/default.rb: -------------------------------------------------------------------------------- 1 | package "monit" 2 | 3 | if platform?("ubuntu") 4 | cookbook_file "/etc/default/monit" do 5 | source "monit.default" 6 | owner "root" 7 | group "root" 8 | mode 0644 9 | end 10 | end 11 | 12 | service "monit" do 13 | action [:enable, :start] 14 | enabled true 15 | supports [:start, :restart, :stop] 16 | end 17 | 18 | directory "/etc/monit/conf.d" do 19 | owner 'root' 20 | group 'root' 21 | mode 0755 22 | action :create 23 | recursive true 24 | end 25 | 26 | template "/etc/monit/monitrc" do 27 | owner "root" 28 | group "root" 29 | mode 0700 30 | source 'monitrc.erb' 31 | variables( 32 | :logfile => node["monit"]["logfile"], 33 | :notify => node["monit"]["notify"], 34 | :httpd => node["monit"]["httpd"], 35 | :poll_period => node["monit"]["poll_period"], 36 | :poll_start_delay => node["monit"]["poll_start_delay"], 37 | :mail => node["monit"]["mail"], 38 | :queue => node["monit"]["queue"], 39 | :config_directory => "/etc/monit/conf.d" 40 | ) 41 | notifies :restart, resources(:service => "monit"), :delayed 42 | end 43 | -------------------------------------------------------------------------------- /templates/default/monitrc.erb: -------------------------------------------------------------------------------- 1 | set daemon <%= @poll_period %> with start delay <%= @poll_start_delay %> 2 | 3 | <% unless @logfile.nil? %>set logfile <%= @logfile %><% end %> 4 | 5 | set eventqueue 6 | basedir <%= @queue[:location] %> 7 | <% unless @queue[:slots].nil? %>slots <%= @queue[:slots] %><% end %> 8 | 9 | <% unless @mail[:server].nil? %> 10 | set mailserver <%= @mail[:server] %> 11 | <%= "PORT #{@mail[:port]}" unless not @mail[:port] %> 12 | <% if @mail[:username] && @mail[:password] -%> 13 | USERNAME <%= @mail[:username] %> PASSWORD "<%= @mail[:password] %>" 14 | <% end -%> 15 | <%= "USING #{@mail[:security]}" unless not @mail[:security] %> 16 | <%= "CERTMD5 #{@mail[:checksum]}" unless not @mail[:checksum] %> 17 | <%= "with TIMEOUT #{@mail[:timeout]} SECONDS" unless not @mail[:timeout] %> 18 | <%= "using HOSTNAME #{@node.fqdn}" unless not @mail[:use_node_fqdn] %> 19 | 20 | set mail-format { 21 | <% if @mail[:format][:from] %>from: <%= @mail[:format][:from] %> 22 | <% else %>from: monit@<%= @node.fqdn %><% end %> 23 | subject: <%= @mail[:format][:subject] %> 24 | message: <%= @mail[:format][:message].to_s.strip %> 25 | } 26 | <% end %> 27 | 28 | <% if @notify[:enable] %> 29 | set alert <%= @notify[:email] %> <%= "NOT" if @notify[:negate_events] -%> ON { 30 | <%= @notify[:events].join(", ") %> 31 | } 32 | <% end %> 33 | 34 | <% if @httpd[:enable] %> 35 | set httpd port <%= @httpd[:port] %> and 36 | <% if @httpd[:address] %> 37 | use address <%= @httpd[:address] %> 38 | <% end %> 39 | <% if @httpd[:allow] %> 40 | <% @httpd[:allow].each do |allow| %> 41 | allow <%= allow %> 42 | <% end %> 43 | <% end %> 44 | signature <%= @httpd[:signature] %> 45 | <% @httpd[:basic_auth_accounts].each do |auth| %> 46 | <%# 47 | NOTE: auth entries are of the form "username:password", but passwords 48 | need to be quoted in the monit config: 'username:"password"' 49 | -%> 50 | allow <%= auth.split(":").first -%>:"<%= auth.split(":").last -%>" 51 | <% end %> 52 | <% end %> 53 | 54 | include <%= @config_directory %>/*.conf 55 | -------------------------------------------------------------------------------- /attributes/default.rb: -------------------------------------------------------------------------------- 1 | default[:monit][:services] = [] # List of items from the "monit" data bag that 2 | # describe services monitored by monit. 3 | default[:monit][:logfile] = "syslog facility log_daemon" 4 | 5 | # The following is a list of events for which monit will send notifications: 6 | default[:monit][:notify][:events] = [] 7 | default[:monit][:notify][:negate_events]= false # if true, will negate the list of notification events 8 | default[:monit][:notify][:enable] = false # set true to send alerts 9 | default[:monit][:notify][:email] = "notify@example.com" # address to which alerts are sent 10 | 11 | default[:monit][:httpd][:enable] = true 12 | default[:monit][:httpd][:port] = 3737 13 | default[:monit][:httpd][:address] = nil # hosts can only connect from this address; monit defaults to "localhost" 14 | default[:monit][:httpd][:allow] = nil # default: %w{localhost} 15 | default[:monit][:httpd][:signature] = "enable" # or disable 16 | default[:monit][:httpd][:basic_auth_accounts] = [] # a list of 'admin:"password"' entries to use for BasicAuth 17 | # the quotes around the password are required! 18 | 19 | default[:monit][:poll_period] = 60 20 | default[:monit][:poll_start_delay] = 120 21 | 22 | default[:monit][:mail][:server] = "localhost" 23 | default[:monit][:mail][:port] = "25" 24 | default[:monit][:mail][:username] = nil 25 | default[:monit][:mail][:password] = nil 26 | default[:monit][:mail][:security] = nil # SSLV2, SSLV3 or TLSV1 27 | default[:monit][:mail][:checksum] = nil # the server certificate checksum 28 | default[:monit][:mail][:timeout] = "5" # timeout in seconds for the mail server 29 | default[:monit][:mail][:use_node_fqdn] = false # Use the Node's FQDN for "using hostname" 30 | default[:monit][:mail][:format][:subject] = "$SERVICE $EVENT" 31 | default[:monit][:mail][:format][:from] = nil # defaults to "monit@hostname" 32 | default[:monit][:mail][:format][:message] = <<-EOS 33 | Monit $ACTION $SERVICE at $DATE on $HOST: $DESCRIPTION. 34 | Yours sincerely, 35 | monit 36 | EOS 37 | 38 | default[:monit][:queue][:location] = "/var/monit" # base directory where events will be stored 39 | default[:monit][:queue][:slots] = nil # limit the size of the queue 40 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Overview 2 | ======== 3 | 4 | Chef cookbook for the [monit](http://mmonit.com/monit/) monitoring and 5 | management tool. This cookbook has been updated to configure services 6 | defined in a data bag. 7 | 8 | This cookbook was forked from [StudyBlue/monit](https://github.com/StudyBlue/monit), version 0.7. 9 | 10 | Attributes 11 | ========== 12 | 13 | 14 | * `default[:monit][:services]` - List of items from the _monit_ data bag that describe services monitored by monit. For more info, see *Usage* below. 15 | * `default[:monit][:logfile]` - The file to which monit logs are written 16 | * `default[:monit][:notify][:enable]` - enable email notifications 17 | * `default[:monit][:notify][:email]` - the email to which notifications are sent. 18 | * `default[:monit][:httpd][:enable]` - enable the http server 19 | * `default[:monit][:httpd][:port]` - port on which the http server listens 20 | * `default[:monit][:httpd][:address]` - the address to which the server binds 21 | * `default[:monit][:httpd][:allow]` - the addresses from which connections are allowed (default `%w{localhost}`) 22 | * `default[:monit][:httpd][:signature]` - either "enable" or "disable". Determines whether monit shows a signature 23 | * `default[:monit][:httpd][:basic_auth_accounts]` - An array of `admin:"password"` entries that are used for HTTP Basic Auth. Not the quotes around the password; these are required. 24 | * `default[:monit][:poll_period]` - the time (in seconds) between poll cycles 25 | * `default[:monit][:poll_start_delay]` - the amount of time (in seconds) before monit starts polling services. 26 | * `default[:monit][:mail][:server]` - host on which the smtp mail server runs 27 | * `default[:monit][:mail][:port]` - The port on which the SMTP server runs (default is 25) 28 | * `default[:monit][:mail][:username]` - Username for an SMTP account 29 | * `default[:monit][:mail][:password]` - Password for an SMTP account 30 | * `default[:monit][:mail][:security]` - Send secure email. Must be one of: SSLV2, SSLV3 or TLSV1 31 | * `default[:monit][:mail][:checksum]` - SMTP Server certificat checksum (optional) 32 | * `default[:monit][:mail][:timeout]` - The timeout (in seconds) for sending mail 33 | * `default[:monit][:mail][:use_node_fqdn]` - Whether or not to use the node's FQDN in as the hostname (default is `false`) 34 | * `default[:monit][:mail][:format][:subject]` - default subject for emails from monit 35 | * `default[:monit][:mail][:format][:from]` - email address from which messages are sent 36 | * `default[:monit][:mail][:format][:message]` - template for messages (see `attributes/default.rb` for details) 37 | * `default[:monit][:queue][:location]` - base directory where events will be stored 38 | * `default[:monit][:queue][:slots]` - limit the size of the queue; this is the number of messages that will be held 39 | 40 | Usage 41 | ===== 42 | 43 | Right now, this cookbook only supports simple process monitoring. To set up process monitors, create a `monit` data bag, and create items that look similar to the following: 44 | 45 | This would be `data_bags/monit/monit_services.json`: 46 | 47 | { 48 | "id": "monit_services", 49 | "postgresql": { 50 | "process_name": "postgres", 51 | "pidfile": "/var/run/postgresql/9.1-main.pid", 52 | "start_command":"/usr/sbin/service postgresql start", 53 | "stop_command": "/usr/sbin/service postgresql stop", 54 | "check_port": "5432", 55 | "check_protocol": "pgsql", 56 | "check_times": 2, 57 | "within_cycles": 3, 58 | "then_action": "restart" 59 | }, 60 | "supervisor": { 61 | "process_name": "supervisor", 62 | "pidfile": "/var/run/supervisord.pid", 63 | "start_command":"/etc/init.d/supervisor start", 64 | "stop_command":"/etc/init.d/supervisor stop", 65 | "check_socket": "/var/run/supervisor.sock", 66 | "check_times": 1, 67 | "within_cycles": 2, 68 | "then_action": "restart" 69 | } 70 | } 71 | 72 | This item would set up a monitor for the `postgres` and `supervisor` processes. Note that it checks for PostgreSQL using a TCP port while Supervisor uses a Unix socket. 73 | 74 | This would result in the following: 75 | * `/etc/monit/conf.d/postgresql.conf`: 76 | 77 | CHECK PROCESS postgres WITH PIDFILE /var/run/postgresql/9.1-main.pid 78 | START PROGRAM "/usr/sbin/service postgresql start" 79 | STOP PROGRAM "/usr/sbin/service postgresql stop" 80 | IF FAILED PORT 5432 PROTOCOL pgsql 2 TIMES WITHIN 3 CYCLES THEN restart 81 | 82 | * `/etc/monit/conf.d/supervisor.conf`: 83 | 84 | CHECK PROCESS supervisor WITH PIDFILE /var/run/supervisord.pid 85 | START PROGRAM "/etc/init.d/supervisor start" 86 | STOP PROGRAM "/etc/init.d/supervisor stop" 87 | IF FAILED unixsocket /var/run/supervisor.sock 1 TIMES WITHIN 2 CYCLES THEN restart 88 | 89 | You can then set up a role, and override any of the existing attributes, and specify any data bag items taht will be used to set up service monitoring. 90 | 91 | You might have a `monit` role that looks like: 92 | 93 | name "monit" 94 | description "Monit role" 95 | run_list( 96 | "recipe[monit::default]", 97 | "recipe[monit::services]" 98 | ) 99 | 100 | default_attributes( 101 | "monit" => { 102 | "services" => ["monit_services", ], 103 | } 104 | ) 105 | 106 | 107 | History 108 | ======= 109 | 110 | version 0.8.1 111 | ------------- 112 | * added additional SMTP configuration 113 | * added additional HTTPD configuration 114 | 115 | version 0.8 116 | ----------- 117 | * added more generic services recipes that pulls configuration data from a data bag 118 | * only supporting Ubuntu/Debian 119 | * removed postfix/ssh recipes 120 | 121 | version 0.7 122 | ----------- 123 | * create /etc/monit/conf.d. Thanks Karel Minarik (https://github.com/karmi) 124 | 125 | version 0.6 126 | ----------- 127 | * Released to github 128 | * Defaults no alert on ACTION event. 129 | When you manually stop/start a service, alerting me about what I just did isn't useful. 130 | 131 | --------------------------------------------------------------------------------