Outils personnels

Monit installation : Différence entre versions

De wikiGite

(Sur CentOS / RedHat)
(Sur CentOS / RedHat)
 
(18 révisions intermédiaires par 3 utilisateurs non affichées)
Ligne 3 : Ligne 3 :
 
= Installation =
 
= Installation =
 
== Sur Debian ==
 
== Sur Debian ==
apt-get install monit
+
apt-get install monit
 +
 
 +
Autoriser le lancement de monit dans /etc/default/monit
 +
startup=1
  
 
<span id="ancre">Dans /etc/monit/monitrc, décommenter :</span>
 
<span id="ancre">Dans /etc/monit/monitrc, décommenter :</span>
Ligne 13 : Ligne 16 :
 
Modifier le format du mail :
 
Modifier le format du mail :
 
  set mail-format {
 
  set mail-format {
     From: monit-<nom du serveur>@example.net
+
     From: monit@example.net
 
  #    Subject: $EVENT $SERVICE
 
  #    Subject: $EVENT $SERVICE
     Subject: $SERVICE $DESCRIPTION
+
     Subject: $HOST $DESCRIPTION
 
     Message: $DATE
 
     Message: $DATE
 
     $SERVICE : $EVENT
 
     $SERVICE : $EVENT
Ligne 23 : Ligne 26 :
  
 
Donner le destinataire :
 
Donner le destinataire :
  set alert ''<mail_admin>''@example.net not on { action }  # receive all alerts except "unmonitor" and "monitor"
+
  set alert '''mail_de_l_admin@example.net''' not on { action }  # receive all alerts except "unmonitor" and "monitor"
 
("not on { action }" est nécessaire pour ne pas avoir de mail sur les actions de unmonitor/monitor (voir plus bas) )
 
("not on { action }" est nécessaire pour ne pas avoir de mail sur les actions de unmonitor/monitor (voir plus bas) )
  
Ligne 47 : Ligne 50 :
 
est décommenté (ajouter le "monit" au milieu, il n'y est pas par défaut).<br/>
 
est décommenté (ajouter le "monit" au milieu, il n'y est pas par défaut).<br/>
 
Créer le répertoire au besoin.
 
Créer le répertoire au besoin.
 +
'''Update''' 5.1.1 : le répertoire est désormais /etc/monit/conf.d, créé à l'installation.
  
Comme indiqué dans le commentaire de l'option "set daemon", il faut aussi modifier /etc/default/monit, mais uniquement si on veut un interval différent de 180 secondes (valeur par défaut). On en profitera pour mettre "startup" à 1, sinon Monit ne démarre pas en tâche de fond :
+
Comme indiqué dans le commentaire de l'option "set daemon", il faut aussi modifier (ou créer) /etc/sysconfig/monit, mais uniquement si on veut un interval différent de 180 secondes (valeur par défaut). On en profitera pour mettre "startup" à 1, sinon Monit ne démarre pas en tâche de fond :
 
  # Defaults for monit initscript
 
  # Defaults for monit initscript
 
  # sourced by /etc/init.d/monit
 
  # sourced by /etc/init.d/monit
Ligne 65 : Ligne 69 :
 
== Sur CentOS / RedHat ==
 
== Sur CentOS / RedHat ==
 
sur CENTOS4 / RedHat4 / BlueQuartz :<br/>
 
sur CENTOS4 / RedHat4 / BlueQuartz :<br/>
 +
cd /root/install
 
Récupérer la dernière version de paquet sur dag.wieers.com (redirigé vers http://packages.sw.be/monit/) (CentOS4 = RHEL4; CentOS5=RHEL5)
 
Récupérer la dernière version de paquet sur dag.wieers.com (redirigé vers http://packages.sw.be/monit/) (CentOS4 = RHEL4; CentOS5=RHEL5)
  wget <nowiki>http://packages.sw.be/monit/monit-5.2.4-1.el</nowiki>'''4'''.rf.i386.rpm
+
  rpm -Uvh <nowiki>http://packages.sw.be/monit/monit-5.2.5-1.el</nowiki>'''4'''.rf.i386.rpm
 
ou sur CENTOS5 / RedHat5 / BlueOnyx :<br/>
 
ou sur CENTOS5 / RedHat5 / BlueOnyx :<br/>
  wget <nowiki>http://packages.sw.be/monit/monit-5.2.4-1.el</nowiki>'''5'''.rf.i386.rpm
+
  rpm -Uvh <nowiki>http://packages.sw.be/monit/monit-5.2.5-1.el</nowiki>'''5'''.rf.i386.rpm
 
+
ou sur CENTOS6 / RedHat6 / BlueOnyx 5108R en 64bits:<br/>
Puis installer monit :
+
  rpm -Uvh <nowiki>http://packages.sw.be/monit/monit-5.5-1.el</nowiki>'''6'''.rf.x86_64.rpm
  rpm -i monit-*.rpm
 
  
 
'''Attention''', le fichier de conf est '''/etc/monit.conf'''. Le répertoire /etc/monit.d est déjà créé. Il n'y a pas de fichier /etc/default/monit.
 
'''Attention''', le fichier de conf est '''/etc/monit.conf'''. Le répertoire /etc/monit.d est déjà créé. Il n'y a pas de fichier /etc/default/monit.
Ligne 86 : Ligne 90 :
 
  chkconfig --level 2345 monit on
 
  chkconfig --level 2345 monit on
 
  chkconfig --list monit
 
  chkconfig --list monit
 +
 +
== Sur rPath / OpenFiler ==
 +
Il n'y a pas de paquet conary pour cette distribution, mais le RPM fait l'affaire.
 +
 +
Installer RPM
 +
conary update rpm
 +
Puis
 +
conary update --keep-existing 'pam:lib[is:x86]'
 +
conary update --keep-existing 'openssl:lib[is:x86]'
 +
conary update --keep-existing 'nss:lib[is:x86]'
 +
ln -s /lib/libssl.so.7 /lib/libssl.so.6
 +
ln -s /lib/libcrypto.so.7 /lib/libcrypto.so.6
 +
 +
wget http://packages.sw.be/monit/monit-5.2.5-1.el5.rf.i386.rpm
 +
rpm -Uvh monit-5.2.5-1.el5.rf.i386.rpm --nodeps
 +
 +
La configuration de monit.conf s'effectue ensuite comme pour une CentOS.
  
 
= Configuration des services =
 
= Configuration des services =
Ligne 114 : Ligne 135 :
 
  check process postfix2 with pidfile /var/spool/postfix/pid/master.pid
 
  check process postfix2 with pidfile /var/spool/postfix/pid/master.pid
 
     start program = "/etc/init.d/postfix start"
 
     start program = "/etc/init.d/postfix start"
     stop program  = "/etc/init.d//postfix stop"
+
     stop program  = "/etc/init.d/postfix stop"
 
     if cpu > 60% for 2 cycles then alert
 
     if cpu > 60% for 2 cycles then alert
 
     if cpu > 80% for 5 cycles then restart
 
     if cpu > 80% for 5 cycles then restart
Ligne 124 : Ligne 145 :
 
     if cpu > 80% for 5 cycles then restart
 
     if cpu > 80% for 5 cycles then restart
 
     if failed port 3306 then restart
 
     if failed port 3306 then restart
  check process postgresql7 with pidfile /var/lib/postgresql/7.4/main/postmaster.pid
+
  check process postgresql9 with pidfile /var/lib/postgresql/9.0/main/postmaster.pid
     start program = "/etc/init.d/postgresql-7.4 start"
+
     start program = "/etc/init.d/postgresql start"
     stop program  = "/etc/init.d/postgresql-7.4 stop"
+
     stop program  = "/etc/init.d/postgresql stop"
 
     if cpu > 60% for 2 cycles then alert
 
     if cpu > 60% for 2 cycles then alert
 
     if cpu > 80% for 5 cycles then restart
 
     if cpu > 80% for 5 cycles then restart
 
     if failed port 5432 then restart
 
     if failed port 5432 then restart
  check device root with path /dev/sda1
+
check process ntpd with pidfile /var/run/ntpd.pid
 +
    noalert '''Mail de l'admin'''
 +
    start program = "/etc/init.d/ntp start"
 +
    stop program  = "/etc/init.d/ntp stop"
 +
  check device root with path /
 
     if space usage > 75% for 5 times within 15 cycles then alert
 
     if space usage > 75% for 5 times within 15 cycles then alert
 
     if space usage > 90% then alert
 
     if space usage > 90% then alert
Ligne 270 : Ligne 295 :
 
  /usr/sbin/monit -c /etc/monit/monitrc unmonitor server.example.net
 
  /usr/sbin/monit -c /etc/monit/monitrc unmonitor server.example.net
 
et "monitor" pour réactiver.
 
et "monitor" pour réactiver.
 +
 +
Centos :
 +
/usr/bin/monit -c /etc/monit.conf unmonitor server.example.net
  
 
'''NOTE''': pour éviter d'avoir un mail pour chaque service au moment du unmonitor/monitor, penser à ajouter "not on { action }" sur l'option "set alert" ([[#ancre|voir plus haut]]).
 
'''NOTE''': pour éviter d'avoir un mail pour chaque service au moment du unmonitor/monitor, penser à ajouter "not on { action }" sur l'option "set alert" ([[#ancre|voir plus haut]]).
Ligne 287 : Ligne 315 :
 
  ln -s /opt/systools/monit-stop.sh /etc/cron.daily/00monit-stop
 
  ln -s /opt/systools/monit-stop.sh /etc/cron.daily/00monit-stop
 
  ln -s /opt/systools/monit-start.sh /etc/cron.daily/zzmonit-start
 
  ln -s /opt/systools/monit-start.sh /etc/cron.daily/zzmonit-start
 +
 +
Pour éviter de recevoir les alertes "monit stopped" et "monit started", ne pas oublier de modifier la ligne set alert :
 +
set alert <mail_admin>@example.net '''not on { action }'''
  
 
= Accès au webserver de l'extérieur sans ouvrir de port supplémentaire =
 
= Accès au webserver de l'extérieur sans ouvrir de port supplémentaire =
Ligne 338 : Ligne 369 :
 
     $endHeader = true;
 
     $endHeader = true;
 
  }
 
  }
   $info = '';                                  // Generate a string to send to the screen
+
   $info = <nowiki>'';                                  // Generate a string to send to the screen</nowiki>
 
   
 
   
 
  while (!feof($urlHandle)) {
 
  while (!feof($urlHandle)) {
Ligne 372 : Ligne 403 :
 
Et créer le fichier de mots de passe avec user admin/'''<mot de passe admin>''' :
 
Et créer le fichier de mots de passe avec user admin/'''<mot de passe admin>''' :
 
  # htpasswd -c /var/www/monit/.htpasswd admin
 
  # htpasswd -c /var/www/monit/.htpasswd admin
 +
 +
= Debug =
 +
Monit est assez bavard dans le log système, mais on peut débugger directement la configuration en ligne de commande par
 +
monit -Iv

Version actuelle datée du 23 juillet 2013 à 09:21

Installation

Sur Debian

apt-get install monit

Autoriser le lancement de monit dans /etc/default/monit

startup=1

Dans /etc/monit/monitrc, décommenter :

set daemon  120 # NOTE POUR DEBIAN : c'est le paramètre de /etc/default/monit CHECK_INTERVAL qui est prioritaire
                # (lancement du démon avec "-d" dans l'init.d), c'est lui qu'il faut donc modifier !
set logfile syslog facility log_daemon
set mailserver   localhost             # primary mailserver

Modifier le format du mail :

set mail-format {
    From: monit@example.net
#    Subject: $EVENT $SERVICE
    Subject: $HOST $DESCRIPTION
    Message: $DATE
    $SERVICE : $EVENT
    $DESCRIPTION
    Monit $ACTION : $SERVICE
}

Donner le destinataire :

set alert mail_de_l_admin@example.net not on { action }  # receive all alerts except "unmonitor" and "monitor"

("not on { action }" est nécessaire pour ne pas avoir de mail sur les actions de unmonitor/monitor (voir plus bas) )

Eventuellement, ouvrir le "embedded webserver" au localhost pour au moins pouvoir lancer un "monit status" ou "monit summary" (voir plus bas pour redirection php si APACHE installé).

UPDATE : ouvrir sur localhost pour pouvoir activer/désactiver les services temporairement (voir plus bas).

set httpd port 2812 and
    use address localhost  # only accept connection from localhost
    allow localhost        # allow localhost to connect to the server and

Note S'il ny a pas d'Apache, ouvrir le port 2812 et configurer monit en SSL autonome :

set httpd port 2812 and
   SSL Enable 
   PEMFILE /etc/apache2/ssl/apache.pem
   allow admin:<mot de passe admin>      # require user 'admin' with password

Note2 L'ouverture du port 2812 peut être un trou de sécurité. Ne l'ouvrir qu'en connaissance de cause. Par exemple si on veut piloter monit depuis la ligne de commande (cf plus bas).

Vérifier que :

include /etc/monit/monit.d/*

est décommenté (ajouter le "monit" au milieu, il n'y est pas par défaut).
Créer le répertoire au besoin. Update 5.1.1 : le répertoire est désormais /etc/monit/conf.d, créé à l'installation.

Comme indiqué dans le commentaire de l'option "set daemon", il faut aussi modifier (ou créer) /etc/sysconfig/monit, mais uniquement si on veut un interval différent de 180 secondes (valeur par défaut). On en profitera pour mettre "startup" à 1, sinon Monit ne démarre pas en tâche de fond :

# Defaults for monit initscript
# sourced by /etc/init.d/monit
# installed at /etc/default/monit by maintainer scripts
# Fredrik Steen <stone@debian.org>

# You must set this variable to for monit to start
startup=1

# To change the intervals which monit should run uncomment
# and change this variable.
CHECK_INTERVALS=120

Ajouter monit à check_service.sh (ou se reporter à "mise à jour automatique des scripts" (maj_script.sh)).

Sur CentOS / RedHat

sur CENTOS4 / RedHat4 / BlueQuartz :

cd /root/install

Récupérer la dernière version de paquet sur dag.wieers.com (redirigé vers http://packages.sw.be/monit/) (CentOS4 = RHEL4; CentOS5=RHEL5)

rpm -Uvh http://packages.sw.be/monit/monit-5.2.5-1.el4.rf.i386.rpm

ou sur CENTOS5 / RedHat5 / BlueOnyx :

rpm -Uvh http://packages.sw.be/monit/monit-5.2.5-1.el5.rf.i386.rpm

ou sur CENTOS6 / RedHat6 / BlueOnyx 5108R en 64bits:

rpm -Uvh http://packages.sw.be/monit/monit-5.5-1.el6.rf.x86_64.rpm

Attention, le fichier de conf est /etc/monit.conf. Le répertoire /etc/monit.d est déjà créé. Il n'y a pas de fichier /etc/default/monit.

SUITE : Modifier monit.conf comme indiqué pour la Debian, ci-dessus. Puis continuer ci-dessous.

UPDATE 5.1.1 - A FAIRE SUR CENTOS : Vérifier que les lignes "set idfile /var/monit/id" et "set statefile /var/monit/state" sont commentées (le répertoire /var/monit n'existe pas, et de plus autant laisser le chemin par défaut $HOME/.monit.id et .monit.state).

Changer les droits de monit.conf :

chmod 700 /etc/monit.conf

Ajouter monit à check_service.sh.

Et ne pas oublier

chkconfig --level 2345 monit on
chkconfig --list monit

Sur rPath / OpenFiler

Il n'y a pas de paquet conary pour cette distribution, mais le RPM fait l'affaire.

Installer RPM

conary update rpm

Puis

conary update --keep-existing 'pam:lib[is:x86]' 
conary update --keep-existing 'openssl:lib[is:x86]'
conary update --keep-existing 'nss:lib[is:x86]' 
ln -s /lib/libssl.so.7 /lib/libssl.so.6
ln -s /lib/libcrypto.so.7 /lib/libcrypto.so.6

wget http://packages.sw.be/monit/monit-5.2.5-1.el5.rf.i386.rpm
rpm -Uvh monit-5.2.5-1.el5.rf.i386.rpm --nodeps

La configuration de monit.conf s'effectue ensuite comme pour une CentOS.

Configuration des services

Exemple de fichier de config /etc/monit/monit.d/serveur.example.net, type DEBIAN/ISPConfig, surveillance locale :

###############################################################################
## serveur.example.net local
###############################################################################
#
check system serveur.example.net
    if loadavg (1min) > 6 then alert
    if loadavg (5min) > 4 then alert
    if memory usage > 85% then alert
    if cpu usage (user) > 70% for 3 cycles then alert
    if cpu usage (system) > 50% for 3 cycles then alert
    if cpu usage (wait) > 60% for 3 cycles then alert
check process apache2 with pidfile /var/run/apache2.pid
    start program = "/etc/init.d/apache2 start"
    stop program  = "/etc/init.d/apache2 stop"
    if cpu > 60% for 2 cycles then alert
    if cpu > 80% for 5 cycles then restart
    if failed port 80 then restart
check process proftpd1 with pidfile /var/run/proftpd.pid
    start program = "/etc/init.d/proftpd start"
    stop program  = "/etc/init.d/proftpd stop"
    if cpu > 60% for 2 cycles then alert
    if cpu > 80% for 5 cycles then restart
    if failed port 21 then restart
check process postfix2 with pidfile /var/spool/postfix/pid/master.pid
    start program = "/etc/init.d/postfix start"
    stop program  = "/etc/init.d/postfix stop"
    if cpu > 60% for 2 cycles then alert
    if cpu > 80% for 5 cycles then restart
    if failed port 25 then restart
check process mysql5 with pidfile /var/run/mysqld/mysqld.pid
    start program = "/etc/init.d/mysql start"
    stop program  = "/etc/init.d/mysql stop"
    if cpu > 60% for 2 cycles then alert
    if cpu > 80% for 5 cycles then restart
    if failed port 3306 then restart
check process postgresql9 with pidfile /var/lib/postgresql/9.0/main/postmaster.pid
    start program = "/etc/init.d/postgresql start"
    stop program  = "/etc/init.d/postgresql stop"
    if cpu > 60% for 2 cycles then alert
    if cpu > 80% for 5 cycles then restart
    if failed port 5432 then restart
check process ntpd with pidfile /var/run/ntpd.pid
    noalert Mail de l'admin
    start program = "/etc/init.d/ntp start"
    stop program  = "/etc/init.d/ntp stop"
check device root with path /
    if space usage > 75% for 5 times within 15 cycles then alert
    if space usage > 90% then alert

Exemple de fichier de config /etc/monit.d/serveur.centos.fr, type CENTOS4/BlueQuartz, surveillance locale :
Penser à changer le Mail de l'admin !

###############################################################################
## serveur.example.fr local
###############################################################################
#
check system serveur.example.fr
    if loadavg (1min) > 6 then alert
    if loadavg (5min) > 4 then alert
    if memory usage > 85% then alert
    if cpu usage (user) > 70% for 3 cycles then alert
    if cpu usage (system) > 50% for 3 cycles then alert
    if cpu usage (wait) > 60% for 3 cycles then alert
check process httpd with pidfile /var/run/httpd.pid
    start program = "/etc/init.d/httpd start"
    stop program  = "/etc/init.d/httpd stop"
    if cpu > 60% for 2 cycles then alert
    if cpu > 80% for 5 cycles then restart
    if failed port 80 then restart
check process sendmail with pidfile /var/run/sendmail.pid
    start program = "/etc/init.d/sendmail start"
    stop program  = "/etc/init.d/sendmail stop"
    if cpu > 60% for 2 cycles then alert
    if cpu > 80% for 5 cycles then restart
    if failed port 25 then restart
check process mysql5 with pidfile /var/run/mysqld/mysqld.pid
    start program = "/etc/init.d/mysqld start"
    stop program  = "/etc/init.d/mysqld stop"
    if cpu > 60% for 2 cycles then alert
    if cpu > 80% for 5 cycles then restart
    if failed port 3306 then restart
check process ntpd with pidfile /var/run/ntpd.pid
    noalert Mail de l'admin
    start program = "/etc/init.d/ntpd start"
    stop program  = "/etc/init.d/ntpd stop"
check device root with path /dev/mapper/VolGroup00-LogVol00
    if space usage > 75% for 5 times within 15 cycles then alert
    if space usage > 90% then alert
check device home with path /dev/mapper/VolGroup00-LogVol04
    if space usage > 75% for 5 times within 15 cycles then alert
    if space usage > 90% then alert

Exemple de fichier de config /etc/monit.d/serveur.centos.fr, type CENTOS5/BlueOnyx, surveillance locale :
Penser à changer le Mail de l'admin !

###############################################################################
## serveur.example.fr local
###############################################################################
#
check system serveur.example.fr
    if loadavg (1min) > 6 then alert
    if loadavg (5min) > 4 then alert
    if memory usage > 85% then alert
    if cpu usage (user) > 70% for 3 cycles then alert
    if cpu usage (system) > 50% for 3 cycles then alert
    if cpu usage (wait) > 60% for 3 cycles then alert
check process httpd with pidfile /var/run/httpd.pid
    start program = "/etc/init.d/httpd start"
    stop program  = "/etc/init.d/httpd stop"
    if cpu > 60% for 2 cycles then alert
    if cpu > 80% for 5 cycles then restart
    if failed port 80 then restart
check process sendmail with pidfile /var/run/sendmail.pid
    start program = "/etc/init.d/sendmail start"
    stop program  = "/etc/init.d/sendmail stop"
    if cpu > 60% for 2 cycles then alert
    if cpu > 80% for 5 cycles then restart
    if failed port 25 then restart
check process mysql5 with pidfile /var/run/mysqld/mysqld.pid
    start program = "/etc/init.d/mysqld start"
    stop program  = "/etc/init.d/mysqld stop"
    if cpu > 60% for 2 cycles then alert
    if cpu > 80% for 5 cycles then restart
    if failed port 3306 then restart
check process ntpd with pidfile /var/run/ntpd.pid
    noalert Mail de l'admin
    start program = "/etc/init.d/ntpd start"
    stop program  = "/etc/init.d/ntpd stop"
check device root with path /dev/mapper/VolGroup00-root
    if space usage > 75% for 5 times within 15 cycles then alert
    if space usage > 90% then alert
check device home with path /dev/mapper/VolGroup00-home
    if space usage > 75% for 5 times within 15 cycles then alert
    if space usage > 90% then alert

On peut executer une commande ou un script à la place de "alert". Dans ce cas on paramètre les seuils de cette façon :

   if loadavg (5min) > 4 then exec "/opt/systools/top.sh LoadAvg5m"
   if memory usage > 85% then exec "/opt/systools/top.sh Memory"
   if cpu usage (user) > 70% for 3 cycles then exec "/opt/systools/top.sh CPUuser"
   if cpu usage (system) > 50% for 3 cycles then exec "/opt/systools/top.sh CPUsys"
   if cpu usage (wait) > 60% for 3 cycles then exec "/opt/systools/top.sh CPUwait

Le fichier top.sh de cet exemple (PENSER A VERIFIER que "sar" (yum install sysstat) et "free" sont installés):

#!/bin/bash
EVENT=$1
ROOT=/opt/systools/
LOG=$ROOT/top.log
HOST=serveur.example.net
MAIL=<Mail de l'admin>

if [ -f $LOG ]
then
   mv $LOG $LOG.0
fi
date > $LOG
sar 1 >> $LOG
free  >> $LOG
for i in 1 2 3 4 5
do
   ps -eo pcpu,args | sort -r -k1 | head -5 >> $LOG
   sleep 1
done
cat $LOG | mail -s "top.log $EVENT $HOST" $MAIL

Exemple de /etc/monit/monit.d/serveur.example.net sur serveur2 qui surveille serveur en remote :

###############################################################################
## serveur.example.net remote
###############################################################################
#
check host serveur.example.net with address 1.2.3.4
       if failed icmp type echo count 3 with timeout 10 seconds then alert
        else if recovered then exec "/usr/bin/logger Monit: $HOST $DESCRIPTION recovered"

Le "ELSE IF RECOVERED" permet de ne pas recevoir un mail lorsque le host/service redevient disponible (après dépannage).

Note: Le mot "SUCCEEDED" à la place de "RECOVERED" indiqué dans la documentation ne fonctionne pas, en tout cas pas avec les v4.8 et 4.9.

Activation/desactivation temporaire de certains services en ligne de commande

Si le monitoring de certains services doit être suspendu, on peut utiliser unmonitor/monitor en ligne de commande.

MAIS ATTENTION, monit doit avoir été lancé avec le support HTTP (voir ci-dessus dans la partie Debian).
Exemple : arrêter le monitoring local des performances - bloc "system", par exemple si monit.d contient :

check system server.example.net
    if loadavg (1min) > 6 then alert
    ....

On stoppera par :

/usr/sbin/monit -c /etc/monit/monitrc unmonitor server.example.net

et "monitor" pour réactiver.

Centos :

/usr/bin/monit -c /etc/monit.conf unmonitor server.example.net

NOTE: pour éviter d'avoir un mail pour chaque service au moment du unmonitor/monitor, penser à ajouter "not on { action }" sur l'option "set alert" (voir plus haut).


Pour arrêter le monitoring le temps des batch cron.daily (souvent générateurs de charge système, donc de faux positif pour monit), on créera (si ça n'a pas déjà été fait dans mise à jour automatique des scripts :

/opt/systools/monit-stop.sh

#!/bin/bash
/usr/bin/monit unmonitor all

et /opt/systools/monit-start.sh

#!/bin/bash
/usr/bin/monit monitor all

Sans oublier

chmod 700 /opt/systools/monit-stop.sh
chmod 700 /opt/systools/monit-start.sh

Puis

ln -s /opt/systools/monit-stop.sh /etc/cron.daily/00monit-stop
ln -s /opt/systools/monit-start.sh /etc/cron.daily/zzmonit-start

Pour éviter de recevoir les alertes "monit stopped" et "monit started", ne pas oublier de modifier la ligne set alert :

set alert <mail_admin>@example.net not on { action }

Accès au webserver de l'extérieur sans ouvrir de port supplémentaire

Renvoie l'alias "/monit" d'Apache vers localhost:2812, après authentification "basic" (login/mot de passe).

Apache doit bien sûr être opérationnel sur la machine.

Créer /var/www/monit/monit.php :

<?php
//  Copyright (C), 2004 David Fletcher, <david@megapico.co.uk>
// 
//  This program is free software; you can redistribute it and/or
//  modify it under the terms of the GNU General Public License as
//  published by the Free Software Foundation; either version 2 of the
//  License, or (at your option) any later version.
// 
//  This program is distributed in the hope that it will be useful, but
//  WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  General Public License for more details.
// 
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software Foundation,
//  Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
//  Requires PHP, and a working webserver!
//  @author Author David Fletcher, <david@megapico.co.uk>
//  @version \$Id: monit.php,v 1.2 2004/11/20 23:53:42 hauk Exp $

//Configuration - set this to match the information in /etc/monitrc
//Set information about the monit server address, port and login details.
$addr = 'localhost';
$port = 2812;
$path = '/';
$user = 'admin';
$pass = 'monit';
$timeout = 30;
//End of configuration

$urlHandle = fsockopen($addr, $port, $errno, $errstr, $timeout);
socket_set_timeout($urlHandle, $timeout);
$urlString = "GET $path HTTP/1.0\r\nHost: $addr\r\nConnection: Keep-Alive\r\nUser-Agent: MonitPHP\r\n";
$urlString .= "Authorization: Basic ".base64_encode("$user:$pass")."\r\n";
$urlString .= "\r\n";
fputs($urlHandle, $urlString);
$response = fgets($urlHandle);
$endHeader = false;                           // Strip initial header information

while ( !$endHeader){
  if (fgets($urlHandle) == "\r\n")
    $endHeader = true;
}
 $info = '';                                  // Generate a string to send to the screen

while (!feof($urlHandle)) {
  $info.=fgets($urlHandle);
}
 fclose ($urlHandle);
echo $info;
?>

Créer /etc/apache2/conf.d/monit.conf :

<IfModule mod_alias.c>
  Alias /monit "/var/www/monit"
</IfModule>

<DirectoryMatch /var/www/monit/>
  Options -FollowSymLinks
  AllowOverride All
  order deny,allow
#  deny from all
#  allow from 127.0.0.0/255.0.0.0
</DirectoryMatch>

Créer /var/www/.htaccess :

AuthUserFile /var/www/monit/.htpasswd
 AuthName "Restricted Access"
 Require valid-user
 AuthType Basic

<Files .*>
deny from all
</Files>

Et créer le fichier de mots de passe avec user admin/<mot de passe admin> :

# htpasswd -c /var/www/monit/.htpasswd admin

Debug

Monit est assez bavard dans le log système, mais on peut débugger directement la configuration en ligne de commande par

monit -Iv