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.

10-digit-no@Service-Provider.com

---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
/home/sriram/Download

[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 mail.sriram.com

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

[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.


Optional,

[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/httpd.pid
group apache
start = "/etc/rc.d/init.d/httpd start"
stop = "/etc/rc.d/init.d/httpd stop"
if failed host 10.10.93.220 port 80 type tcp then restart
set alert sriram003@gmail.com with
mail-format {
from: sriram@sriram.com
subject: $SERVICE $EVENT at $DATE
message: Monit $ACTION $SERVICE at $DATE on $HOST: $DESCRIPTION.
Yours sincerely,
Sriram
}



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 sriram.com 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:
http://www.tildeslash.com/monit/