How To Install Monit on Ubuntu 18.04 / Ubuntu 16.04

THIS DOCUMENT IS ALSO AVAILABLE FOR

Monit is an open-source process monitoring tool for Linux operating system that monitors the system processes for problems. Whenever the services or process goes down, Monit will automatically do the maintenance or repair of a particular process (i.e., restarting the service) to bring back online.

Monit can also be used for managing and monitoring of files, directories, and devices for permission changes, timestamps changes, size changes, and checksum changes.

It logs to its log file and notifies the user via customizable email messages.

This guide will help you to install Monit on Ubuntu 18.04 / Ubuntu 16.04.

Install Monit

Update the repository index.

sudo apt update

Monit is available on Ubuntu repositories, so you can use the apt to install it.

sudo apt install -y monit

Configure Monit

The main configuration file of Monit is /etc/monit/monitrc and additional configuration files for process/service are usually placed in /etc/monit/conf-available and /etc/monit/conf-enabled directories.

Edit the main configuration file to configure important parameters of Monit’s to meet our requirements.

sudo nano /etc/monit/monitrc

If you want to change the time intervals between service check, edit the below value.

set daemon 30

Set the mail server information for relaying the alerts generated by Monit.

set mailserver mx.itzgeek.local port 25

Specify an email address on which you want to be alerted in case of any failures/events.

set alert [email protected]

If you only want alert emails for specific events like Timeout and Ping failed events, use the below alert filtering.

set alert [email protected] only on { timeout, icmp }

Check out How to filter an event.

By default, Monit sends only one notification for the failure of service and another one when it recovers. If you want to be alerted on each N cycle if service remains in the failed state.

For example, to get alerts for four cycles, you can use the below settings.

alert [email protected] with reminder on 4 cycles

Enable Monit Web Interface

Monit also comes with an embedded web interface to view the status of services and manage them through a web browser.

By default, the Monit web interface is disabled. To enable it, edit the Monit configuration file /etc/monit/monitrc.

sudo nano /etc/monit/monitrc

Look for the set httpd port 2812 line and change the following entries.

set httpd port 2812 and
     use address 0.0.0.0    # Listen all interface
     allow 0.0.0.0/0        # Allow admin user to login to web interface from anywhere
     allow admin:monit      # require user 'admin' with password 'monit'
     #with ssl {            # enable SSL/TLS and set path to server certificate
     #    pemfile: /etc/ssl/certs/monit.pem
     #}

Restart the Monit service.

sudo systemctl restart monit

Check whether the Monit service is listening correctly on all interfaces.

sudo netstat -antup | grep -i monit
Output:

tcp        0      0 0.0.0.0:2812            0.0.0.0:*               LISTEN      3450/monit

Access Monit Web Interface

Access the web interface by using the below URL.

http://your-ip-addr-ess:2812

You need to use the username as admin and password as monit to access the Monit’s web interface.

Monit Login Page
Monit Login Page

Monit’s home page will look like this.

Monit Dashboard
Monit Dashboard

Click on server.itzgeek.local to see the detailed information of Monit.

Monit Status
Monit Status

You can also use Monit’s command-line interface to see the status of Monit.

sudo monit status
Output:

Monit 5.25.1 uptime: 1m

System 'server.itzgeek.local'
  status                       OK
  monitoring status            Monitored
  monitoring mode              active
  on reboot                    start
  load average                 [0.07] [0.02] [0.01]
  cpu                          0.0%us 0.1%sy 0.1%wa
  memory usage                 177.9 MB [30.7%]
  swap usage                   0 B [0.0%]
  uptime                       22m
  boot time                    Sun, 09 Feb 2020 05:58:39
  data collected               Sun, 09 Feb 2020 06:21:29

Configure Services For Monitoring

Pre-defined Service Configuration Templates

Monit also comes with a pre-defined configuration template for some of process and services. The templates are found in /etc/monit/conf-available/ directory, and you can enable them by creating a symbolic link to /etc/monit/conf-enabled/ directory.

sudo ls -al /etc/monit/conf-available/
Output:

total 72
drwxr-xr-x 2 root root 4096 Feb  9 07:00 .
drwxr-xr-x 6 root root 4096 Feb  9 06:20 ..
-rw-r--r-- 1 root root  481 Nov 28  2017 acpid
-rw-r--r-- 1 root root  640 Nov 28  2017 apache2
-rw-r--r-- 1 root root  455 Nov 28  2017 at
-rw-r--r-- 1 root root  691 Nov 28  2017 cron
-rw-r--r-- 1 root root  602 Nov 28  2017 mdadm
-rw-r--r-- 1 root root  669 Nov 28  2017 memcached
-rw-r--r-- 1 root root  703 Nov 28  2017 mysql
-rw-r--r-- 1 root root  521 Nov 28  2017 nginx
-rw-r--r-- 1 root root  192 Feb  9 07:00 ntp
-rw-r--r-- 1 root root  471 Nov 28  2017 openntpd
-rw-r--r-- 1 root root  950 Nov 28  2017 openssh-server
-rw-r--r-- 1 root root  683 Nov 28  2017 pdns-recursor
-rw-r--r-- 1 root root 1421 Nov 28  2017 postfix
-rw-r--r-- 1 root root  869 Nov 28  2017 rsyslog
-rw-r--r-- 1 root root  501 Nov 28  2017 smartmontools
-rw-r--r-- 1 root root  306 Nov 28  2017 snmpd

Monitor Apache With Monit

Let us monitor the Apache2 service using the pre-defined template provided by Monit.

Symlink the templates to /etc/monit/conf-enabled/ directory.

sudo ln -s /etc/monit/conf-available/apache2 /etc/monit/conf-enabled/

Reload Monit service to take effect of changes.

sudo systemctl reload monit

Access the Monit web interface. You would find the new services that we configured using the template.

Monitor Apache2 Using Monit Templates
Monitor Apache2 Using Monit Templates

Manually Configure Monit for NTP

Configure Monit to start when the NTP daemon dies for any reason. Create the config file called ntp under /etc/monit/conf-available directory.

sudo nano /etc/monit/conf-available/ntp

Append the following config to the above file.

 check process ntp with pidfile /var/run/ntpd.pid
   start program = "/etc/init.d/ntp start"
   stop  program = "/etc/init.d/ntp stop"
   if failed host 127.0.0.1 port 123 type udp then alert

Enable the configuration file symlinking to /etc/monit/conf-enabled/ directory.

sudo ln -s /etc/monit/conf-available/ntp /etc/monit/conf-enabled/

Verify the Monit syntax.

sudo monit -t
Output:

Control file syntax OK

Reload Monit service to take effect of changes.

sudo systemctl reload monit

You should see the NTP service on the web interface.

Monitoring NTP Service
Monitoring NTP Service

Click on the service name to see detailed information about the service.

Status of NTP Service
Status of NTP Service

Test Monit

Now, we will test the action that will be taken by Monit when the service or process dies for no reason.

Stop the ntp daemon.

sudo systemctl stop ntp

Wait for 30 seconds. NTP service will be up and running because of Monit. You can check the Monit log to find out.

sudo tail -f /var/log/monit.log
Output:

[UTC Feb  9 07:01:51] error    : 'ntp' process is not running
[UTC Feb  9 07:01:51] info     : 'ntp' trying to restart
[UTC Feb  9 07:01:51] info     : 'ntp' start: '/etc/init.d/ntp start'
[UTC Feb  9 07:02:21] info     : 'ntp' process is running with pid 6889

Conclusion

That’s All. We have successfully configured Monit on Ubuntu 18.04 / Ubuntu 16.04. We welcome your feedback, please post your valuable comments below.

Prev Post
Next Post
comments powered by Disqus