Monitoring and restart server services (Apache, Mysql, Bind) with Monit to prevent server downtimes

Monit Daemon Service Logo

If you’re a system administrator responsible for a server running a heavy loaded high traffic website equipped with Apache, MySQL, Bind, Spamassassin .. you name the service, you have probably already faced the stressful situations whenever unexpected server services crashes occur.

You should already know by experience that whenever an Apache or Database server goes down especially on a well known respected website, then you’re into real shit.

In my prefessional work experience as a system admin some time ago I found a handy small program written especially to save you the many nerves and worries, of unexpected server services failures caused by an unexpected running server software bugs.

The great program able to monitor your specified system services is called Monit

Monit is specially written with the idea in mind to track daemon, processes, files, devices and even a remote systems.

What makes monit a really superb and a desireful piece of software to install on each and every software you take care of is it’s ability to trigger certain activities whenever some critical problems with monitor system files are missing, daemons are not runnign any more, some kind of critical system overload occurs and even almost everything bad you can imagine could happen could be previously foreseen and described from the monit configuration file monitrc

For instance if your MySQL server is crashing every now and then without logging any meaningful to its log file, and yet your site users have to wait before it gets restarted by you, you can easily automate that using monit.

Monit is also built with a small daemon listening on a port that is able to show you the monitored services state, it’s also capable of sending altert emails in case if critical predefined occurances.

Monit is even able to be installed on a number of server nodes and be used directly from a common web interface and therefore a management of all server services from just one interface is possible.
This is especially handy if you’re a system administrator responsible for 10+ servers like I used to be some time ago.

Another handy example of whenever using monit would be of a immesurable extra benefit is for instance if you have a server where you have a dying SSH server every now and then on your dedicated server located some 100 000 km away.

If you have properly installed and configure monit and this kind of emergency situation occurs monit will handle the situation and restart your SSHD daemon ASAP as it realizes the sshd daemon is not properly running on your server.

In this article I’m about to explain how to install and configure monit on a Debian GNU/Linux.
The install and configure procedure should be most likely also compatible with Ubuntu and any other Debian based distribution like Sidux, on Redhat based Linuxes the steps to be followed might be a bit different but yet easily adapted from the one I explain here.

1. Install the monit deb package on your Debian serverdebian-server:~# apt-get install monit

2. Configure monit to enabled as a daemon and start up on boot

– Edit /etc/default/monit :

By default /etc/default/monit looks like so:

# Defaults for monit initscript
# sourced by /etc/init.d/monit
# installed at /etc/default/monit by maintainer scripts
# Fredrik Steen <stone@debian.org>

# You must set this variable to for monit to start
startup=0

# To change the intervals which monit should run uncomment
# and change this variable.
# CHECK_INTERVALS=180

Change in the above /etc/default/monit configuration:

startup=1

This is necessary otherwise the monit daemon won’t start up.

3. Download my monitrc already configured to track and Restart MySQL, Apache and Bind

Apart from monitoring MySQL, Apache and Bind, with this configuration monit is configured to alert on an emailthat needs to be configured from /etc/monit/monitrc .

It also checks for free disk space availability dangerously decreasing as well as for high load avarage and critical low levels of memory usage.

You will have to modify the monitrc provided and substitute the my default domain pc-freak.net with whatever is the name of yours.

Some other minor modifations to the conf file might be necessary so review it before proceeding with running up the monit daemon.

You can either download a working copy of monitrc or directly execute on your server:

debian-server:~# cd/etc/monit
debian-server:/etc/monit # mv monitrc monitrc.orig
debian-server:/etc/monit # wget http://www.pc-freak.net/files/monitrc

After a minor modifications to monitrc you should be ready to start up the monit service and it will monitor and alert whenever it matches any of the configuration prerogative.

4. Test monit configuration

To test if your prepared monitrc has a valid syntax issue:


debian-server:~# monit -t

5. Start up the monit service

debian-debian-server:~# /etc/init.d/monit start

6. Accessing Monit Web interface

Monit Web interface will run on the port number 2812.If you have any firewall preventing acccess to 2812 make sure you allow access to the port in order to be able to access it.

Now point your browser to http://www.yourdomain.com:2812/.
Log in with admin and test, and you should see the monit web interface

Having a monit on your server can assure you a better both server and services uptime as well as inform you of a possible approaching problems, the uses of monit to track and react on certain unexpected common troubles comes up to your imagination and creativity.


Share this on

Download PDFDownload PDF

Tags: , ,

12 Responses to “Monitoring and restart server services (Apache, Mysql, Bind) with Monit to prevent server downtimes”

  1. admin says:
    Epiphany 2.29.92 Epiphany 2.29.92 Debian GNU/Linux x64 Debian GNU/Linux x64
    Mozilla/5.0 (X11; U; Linux x86_64; en-us) AppleWebKit/531.2+ (KHTML, like Gecko) Safari/531.2+ Debian/squeeze/sid () Epiphany/2.29.92

    I have to say that the /etc/monit/monitrc configuration for apache is configured in a way that a request is made each time to /monit/token and if Apache is not responding it is predetermined to restart Apache. This kind of check could prevent many oddities when your Apache will be able to bind a TCP socket but won’t return any content to the client.

    View CommentView Comment
  2. Osi Port Numbers says:
    Firefox 3.8 Firefox 3.8 Ubuntu 9.25 Ubuntu 9.25
    Mozilla/5.0 (X11; U; Linux i686; it-IT; rv:1.9.0.2) Gecko/2008092313 Ubuntu/9.25 (jaunty) Firefox/3.8

    I am changing vocations and would like to be a Network Administrator of some variety. I have a knight bachelor degree, non IT, I was wondering the best route to take to take Some kind of Certification course, or is a degree program necessary? Thank all the best all the best.

    View CommentView Comment
  3. Backgammon Doubling says:
    Firefox 3.5.3 Firefox 3.5.3 Windows XP Windows XP
    Mozilla/5.0 (Windows; U; Windows NT 5.1; pl; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3

    excelenta enticos de centinso y pladeis con ssemo froglo. umpra a aramoss y amombralo onicalt con cotos tameria!

    View CommentView Comment
  4. Tragaperra King Cash says:
    Firefox 3.5.3 Firefox 3.5.3 Windows XP Windows XP
    Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3 (.NET CLR 3.5.30729)

    super ingen der geschlu zu mebell mit ausspru und aufphant. ammspeing den jehen des estung ohne vonzeis, ausert, gnaft oder zeneckt.

    View CommentView Comment
  5. admin says:
    Firefox 3.6.3 Firefox 3.6.3 Windows 7 Windows 7
    Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3

    monit rulz 🙂

    View CommentView Comment
  6. admin says:
    Firefox 3.6.3 Firefox 3.6.3 Windows 7 Windows 7
    Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3

    One little note to make here:

    /var/run/apache2.pid
    The monit rc config as explainedf in step 3 is configured to monitor (alert and restart) BIND (Named) DNS resolver server

    If wherever you configure monit you have no don’t use bind, comment inside my sample monitrc:


    check process named with pidfile /var/run/bind/run/named.pid
    start program = "/etc/init.d/bind9 start"
    stop program = "/etc/init.d/bind9 stop"
    if failed host 127.0.0.1 port 53 type tcp protocol dns then restart
    if failed host 127.0.0.1 port 53 type udp protocol dns then restart
    if 5 restarts within 5 cycles then timeout


    ##check process named with pidfile /var/run/bind/run/named.pid
    ## start program = "/etc/init.d/bind9 start"
    ## stop program = "/etc/init.d/bind9 stop"
    ## if failed host 127.0.0.1 port 53 type tcp protocol dns then restart
    ## if failed host 127.0.0.1 port 53 type udp protocol dns then
    restart
    ## if 5 restarts within 5 cycles then timeout

    View CommentView Comment
  7. admin says:
    Firefox 3.6.3 Firefox 3.6.3 Windows 7 Windows 7
    Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3

    Also as I just installed monit service on one host with Debian Squeeze I can confirm. Config is working fine with Debian Squeeze shipped monit version:

    pcfreak:~# dpkg -l |grep ‘^ii monit’
    ii monit 1:5.1.1-1 A utility for monitoring and managing daemons or similar programs

    View CommentView Comment
  8. admin says:
    Firefox 3.6.3 Firefox 3.6.3 Windows 7 Windows 7
    Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3

    Also i’ve figured out later. This alerts as I configured in this article might be sometimes really annoying if you receive e-mails every few seconds. Hence it might be good idea to comment alerts in my monitrc sample conf:

    To do so, remove or comment lines in Apache and MySQL monit config section:


    ## if cpu usage is greater than 80 percent for 3 cycles then alert


    ## if cpu is greater than 50% for 2 cycles then alert

    View CommentView Comment
  9. admin says:
    Firefox 3.6.3 Firefox 3.6.3 Windows 7 Windows 7
    Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3

    Also the monitrc config from my article is configured to check and report for “disk full” and low disk space on /dev/md0 which used to be a software mirror type RAID1. Therefore config in monitrc reads:


    check device disk1 with path /dev/md0

    On other non-RAIDed disks, usually the disk to monitor is /dev/sda1 or /dev/sdb1

    Thus if you’re not using a hardware or software mirror probably this config section should be substituted with:
    check device disk1 with path /dev/sda1, i.e.:


    check device disk1 with path /dev/sda1

    View CommentView Comment
  10. admin says:
    Firefox 30.0 Firefox 30.0 Windows 7 x64 Edition Windows 7 x64 Edition
    Mozilla/5.0 (Windows NT 6.1; WOW64; rv:30.0) Gecko/20100101 Firefox/30.0

    If you have more than one server that you need to monitor, then you can use M/Monit– an extended version of Monit that provides a simple way to monitor multiple machines.

    View CommentView Comment

Leave a Reply

CommentLuv badge