How to monitor and restart services with Monit

In this article, we will show you how to install and configure Monit on Ubuntu 16.04. Monit is an open source tool for monitoring and managing, processes, files, directories and file systems on a UNIX system. You can use Monit to stop or restart your web server if it is using too much resources, to monitor daemon processes, to monitor files, directories and get an email alert or perform a predefined action if they change. With Monit you can also monitor your network connections, CPU usage, RAM memory load average and much more. This guide should work on other Linux VPS systems as well but was tested and written for an Ubuntu 16.04 VPS.

Login to your VPS via SSH

ssh my_sudo_user@my_server

Install Monit

You can install the monit package from the official Ubuntu repositories by issuing the following command:

sudo apt install monit

If you want to enable the Monit HTTP interface open the /etc/monit/monitrc file and uncomment the following lines:

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'

Reload the Monit service with the following command:

monit reload

and you should be able to login to the Monit web interface at : localhost:2812

You can now configure Monit to monitor your processes, file systems and resources by editing the /etc/monit/monitrc  file.

Below are some example configurations:

Nginx

check process nginx with pidfile /run/nginx.pid
    start program = "systemctl start nginx" with timeout 30 seconds
    stop program  = "systemctl stop nginx"
    if cpu usage > 80% for 5 cycles then alert
    if failed port 80 protocol http then restart
    if 5 restarts within 10 cycles then timeout

PHP-FPM

check process php-fpm with pidfile /run/php/php7.0-fpm.pid
    start program = "systemctl start php7.0-fpm" with timeout 30 seconds
    stop program  = "systemctl stop php7.0-fpm"
    if cpu usage > 80% for 5 cycles then alert
    if failed unixsocket /run/php/php7.0-fpm.sock then restart
    if 5 restarts within 5 cycles then timeout

MariaDB

check process mariadb with pidfile /run/mysqld/mysqld.pid
    start program = "systemctl start mariadb" with timeout 30 seconds
    stop program  = "systemctl stop mariadb"
    if cpu usage > 80% for 5 cycles then alert
  	if failed host 127.0.0.1 port 3306 then restart
	if 5 restarts within 5 cycles then timeout

File size

check file syslog with path /var/log/syslog
	if size > 50 MB then alert

Filesystem

check filesystem "sda1" with path /dev/sda1
	if space usage > 95% for 10 cycles then alert

That’s it. You have successfully installed and configured Monit on your Ubuntu 16.04 VPS. For more information about Monit, please refer to the official Monit documentation.


Of course, you don’t have to do any of this if you use one of our Linux VPS Hosting services, in which case you can simply ask our expert Linux admins to setup this for you. They are available 24×7 and will take care of your request immediately.

PS. If you liked this post please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.

Leave a Comment