Latest Monit setup on Ubuntu

Install

apt-based

apt-get install monit

Following installs Monit 5.3 on Ubuntu 12.04. Which is very old!

Binary Install

Go to http://mmonit.com/monit/#download and pick correct binary for your system. Following lines will install Monit 5.14 for Ubuntu 64-bit.

cd ~
wget http://mmonit.com/monit/dist/binary/5.14/monit-5.14-linux-x64.tar.gz
tar zxvf monit-5.14-linux-x64.tar.gz
cd monit-5.14/
cp bin/monit /usr/bin/monit
mkdir /etc/monit
touch /etc/monit/monitrc
chmod 0700 /etc/monit/monitrc 
ln -s /etc/monit/monitrc /etc/monitrc
wget https://gist.githubusercontent.com/rahul286/9975061/raw/1aa107e62ecaaa2dacfdb61a12f13efb6f15005b/monit -P /etc/init.d/
chmod u+x /etc/init.d/monit
echo "START=yes" > /etc/default/monit
monit -t

Config

Monit Config File

Add/Update following in /etc/monit/monitrc

#Monitoring Interval in Seconds
set daemon  60

#Enable Web Access
set httpd port 2812
     allow admin:easyengine

#Event Queue for 5000 events
set eventqueue basedir /var/monit slots 5000

#MySQL Monitoring
check process mysqld with pidfile "/var/run/mysqld/mysqld.pid"              
    if cpu > 80% for 2 cycles then alert

#PHP-FPM
check process php5-fpm with pidfile "/var/run/php5-fpm.pid"
    if cpu > 80% for 2 cycles then alert

#Nginx
check process nginx with pidfile "/var/run/nginx.pid"
    if cpu > 80% for 2 cycles then alert

#System Monitoring 
check system PUT_YOUR_HOSTNAME_HERE
    if memory usage > 80% for 2 cycles then alert
    if cpu usage (user) > 70% for 2 cycles then alert
        if cpu usage (system) > 30% then alert
    if cpu usage (wait) > 20% then alert
    if loadavg (1min) > 6 for 2 cycles then alert 
    if loadavg (5min) > 4 for 2 cycles then alert
    if swap usage > 5% then alert

check filesystem rootfs with path /
       if space usage > 80% then alert
set mailserver localhost
set alert [email protected]

##mmonit (optional)
#set mmonit http://user:pass@IP:port/collector

You may also wish to change username admin and password easyengine. Also, change numbers 6 and 4 in loadavg lines based on number of CPU cores on your system.

Start

First test your config using monit -t command. Then start monit using service monit start

Verify

Run command  monit status, it should something like:

The Monit daemon 5.13 uptime: 2m 

System 'example.com'
  status                            Running
  monitoring status                 Monitored
  load average                      [0.48] [0.40] [0.81]
  cpu                               0.0%us 0.0%sy 0.0%wa
  memory usage                      8276900 kB [25.1%]
  swap usage                        1192 kB [0.0%]
  data collected                    Thu, 03 Apr 2014 10:41:45

#many more lines...

You may also open example.com:2182 in your browser to use web-based interface.

Monit-Graph

You can use commercial version M/Monit for a lot many features or free script monit-graph

Install monit-graph

cd /var/www/example.com/htdocs
git clone https://github.com/danschultzer/monit-graph mg
cd mg
chmod -R 0777 data
chmod 0644 data/index.php

Config File

Open config.php and adjust following values:

"url" => "localhost:2812",   
"url_ssl" => false,
"http_username" => "admin",
"http_password" => "easyengine",

Setup Cron

Add following line to cron:

* * * * * php /var/www/example.com/htdocs/mg/cron.php >> /var/log/monit-graph.log

View Graphs

You can open http://example.com/mg in browser to see visual graphs!

TODO

  1. monit check program for custom script to monitor php5-fpm status page and nginx status page.
  2. Explore: https://github.com/perusio/monit-miscellaneous/blob/master/php-fpm-tcp
  3. Explore: https://github.com/regilero/check_phpfpm_status