Monit basics

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the web-development category.

Last Updated: 2024-12-03

Install monit with

apt install monit

Configure with:

vim /etc/monit/montirc
 set alert info@example.co.uk not on { instance, action }

set mailserver smtp.sendgrid.com port 587
        username "myname" password "mypassword"
        using tlsv1
        with timeout 30 seconds
set httpd port 2812
     allow admin:monit

Now visit IP-of-remote-host:2812 entering the combo admin and monit to access

 check system $HOST
    if loadavg (1min) > 4 then alert
    if loadavg (5min) > 2 then alert
    if cpu usage > 95% for 10 cycles then alert
    if memory usage > 75% then alert
    if swap usage > 25% then alert
check process nginx with pidfile /var/run/nginx.pid
start program = "/etc/init.d/nginx start"
stop program = "/etc/init.d/nginx stop"

Get it running as a system daemon

sudo systemctl restart monit
sudo systemctl enable monit

Check its logs

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

One way to check that email actually sends if by creating a test instance

check file alerttest with path /.nonexistent
  alert youradress@example.com with reminder on 500 cycles

Note that it will probably arrive as spam at first, so filter it.