Monit is a reliable and simple to configure tool for monitoring and managing processing and resources on UNIX based systems. Presented to the NH Ruby User Group on 21 May 2009.
3. Take Action!
alert Send an alert
restart Stop then start a
process
start Start a process
stop Stop a process
exec Execute arbitrary
program
unmonit Disable monitoring
or
timeout Disable monitoring
4. Prerequisites
UN*X
Start/Stop Scripts for Processes
Mail Server for Alerts
6. Configure
## /etc/monit/monitrc
set daemon 120
set logfile /var/log/monit.log
set alert alert@brianturnbull.com
set mail-format {
from: monit@brianturnbull.com
subject: $SERVICE service - $EVENT
message: $ACTION $SERVICE on $HOST: $DESCRIPTION
}
set mailserver localhost port 25
set httpd port 8877
allow monit:passwd
include /etc/monit/monit.d/*
7. Monitor
## /etc/monit/monit.d/apache
check process apache with pidfile /var/run/apache2.pid
start program = quot;/etc/init.d/apache2 startquot;
stop program = quot;/etc/init.d/apache2 stopquot;
if failed host localhost port 80 protocol http then restart
if 2 restarts within 3 cycles then timeout
## /etc/monit/monit.d/sshd
check process sshd with pidfile /var/run/sshd.pid
start program = quot;/etc/init.d/ssh startquot;
stop program = quot;/etc/init.d/ssh stopquot;
if failed port 22 protocol ssh then restart
if 5 restarts within 5 cycles then timeout
8. Monitor Files
## /etc/monit/monit.d/apache_log
check file access.log with path /var/log/apache2/access.log
if size > 100Mb
then exec quot;/usr/sbin/logrotate -f /etc/logrotate.d/apache2quot;
check file error.log with path /var/log/apache2/error.log
if size > 100Mb
then exec quot;/usr/sbin/logrotate -f /etc/logrotate.d/apache2quot;
11. Monitor Resources
## /etc/monit/monit.d/system
check system brianturnbull.com
if loadavg (1min) > 4 then alert
if loadavg (5min) > 2 then alert
if memory usage > 75% then alert
if cpu usage (user) > 70% then alert
if cpu usage (system) > 30% then alert
if cpu usage (wait) > 20% then alert
12. Monitor Services
## /etc/monit/monit.d/apache
check process apache with pidfile /var/run/apache2.pid
start program = quot;/etc/init.d/apache2 startquot;
stop program = quot;/etc/init.d/apache2 stopquot;
if failed host localhost port 80 protocol http then restart
if 2 restarts within 3 cycles then timeout
## /etc/monit/monit.d/mail
check host mail.brianturnbull.com with address mail.brianturnbull.com
if failed port 25
expect “^220.*rn”
send “HELO brianturnbull.com
expect “^250.*rn”
send “QUIT.*rn”
expect “^221.*rn”
then alert
13. Dependancies
## /etc/monit/monit.d/apache
check process apache with pidfile /var/run/apache2.pid
group www
start program = quot;/etc/init.d/apache2 startquot;
stop program = quot;/etc/init.d/apache2 stopquot;
if 2 restarts within 3 cycles then timeout
if failed host localhost port 80 protocol http then restart
depends on apache_bin
depends on apache_rc
check file apache_bin with path /usr/sbin/apache2
group www
if failed checksum then unmonitor
if failed permission 755 then unmonitor
if failed uid root then unmonitor
if failed gid root then unmonitor
check file apache_rc with path /etc/init.d/apache2
group www
if failed checksum then unmonitor
if failed permission 755 then unmonitor
if failed uid root then unmonitor
if failed gid root then unmonitor
14. More Info
& Examples
http://mmonit.com/monit
Thank You
http://brianturnbull.com