Het monitoren van je Linux server is essentieel om een goed inzicht te hebben in het gebruik en waar eventueel knelpunten zitten.

Als je thuis een webserver hebt draaien is het verstandig om deze te monitoren of alles nog correct draait. Het is zelfs nog handiger als je een watchdog installeert die automatisch een process opnieuw opstart indien deze is gecrasht. Goede software hiervoor is Monit dat voor diverse platforms beschikbaar is.

Om dit pakket te installeren in Debian hoef je alleen maar het volgende commando in te geven:

apt-get install monit

Het configureren gaat eenvoudig door de file ‘/etc/monit/monitrc’ te editten

cp /etc/monit/monitrc /etc/monit/monitrc.org
vi /etc/monit/monitrc

De configuratie-file wijst zichzelf uit door erg veel commentaar-regels. In eerste instantie hoef je eigenlijk alleen maar de ingebouwde web-server te enablen:

set httpd port 2812 and
  allow localhost
  allow <user>:<password>

Verder moet je nog de lijst van services die je wilt monitoren configureren. Ik heb in eerste instantie de volgende regels hiervoor aangelegd:

# Apache2 Webserver
check process apache2 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 eye-vision.homeip.net port 80 protocol http
and request "/token" then restart
if 3 restarts within 5 cycles then timeout

# ProFTPD
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

# Secure Shell
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

# 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

# Cron
check process cron with pidfile /var/run/crond.pid
group system
start program = "/etc/init.d/cron start"
stop program = "/etc/init.d/cron stop"
if 5 restarts within 5 cycles then timeout

# NTPD Time Server
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

# Dovecot IMAP Server
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 eye-vision.homeip.net port 143 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

# Fetchmail
check process fetchmail with pidfile /var/run/fetchmail/fetchmail.pid
start program = "/etc/init.d/fetchmail start"
stop program = "/etc/init.d/fetchmail stop"

# Webmin
check process webmin with pidfile /var/webmin/miniserv.pid
group webmin
start program = "/etc/init.d/webmin start"
stop program = "/etc/init.d/webmin stop"
if failed host eye-vision.homeip.net port 10000 then restart
if 5 restarts within 5 cycles then timeout

# Samba
check process samba with pidfile /var/run/samba/smbd.pid
start program = "/etc/init.d/samba start"
stop program = "/etc/init.d/samba stop"

# Couchpotato
check process couchpotato with pidfile /var/run/couchpotato/server.pid
start program = "/etc/init.d/couchpotatoserver start"
stop program = "/etc/init.d/couchpotatoserver stop"
if failed port 5050 then restart
if 5 restarts within 5 cycles then timeout

# Sickbeard
check process sickbeard with pidfile /var/run/sickbeard/sickbeard.pid
start program = "/etc/init.d/sickbeard start"
stop program = "/etc/init.d/sickbeard stop"
if failed host eye-vision.homeip.net port 8081 then restart
if 5 restarts within 5 cycles then timeout

# Headphones
check process headphones with pidfile /var/run/headphones.pid
start program = "/etc/init.d/headphones start"
stop program = "/etc/init.d/headphones stop"
if failed host eye-vision.homeip.net port 8083 then restart
if 5 restarts within 5 cycles then timeout

# Sabnzbd+
check process sabnzbdplus with pidfile /var/run/sabnzbdplus/pid
start program = "/etc/init.d/sabnzbdplus start"
stop program = "/etc/init.d/sabnzbdplus stop"
if failed host eye-vision.homeip.net port 8082 then restart
if 5 restarts within 5 cycles then timeout

# Subsonic
check process subsonic with pidfile /var/run/subsonic.pid
start program = "/etc/init.d/subsonic start"
stop program = "/etc/init.d/subsonic stop"
if failed port 4040 then restart
if 5 restarts within 5 cycles then timeout

Om te checken of mijn Webserver nog werkt, controleer ik op de aanwezigheid van de file ‘token’ op mijn default web-adres. Je moet deze file zelf aanmaken en dan met een willekeurig string vullen. Zorg er wel voor dat de owner van deze file hetzelfde is als waarmee je apache2 opstart.

Start hierna Monit opnieuw op met het volgende commando:

/etc/init.d/monit restart

Vervolgens kun je de Monit webserver benaderen op:

http:/<ip-address>:2812/

Hieronder zie je een aantal screenshots van Monit.

M/Monit
Met M/Monit kun je meerdere servers waarop Monit draait op een overzichtelijk manier beheren. Ook hiervan is de installatie vrij eenvoudig. Download eerst de juiste versie voor jouw Linux server. Ga vervolgens naar de directory waar je deze hebt gedownload en voer de volgende commando’s uit:

tar zxf mmonit-3.1-linux-x64.tar.gz
mv mmonit-3.1 /usr/local

Vervolgens kun je het poort-nummer configureren waarop je M/Monit wilt benaderen in de file ‘/usr/local/mmonit-3.1/conf/server.xml. Ik heb dit zelf veranderd van 8080 in 8888.

Om M/Monit te starten voer je de volgende commando’s uit:

cd /usr/local/mmonit-3.1
./bin/mmonit start

Als je M/Monit automatisch wilt opstarten bij elke reboot van je Linux server dan moet je bovenstaande regels toevoegen aan de file ‘/etc/rc.local’.

Je kunt default inloggen met user-naam ‘admin’ en password ‘swordfish’. Dit kun je in het Admin menu eventueel nog aanpassen. Je moet vervolgens elke server die je met M/Monit wilt monitoren enablen in de configuratie file ‘/etc/monit/monitorrc’ door de volgende regel te enablen van het Monit package op de betreffende server.

set mmonit http://<user>:<password>@<ip-address>/collector

Vervolgens kun je M/Monit benaderen vanaf:

http://<ip-address>:<port>/

Helaas is M/Monit niet gratis, maar kun je deze wel 1 maand kostenloos uitproberen. Een 5-hosts licentie kost Euro 65,= en kun je online aanschaffen.

Hieronder zie je een aantal screenshots van M/Monit.

Vooral ‘alerts’ vind ik een erg handig functie. Hierbij wordt een email naar mezelf gestuurd indien er een fout-situatie wordt gedetecteerd. Verder zal Monit een crashed process automatisch opnieuw opstarten. Het bijhouden van mijn Linux server wordt steeds eenvoudiger zo 😀 .