Using email alerts with Monit


What is Monit? Let’s refresh our knowledge a little bit.

From Monit’s website:

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.

Monit: https://mmonit.com/monit/documentation/monit.html

Adding email alerts

You can find additional posts about Monit on my website, and today we’re going to add email based alerts.

Let’s say we have Redis configuration for Monit:

check process redis-server with pidfile "/var/run/redis/redis-server.pid"
    start program = "/etc/init.d/redis-server start"
    stop program = "/etc/init.d/redis-server stop"
    if failed host 127.0.0.1 port 6379 then restart
    if 5 restarts within 5 cycles then timeout

Let’s add configuration section first into /etc/monit/monitrc:

set mail-format {
  from: monit@yourwebsite.com
  subject: monit alert --  $EVENT
  message: $EVENT Service $SERVICE
                Date:        $DATE
                Action:      $ACTION
                Host:        $HOST
                Description: $DESCRIPTION

           Your
           Monit }
set mailserver smtp.sendgrid.net port 587
  username XXX password "XXX"
  using TLSV1 with timeout 30 seconds
set alert youremail@website.com

And simple restart Monit with:

sudo /etc/init.d/monit restart

Final

Example email with Monit service monitoring alert:

monit_web