How to Install and Configure Monit on Linux Systems
In this article, I’ll show you how to install and configure monit “servers and processes monitoring tool” on Linux systems (RPM Family “Redhat / CentOS / Scientific Linux” and Debian Family “Debian / Ubuntu”). Monit has capability to monitor any service running on a Linux system, We only need to add settings for this service to monit. This article will help you to monitor the local running processes on your Linux machine and try to start the crashed processes.
So, What is Monit?
Monit ( A Process and Services Monitoring Tool) is a small Open Source utility for monitoring and managing Unix like systems. Monit monitors the server programs to increase services up-time and ensures that they stay online consistently. It’ll try to start any crashed service it monitors for a certain number of tries. You can tell Monit exactly what you would do if a program stops running, or begins using too much RAM, or another host becomes unreachable.
With Monit you get:
- Automatic process maintenance in a lightweight package.
- Capability to act on out-of-bounds values for CPU, RAM, disk, file size, age and more.
- Monitoring of running services, and the ability to start, kill or restart.
- Automatic email alerts sent at event triggers.
- Web interface for status monitoring.
- Availability from main package repositories.
Part 1: Installing Monit Server
Monit can be easily installed with package manager in most of Linux flavors.
- On CentOS, RHEL, Fedora and Scientific Linux
We need to enable the epel repository in his system before installing it. run the following commands:
# yum -y install epel-release # yum -y install monit
Now, we need to enable and start monit, run the following commands
On CentOS/RHEL 7
# systemctl enable monit # systemctl start monit
On CentOS/RHEL 6 and earlier
# chkconfig monit on # service monit start
- On Debian / Ubuntu
# apt-get -y install monit
Now, after we installed and run the monit, it’s time to configure it monitor some services.
Part 2: Enabling Web Interface in Monit
Monit also provided an web interface to view services and processes status. To enable monit web interface, edit configuration file ( For CentOS/RHEL 6 based /etc/monit.conf & For CentOS/RHEL 7 based /etc/monitrc & For Dabian Based System /etc/monit/monitrc ) in your favorite editor and uncomment following lines
set httpd port 2812 and use address localhost allow localhost allow admin:monit allow @monit allow @users readonly
And restart monit service.
# service monit restart
As per above configuration monit will start on port 2812 and only accessible from localhost. To change default port simply update first line of above showing configuration and set your preferred port.
Hint: 1. Enabling the web interface is important for monit cli to work, even if you do not have a gui on your server.
Part 3: Configuring Monit To Monitor Services
Now, it’s time to add services to monit. By default when we start monit it’ll monitor the machine it installed on, run the following commands to check the status of monit:
# monit status The Monit daemon 5.14 uptime: 0m System 'SemariLabs00' status Running monitoring status Monitored load average [0.00] [0.00] [0.00] cpu 0.0%us 0.0%sy 0.0%wa memory usage 126.7 MB [12.8%] swap usage 0 B [0.0%] data collected Mon, 17 Oct 2016 12:03:57
Also, run the following command to get the summary of monit:
# monit summary The Monit daemon 5.14 uptime: 0m System 'SemariLabs00' Running
From the above outputs, monit is up, running, and by default monitors the host it installed on.
Let’s start with adding services to Monit. I’ll create a configuration file for each service I need to add to monit
- Monitoring apache web server
On CentOS / RHEL
Create the service configuration file in “/etc/monit.d/” as the following:
# vim /etc/monit.d/apache
Then add the following lines, and save then exit
check process apache with pidfile /var/run/httpd/httpd.pid start program = "/etc/init.d/httpd start" with timeout 60 seconds stop program = "/etc/init.d/httpd stop"
On Dabian / Ubuntu
Create the service configuration file in “/etc/monit/conf.d/” as the following:
# vim /etc/monit/conf.d/apache
Then add the following lines, and save then exit
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"
Now restart monit service, run the following command
# service monit restart
And check the status of service using command line:
# monit summary The Monit daemon 5.14 uptime: 0m Process 'apache' Running System 'SemariLabs00' Running
- Monitoring MySQL/MariaDB database server
On CentOS / RHEL
Create the service configuration file in “/etc/monit.d/” as the following:
# vim /etc/monit.d/mysql
Then add the following lines, and save then exit
check process mysqld with pidfile /var/run/mysqld/mysqld.pid start program = "/etc/init.d/mysql start" stop program = "/etc/init.d/mysql stop"
On Dabian / Ubuntu
Create the service configuration file in “/etc/monit/conf.d/” as the following:
# vim /etc/monit/conf.d/mysql
Then add the following lines, and save then exit
check process mysqld with pidfile /var/run/mysqld/mysqld.pid start program = "/etc/init.d/mysql start" stop program = "/etc/init.d/mysql stop"
Part 4: Verifying and Testing the Configuration
After adding required services in monit monitoring configuration file, Use the below command to verify syntax of file.
# monit -t Control file syntax OK
Now restart monit service
# service monit restart
Now view the summary of monitoring processes and services by Monit.
# monit summary The Monit daemon 5.14 uptime: 0m Process 'mysqld' Running Process 'apache' Running System 'SemariLabs00' Running
To view detailed description of each service use the following command
# monit status
[Sample Output]
# monit status The Monit daemon 5.14 uptime: 0m Process 'mysqld' status Running monitoring status Monitored pid 1210 parent pid 1083 uid 497 effective uid 497 gid 498 uptime 46m children 0 memory 56.2 MB memory total 56.2 MB memory percent 5.6% memory percent total 5.6% cpu percent 0.0% cpu percent total 0.0% data collected Mon, 17 Oct 2016 12:49:19 Process 'apache' status Running monitoring status Monitored pid 1354 parent pid 1 uid 0 effective uid 0 gid 0 uptime 46m children 8 memory 11.0 MB memory total 58.2 MB memory percent 1.1% memory percent total 5.8% cpu percent 0.0% cpu percent total 0.0% data collected Mon, 17 Oct 2016 12:49:19 System 'SemariLabs00' status Running monitoring status Monitored load average [0.00] [0.00] [0.00] cpu 0.0%us 0.0%sy 0.0%wa memory usage 128.3 MB [12.9%] swap usage 0 B [0.0%] data collected Mon, 17 Oct 2016 12:49:19
As you see, we added two services to be monitored with monit and everything is fine
Hints: 1. Take care when adding your own service to populate the service's configuration file with valid locations for pidfile, start program, and stop program. 2. The three needed paths for configuring services is different from a distribution to another.
Summary
In this article, we have explained how to install and configure monit to monitor servers and services. Our installation done on many Linux distributions “CentOS, redhat, Debian, Ubuntu, etc…”. we explained enabling the web interface and the addition of two services to be monitored by monit and if any service crashed, monit will try to bring it up again. Finally, monit can be used to start services at boot time ” I’m using it to start puma ‘application server’ used with ROR at boot time and start it after any crash”.
I hope this article is good enough for you.
See you in other articles.
If You Appreciate What We Do Here On Mimastech, You Should Consider:
- Stay Connected to: Facebook | Twitter | Google+
- Support us via PayPal Donation
- Subscribe to our email newsletters.
- Tell other sysadmins / friends about Us - Share and Like our posts and services
We are thankful for your never ending support.