Monit is an open-source utility used to supervise the processes and restart the services which are configured for it and have failed. Monit supervises the processes and restarts them on failure detection. This tutorial provides the steps required to install Monit on the popular Linux distribution Ubuntu. It provides all the steps required to install and use Monit on Ubuntu 18.04 LTS. The steps should be similar for other Linux systems and Ubuntu versions.
Official Definition
Monit is a small Open Source utility for managing and monitoring Unix systems. Monit conducts automatic maintenance and repair and can execute meaningful causal actions in error situations.
Install Monit
Use the below-mentioned command to install Monit on Ubuntu.
# Install Monit sudo apt-get install monit
# Sample output .... .... Unpacking monit (1:5.25.1-1build1) ... Setting up monit (1:5.25.1-1build1) ... Processing triggers for man-db (2.8.3-2ubuntu0.1) ... Processing triggers for ureadahead (0.100.0-21) ... Processing triggers for systemd (237-3ubuntu10.29) ...
Important Commands
We can use the below-mentioned commands to enable/disable, stop, start, and restart Monit.
# Enable Monit sudo systemctl enable monit
# Disable Monit sudo systemctl disable monit
# Start Monit
sudo systemctl start monit
# OR sudo service monit start
# Stop Monit sudo systemctl stop monit # OR sudo service monit stop
# Restart Monit
sudo systemctl restart monit
# OR sudo service monit restart
# Reload Monit sudo systemctl reload monit # OR sudo service monit reload
Configure Monit
We can configure Monit by updating the configuration file /etc/
# Backup config file sudo cp /etc/monit/monitrc /etc/monit/monitrc.bck
# Update config file - using nano editor sudo nano /etc/monit/monitrc
# Default config
...
... ## Monit has an embedded HTTP interface which can be used to view status of ## services monitored and manage services from a web interface. The HTTP ## interface is also required if you want to issue Monit commands from the ## command line, such as 'monit status' or 'monit restart service' The reason ## for this is that the Monit client uses the HTTP interface to send these ## commands to a running Monit daemon. See the Monit Wiki if you want to ## enable SSL for the HTTP interface. #
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' ... ...
Save and close the editor. Now restart the Monit using the command as mentioned in the previous section.
# Test configuration changes sudo monit -t
# Output Control file syntax OK
# Restart Monit sudo systemctl restart monit
You may also configure Monit to trigger alert emails as shown below. It expects that an email server is already installed on the system. The next section explains how to configure Monit to trigger emails via SMTP.
# Set Alert Email set alert <email> # OR # Set Alert Email only for Security set alert <email> on { checksum, permission, uid, gid }
# Example set alert admin@mydomain.com
We can also configure the alerts to be triggered only for specific failed cycles as shown below.
# Set Alert Email for specific cycle alert <email> with reminder on 5 cycles
Configure SMTP
This section explains how to configure Monit to use the custom email template and trigger the mail using SMTP using Gmail. We can use a similar setup to trigger the emails via any other mail service.
#Mail Template set mail-format { from: <from email> subject: Monit Alert - $EVENT message: $EVENT Service $SERVICE Date: $DATE Action: $ACTION Host: $HOST Description: $DESCRIPTION
Yours, Monit }
# Configure Mail Server
set mailserver smtp.gmail.com port 587
username "<email>" password "<password>" using tlsv1 with timeout 30 seconds
# Configure Alert Receiver set alert <receiver email>
An example of the above-mentioned configuration is as shown below.
#Mail Template set mail-format { from: admin@mydomain.com subject: Monit Alert - $EVENT message: $EVENT Service $SERVICE Date: $DATE Action: $ACTION Host: $HOST Description: $DESCRIPTION Yours, Monit }
# Configure Mail Server set mailserver smtp.gmail.com port 587 username "admin@mydomain.com" password "adminpwd" using tlsv1 with timeout 30 seconds
# Configure Alert Receiver
set alert notify@mydomain.com
This is how we can configure the email template and SMTP to trigger the alert emails.
Summary
This is how we can install the Monit on Ubuntu. It also provided the commonly used commands to start, stop, or restart the Monit service. The last sections explained the steps required to configure Monit and to trigger the emails via SMTP using the customized template.