Monit is utility or package we usually used in Linux machines for managing and monitoring service running in the Linux machines. For example services like HTTPD, MySQL etc. 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. The monit also 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. In this blog we disscussing how we can implement monit on a Centos7 server. The Centos version I am using CentOS Linux release 7.2.1511
[[email protected] ~]# cat /etc/redhat-release CentOS Linux release 7.2.1511 (Core)
At this moment monit is not available in the base repository of Centos 7. So I used EPEL repository to install monit using yum.
wget http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-8.noarch.rpm rpm -ivh epel-release-7-8.noarch.rpm
yum install monit
Now we are going to enable monit web interface by configuring the monit conf file. After enabling we will be having a nice web interface of monit that shows status of services we are monitoring, how long it was up in the server etc. We have added a screenshot for the same at the end of this article. You are refer the same for how exactly it look like.
open /etc/monitrc using vi editer and make below changes.
originally it was like
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'
After changes it will look like
set httpd port 2812 and use address x.x.x.x(server IP) # only accept connection from localhost allow y.y.y.y(Your IP) # allow localhost to connect to the server and allow admin:monit # require user 'admin' with password 'monit'
Restart service using below command
service monit restart
Allow port 2812 in the firewall and verify its listening from outside using telnet commands. In my case I was using csf firewall. So I just need to add port in the csf conf file under TCP_IN and TCP_OUT section.
After that access the monit web interface using the url like http://x.x.x.x:2812
In this section we are going to monitor our server mySQL service by monit and restart it if its failed. Below are the changes.
Create a file named mysql.conf under directory /etc/monit.d/.
Add below entries and make necessary changes as per your server and pid file name and location.
check process mysqld with pidfile /var/lib/mysql/server.namemysql.pid group mysql start program = "/usr/bin/systemctl start mysql.service" stop program = "/usr/bin/systemctl stop mysql.service" if failed host 127.0.0.1 port 3306 then restart if 5 restarts within 5 cycles then timeout
Once added check the syntax using command “monit -t” and you will get result like below.
monit -t Control file syntax OK
restart monit service using below command.
service monit restart
After that refresh the monit web panel and you will see MySQL is monitored by Monit.
In centos 7 we can see monit log from the location tail -f /var/log/monit.log