Debian Linux Dienste überwachen mit Monit

Monit ist in der Lage Linux Dienste überwachen zu können und ggf. den problematischen Server Dienst (z.B. Apache, MySQL, FTP, Postfix etc.) neu zu starten, darüber hinaus verschickt Monit eine Mail.Über die Weboberfläche von Monit kann man die Dienste und die Belastung für das System sehen. Vorsicht, über die Weboberfläche kann man alle überwachten Dienste stoppen und starten, d.h. man muss den Zugang gut absichern!

Linux Dienste überwachen – Installation von Monit

~# apt-get install monit

Linux Dienste überwachen – Konfiguration von Monit

~# mcedit /etc/monit/monitrc

Groß geschriebene und / oder in Klammern<> gesetzte Teile individuell anpassen.

Eintragen an das Ende von monitrc (bitte beachtet die richtige Schreibweise der Anführungszeichen!)

# Aktualisierungsinterval
set daemon  60

# Speicherort des monit Logfiles
set logfile /var/log/monit

# Mailversand ueber
set mailserver localhost # primary mailserver
set mail-format { from: monit@DOMAIN DEINES SERVERS}
# Mail an
set alert DEINE.EMAIL@DOMAIN

#weboberflaesche
set httpd port 2812 and
use address blog.tim-bormann.de
allow <BENUTZERNAME>:<PASSWORT>

# check ftp
check process proftpd with pidfile /var/run/proftpd.pid
start program = „/etc/init.d/proftpd start“
stop program  = „/etc/init.d/proftpd stop“
if failed port 21 protocol ftp then restart
if 5 restarts within 5 cycles then timeout

# check http
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 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

# check mysql
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 host 127.0.0.1 port 3306 then restart
if 5 restarts within 5 cycles then timeout

## AUSKOMMENTIERT als BEISPIEL #check 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 port 25 protocol smtp then restart
#if 5 restarts within 5 cycles then timeout

# check ssh
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

#samba
check file smbd_bin with path /usr/sbin/smbd
if failed uid root then unmonitor
if failed gid root then unmonitor
group samba

check process smbd with pidfile /var/run/samba/smbd.pid
group samba
start program = „/etc/init.d/samba restart“
stop  program = „/etc/init.d/samba stop“
if failed host localhost port 139 type TCP  then restart
if 5 restarts within 5 cycles then timeout
depends on smbd_bin

check process nmbd with pidfile /var/run/samba/nmbd.pid
group samba
start program = „/etc/init.d/samba restart“
stop  program = „/etc/init.d/samba stop“
if failed host localhost port 138 type UDP  then restart
if failed host localhost port 137 type UDP  then restart
if 5 restarts within 5 cycles then timeout
depends on nmbd_bin

check file nmbd_bin with path /usr/sbin/nmbd
group samba
if failed checksum then unmonitor
if failed permission 755 then unmonitor
if failed uid root then unmonitor
if failed gid root then unmonitor

Um Postfix zu überprüfen muss natürlich Postfix und nicht Exim installiert sein.

Speichern der Konfiguration und überprüfen

~# monit –t

Monit starten und Linux Dienste überwachen

~# mcedit /etc/default/monit

startup=0

auf

startup=1

setzen. Und jetzt noch ein Neustart von Monit.

~# /etc/init.d/monit restart

Und ab jetzt … die Linux Dienste überwachen und ggf. neu starten!