Gian Saß

Monit: A Small Introduction

· Gian Sass

Monit is an awesome server monitoring tool that I have used for quite a while and recommend. What Monit does great is monitoring processes and services, so that in a way your server becomes self-healing. Monit can also notify you by email if unfortunate things happen. It is very easy to install and to set up. Let’s take a look!

1. Installation
sudo apt-get install monit

Edit the config file /etc/monit/monitrc. Here you may set up services, your email, and other things.

You should first set up the admin credentials, it is also important to enable SSL but more on that in just a bit.

set httpd port 2812 and
 use address localhost
 allow localhost
 allow admin:"YOUR_PASSWORD"
1.1 Set up services

Monit has a very straightforward syntax. Below you see an example on how to monitor an MySQL process, including port checks. Monit will always need a start and stop script in order to restart your processes.

check process mysqld with pidfile /var/run/mysqld/mysqld.pid
  group database
  start program = "/etc/init.d/mysql start"
  stop program = "/etc/init.d/mysql stop"
  if failed port 3306 protocol mysql then restart

You can also monitor files.

check file passwd path /etc/passwd
  if changed checksum then alert

And external hosts.

check host example.com with address example.com
 if failed url https://example.com timeout 10 seconds for 1 cycles then alert

Look here for more configuration examples.

Any “checked” things will then appear on the dashboard.

Screenshot 2016-02-29 18.42.13 copy
2. Nginx as Proxy

Monit provides its own web server, but its more comfortable if you have nginx take care of networking. In that way it will also be more comfortable to set up SSL. Here is a example configuration where Monit runs on a subdomain, with SSL enabled.

server {
  listen 443;
  server_name monit.yoursite.com;

  ssl_certificate /etc/nginx/ssl/monit.crt;
  ssl_certificate_key /etc/nginx/ssl/monit.key;

  # Additional SSL configuration
  # ...

  location / {
    proxy_pass http://127.0.0.1:2812;
    proxy_set_header Host $host;
  }
}
3. Set up your email

First set up the account that Monit will use to send mails.

set mailserver mail.mailserver.com port 587 username "monit@yoursite.com" password "XXX" using TLSV1 with timeout 30 seconds

Then add your email. You can choose if you want to get notified about manual actions or not. I don’t want them so I turned them off.

set alert john.smith@yourserver.com not on { instance, action }