Monit
Monit est utilisé pour surveiller les processus "daemon" - démons en français, programme qui tournent en arrière plan - ou des programmes similaires sur votre serveur local.
Monit est particulièrement utile pour les processus démons lancés au démarrage du système à partir de /etc/init.d/ (sendmail, sshd, apache, mysql...)
A la différence de nombreux systèmes de surveillance, Monit peut en plus agir en cas de situation d'erreur; Par exemple, si sendmail s'arrête, Monit peut le relancer; Si Apache utilise trop de ressources (en cas d'attaque DoS en cours) Monit peut arrêter et/ou redémarrer Apache puis vous envoyer un message d'alerte.
Monit peut également surveiller l'état des processus et détecter par exemple les processus zombie ou qui utilisent trop de CPU.
Vous pouvez également utiliser Monit pour surveiller des fichiers, répertoires et systèmes de fichiers sur votre serveur local. Vous pouvez aussi contrôler le checksum MD5 de fichiers qui ne devraient pas changer...
Même s'il n'est pas fait pour ça Monit peut surveiller des hôtes distants.
Vous pouvez suivre l'état de votre système (utilisation du CPU, la mémoire et la charge moyenne)
Installation
# apt-get install monit
Pour les impatients
Nous allons activer monit et accéder tout de suite à l'interface Web
root@vanille:~# nano /etc/default/monit
# You must set this variable to for monit to start startup=1
root@vanille:~# cp /etc/monit/monitrc /etc/monit/monitrc.ori
Modifiez le fichier /etc/monit/monitrc pour qu'il ressemble à ceci:
root@vanille:~# $editor /etc/monit/monitrc
set daemon 60 set httpd port 2812 and allow admin:test
/!\ Attention à la clause allow, mal renseignée elle peut devenir la source du message d'erreur error : monit: unable to read data from the file, lorsque vous tentez par exemple un monit summary ou encore d'accéder à l'interface web :
root@vanille:~# service monit start Starting daemon monitor: monit.
Rendez-vous ensuite sur le service Web de Monit (http://votre_ip:2812)
Bien entendu cette configuration est très insuffisante; A part savoir que votre serveur est en fonction, vous n'avez pas beaucoup d'autres informations... De plus l'interface n'est pas sécurisée...
Explication des règles
D'une façon générale, les règles de surveillance s'articulent comme ceci:
- Surveillance d'un processus (le fichier PID du service);
- Les commandes pour arrêter et démarrer les processus surveillés;
- L'utilisateur système du service,
- La condition de test à effectuer:
- Interfaces et ports ou ces processus doivent écouter (par défaut localhost si vous ne précisez rien);
- Charge du CPU par service;
- Fichiers (s'ils existent, leur checksum...).
- ...
- La commande a effectuer en cas d'échec
Pensez à surveiller les services sur les bonnes interfaces... Inutile par exemple de surveiller ssh sur localhost alors que vous en avez besoin sur eth0...
Les possibilités sont très grandes, et la finesse de la surveillance tout autant. N'hésitez pas à lire les documentations elles regorgent d'astuces.
Plus de services
Voici quelques exemples de services à surveiller. Les fichiers sont à placer dans /etc/monit/conf.d
amavis
# cat /etc/monit/conf.d/amavis check process Amavisd-new with pidfile /var/run/amavis/amavisd.pid group mail start program = "/etc/init.d/amavis start" stop program = "/etc/init.d/amavis stop" if failed port 10024 protocol smtp then restart if 5 restarts within 5 cycles then timeout depends on amavisd_bin check file amavisd_bin with path /usr/sbin/amavisd-new group mail if failed checksum then unmonitor if failed permission 755 then unmonitor if failed uid root then unmonitor if failed gid root then unmonitor
apache2
# cat /etc/monit/conf.d/apache2 check process apache with pidfile /var/run/apache2.pid group www start program = "/etc/init.d/apache2 start" stop program = "/etc/init.d/apache2 stop" if failed host vanille.zehome.org port 80 protocol http and request "/monit/token" then restart if cpu is greater than 60% for 2 cycles then alert if cpu > 80% for 5 cycles then restart if totalmem > 500 MB for 5 cycles then restart if children > 250 then restart if loadavg(5min) greater than 10 for 8 cycles then stop if 3 restarts within 5 cycles then timeout
Sans oublier de créer le fichier 'token' sur votre serveur...
root@vanille:~# su www-data $ cd $ mkdir monit $ cd monit $ touch token $ exit
Du coup votre "access.log" va se remplir avec ceci:
127.0.1.1 - - [24/Aug/2011:07:31:19 +0300] "GET /monit/token HTTP/1.1" 200 250 "-" "monit/5.1.1" 127.0.1.1 - - [24/Aug/2011:07:32:25 +0300] "GET /monit/token HTTP/1.1" 200 250 "-" "monit/5.1.1" 127.0.1.1 - - [24/Aug/2011:07:33:31 +0300] "GET /monit/token HTTP/1.1" 200 250 "-" "monit/5.1.1" 127.0.1.1 - - [24/Aug/2011:07:34:37 +0300] "GET /monit/token HTTP/1.1" 200 250 "-" "monit/5.1.1" 127.0.1.1 - - [24/Aug/2011:07:35:43 +0300] "GET /monit/token HTTP/1.1" 200 250 "-" "monit/5.1.1" 127.0.1.1 - - [24/Aug/2011:07:36:49 +0300] "GET /monit/token HTTP/1.1" 200 250 "-" "monit/5.1.1" 127.0.1.1 - - [24/Aug/2011:07:37:55 +0300] "GET /monit/token HTTP/1.1" 200 250 "-" "monit/5.1.1"
Pour éviter cela, ajoutez ceci:
SetEnvIf Request_URI "^\/monit\/token$" dontlog
Puis ajoutez env=!dontlog à la fin de la ligne CustomLog...
CustomLog ${APACHE_LOG_DIR}/access.log combined env=!dontlog
Dans votre fichier /etc/apache2/sites-available/default
bind9
# cat /etc/monit/conf.d/bind9 check process Bind9 with pidfile /var/run/named/named.pid start program = "/etc/init.d/bind9 start" stop program = "/etc/init.d/bind9 stop" if failed port 53 use type udp protocol dns then restart if 3 restarts within 5 cycles then timeout
clamav
# cat /etc/monit/conf.d/clamav check process ClamAV with pidfile /var/run/clamav/clamd.pid group virus start program = "/etc/init.d/clamav-daemon start" stop program = "/etc/init.d/clamav-daemon stop" if 5 restarts within 5 cycles then timeout depends on clamavd_bin check file clamavd_bin with path /usr/sbin/clamd group virus if failed checksum then unmonitor if failed permission 755 then unmonitor if failed uid root then unmonitor if failed gid root then unmonitor
dovecot
# cat /etc/monit/conf.d/dovecot check process Dovecot with pidfile /var/run/dovecot/master.pid start program = "/etc/init.d/dovecot start" stop program = "/etc/init.d/dovecot stop" group mail if failed host mail.zehome.org port 993 type tcpssl sslauto protocol imap for 5 cycles then restart if 3 restarts within 5 cycles then timeout depends dovecot_init depends dovecot_bin check file dovecot_init with path /etc/init.d/dovecot group mail check file dovecot_bin with path /usr/sbin/dovecot group mail
fail2ban
# cat /etc/monit/conf.d/fail2ban check process Fail2ban with pidfile /var/run/fail2ban/fail2ban.pid start program = "/etc/init.d/fail2ban start" with timeout 60 seconds stop program = "/etc/init.d/fail2ban stop"
fetchmail
# cat /etc/monit/conf.d/fetchmail check process Fetchmail with pidfile /var/run/fetchmail/fetchmail.pid start program = "/etc/init.d/fetchmail start" with timeout 60 seconds stop program = "/etc/init.d/fetchmail stop"
freeradius
# cat /etc/monit/conf.d/freeradius check process Freeradius with pidfile /var/run/freeradius/freeradius.pid start program = "/etc/init.d/freeradius start" with timeout 60 seconds stop program = "/etc/init.d/freeradius stop" if failed host 10.9.8.3 port 1812 type udp then restart if failed host 10.9.8.3 port 1813 type udp then restart if failed host 10.9.8.3 port 1814 type udp then restart
imapproxyd
# cat /etc/monit/conf.d/imapproxyd check process Imapproxy with pidfile /var/run/imapproxy.pid start program = "/etc/init.d/imapproxy start" with timeout 60 seconds stop program = "/etc/init.d/imapproxy stop" if failed host 10.9.8.3 port 1143 then restart
mdadm
# cat /etc/monit/conf.d/mdadm check process mdadm with pidfile /var/run/mdadm/monitor.pid start program = "/etc/init.d/mdadm start" stop program = "/etc/init.d/mdadm stop" if 5 restarts within 5 cycles then timeout
mysql
# cat /etc/monit/conf.d/mysql check process MySql with pidfile /var/run/mysqld/mysqld.pid group mysql start program = "/etc/init.d/mysql start" stop program = "/etc/init.d/mysql stop" if failed host localhost port 3306 protocol mysql then restart if 5 restarts within 5 cycles then timeout depends on mysql_bin depends on mysql_rc check file mysql_bin with path /usr/sbin/mysqld group mysql if failed checksum then unmonitor if failed permission 755 then unmonitor if failed uid root then unmonitor if failed gid root then unmonitor check file mysql_rc with path /etc/init.d/mysql group mysql if failed checksum then unmonitor if failed permission 755 then unmonitor if failed uid root then unmonitor if failed gid root then unmonitor
ntp
# cat /etc/monit/conf.d/ntp check process ntpd with pidfile /var/run/ntpd.pid start program = "/etc/init.d/ntp start" stop program = "/etc/init.d/ntp stop" if failed host 127.0.0.1 port 123 type udp then alert if 5 restarts within 5 cycles then timeout
postfix
# cat /etc/monit/conf.d/postfix check process Postfix with pidfile /var/spool/postfix/pid/master.pid group mail start program = "/etc/init.d/postfix start" stop program = "/etc/init.d/postfix stop" if failed host 10.9.8.3 port 25 protocol smtp then restart if 5 restarts within 5 cycles then timeout depends on postfix_rc check file postfix_rc with path /etc/init.d/postfix group mail if failed checksum then unmonitor if failed permission 755 then unmonitor if failed uid root then unmonitor if failed gid root then unmonitor
rsyslogd
# cat /etc/monit/conf.d/rsyslogd check process Rsyslogd with pidfile /var/run/rsyslogd.pid start program = "/etc/init.d/rsyslog start" stop program = "/etc/init.d/rsyslog stop" if 5 restarts within 5 cycles then timeout check file rsyslogd_file with path /var/log/syslog
spamassassin
# cat /etc/monit/conf.d/spamassassin check process Spamassassin with pidfile /var/run/spamd.pid group mail start program = "/etc/init.d/spamassassin start" stop program = "/etc/init.d/spamassassin stop" if 5 restarts within 5 cycles then timeout if cpu usage > 99% for 5 cycles then alert if mem usage > 99% for 5 cycles then alert depends on spamd_bin depends on spamd_rc check file spamd_bin with path /usr/bin/spamassassin group mail if failed checksum then unmonitor if failed permission 755 then unmonitor if failed uid root then unmonitor if failed gid root then unmonitor check file spamd_rc with path /etc/init.d/spamassassin group mail if failed checksum then unmonitor if failed permission 755 then unmonitor if failed uid root then unmonitor if failed gid root then unmonitor
sshd
# cat /etc/monit/conf.d/sshd check process SSH with pidfile /var/run/sshd.pid start program = "/etc/init.d/ssh start" with timeout 60 seconds stop program = "/etc/init.d/ssh stop" if failed host 10.9.8.3 port 22 then restart
Logging
Vous pouvez archiver les logs dans /var/log/syslog: Ajoutez ceci à /etc/monit/monitrc
set logfile syslog facility log_daemon
Si comme moi vous préférez séparer les logs, insérez ceci:
set logfile /var/log/monit.log
Et n'oubliez pas de prévoir une rotation...
nano /etc/logrotate.d/monit
/var/log/monit.log { missingok notifempty size 100k create 0644 root root postrotate /bin/kill -HUP `cat /var/run/monit.pid 2>/dev/null` 2> /dev/null || true endscript }
Les alertes par email
Ajoutez ceci à /etc/monit/monitrc:
set mailserver vanille.zehome.org set alert admin@zehome.org set mail-format { from: monit@vanille.zehome.org }
Au redémarrage de monit vous recevrez un message dans votre boite mail:
monit alert -- Monit instance changed vanille.zehome.org
Monit instance changed Service vanille.zehome.org Date: Tue, 23 Aug 2011 14:55:10 +0300 Action: start Host: vanille.zehome.org Description: Monit started Your faithful employee, monit
En cas de service qui s'arrêterait...
Objet: monit alert -- Does not exist Freeradius
Does not exist Service Freeradius Date: Tue, 23 Aug 2011 14:59:35 +0300 Action: restart Host: vanille.zehome.org Description: process is not running Your faithful employee, monit
Puis peu de temps plus tard...
Objet: monit alert -- Exists Freeradius
Les options sont très nombreuses, vous pouvez par exemple décider d'être averti de façon différente service par service.
Vous pouvez aussi décider ne n'être averti que pour certaines alertes:
En voici la liste:
alert foo@bar on { action checksum content data exec gid icmp invalid fsflags nonexist permission pid ppid size timeout timestamp }
Et avec ssl
root@vanille:~# openssl req -new -x509 -days 365 -nodes -out /etc/ssl/private/monit.pem -keyout /etc/ssl/private/monit.pem Generating a 1024 bit RSA private key ..............++++++ ..............++++++ writing new private key to '/etc/ssl/private/monit.pem' ----- You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Country Name (2 letter code) [AU]:MG State or Province Name (full name) [Some-State]:Ihorombe Locality Name (eg, city) []:Ranohira Organization Name (eg, company) [Internet Widgits Pty Ltd]:Net Organizational Unit Name (eg, section) []:SHI Common Name (eg, YOUR name) []:vanille.zehome.org Email Address []:admin@zehome.org
root@vanille:~# openssl gendh 512 >> /etc/ssl/private/monit.pem Generating DH parameters, 512 bit long safe prime, generator 2 This is going to take a long time ............++*++*++*++*++*++*
root@vanille:~# openssl x509 -subject -dates -fingerprint -noout -in /etc/ssl/private/monit.pem subject= /C=MG/ST=Ihorombe/L=Ranohira/O=Net/OU=SHI/CN=vanille.zehome.org/emailAddress=admin@zehome.org notBefore=Aug 23 09:04:36 2011 GMT notAfter=Aug 22 09:04:36 2012 GMT SHA1 Fingerprint=15:DF:66:74:4C:B2:58:FF:0B:AF:1C:8B:D2:9A:63:65:5A:06:E5:D8
root@vanille:~# chmod 700 /etc/ssl/private/monit.pem
root@vanille:~# nano /etc/monit/monitrc
set daemon 60 set httpd port 2812 ssl enable pemfile /etc/ssl/private/monit.pem allow admin:test include /etc/monit/conf.d/*
Ecran final
En guise de conclusion: Testez chaque service avant de vous fier aux résultats... Surveillez les logs de monit, stoppez service par service et vérifiez la réaction de monit!
L'alerte doit être envoyée, le service redémarré.
Rendez-vous sur
https://votre.domaine.org:2812/
ou
https://votre_ip:2812/
Sources
- (en) Site officiel
- (en) Manuel
- (en) Exemples de services
- (en) Installation sur une etch - c'est pas jeune...