How to Monitor and Restart Daemons Automatically Using Monit
Posted on Updated onFor those of you who operate their own dedicated server (or VPS for that matter), you know how crucial it is to ensure that daemons are up and running all the time. On cPanel servers, there is a process named TailWatch that monitors log files and takes the appropriate action if a service fails.
However, if you are not running a cPanel server, you can use Monit, a lightweight system monitoring and error recovery tool for Linux/Unix servers. It is completely free and installation is a breeze. Monit is made out of two components: Monit and M/Monit. Monit is the agent that must be installed locally on each server you wish to monitor. M/Monit is a web interface that allows you to manage all of your Monit instances.
Installing Monit Using Distribution Packages
Monit is already packaged for various Linux distributions which makes it easy to install. Here’s how to install Monit on some popular distros:
RHEL / Fedora / CentOS Linux 6:
First you will need to enable to EPEL repository:
wget http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm rpm -ivh epel-release-6-8.noarch.rpm
Then you can install Monit by using YUM:
yum install monit -y
Debian 7.5:
aptitude install monit
OpenSUSE 10.x:
yast2 -i monit
Installing Monit Manually
If your Linux distribution doesn’t provide you with packaged installation of Monit, here’s how to install it manually from sources:
cd /usr/src wget http://mmonit.com/monit/dist/monit-5.8.1.tar.gz tar xvzf monit-5.8.1.tar.gz cd monit-5.8.1 ./configure --without-pam make make install
If you want to enable PAM authentication, simply remove the “–without-pam” switch from the “configure” command line.
Basic Monit Configuration
The first thing you need to do is to configure the web interface. The Monit configuration file can be located in one of these two directories, depending if you installed it from sources or from a distribution package:
- /etc/monit.conf
- /etc/monit/monitrc
If you wish to receive email notifications from Monit, edit the configuration file, uncomment the following line and add your email address:
set alert sysadm@foo.bar
Next you will need to configure the Monit web server. Find the httpd parameters and make the following modifications:
set httpd port 2812 and # use address localhost # only accept connection from localhost # allow localhost # allow localhost to connect to the server and allow YOURUSERNAME:PASSWORD # require user 'admin' with password 'monit' # allow @monit # allow users of group 'monit' to connect (rw) # allow @users readonly # allow users of group 'users' to connect readonly
If you wish to enable SSL for the Monit web server, follow this guide.
Starting Monit From Init
If you have installed Monit from a distribution package, you will find the init script located in /etc/init.d/monit. You can enable the Monit service by running these commands:
RHEL / Fedora / CentOS Linux 6:
chkconfig --add monit chkconfig monit on
Debian:
update-rc.d monit enable
OpenSUSE 10.x:
systemctl enable monit.service
Once the service is enabled, you can start it by issuing:
/etc/init.d/monit start
Make sure the web interface is accessible by pointing your web browser to http://yourserver.com:2812:
Sample Daemon Monitoring Configuration
In order to monitor a daemon, create a new file in /etc/monit.d:
vi /etc/monit.d/daemon-name.conf
To have the daemon monitored by its PID file and restarted in case of failure, add the following content to the file:
check process processname with pidfile /var/run/process.pid start program = "/etc/init.d/process-script start" stop program = "/etc/init.d/process-script stop" if 5 restarts within 5 cycles then timeout
Make sure to change the values highlighted in red and save the new configuration file. Now restart the Monit service:
/etc/init.d/monit restart
If you reload the Monit web interface, you will now see the monitored process listed.
Of course, there are some more complex ways to monitor daemons and Monit has a lot more to offer than this. You can find more configuration examples here.