Monitoring with Monit PDF Print E-mail
User Rating: / 0
PoorBest 
Written by Bart Dorlandt   
Tuesday, 07 July 2009 11:01

Some time ago I discovered Monit. It was by accident actually... But what is monit.

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 timestamp changes, checksum changes or size changes. You can also monitor remote hosts; Monit can ping a remote host and can check TCP/IP port connections and server protocols. Monit is controlled via an easy to use 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 and recovery status via customizable alert.

To install Monit:

apt-get install monit

Next to configure monit. I have a very small monitrc configuration in /etc/monit/. I rarely have to change this configuration, because I use the include statement for the other configurations. Here is my monitrc.

set daemon  120
set logfile syslog facility log_daemon
set mailserver localhost
set mail-format { from: This e-mail address is being protected from spambots. You need JavaScript enabled to view it }
set alert root@localhost
set httpd port 2812 and
allow localhost
allow user:password
check system bama.bamweb.nl
#if loadavg (1min) > 5 then alert
#if loadavg (5min) > 3 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
include /etc/monit.d/*

Because my machine isn't state of the art, I've disabled some checks. Else I get alerts every time my backup runs.

Next are the configs that are being included. These are located at /etc/monit.d/ . You need to create this directory yourself.

apache

###############################################################################
## Monit control file -- Web related
###############################################################################
## Check that a process is running, in this case Apache, and that it respond
## to HTTP and HTTPS requests. Check its resource usage such as cpu and memory,
## and number of children. If the process is not running, monit will restart
## it by default. In case the service was restarted very often and the
## problem remains, it is possible to disable monitoring using the TIMEOUT
## statement. This service depends on another service (apache_bin) which
## is defined above.

check process apache2 with pidfile /var/run/apache2.pid
start program = "/etc/init.d/apache2 start"
stop program = "/etc/init.d/apache2 stop"
if cpu > 60% for 2 cycles then alert
if cpu > 80% for 5 cycles then restart
if totalmem > 300.0 MB for 5 cycles then restart
if children > 250 then restart
if loadavg(5min) greater than 10 for 8 cycles then stop
if failed host www.bamweb.nl port 80 protocol http
# and request "/monit/doc/next.php" then restart
and request "/images/stories/bart/bart_hoofd.jpg" then restart
# if failed port 443 type tcpssl protocol http
# with timeout 15 seconds then restart
if 3 restarts within 5 cycles then timeout
# depends on apache_bin
group server

ftp (not using it yet)

###############################################################################
## Monit control file -- FTP related
###############################################################################

#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
# if 5 restarts within 5 cycles then timeout

mail

###############################################################################
## Monit control file -- Mail related
###############################################################################

check process postfix with pidfile /var/spool/postfix/pid/master.pid
group mail
start program = "/etc/init.d/postfix start"
stop program = "/etc/init.d/postfix stop"
if failed port 25 protocol smtp then restart
if 5 restarts within 5 cycles then timeout

check process dovecot with pidfile /var/run/dovecot/master.pid
start program "/etc/init.d/dovecot start"
stop program "/etc/init.d/dovecot stop"
if failed host 127.0.0.1 port 143 then restart
if 5 restarts within 5 cycles then timeout


check process amavisd with pidfile /var/run/amavis/amavisd.pid
start program "/etc/init.d/amavis start"
stop program "/etc/init.d/amavis stop"
if failed host 127.0.0.1 port 10024 then restart
if 5 restarts within 5 cycles then timeout

check process postgrey with pidfile /var/run/postgrey.pid
start program "/etc/init.d/postgrey start"
stop program "/etc/init.d/postgrey stop"
if failed host 127.0.0.1 port 60000 then restart
if 5 restarts within 5 cycles then timeout

mysql

###############################################################################
## Monit control file -- MySQL related
###############################################################################

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
if 5 restarts within 5 cycles then timeout

ssh

###############################################################################
## Monit control file -- SSH related
###############################################################################

check process sshd with pidfile /var/run/sshd.pid
start program "/etc/init.d/ssh start"
stop program "/etc/init.d/ssh stop"
if failed port 22 protocol ssh then restart
if 5 restarts within 5 cycles then timeout

After this is done and you tuned it to your needs, you may (re)start monit. After setting the value to 1 in the defaults directory

edit /etc/default/monit. Change the value of startup to 1, like below.

startup=1

Now you may restart monit.

/etc/init.d/monit restart

Now, you are able to see the webpage as well, from here you can also manage your processes.

proxy to apache

I already had apache running and therefore I use the command proxypass to connect to this webserver via apache. To do this add the following lines to your virtual host.

        ProxyPass /monit-manager/ http://localhost:2812/
ProxyPassReverse /monit-manager/ http://localhost:2812/

allow from all

Now you can access it by going to your virtualhost/monit-manager.

Now I know about it, I have no system running without it. Thanks monit.

Extra info: howtoforge

 

 

 

Last Updated on Wednesday, 22 July 2009 13:22
 


Related items: