Did You Know?
Got a tip? Let us know

LFCS - Linux Foundation Certified SysAdmin - Exam Preparation Guide

How to Install and Setup Monit (Linux Process and Services Monitoring) Program

Download Your Free eBooks NOW - 10 Free Linux eBooks for Administrators
CW

Monit is a free open source and very useful tool that automatically monitors and manages server process, files, directories, checksums, permissions, filesystems and services like Apache, Nginx, MySQL, FTP, SSH, Sendmail and so on in a UNIX/Linux based systems and provides an excellent and helpful monitoring functionality to system administrators.

The monit has user friendly web interface where you can directly view the system status and setup up processes using native HTTP(S) web server or via the command line interface. This means you must have web server like Apache or Nginx installed on your system to access and view monit web interface.

Read Also : 10 Linux Performance Monitoring Tools

What Monit can do

Monit has a ability to start a process if it is not running, restart a process if not responding and stop a process if uses high resources. Additionally you can also use Monit to Monitor files, directories and filesystems for changes, checksum changes, file size changes or timestamp changes. With Monit you can able to monitor remote hosts TCP/IP port, server protocols and ping. Monit keeps its own log file and alerts about any critical error conditions and recovery status.

This article is written to describe a simple guide on Monit installation and configuration on a RHEL, CentOS, Fedora, Ubuntu, Linux Mint and Debian Linux Operating Systems, but it should be easily compatible to Scientific Linux as well.

Step 1: Installing Monit

By default, Monit tool is not available from the system base repositories, you need to add and enable third party epel repository to install monit package under your RHEL/CentOS systems. Once you’ve added epel repository, install package by running the following yum command. For Ubuntu/Debian/Linux Mint user’s can easily install using apt-get command as shown.

On RedHat/CentOS/Fedora/
# yum install monit
On Ubuntu/Debian/Linux Mint
$ sudo apt-get install monit

Step 2: Configuring Monit

Monit is very easy to configure, in fact the configuration files are created to be very easily readable and making them easier for users to understand. It is designed to monitor the running services in every 2 minutes and keeps the logs in “/var/log/monit“.

Monit has it’s web interface that runs on port 2812 using web server. To enable web interface you need to make changes in monit configuration file. The main configuration file of monit located at /etc/monit.conf under (RedHat/CentOS/Fedora) and /etc/monit/monitrc file for (Ubuntu/Debian/Linux Mint). Open this file using your choice of editor.

# vi /etc/monit.conf
$ sudo vi /etc/monit/monitrc

Next, uncomment the following section and add the IP address or domain name of your server, allow anyone to connect and change monit user and password or you can use default ones.

 set httpd port 2812 and
     use address localhost  # only accept connection from localhost
     allow localhost        # 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

Once you’ve configured it, you need to start the monit service to reload the new configuration settings.

# /etc/init.d/monit start
$ sudo /etc/init.d/monit start

Now, you will able to access the monit web interface by navigating to the “http://localhost:2812” or “http://example.com:2812“. Then enter user name as “admin” and password as “monit“. You should get screen similar to below.

Install Monit in Fedora

Monit Web Interface

Step 3: Adding Monitoring Services

Once monit web interface correctly setup, start adding the programs that you want to monitor into the /etc/monit.conf under (RedHat/CentOS/Fedora) and /etc/monit/monitrc file for (Ubuntu/Debian/Linux Mint) at the bottom.

Following are some useful configuration examples for monit, that can be very helpful to see how a service is running, where it keeps its pidfile and how to start and stop a service etc.

Apache
check process httpd with pidfile /var/run/httpd.pid
group apache
start program = "/etc/init.d/httpd start"
stop program = "/etc/init.d/httpd stop"
if failed host 127.0.0.1 port 80
protocol http then restart
if 5 restarts within 5 cycles then timeout
Apache2
check process apache with pidfile /run/apache2.pid
start program = "/etc/init.d/apache2 start" with timeout 60 seconds
stop program  = "/etc/init.d/apache2 stop"
Nginx
check process nginx with pidfile /var/run/nginx.pid
start program = "/etc/init.d/nginx start"
stop program = "/etc/init.d/nginx stop"
MySQL
check process mysqld with pidfile /var/run/mysqld/mysqld.pid
group mysql
start program = "/etc/init.d/mysqld start"
stop program = "/etc/init.d/mysqld stop"
if failed host 127.0.0.1 port 3306 then restart
if 5 restarts within 5 cycles then timeout
SSHD
check process sshd with pidfile /var/run/sshd.pid
start program "/etc/init.d/sshd start"
stop program "/etc/init.d/sshd stop"
if failed host 127.0.0.1 port 22 protocol ssh then restart
if 5 restarts within 5 cycles then timeout

Once you’ve configured all programs for monitoring, check monit syntax for errors. If found any errors fix them, it’s not so tough to figure out what’s went wrong. When you get message like “Control file syntax OK“, or if you see no errors, you can proceed ahead.

# monit -t
$ sudo monit -t

After fixing all possible errors, you can type the following command to stat the monit service.

# /etc/init.d/monit restart
$ sudo /etc/init.d/monit restart

You can verify that monit service is started by checking log file.

# tail -f /var/log/monit
$ sudo tail -f /var/log/monit.log
Sample Output
[BDT Apr  3 03:06:04] info     : Starting monit HTTP server at [localhost:2812]
[BDT Apr  3 03:06:04] info     : monit HTTP server started
[BDT Apr  3 03:06:04] info     : 'tecmint.com' Monit started
[BDT Apr  3 03:06:04] error    : 'nginx' process is not running
[BDT Apr  3 03:06:04] info     : 'nginx' trying to restart
[BDT Apr  3 03:06:04] info     : 'nginx' start: /etc/init.d/nginx
Monit Screenshot

This is how looks monit after adding all process for monitoring.

Monit Monitoring Process

Monit Monitoring All Process

Reference Links

  1. Monit Home Page
  2. Monit Documentation
  3. Monit Configuration Examples

Ravi Saive

Owner at TecMint.com
Simple Word a Computer Geek and Linux Guru who loves to share tricks and tips on Internet. Most Of My Servers runs on Open Source Platform called Linux.

Linux Services & Free WordPress Setup

Our post is simply ‘DIY’ aka ‘Do It Yourself, still you may find difficulties and want us to help you out. We offer wide range of Linux and Web Hosting Solutions at fair minimum rates. Please submit your orders by Clicking Here.

27 Responses

  1. Babu says:

    Greate .

  2. Alan says:

    Great article, I just implemented this on a few VM’s. Thank you very much!

  3. Kevin says:

    Pretty cool. Is it possible to monitor a bunch of servers on the front page?

    • Ravi Saive says:

      Yes! you can monitor bunch of server by simply creating each block for each service, like this.

      check host example.com with address
      172.16.25.125
      if failed port 80 protocol httpd with timeout 15 seconds
      then exec "/usr/bin/ssh root@example.com /etc/init.d/httpd restart"
      
  4. Kevin says:

    And of course I see the M/Monit banner just after I hit send :)

  5. Aswin Roy says:

    Can you give me the code to monitor MySQL on another computer on the same network?
    I am using this code:

    check host remote with address 192.168.0.187
    if failed icmp type echo count 3 with timeout 3 seconds then alert
    if failed port 3306 protocol mysql with timeout 15 seconds then alert

    But, connection failed. It says “cannot open connection to INET via TCP.” What to do?

    • Ravi Saive says:

      The code is already given above, just replace IP there and make sure port 3306 is opened on that remote machine.

  6. Gopi says:

    How to monitor remote hosts….?

  7. Gopi says:

    Thank You very much..
    How to add multiple mysql remote services

  8. Moose says:

    Hi,

    I have an issue to access monit GUI after start the monit service.

    In monit.conf i keep the config file as default

    set httpd port 2812 and
    use address localhost # only accept connection from localhost
    allow localhost # 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

    And try to access using server ip i.e: 10.1.30.3:2812 but the page is unable to connect

    In /var/log/monit it shows denied connection from non-authorized client

    Do u have any idea on above issue?

    Ur help is highly appreciated

  9. Madan says:

    Hi,

    I have issue with my monit configuration. When I include specific file to include in the monitrc file it does not start but if I include the fulle directory like “include /etc/monit.d/*”. It work perfectly.

    Pleass advise.

    Thanks.

  10. venkat says:

    i monitored all services using monit tool except
    tomcat server i create tomcat as service also but process Id not created how to monitor tomcat using monit tool this is i am using tomcat script

    #!/bin/bash
    # description: Tomcat Start Stop Restart
    # processname: tomcat
    # chkconfig: 234 20 80
    JAVA_HOME=/usr/sunjdk/jdk1.7.0_25
    PATH=$PATH:$HOME/bin:$JAVA_HOME/bin
    export JAVA_HOME
    export PATH
    CATALINA_HOME=/tomcat-7.0.27/

    case $1 in
    start)
    sh /tomcat-home/bin/startup.sh
    ;;
    stop)
    sh /tomcat-home/bin/shutdown.sh
    ;;
    restart)
    sh /tomcat-home/bin/shutdown.sh
    sh /tomcat-home/bin/startup.sh
    ;;
    esac
    exit 0

    please guide me

  11. feroz says:

    how to install it on server i have logged into my server using ssh, installed the monit , the monit status command showing everything fine in the ssh console,but i am unable to access the web panel from browser as http://www.abasc.com:2812 nothing happes just keep on loading

  12. imran says:

    I have written an article for moit and mmonit, to control your monit instance through web interface. hope this will help some one.

    http://techinfozone.net/install-configure-monit-linux-server/
    http://techinfozone.net/install-configure-mmonit-rhel-x86_64-step-setp/

  13. Brent says:

    Very nice. I’m quite new to linux and this tutorial got me through the basics without getting bogged down in all the different things that you can use monit for.
    I appreciated you specifying the commands for the different linux versions. Often tutorials on websites assume you are using a certain flavor of linux, but never mention what that flavor is.

  14. Mann says:

    how to monitor windows machine with monit ???

  15. Mann says:

    check system is not working for the remote host neither the running services are showing up in Monit window

  16. Al says:

    I am unable to access the webUI page and the message I am getting is “The connection was interrupted”and firewall is disabled already. below is my configuration:

    set httpd port 2812 and
    use address 172.16.172.200 # only accept connection from localhost
    allow 172.16.172.200 # 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

    Any help would be appreciated

  17. Jean says:

    Hi Ravi,

    Thx for this post. It really helps. i’m using Ubuntu 12.10 server, and i was wondering if it should be “mysql” instead of “mysqld” on the third and fourth row for the MySql monitoring services :

    check process mysqld with pidfile /var/run/mysqld/mysqld.pid
    group mysql
    start program = “/etc/init.d/mysqld start”
    stop program = “/etc/init.d/mysqld stop”
    if failed host 127.0.0.1 port 3306 then restart
    if 5 restarts within 5 cycles then timeout

    Because when i use “mysqld” an error occur.

    thx a lot

  18. ejaz says:

    can you send me step by step installation monit. on Ubuntu 12.04 32 desktop

    • Ravi Saive says:

      The given instructions also works on Ubuntu 12.04 32-bit Desktop. Have you tried these instructions?

Leave a Reply

This work is licensed under a (cc) BY-NC | TecMint uses cookies. By using our services, you comply to use of our cookies. More info: Privacy Policy.
© 2012-2014 All Rights Reserved.