You can use Monit to monitor daemon processes or similar programs running on localhost. Monit is particular useful for monitoring daemon processes, such as those started at system boot time from /etc/init.d/. For instance sendmail, sshd, apache and mysql.
In difference to many monitoring systems, Monit can act if an error situation should occur, e.g.; if sendmail is not running, Monit can start sendmail again automatically or if apache is using too much resources (e.g. if a DoS attack is in progress) Monit can stop or restart apache and send you an alert message. Monit can also monitor process characteristics, such as; how much memory or cpu cycles a process is using.
Install Monit 5.5 on CentOS RHEL Debian Ubuntu
Install some prerequisites
On CentOS/RHEL, Fedora
# yum install openssl-devel gcc make gcc-c++ pam-devel
On Debian, Ubuntu
$ sudo apt-get install libssl-dev build-essential libpam0g-dev
Download the source tarball untar it and run configure, make, make install
# wget http://mmonit.com/monit/dist/monit-5.5.tar.gz
# tar zxvf monit-5.5.tar.gz
# cd monit-5.5
# ./configure --enable-optimized --enable-profiling
# make install
Create monit conf file
# nano /etc/monitrc
Set monit web user inteface to start listening on port 2812, you can change the port number per your wish.
set httpd port 2812 and
use address 0.0.0.0 # only accept connection from localhost
allow 192.168.209.0/24 # allow localhost to connect to the server and
allow admin:monit # 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
Now for this example I would like monit to monitor ntp daemon on my machine and start it automatically if it dies, you can monitor web servers, database services and what not. Replace the alert email address with your email address. Also set your mailserver, port number, username and password, I created a user account on my mail server for monit to sent email from.
set alert firstname.lastname@example.org
check process ntpd with pidfile /var/run/ntpd.pid
start program = "/etc/init.d/ntp start"
stop program = "/etc/init.d/ntp stop"
if failed host 127.0.0.1 port 123 type udp then alert
if 5 restarts within 5 cycles then timeout
set mailserver mail01.linuxdrops.com port 25
username "email@example.com" password "Password"
To see other configuration examples
Change the permission of the conf file
# chmod 700 /etc/monitrc
Open inittab in an editor of choice
# vi /etc/inittab
Append the following to the file
mo:2345:respawn:/usr/local/bin/monit -Ic /etc/monitrc
Tells the init command to re-examine the /etc/inittab file.
# telinit q