Introduction to Monit for server monitoring

Contents

Monit is a server monitoring program that can check the processes running on your system to make sure they are always online and running within reasonable CPU and memory limits. It also comes with a web interface to monitor your system.

What does Monit?

Simply, Monit will verify a certain procedure every two minutes (by default) to see how it is working. Keeps track of resource usage and can take action if an error occurs. this includes relaunching blocked processes and restarting processes that use too many resources.

Monit establishes its behavior entirely through configuration files. Here is the default settings for MySQL as an example:

check process mysql
  with pidfile /var/run/mysqld/mysqld.pid
  start program = "/usr/sbin/service mysql start" with timeout 60 seconds
  stop program = "/usr/sbin/service mysql stop" with timeout 60 seconds
  if totalmem > 400 MB for 5 cycles then alert
  if totalmem > 600 MB for 5 cycles then restart
  if cpu > 50% for 5 cycles then alert
  if cpu > 90% for 5 cycles then restart
  if 3 restarts within 5 cycles then timeout

Monit is configured to monitor the running MySQL procedure. If it falls, you can start it again with the start program = config. If MySQL starts using too many resources, can be restarted automatically, even though you will first be warned by email.

It can also be configured to monitor your system as a whole and can alert you if your server is experiencing an unusual load..

check system wp01
  if loadavg(5min) > 1 then alert
  if memory usage > 90% for 5 cycles then alert
  if cpu usage (user) > 90% for 5 cycles then alert

Everything Monit monitors can be viewed from the web interface, running on the port 2812 by default.

At the same time, you can view data from multiple hosts in one place with the Utility M / Monit, you need a license but it's free to try.

RELATED: How to set up Slack notifications for low disk space on your server

Monit installation and configuration

Install Monit from your distribution's package manager; for Debian based systems like Ubuntu, that would be:

sudo apt-get install monit

Monit should come with default settings, generally located in ~/.monitrc. If you didn't (as was the case with a macOS installation), you can find the default configuration files here.

The Monit web interface is configured to run on the port 2812. It's okay, but it is fully open by default with a default password. We want to block you on an authorized IP address and

set httpd port 2812
    allow your_ip
    allow admin:password

Change this password, in any case. There is a some more configuration options for the web interface, but this is good for now.

Subsequently, Monit can be configured to monitor individual processes. In general, this is done with the process pid file (which is often found in /var/run/) that stores the current PID of the instance running that procedure, because the pid will change each time the procedure is restarted.

check process nginx with pidfile /var/run/nginx.pid
    start program = "/etc/init.d/nginx start"
    stop program = "/etc/init.d/nginx stop"

you will need to pass a command to monit to start and stop your procedure. It should restart automatically if the procedure fails, no additional configuration, but if you want to restart based on resource usage, you will have to specify it yourself with some if statements:

check process nginx with pidfile /var/run/nginx.pid
    start program = "/etc/init.d/nginx start"
    stop program = "/etc/init.d/nginx stop"
    if totalmem > 600 MB for 5 cycles then restart
    if cpu > 90% for 5 cycles then restart
    if 3 restarts within 5 cycles then timeout

Monit will send alerts every time something is restarted, so you will be notified when something is wrong.

Once you're done with the setup, reload the monit daemon with:

monit reload

And start monitoring with:

monit start all

This should also start the web interface, which you can use to verify that the Monit is working properly, generally running in port 2812.

This is the basic configuration for Monit to run out of the box, but Monit has much more syntax. You can read the full manual fundamentally what Monit can do to learn more.

Configure alerts

Monit can be configured to send email alerts whenever major issues occur or when processes restart. You will need to add the settings for your mail server:

set mailserver smtp.gmail.com
  port 587
  username "[email protected]"
  password "secret"
  with timeout 60 seconds

set eventqueue
  basedir /var/lib/monit/events
  slots 100

The first block establishes the mail server to use. The simplest method would be to use smtp.gmail.com with a Gmail account, even though you can install Postfix on your server to run locally or use an enterprise email service.

The second line tells Monit to queue the alerts, if the mail server is not reachable for any reason.

Evidently, you must also configure the email address to which you send:

set alert [email protected]

At the same time, since Monit has the ability to run scripts with the exec command, you can set custom alerts as you like. As an example, you can set up a script to send you notifications from Slack, store it as /usr/local/bin/slack-webhook.shand run it whenever nginx changes the PID or is restarted by Monit:

check process nginx with pidfile /var/run/nginx.pid
  start program = "/etc/init.d/nginx start"
  stop program = "/etc/init.d/nginx stop"
  if changed pid then exec "/bin/bash -c 'PROCESS=Nginx /usr/local/bin/slack-webhook.sh'"
  if 1 restart within 1 cycle then exec "/bin/bash -c 'PROCESS=Nginx /usr/local/bin/slack-webhook.sh'"

Please note that all routes must be fully rated, included /bin/bash and paths to executables on your premises bin. And once you are done modifying the config files, you will need to reload Monit.

Subscribe to our Newsletter

We will not send you SPAM mail. We hate it as much as you.