search
top

Mise en place de Monit

Monit a pour fonction de « surveiller » service et/ou les ressources système et d’exécuter une ou plusieurs actions en fonction de condition. Par exemple, il peut redémarrer Nginx si celui ci ne répond plus.

Configuration de base épuré

set daemon  60
set logfile syslog facility log_daemon

## Interface web de monit
#set httpd port 2812 and
#     allow login:motdepasse

include /etc/monit/conf.d/*

Configuration des services

Dans /etc/monit/conf.d/, j’y ai placé un fichier de configuration par service :

apache2

check process apache with pidfile /var/run/apache2.pid
    start program = "/etc/init.d/apache2 start"
    stop program  = "/etc/init.d/apache2 stop"
    if children > 149 then restart
    if totalmem > 4096 MB for 5 cycles then restart
    if 3 restarts within 5 cycles then timeout

clamd

check process clamd with pidfile /var/run/clamav/clamd.pid
   group clamav
   start program = "/etc/init.d/clamav-daemon start"
   stop  program = "/etc/init.d/clamav-daemon stop"
   if failed unixsocket /var/run/clamav/clamd.ctl then restart
   if 5 restarts within 5 cycles then timeout

dovecot

check process dovecot with pidfile /var/run/dovecot/master.pid
   group dovecot
   start program = "/etc/init.d/dovecot start"
   stop  program = "/etc/init.d/dovecot stop"
   if failed host 127.0.0.1 port 143 protocol imap then restart
   if failed host 127.0.0.1 port 993 type tcpssl sslauto protocol imap then restart
   if 5 restarts within 5 cycles then timeout

freshclam

check process freshclam with pidfile /var/run/clamav/freshclam.pid
   group clamav
   start program = "/etc/init.d/clamav-freshclam start"
   stop  program = "/etc/init.d/clamav-freshclam stop"
   if 5 restarts within 5 cycles then timeout

mysql

check process mysql with pidfile /var/lib/mysql/mysql.pid
   group mysql
   start program = "/etc/init.d/mysql start"
   stop program = "/etc/init.d/mysql stop"
   if failed host localhost port 3306 then restart
   if 5 restarts within 5 cycles then timeout

named

check process named with pidfile /var/run/named/named.pid
 start program = "/etc/init.d/bind9 start"
 stop program = "/etc/init.d/bind9 stop"
 if failed host 127.0.0.1 port 53 type tcp protocol dns then restart
 if failed host 127.0.0.1 port 53 type udp protocol dns then restart
 if 5 restarts within 5 cycles then timeout

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 localhost port 25 protocol smtp then restart
   if 5 restarts within 5 cycles then timeout

sshd

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

Vérification de la configuration Bind9

Codé un peut a l’arrache mais bon, l’idée est là.

#!/bin/sh
echo -n /tmp/liste-espace

echo "Liste des fichiers de zone avec des espaces dans le domaine (find dans /var/named/) :" > /tmp/liste-espace
find /var/named/ -name "*.fwd" -print0 | xargs -0 grep -F " ." | awk -F":" {' print $1 '} >> /tmp/liste-espace
echo "" >> /tmp/liste-espace

echo "Liste des domaines avec un espace dans le named.com" >> /tmp/liste-espace
grep 'zone "' /etc/named.conf | grep -F ' " {' >> /tmp/liste-espace
echo "" >> /tmp/liste-espace

echo "Liste des domaines avec un _ dans le nom dans le fichier de zone (grep du named.conf) :" >> /tmp/liste-espace
grep 'zone "' /etc/named.conf | awk -F'"' {' print $2 '} | grep "_" >> /tmp/liste-espace
echo "" >> /tmp/liste-espace

echo "Liste des fichiers de zone manquant (grep dans syslog):" >> /tmp/liste-espace
grep named /var/log/syslog | grep "file not found" | awk '{ print $7}' | awk -F"/" '{ print $1 }' | sort | uniq >> /tmp/liste-espace
echo "" >> /tmp/liste-espace
grep named /var/log/syslog | grep "file not found" >> /tmp/liste-espace

cat /tmp/liste-espace | mail -s "Liste des domaines invalide." email1,email2,email3

Named – Checkconf Bind

#!/bin/sh

echo -n > /tmp/liste-espace

echo « * Liste des fichiers de zone avec des espaces dans le domaine (find dans /var/named/) : » > /tmp/liste-espace
find /var/named/ -name « *.fwd » -print0 | xargs -0 grep -F  » . » | awk -F »: » {‘ print $1 ‘} >> /tmp/liste-espace
echo «  » >> /tmp/liste-espace

echo « * Liste des domaines avec un espace dans le named.com » >> /tmp/liste-espace
grep ‘zone « ‘ /etc/named.conf | grep -F ‘  » {‘ >> /tmp/liste-espace
echo «  » >> /tmp/liste-espace

echo « * Liste des domaines avec un _ dans le nom dans le fichier de zone (grep du named.conf) : » >> /tmp/liste-espace
grep ‘zone « ‘ /etc/named.conf | awk -F’ »‘ {‘ print $2 ‘} | grep « _ » >> /tmp/liste-espace
echo «  » >> /tmp/liste-espace

echo « * Liste des fichiers de zone manquant (grep dans syslog): » >> /tmp/liste-espace
grep ‘named’ /var/log/syslog | grep « file not found » | awk ‘{ print $7}’ | awk -F »/ » ‘{ print $1 }’ | sort | uniq >> /tmp/liste-espace
echo «  » >> /tmp/liste-espace
grep named /var/log/syslog | grep « file not found » >> /tmp/liste-espace

echo « * Nombre de zones : » >> /tmp/liste-espace
echo « Nombre de zone dans /etc/named.conf : `grep zone /etc/named.conf | wc -l`, nombre de fichier de zones : `ls -la /var/named/ | wc -l` » >> /tmp
/liste-espace
echo «  » >> /tmp/liste-espace

cat /tmp/liste-espace | mail -s « sujet mail » user@domain.ltd

top