Benutzer-Werkzeuge

Webseiten-Werkzeuge


howto:monit

Monit


Autor: F4RR3LL|sven
E-Mail: sven.richter@nixhelp.de
Stand: 01.04.2015


Monit ist ein nettes Tool mit dem man Systemprozesse überwachen kann. Mann kann auch andere Sachen damit machen… DNS pingen lassen, Rechte auf Ordner überwachen etc. Habe ich alles mal durchprobiert. Ich finde diese *extras* nicht so sinnig da Monit auch ganz schnell mal anfängt zu nerven. Man kann sich bei Änderungen auch per Mail informieren lassen. Man kann es aber auch einfach nur im Browser anschaun, oder man lässt Monit einfach werkeln.

Die Installation


apt-get install monit


Nach der Installation erhalten wir eine Meldung des Systems →

Starting daemon monitor: monit won't be started/stopped
        unless it it's configured
        please configure monit and then edit /etc/default/monit
        and set the "startup" variable to 1 in order to allow
        monit to start


Diese wollen wir natürlich nicht unbeachtet lassen also begeben wir uns auf direktem Weg zu

nano /etc/default/monit


hier aus startup=0 eine 1 machen und wieder speichern. (das geht mit strg o)

Ab jetzt sollte sich Monit auch starten lassen. Bringt aber noch nix, da wir Monit ja noch gar keine Aufgabe gegeben haben ;)
Also machen wir mal weiter.

Die Konfiguration


Um Monit sinnvoll nutzen zu können müssen wir uns in die Konfigurationsdatei begeben

nano /etc/monit/monitrc

Das Allgemeine


Zuerst passen wir das Intervall an wie oft Monit tätig werden soll, hier 60 Sekunden:

aus 

# set daemon  120
 
wird 

set daemon  60


Wir möchten natürlich Logs haben von dem was Monit so treibt:

aus 

# set logfile syslog facility log_daemon

wird

set logfile /var/log/monit


In der folgenden Passage besteht die Möglichkeit, dass ihr euch per Mail informieren lasst was euer Monit so feststellt und treibt. Ich werde mal Beispielhaft die ganze Passage in geändertem Zustand posten. Ich denke dann erkennt ihr leicht was ihr ändern müsst.

## Set list of mailservers for alert delivery. Multiple servers may be
## specified using comma separator. By default monit uses port 25 - it is
## possible to override it with the PORT option.
#
 set mailserver mail.mailserver.tld,               # primary mailserver
#                backup.bar.baz port 10025,  # backup mailserver on port 10025
#                localhost                   # fallback relay
#
#
## By default monit will drop the event alert, in the case that there is no
## mailserver available. In the case that you want to keep the events for
## later delivery retry, you can use the EVENTQUEUE statement. The base
## directory where undelivered events will be stored is specified by the
## BASEDIR option. You can limit the maximal queue size using the SLOTS
## option (if omited then the queue is limited just by the backend filesystem).
#
# set eventqueue
#     basedir /var/monit  # set the base directory where events will be stored
#     slots 100           # optionaly limit the queue size
#
#
## Monit by default uses the following alert mail format:
##
## --8<--
## From: monit@$HOST                         # sender
## Subject: monit alert --  $EVENT $SERVICE  # subject
##
## $EVENT Service $SERVICE                   #
##                                           #
##      Date:        $DATE                   #
##      Action:      $ACTION                 #
##      Host:        $HOST                   # body
##      Description: $DESCRIPTION            #
##                                           #
## Your faithful employee,                   #
##  monit                                     #
## --8<--
##
## You can override the alert message format or its parts such as subject
## or sender using the MAIL-FORMAT statement. Macros such as $DATE, etc.
## are expanded on runtime. For example to override the sender:
#
 set mail-format { from: monit@mailserver.tld }
#
#
## You can set the alert recipients here, which will receive the alert for
## each service. The event alerts may be restricted using the list.
#
 set alert wo-es-hinsoll@mailserver.tld                       # receive all alerts
 set alert wo-es-hinsoll@mailserver.tld only on { timeout }  # receive just service-
#                                                # timeout alert
#

Als nächstes besteht die Möglichkeit sich den ganzen Status von Monit auch im Browser anzuschaun, und dort auch Dienste zu stoppen oder auch zu starten.
Also hier nicht sorglose Passwörter verteilen.
Die Config ist wie folgt anzupassen:

## Monit has an embedded webserver, which can be used to view the
## configuration, actual services parameters or manage the services using the
## web interface.
#
 set httpd port 12345 and    # hier könnt ihr einen Port frei nach Nase wählen ;)
#     use address localhost  # only accept connection from localhost
#     allow $IP        # wer eine feste IP hat oder über OPENVPN auf den Server geht kann hier die feste IP eintragen
     allow $username:$supergeheimespass      # wer das Pass kennt kann viel Mist machen ;)
#


Das Spezielle


Nachdem nun die allgemeinen Einstellungen für Monit vorgenommen sind soll es ja endlich Dienste überwachen. Hierzu einige Beispielconfigs wie man sie am Ende der Konfiguration anhängen kann. Diese Liste kann nach selbem Schema für jeden x beliebigen Dienst erweitert werden.

# SSH Server Überwachung
#
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 host 127.0.0.1 port 22 protocol ssh then restart
if 5 restarts within 5 cycles then timeout
#
# Lighttpd Server Überwachung
#
check process lighttpd with pidfile /var/run/lighttpd.pid
start program = "/etc/init.d/lighttpd start"
stop program = "/etc/init.d/lighttpd stop"
if failed host 127.0.0.1 port 80 then restart
if failed host 127.0.0.1 port 443 then restart
#
# MSYSQL Server Überwachung
#
check process mysqld 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
#
# POSTFIX Server Überwachung
#
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 127.0.0.1 port 25 protocol smtp then restart
if 5 restarts within 5 cycles then timeout
#
# Policyd-Weight Server Überwachung
#
check process policyd-weight with pidfile /var/run/policyd-weight.pid
group  mail
start program = "/etc/init.d/policyd-weight start"
stop program = "/etc/init.d/policyd-weight stop"
if failed host 127.0.0.1 port 12525 then restart
if 5 restarts within 5 cycles then timeout
#
# Postgrey Server Überwachung
#
check process postgrey with pidfile /var/run/postgrey.pid
group mail
start program = "/etc/init.d/postgrey start"
stop program = "/etc/init.d/postgrey stop"
if failed host 127.0.0.1 port 60000 then restart
if 5 restarts within 5 cycles then timeout
#
# Dovecot Server Überwachung
#
check process dovecot with pidfile /var/run/dovecot/master.pid
group mail
start program = "/etc/init.d/dovecot start"
stop program = "/etc/init.d/dovecot stop"
if failed host 127.0.0.1 port 110 then restart
if 5 restarts within 5 cycles then timeout
#
# Crons
#
 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
#
# OpenVPN
#
 check process openvpn with pidfile /var/run/openvpn.server.pid
   group system
   start program = "/etc/init.d/openvpn start"
   stop  program = "/etc/init.d/openvpn stop"
   if 5 restarts within 5 cycles then timeout
#
# Squid Proxy
#
 check process squid with pidfile /var/run/squid.pid
   group system
   start program = "/etc/init.d/squid start"
   stop  program = "/etc/init.d/squid stop" 
   if failed host 127.0.0.1 port 3128 protocol ssh then restart
   if 5 restarts within 5 cycles then timeout 
#
# Openfire Jabber
#
 check process openfire with pidfile /var/run/openfire.pid
   group system
   start program = "/etc/init.d/openfire start"
   stop  program = "/etc/init.d/openfire stop" 
   if failed host 127.0.0.1 port 5222 protocol ssh then restart
   if 5 restarts within 5 cycles then timeout 
#
# Pure-FTP
#
check process pure-ftpd with pidfile /var/run/pure-ftpd/pure-ftpd.pid
   start program  "/etc/init.d/pure-ftpd-mysql start"
   stop program  "/etc/init.d/pure-ftpd-mysql stop"
   if failed host 127.0.0.1 port 21 then restart
   if 5 restarts within 5 cycles then timeout
#
# courier pop3
#
check process pop3 with pidfile /var/run/courier/pop3d.pid
   group mail
   start program = "/etc/init.d/courier-pop start"
   stop  program = "/etc/init.d/courier-pop stop"
   if failed port 110 then restart
   if 5 restarts within 5 cycles then timeout
#
# courier pop3 ssl
#
check process pop3-ssl with pidfile /var/run/courier/pop3d-ssl.pid
   group mail
   start program = "/etc/init.d/courier-pop-ssl start"
   stop  program = "/etc/init.d/courier-pop-ssl stop"
   if failed port 995 then restart
   if 5 restarts within 5 cycles then timeout
#
# courier imap
#
check process imap with pidfile /var/run/courier/imapd.pid
   group mail
   start program = "/etc/init.d/courier-imap start"
   stop  program = "/etc/init.d/courier-imap stop"
   if failed port 143 then restart
   if 5 restarts within 5 cycles then timeout
#
# Spamassassin
#
check process spamd with pidfile /var/run/spamd.pid
start program = "/etc/init.d/spamassassin start"
stop  program = "/etc/init.d/spamassassin stop"
if failed port 783 then restart
if 5 restarts within 5 cycles then timeout
#
# Proftpd Server Überwachung
#
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 host 127.0.0.1 port 21 then restart
if 5 restarts within 5 cycles then timeout
#
# Amavis
#
check process amavis with pidfile /var/run/amavis/amavisd.pid
start program = "/etc/init.d/amavis start"
stop  program = "/etc/init.d/amavis stop"
if failed port 10024 then restart
if 5 restarts within 5 cycles then timeout
#
# Bind9
#
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 port 53 then restart
if 5 restarts within 5 cycles then timeout
#
# Apache2
#
check process apache2 with pidfile /var/run/apache2.pid
start program = "/etc/init.d/apache2 start"
stop  program = "/etc/init.d/apache2 stop"
if failed port 80 then restart
if 5 restarts within 5 cycles then timeout
#
# Nginx
#
check process nginx with pidfile /var/run/nginx.pid
start program = "/etc/init.d/nginx start"
stop  program = "/etc/init.d/nginx stop"
if failed port 80 then restart
if 5 restarts within 5 cycles then timeout
#
# Varnish
#
check process varnishd with pidfile /var/run/varnishd.pid
start program = "/etc/init.d/varnish start"
stop  program = "/etc/init.d/varnish stop"
if failed port 6081 then restart
if 5 restarts within 5 cycles then timeout
#
# php5-fpm
#
check process php5-fpm with pidfile /var/run/php5-fpm.pid
  start program = "/etc/init.d/php5-fpm start"
  stop program  = "/etc/init.d/php5-fpm stop"
  if failed unixsocket /var/run/php5-fpm.sock then restart
  if 5 restarts within 5 cycles then timeout

Der Start

Nachdem nun alles in der config drin ist was rein soll kann man mit monit testen ob man auch alles richtig gemacht hat. Mit folgendem Befehl prüft ihr ob die Syntax in eurer config stimmt.

/etc/init.d/monit syntax

Die Ausgabe sollte sein
Control file syntax OK

Nun ist es soweit wir starten Monit

/etc/init.d/monit start


Von nun an läuft Monit auf eurem Server und überwacht die Dienste auf eurem Server. Sollte ein Dienst abschmieren wird Monit ihn wieder starten.
Wer die Infos per Mail an hat bekommt auch Mails wenn mal eine pid wechselt usw. Das lasse ich meistens aus weil das nur nervt ;)
Wer Webzugriff aktiviert hat conected auf seinen Server via:

http://$username:$supergeheimespass@$Serverip:12345

So das wars eigentlich. Viel Spaß mit Monit. Gruß Sven

Zurück zum Index


howto/monit.txt · Zuletzt geändert: 2022/10/11 10:55 (Externe Bearbeitung)

Seiten-Werkzeuge