Serverüberwachung · 4 min read · Oct 16, 2025

Serverüberwachung Mit munin Und monit Auf CentOS 5.2 - Seite 2

5 Installieren Und Konfigurieren von monit

Als nächstes installieren wir monit:

yum install monit

Dann erstellen wir die Systemstartlinks für monit:

chkconfig --levels 235 monit on

monits Standardkonfigurationsdatei ist /etc/monit.conf, wo Sie einige Konfigurationsbeispiele finden können (Sie können weitere Konfigurationsbeispiele auf http://mmonit.com/wiki/Monit/ConfigurationExamples finden), die alle auskommentiert sind, aber es sagt monit auch, dass es im Verzeichnis /etc/monit.d nach Konfigurationsdateien suchen soll. Daher erstellen wir anstelle der Modifikation von /etc/monit.conf eine neue Konfigurationsdatei /etc/monit.d/monitrc. In meinem Fall möchte ich proftpd, sshd, mysql, apache und postfix überwachen, ich möchte die monit-Weboberfläche auf Port 2812 aktivieren, ich möchte eine https-Weboberfläche, ich möchte mich mit dem Benutzernamen admin und dem Passwort test in die Weboberfläche einloggen, und ich möchte, dass monit E-Mail-Benachrichtigungen an root@localhost sendet, sodass meine Datei so aussieht:

vi /etc/monit.d/monitrc

| set daemon 60 set logfile syslog facility log_daemon set mailserver localhost set mail-format { from: [email protected] } set alert root@localhost set httpd port 2812 and SSL ENABLE PEMFILE /var/certs/monit.pem allow admin:test 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 process sshd with pidfile /var/run/sshd.pid start program "/etc/init.d/sshd start" stop program "/etc/init.d/sshd stop" if failed port 22 protocol ssh then restart if 5 restarts within 5 cycles then timeout check process mysql with pidfile /var/run/mysqld/mysqld.pid group database start program = "/etc/init.d/mysqld start" stop program = "/etc/init.d/mysqld stop" if failed host 127.0.0.1 port 3306 then restart if 5 restarts within 5 cycles then timeout check process apache with pidfile /var/run/httpd.pid group www start program = "/etc/init.d/httpd start" stop program = "/etc/init.d/httpd stop" if failed host www.example.com 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 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 |

(Bitte stellen Sie sicher, dass Sie nur Prozesse überprüfen, die tatsächlich auf Ihrem Server vorhanden sind - andernfalls wird monit nicht gestartet. Das heißt, wenn Sie monit anweisen, Postfix zu überprüfen, aber Postfix nicht auf dem System installiert ist, wird monit nicht gestartet.)

Die Konfigurationsdatei ist ziemlich selbsterklärend; wenn Sie sich über eine Option unsicher sind, werfen Sie einen Blick in die monit-Dokumentation: http://mmonit.com/monit/documentation/monit.html

Im Apache-Teil der monit-Konfiguration finden Sie dies:

| if failed host www.example.com port 80 protocol http and request "/monit/token" then restart |

was bedeutet, dass monit versucht, sich mit www.example.com über Port 80 zu verbinden und versucht, auf die Datei /monit/token zuzugreifen, die /var/www/www.example.com/web/monit/token ist, da das Dokumentenstammverzeichnis unserer Website /var/www/www.example.com/web ist. Wenn monit nicht erfolgreich ist, bedeutet das, dass Apache nicht läuft, und monit wird versuchen, es neu zu starten. Jetzt müssen wir die Datei /var/www/www.example.com/web/monit/token erstellen und einen zufälligen String hinein schreiben:

mkdir /var/www/www.example.com/web/monit  
echo "hello" > /var/www/www.example.com/web/monit/token

Als nächstes erstellen wir das pem-Zertifikat (/var/certs/monit.pem), das wir für die SSL-verschlüsselte monit-Weboberfläche benötigen:

mkdir /var/certs  
cd /var/certs

Wir benötigen eine OpenSSL-Konfigurationsdatei, um unser Zertifikat zu erstellen. Sie kann so aussehen:

vi /var/certs/monit.cnf

| # create RSA certs - Server RANDFILE = ./openssl.rnd [ req ] default_bits = 1024 encrypt_key = yes distinguished_name = req_dn x509_extensions = cert_type [ req_dn ] countryName = Country Name (2 letter code) countryName_default = MO stateOrProvinceName = State or Province Name (full name) stateOrProvinceName_default = Monitoria localityName = Locality Name (eg, city) localityName_default = Monittown organizationName = Organization Name (eg, company) organizationName_default = Monit Inc. organizationalUnitName = Organizational Unit Name (eg, section) organizationalUnitName_default = Dept. of Monitoring Technologies commonName = Common Name (FQDN of your server) commonName_default = server.monit.mo emailAddress = Email Address emailAddress_default = [email protected] [ cert_type ] nsCertType = server |

Jetzt erstellen wir das Zertifikat wie folgt:

openssl req -new -x509 -days 365 -nodes -config ./monit.cnf -out /var/certs/monit.pem -keyout /var/certs/monit.pem
openssl gendh 512 >> /var/certs/monit.pem
openssl x509 -subject -dates -fingerprint -noout -in /var/certs/monit.pem
chmod 700 /var/certs/monit.pem

Schließlich können wir monit starten:

/etc/init.d/monit start

Jetzt zeigen Sie mit Ihrem Browser auf https://www.example.com:2812/ (stellen Sie sicher, dass Port 2812 nicht von Ihrer Firewall blockiert wird), melden Sie sich mit admin und test an, und Sie sollten die monit-Weboberfläche sehen. Sie sollte so aussehen:

(Hauptbildschirm)

(Apache-Statusseite)

Je nach Ihrer Konfiguration in /etc/monit.d/monitrc wird monit Ihre Dienste neu starten, wenn sie ausfallen, und Benachrichtigungs-E-Mails senden, wenn sich die Prozess-IDs der Dienste ändern usw.

Viel Spaß!

6 Links

Share: X/Twitter LinkedIn

Erhalte neue Beiträge in deinem Posteingang.

Kein Spam. Jederzeit abmelden.