Installer et paramétrer MONIT

Installer MONIT :

sudo apt-get install monit
Attention, monit n’est plus dans les dépots de Debian 10 Buster, mais il est disponible dans les backports :
printf "%s\n" "deb http://ftp.de.debian.org/debian buster-backports main" | \
sudo tee /etc/apt/sources.list.d/buster-backports.list
sudo apt update
sudo apt install -t buster-backports monit

Configurer MONIT :

# vim /etc/monit/monitrc
# monit -t (pour vérifier les erreurs de syntaxe)
# monit reload (pour prendre en compte les MaJ)
# monit summary (pour un affichage en mode console)

Autoriser l’interface Web de Monit :

Pour que l’interface ne soit accessible que depuis le serveur / PC lui-même, décommenter les 2 lignes en bleu :

 set httpd port 2812 and
    use address localhost  # only accept connection from localhost
    allow localhost        # allow localhost to connect to the server and
    allow username:password      # require user 'username' with password 'password'
#    allow @monit           # allow users of group 'monit' to connect (rw)
#    allow @users readonly  # allow users of group 'users' to connect readonly

Paramétrer SMTP pour envoi des mails Admin :

set mailserver smtp.hitec.fr PORT 587 USERNAME myname PASSWORD mypswd, localhost

Monitorer & redémarrer automatiquement MySQL avec Monit

Ajouter les lignes ci-dessous pour permettre le monitoring de MySQL :
Monit redémarrera automatiquement MySQL s’il ne tourne pas.

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

Monitorer & redémarrer automatiquement APACHE avec Monit

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

Consultez la documentation Monit Documentation pour plus de détail.

Démarrer Monit

sudo monit
sudo monit reload  #in case monit was already started
sudo monit start all

Pour vérifier le bon fonctionnement de monit :

sudo monit status

Accès à l’interface Web :

http://server_ip:2812

Réf. superdevresources.com/server-monitoring-program/

Exemple le fichier de paramétrage complet :
set log /var/log/monit.log
set idfile /var/lib/monit/id
set statefile /var/lib/monit/state
set mailserver ns0.ovh.net PORT 587 USERNAME monmail@admin.fr PASSWORD motdepasse , localhost # smtp OVH
set eventqueue

set mail-format {
from: Monit <monit@$HOST>
subject: Monit Alert -- $EVENT $SERVICE
message: $EVENT Service $SERVICE
Date: $DATE
Action: $ACTION
Host: $HOST
Description: $DESCRIPTION

Your faithful employee,
Monit
}

set httpd port 2812 and allow admin:motdepasse

check system $HOST
if loadavg (1min) per core > 2 for 5 cycles then alert
if loadavg (5min) per core > 1.5 for 10 cycles then alert
if cpu usage > 95% for 10 cycles then alert
if memory usage > 75% then alert
if swap usage > 25% then alert

# Pour trouver les .pid : find /var/run -iname "*pid"

check process mysql 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 unix "/var/run/mysqld/mysqld.sock" then restart
if failed host 127.0.0.1 port 3306 then restart
if 5 restarts within 5 cycles then timeout

check process sshd with pidfile /var/run/sshd.pid
start program "/etc/init.d/ssh start"
stop program "/etc/init.d/ssh stop"
if failed port 22 protocol ssh then restart
if 5 restarts within 5 cycles then timeout

check filesystem boot with path /dev/md0
if space usage > 80% for 5 times within 15 cycles then alert
group server
check filesystem root with path /dev/mapper/bravo-root
if space usage > 80% for 5 times within 15 cycles then alert
group server
check filesystem home with path /dev/mapper/bravo-home
if space usage > 80% for 5 times within 15 cycles then alert
group server

check file RAID-proc_mdstat with path /proc/mdstat
if match "\[.*_.*\]" then alert

include /etc/monit/conf.d/*
include /etc/monit/conf-enabled/*