How to Install and Setup Monit Program
To Install and Setup Monit (Linux Process and Services Monitoring) Program
Monit is an utility tool for managing and monitoring processes, programs, files, directories and filesystems on a Unix system. Monit conducts automatic maintenance and repair and can executes causal actions in error situations. This tutorial covers the installation process of monit program.
To Install Monit
First step is to add epel repository, and you can install it by running the following command.
For CentOS/RedHat/Fedora
Run the following yum command to install monit.
# yum install monit
For Debian/Ubuntu/Linux Mint
user1@linuxhelp:~$ sudo apt-get install monit
Reading package lists... Done
Building dependency tree
Reading state information... Done
Suggested packages:
exim4 postfix mail-transport-agent sysvinit-core
The following NEW packages will be installed:
monit
0 upgraded, 1 newly installed, 0 to remove and 37 not upgraded.
Need to get 259 kB of archives.
After this operation, 842 kB of additional disk space will be used.
Get:1 http://in.archive.ubuntu.com/ubuntu/ wily/universe monit amd64 1:5.14-2 [259 kB]
Fetched 259 kB in 1s (138 kB/s)
Selecting previously unselected package monit.
(Reading database ... 176478 files and directories currently installed.)
Preparing to unpack .../monit_1%3a5.14-2_amd64.deb ...
Unpacking monit (1:5.14-2) ...
Processing triggers for man-db (2.7.4-1) ...
Processing triggers for systemd (225-1ubuntu9.1) ...
Processing triggers for ureadahead (0.100.0-19) ...
Setting up monit (1:5.14-2) ...
Processing triggers for systemd (225-1ubuntu9.1) ...
Processing triggers for ureadahead (0.100.0-19) ...
To Configure Monit
It is designed to monitor the running services in every 2 minutes and keeps the logs in “ /var/log/monit“ and it has web interface that runs on port 2812 using web server. Open the config files and make necessary changes to enable it.
To open the config file
Run the below command, to open the config file.
user1@linuxhelp:~$ sudo nano /etc/monit/monitrc
Uncomment the following section and insert the domain name or IP address of your server and permit anyone to connect and modify monit username and password or 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
After configuring, start the monit service to load the new configuration settings.
user1@linuxhelp:~$ sudo /etc/init.d/monit start
Now, you can access the monit web interface by navigating to the “ http://< SERVER-IP> :2812“ or “ http://localhost:2812” . Enter user name as “ admin” and password as “ monit“ .
System Status
Click on the respective domain to know the system status.
Addition of Monitoring Services
Add the programs that are to be monitored into the /etc/monit.conf for (RedHat/CentOS/Fedora) and /etc/monit/monitrc file for (Ubuntu/Debian/Linux Mint) at the bottom.
Here is a list of some useful configuration examples
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"
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 y
Check whether there are any errors in monit syntax.
user1@linuxhelp:~$ sudo monit -t
Control file syntax OK
When getting a message of “ Control file syntax OK“ , or if no errors, you can continue.
To install sshd server
Execute the following command to install the SSH.
user1@linuxhelp:~$ sudo apt-get install ssh
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
libck-connector0 ncurses-term openssh-server openssh-sftp-server ssh-import-id
...
...
Setting up ssh-import-id (4.5-0ubuntu1) ...
Processing triggers for ufw (0.34-2) ...
Setting up ssh (1:6.9p1-2ubuntu0.1) ...
Processing triggers for libc-bin (2.21-0ubuntu4.1) ...
Processing triggers for systemd (225-1ubuntu9.1) ...
Processing triggers for ureadahead (0.100.0-19) ...
To restart the monit service
Execute the below command to restart the monit service.
user1@linuxhelp:~$ sudo /etc/init.d/monit restart
[ ok ] Restarting monit (via systemctl): monit.service.
Monit Process Screen
After the addition of all process for monitoring, Monit Screen looks as follows.
Process Status
Click on the respective process to know the status.