Saturday, 17 October 2015

Install and configure Monit for automated monitoring, alerting and restarting of services in Linux

Monit is a utility for managing and monitoring processes, programs, files, directories and filesystems on a Unix system. Monit conducts automatic maintenance and repair and can execute meaningful causal actions in error situations. E.g. Monit can start a process if it does not run, restart a process if it does not respond and stop a process if it uses too much resources. You can use Monit to monitor files, directories and filesystems for changes, such as timestamps changes, checksum changes or size changes.

Monit is controlled via an easy to configure control file based on a free-format, token-oriented syntax. Monit logs to syslog or to its own log file and notifies you about error conditions via customisable alert messages. Monit can perform various TCP/IP network checks, protocol checks and can utilise SSL for such checks. Monit provides a HTTP(S) interface and you may use a browser to access the Monit program.


To install monit on Ubuntu,

sudo apt-get install monit

This will install monit on your Ubuntu server. Monit configuration file is located at /etc/monit/monitrc

In this tutorial, we are using monit to monitor some basic services like apache, varnish, mysql, postfix, disk space, cpu load etc .

First take a back up of the original monit configuration.

sudo mv /etc/monit/monitrc /usr/local/src/monitrc.orig

Then create /etc/monit/monitrc using  opening it through vim and copy paste the below contents:

vim /etc/monit/monitrc

set daemon  60 
set logfile syslog facility log_daemon 
set mailserver localhost 

set mail-format {
     from: [email protected]
     subject: [ $SERVICE ] $EVENT - $DATE
     message: This is a $ACTION: $DESCRIPTION [$SERVICE], tested remotely from my.servername.com }

set alert [email protected]

system monitor 
check system localhost 
   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 

#apache monitor 
check process apache with pidfile /var/run/apache2.pid 
   group www 
   start program = "/etc/init.d/apache2 start" 
   stop program  = "/etc/init.d/apache2 stop" 
   if failed host 127.0.0.1 port 8080 protocol http then restart 

#mysql monitor 
check process mysql with pidfile /var/run/mysqld/mysqld.pid 
   group database 
   start program = "/etc/init.d/mysql start"   
   stop program = "/etc/init.d/mysql stop" 
   if failed host 127.0.0.1 port 3306 then restart 

#varnish monitor 
check process varnish with pidfile /var/run/varnishd.pid 
   start program = "/etc/init.d/varnish start" 
   stop program = "/etc/init.d/varnish stop" 
   if failed host 127.0.0.1 port 80 protocol http then restart 

#disk space monitoring
check device root with path /
    if SPACE usage > 80% then alert

#postfix monitor
check process postfix with pidfile /var/spool/postfix/pid/master.pid
    start program = "/etc/init.d/postfix start"
    stop program  = "/etc/init.d/postfix stop"

#proftpd monitor
check process proftpd with pidfile /var/run/proftpd.pid
   start program = "/etc/init.d/proftpd start"
   stop program  = "/etc/init.d/proftpd stop"
   if failed port 21 protocol ftp then restart

#BIND monitoring
check process named with pidfile /var/named/chroot/var/run/named/named.pid
   start program = "/etc/init.d/named start"
   stop program = "/etc/init.d/named stop"
   if failed host 127.0.0.1 port 53 type tcp protocol dns then alert
   if failed host 127.0.0.1 port 53 type udp protocol dns then alert

Now restart monit by running below commands:

sudo /etc/init.d/monit restart

This will restart monit and new configuration will take into effect. You check your email and you should have got an email alert from monit.



No comments:

Post a Comment