Sunday, February 12, 2006

Linux - Setting up System Alerts

Consider a scenario where you are the System Administrator of a company running vital services and you do not have access to your Servers on weekdays.

You may want the following things to happen :

---Send a Alert to you by way of mail in case the Service Fails.
Obviously, I dont check mails on weekdays, so I am redirecting it to my cell.

---Auto restart the Service upon checking if the service has stopped.

For this I have downloaded monit-4.7.tar.gz from :

You can download the above version from here

I Untarred it in,

#cd /home/sriram/Download

[root@mybox Download]# pwd

[root@mybox Download]# ls
flash-0.4.10/ monit-4.7/ monit-howto
flash-0.4.10.tgz monit-4.7.tar.gz phplist-2.10.2.tgz

Now to install it,
[root@mybox Download]#cd monit-4.7

[root@mybox monit-4.7]#./configure
[root@mybox monit-4.7]# make
[root@mybox monit-4.7]#make install

This has installed monit

Now Configuration,

You need to create a configuration file called .monitrc in your home directory

I created under /home/sriram

#touch .monitrc
#chmod 0700 .monitrc

Contents of my .monitrc file is as follows :

[root@mybox sriram]# cat .monitrc

#Monit Configuration file

set daemon 300
set logfile /var/log/monit

#set mailserver

check apache with pidfile /var/run/
group apache
start = "/etc/rc.d/init.d/httpd start"
stop = "/etc/rc.d/init.d/httpd stop"
if failed host port 80 type tcp then restart
set alert with mail-format { from: }

[root@mybox sriram]#

Explanation :

Line 1 sets daemon to check for services every 5 Minutes

Line 3 I have hashed, you can define which mailserver to use to
send alerts in case you are using other than localhost.

Rest of the things are pretty explanatory if you look at them
Note : Set is a Global Variable it applies to the whole of Monit Configuration.


[root@mybox sriram]# cat .monitrc

#$Id: monit.html,v 1.1 2002/04/30 16:52:36 kclark Exp $

set daemon 300
set logfile /var/log/monit

check apache with pidfile /var/run/
group apache
start = "/etc/rc.d/init.d/httpd start"
stop = "/etc/rc.d/init.d/httpd stop"
if failed host port 80 type tcp then restart
set alert with
mail-format {
subject: $SERVICE $EVENT at $DATE
Yours sincerely,

The file monitors http service and sends messages to my gmail account in case
a service fails and restarts the same.

Note Before Setting up Alerts you will need a running Smtp Server which can send mails from the domain specified in the From ID.

I have configured postfix to accept mails from domain.

To start the monit Service I do,

[root@mybox sriram]# monit -c /home/sriram/.monitrc

Web-Based Monit :

Monit can also be access web based for checking services

Just add these 3 lines at the end of .monitrc file

set httpd port 2812 address localhost

allow localhost
allow admin:sriram

Line 1 specifies Monit should start its own Built in server and bind
the server to localhost only.

If you want the Monit Http server to be accessible from other systems
in your network simply omit the address to bind to.

Line 2 specifies access control to httpd server host allowed to connect to
httpd server should be listed with one or more allow host statement.

Line 3 specifies a Username:Password user for Basic Authentication

Although its not recommened to Web based Configuration if you are running
a Live Server which is accessible from outside.

For more information on monit to suit your needs visit: