(Last Updated On: June 7, 2018)

Monit is an open source program used to monitor services on Linux systems and ensure they are always online. If for any reason the program shuts down, monit will try to bring it back online and make it stay online consistently.

Monit also comes with a web interface that you can use to control and monitor the state of your application/process. In this guide, we’ll cover installation and configuration of monit on Ubuntu 18.04.

Installing Monit on Ubuntu 18.04

Monit package is available on official Ubuntu repositories. Install it using the command:

$ sudo apt-get install monit

To start the process, use systemctl command available on Ubuntu 18.04

$ sudo systemctl start monit

Check if the service is running:

$ sudo systemctl status monit
● monit.service - LSB: service and resource monitoring daemon
   Loaded: loaded (/etc/init.d/monit; generated)
   Active: active (running) since Wed 2018-06-06 19:29:37 UTC; 1min 45s ago
     Docs: man:systemd-sysv-generator(8)
    Tasks: 1 (limit: 4704)
   CGroup: /system.slice/monit.service
           └─21382 /usr/bin/monit -c /etc/monit/monitrc

Jun 06 19:29:37 ubuntu18.04.computingforgeeks.com systemd[1]: Starting LSB: service and resource monitoring daemon...
Jun 06 19:29:37 ubuntu18.04.computingforgeeks.com monit[21366]:  * Starting daemon monitor monit
Jun 06 19:29:37 ubuntu18.04.computingforgeeks.com monit[21366]:    ...done.
Jun 06 19:29:37 ubuntu18.04.computingforgeeks.com systemd[1]: Started LSB: service and resource monitoring daemon.

Monit configuration files are located under /etc/monit/ directory.The main configuration file is /etc/monit/monitrc. This file is highly commented out, you can reference it for all configs. By default all files located on /etc/monit/conf.d/ and /etc/monit/conf-enabled/ are read by monit when it is started, you can place your process monitoring configurations on this directory to keep things organized.

Enable Monit HTTP interface

Monit has an embedded HTTP interface which can be used to view the status of services monitored and manage services from a web interface. By default monit HTTP interface is not enabled, enable it by uncommenting the following lines on  /etc/monit/monitrc file.

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'

You can change admin:monit with username and password you want to use. To allow access from a different IP, add it like below:

allow 192.268.1.20

Restart monit after making the change:

$ sudo systemctl restart monit
or
$ sudo monit reload

Check the status of Monit

Typing monit status command will display details about monit status.

$ sudo monit status
Monit 5.25.1 uptime: 0m

System 'ubuntu18.04.computingforgeeks.com'
  status                       OK
  monitoring status            Monitored
  monitoring mode              active
  on reboot                    start
  load average                 [0.00] [0.01] [0.00]
  cpu                          0.0%us 0.0%sy 0.0%wa
  memory usage                 513.8 MB [13.0%]
  swap usage                   0 B [0.0%]
  uptime                       4d 21h 31m
  boot time                    Fri, 01 Jun 2018 22:25:13
  data collected               Wed, 06 Jun 2018 19:56:00

To check configuration files syntax, use:

# monit -t
Control file syntax OK

Check monitored processes summary:

# monit  summary
Monit 5.25.1 uptime: 6m
┌─────────────────────────────────┬────────────────────────────┬───────────────┐
│ Service Name                    │ Status                     │ Type          │
├─────────────────────────────────┼────────────────────────────┼───────────────┤
│ ubuntu18.04.computingforgeek... │ OK                         │ System        │
└─────────────────────────────────┴────────────────────────────┴───────────────┘

Monitoring Processes/Programs with Monit

I’ll do a demo of how to monitor a process with monit. On my local machine, I have apache2 and MariaDB running programs. I’m going to create a custom configuration file for the two on /etc/monit/conf.d/custom.conf.

# Apache configuration
check process apache2 with pidfile /run/apache2/apache2.pid
    start program = "/bin/systemctl start apache2" with timeout 60 seconds
    stop program  = "/bin/systemctl stop apache2"

# MariaDB configuration
#
check process mariadb with pidfile /run/mysqld/mysqld.pid
    start program = "/bin/systemctl start mariadb" with timeout 60 seconds
    stop program  = "/bin/systemctl stop mariadb"

# Docker configuration
#
check process docker with pidfile /run/docker.pid
    start program = "/bin/systemctl start docker" with timeout 60 seconds
    stop program  = "/bin/systemctl stop docker"

Note that “start program” and “stop program” command needs to be an absolute path, e.g /bin/systemctl instead of systemctl.

Check for syntax errors after making the change:

# monit -t
Control file syntax OK

If all is Okay, reload monit configuration file for it to read new changes.

# monit reload
Reinitializing monit daemon

Now check the summary of process list being monitored:

~# monit summary
Monit 5.25.1 uptime: 41m
┌─────────────────────────────────┬────────────────────────────┬───────────────┐
│ Service Name                    │ Status                     │ Type          │
├─────────────────────────────────┼────────────────────────────┼───────────────┤
│ ubuntu18.04.computingforgeek... │ OK                         │ System        │
├─────────────────────────────────┼────────────────────────────┼───────────────┤
│ apache2                         │ OK                         │ Process       │
├─────────────────────────────────┼────────────────────────────┼───────────────┤
│ mariadb                         │ OK                         │ Process       │
├─────────────────────────────────┼────────────────────────────┼───────────────┤
│ docker                          │ OK                         │ Process       │
└─────────────────────────────────┴────────────────────────────┴───────────────┘

To start running all of the monitored programs.

# monit start all

Access Monit Web Interface:

To access the web interface use the URL:

http://[ip-address|domain]:2812

Login with the username as “admin” and password as “monit”.

To allow access to port from remote IP addresses on the firewall, run:

# ufw allow 2812
Rule added
Rule added (v6)

Monit’s web interface looks like this.

Click on the service name to get process details including stop and start buttons: