Monitoring Your Server Services With Monit
What is Monit ?
monit is a utility for managing and monitoring, processes, files, directories and devices on a UNIX system. Monit conducts automatic maintenance and repair and can execute meaningful causal actions in error situations.
Download monit
http://www.tildeslash.com/monit/download/
monit documentation
http://www.tildeslash.com/monit/doc/
monit FAQ
http://www.tildeslash.com/monit/doc/faq.php
Installing monit in debian
If you want to install monit in Debian just follow this
#apt-get install monit
Reading Package Lists... Done
Building Dependency Tree... Done
The following NEW packages will be installed:
monit
0 upgraded, 1 newly installed, 0 to remove and 3 not upgraded.
Need to get 228kB of archives.
After unpacking 643kB of additional disk space will be used.
Get:1 http://mirror.ox.ac.uk stable/main monit 1:4.5-1 [228kB]
Fetched 228kB in 0s (678kB/s)
Selecting previously deselected package monit.
(Reading database ... 15512 files and directories currently installed.)
Unpacking monit (from .../monit_1%3a4.5-1_i386.deb) ...
Setting up monit (4.5-1) ...
Starting daemon monitor: monit won't be started/stopped
unless it it's configured
please configure monit and then edit /etc/default/monit
and set the "startup" variable to 1 in order to allow
monit to start
This completes the installation Process.
Configuring monit
monit configuration file is located at /etc/monit/monitrc.If you want to see the default configuration file click here
By default all the lines in configuration file is commented out you can uncomment the lines which ever you want to monitor
Here we are going to see the some example configuration file
set daemon 120
# poll at 2-minute intervals. Monit will wakeup every two minute to
# monitor things. Time must be given in seconds.
set logfile syslog facility log_daemon
# Set syslog logging.
# Set a default mail from-address for all alert messages emitted by monit.
# All alert mail will be sent to below mail address.
set mail-format { from: [email protected] }
set alert [email protected]
# You can define your mail-notification format. Do man monit
mail-format {
from: [email protected]
subject: $SERVICE $EVENT at $DATE
message: Monit $ACTION $SERVICE at $DATE on $HOST,
Yours sincerely,
Debianhelp
}
# Make monit start its web-server. So you can access it from web browser.
set httpd port 2812 and
use address domain.com
##Monit web-server ACL.
allow localhost # allow localhost to connect to the server and
allow 172.19.1.2 # allow 172.19.1.2 to connect to the server,
# You can give only one per entry
allow admin:monit # user name and password for authentication.
allow debianhelp:debianhelp # set multiple user to access through browser.
# Monitoring the apache2 web services.
# It will check process apache2 with given pid file.
# If process name or pidfile path is wrong then monit will
# give the error of failed. tough apache2 is running.
check process apache2 with pidfile /var/run/apache2.pid
#Below is actions taken by monit when service got stuck.
start program = "/etc/init.d/apache2 start"
stop program = "/etc/init.d/apache2 stop"
# Admin will notify by mail if below of the condition satisfied.
if cpu is greater than 60% for 2 cycles then alert
if cpu > 80% for 5 cycles then restart
if totalmem > 200.0 MB for 5 cycles then restart
if children > 250 then restart
if loadavg(5min) greater than 10 for 8 cycles then stop
if 3 restarts within 5 cycles then timeout
group server
#Monitoring slapd process.
check process slapd with pidfile /var/ldap/var/run/slapd.pid
start program = "/etc/init.d/slapd start"
stop program = "/etc/init.d/slapd stop"
if failed port 389 then restart
if 2 restarts within 3 cycles then timeout
group server
# monit will only monit/give alert in this mode.
mode passive
# Check Database file, size,permission etc...
check file slapd-database with path /var/ldap/var/openldap-data/mydata.db
if failed permission 700 then alert
if failed uid data then alert
if failed gid data then alert
if timestamp > 15 minutes then alert
if size > 100 MB then alert
#Check directory for permission.
check directory bin with path /bin
if failed permission 755 then unmonitor
if failed uid 0 then unmonitor
if failed gid 0 then unmonitor
#Check host for which services up/down on particular port.
check host domain.com with address 192.168.1.1
if failed icmp type echo with timeout 4 seconds then alert
if failed port 21 then alert
if failed port 22 type tcp 22 with timeout 40 seconds then aler
if failed port 80 protocol http then alert
if failed port 389 type tcp with timeout 15 seconds then alert
# check directory bin with path /bin
if failed permission 755 then unmonitor
if failed uid 0 then unmonitor
if failed gid 0 then unmonitor
You can also include other configuration files via include directives:
include /etc/monit/default.monitrc
include /etc/monit/mysql.monitrc
After modifying the configuration file you should check for the syntax to make sure they are correct. To do this run:
# monit -t
Now you can run monit directly:
# monit
Once monit is running you can check for activity with your web-browser.
You can of course use the Debian init script to start the monitoring:
#/etc/init.d/monit start
For this to work you must enable the service by changing the file /etc/default/monit:
# You must set this variable to for monit to start
startup=1
# To change the intervals which monit should run uncomment
# and change this variable.
# CHECK_INTERVALS=180
Monit Web interface
Monit Web interface will run on the port number 2812.If you have any firewall in your network setup you need to enable this port.
Now point your browser to http://www.example.com:2812/ (make sure port 2812 isn't blocked by your firewall), log in with admin and test, and you should see the monit web interface