Serwer www na Raspberry Pi5 z dostępem do internetu | Monit

Czas czytania: 7 minuty

W dzisiejszym świecie, gdzie niezawodność i dostępność usług są kluczowe, narzędzia do monitorowania systemów stają się niezbędnym elementem zarządzania infrastrukturą IT. Jednym z takich narzędzi jest Monit – wszechstronne rozwiązanie do monitorowania i zarządzania systemami Linux oraz innymi systemami z rodziny Unix. W tym artykule omówię, do czego służy Monit, jakie ma funkcje i jak można go efektywnie wykorzystać w codziennej pracy administratora systemów.

Czym jest Monit?

Monit jest otwartoźródłowym narzędziem do monitorowania i zarządzania procesami, usługami oraz zasobami systemowymi na systemach Unixowych. Jego głównym zadaniem jest automatyczne monitorowanie stanu systemu oraz reagowanie na wykryte problemy. Dzięki Monit administratorzy mogą zminimalizować przestoje systemowe i zwiększyć niezawodność swoich usług.

Jego główne funkcje i zastosowania to:

Monitorowanie procesów: Monit pozwala monitorować procesy systemowe i aplikacje, sprawdzając, czy działają poprawnie. Jeśli proces się zawiesi lub przestanie działać, Monit może go automatycznie zrestartować.

Monitorowanie zasobów systemowych: Monit może monitorować zużycie zasobów systemowych, takich jak pamięć, CPU, przestrzeń dyskowa, itp. Można ustawić progi, po przekroczeniu których Monit podejmie określone działania.

Monitorowanie plików i katalogów: Monit umożliwia monitorowanie plików i katalogów pod kątem zmian, takich jak modyfikacje, usunięcia czy zmiana uprawnień.

Monitorowanie usług sieciowych: Monit może monitorować różne usługi sieciowe, takie jak HTTP, FTP, SMTP, itp., sprawdzając ich dostępność i reagując na problemy.

Automatyczne naprawianie problemów: Jeśli Monit wykryje problem, może automatycznie podjąć działania naprawcze, takie jak restartowanie usług, wysyłanie powiadomień do administratorów, lub uruchomienie skryptów naprawczych.

Wysyłanie powiadomień: Monit może wysyłać powiadomienia e-mail lub inne powiadomienia, aby informować administratorów o wykrytych problemach i podjętych działaniach.

Łatwe konfigurowanie i używanie: Monit jest stosunkowo łatwy do skonfigurowania i używania, dzięki czytelnej składni plików konfiguracyjnych oraz przejrzystej dokumentacji.

monit2
monit

Instalacja Monit na Raspberry Pi 5

Instalacja Monit w systemie Linux jest prosta i można ją przeprowadzić za pomocą menedżera pakietów dostępnego w danej dystrybucji. Przykładowo, w systemie Raspberry Pi OS można to zrobić w następujący sposób:

sudo apt update
sudo apt install monit

Przykładowa konfiguracja Monit

Po zainstalowaniu Monit, następnym krokiem jest jego konfiguracja. Plik konfiguracyjny Monit jest czytelny i prosty w edycji. Oto przykład mojej konfiguracji dla Raspberry Pi 5:

set daemon 60
set logfile syslog facility log_daemon

# Konfiguracja serwera mailowego
set mailserver smtp.example.com port 587
    username "your-username"
    password "your-password"
    using tlsv1.2

# E-mail, na który będą wysyłane alerty
set alert [email protected]

set httpd port 2812 and SSL ENABLE PEMFILE /var/certs/monit.pem
allow admin:twojehasło

# minitorowanie load serwera
check system $HOST
    if loadavg (1min) > 3.8 then alert

check process sshd with pidfile /var/run/sshd.pid
 start program "/usr/sbin/service ssh start"
 stop program "/usr/sbin/service ssh stop"
 if failed port 22 protocol ssh then restart
 if 5 restarts within 5 cycles then timeout

check process apache with pidfile /var/run/apache2/apache2.pid
 group www-data
 start program = "/usr/sbin/service apache2 start" with timeout 60 seconds
 stop program = "/usr/sbin/service 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 4 for 8 cycles then stop
 if failed port 80 protocol http with timeout 15 seconds then restart
 if failed port 443 protocol https with timeout 15 seconds then restart
 if 3 restarts within 5 cycles then timeout

check process mysql with pidfile /var/run/mysqld/mysqld.pid
 group database
 start program = "/usr/sbin/service mysql start"
 stop program = "/usr/sbin/service mysql stop"
 if failed host 127.0.0.1 port 3306 then restart
 if 5 restarts within 5 cycles then timeout

#check process proftpd with pidfile /var/run/proftpd.pid
# start program = "/usr/sbin/service proftpd start"
# stop program = "/usr/sbin/service proftpd stop"
# if failed port 21 protocol ftp then restart
# if 5 restarts within 5 cycles then timeout
#
#check process postfix with pidfile /var/spool/postfix/pid/master.pid
# group mail
# start program = "/usr/sbin/service postfix start"
# stop program = "/usr/sbin/service postfix stop"
# if failed port 25 protocol smtp then restart
# if 5 restarts within 5 cycles then timeout
#
#check process nginx with pidfile /var/run/nginx.pid
# start program = "/usr/sbin/service nginx start"
# stop program = "/usr/sbin/service nginx stop"
# if failed host 127.0.0.1 port 80 then restart
#
# redis
check process redis with pidfile /var/run/redis/redis-server.pid
  group cache
  group redis
  start program = "/usr/sbin/service redis-server start" with timeout 60 seconds
  stop program  = "/usr/sbin/service redis-server stop" with timeout 60 seconds
  if failed host 127.0.0.1 port 6379 then restart
  if totalmem > 500 Mb then alert
  if cpu > 60% for 2 cycles then alert
  if cpu > 98% for 5 cycles then restart
  if 2 restarts within 2 cycles then alert
  depend redis_bin
  depend redis_rc

check file redis_bin with path /usr/bin/redis-server
  group redis
  include /etc/monit/templates/rootbin

check file redis_rc with path /etc/init.d/redis-server
  group redis
  include /etc/monit/templates/rootbin

check process fail2ban with pidfile /var/run/fail2ban/fail2ban.pid
    group services
    start program = "/etc/init.d/fail2ban force-start"
    stop  program = "/etc/init.d/fail2ban stop"
    if failed unixsocket /var/run/fail2ban/fail2ban.sock then restart
    if 5 restarts within 5 cycles then timeout

check file fail2ban_log with path /var/log/fail2ban.log
    if match "ERROR|WARNING" then alert

#
check process pureftpd with pidfile /var/run/pure-ftpd/pure-ftpd.pid
 start program = "/usr/sbin/service pure-ftpd-mysql start"
 stop program = "/usr/sbin/service pure-ftpd-mysql stop"
 if failed port 21 protocol ftp then restart
 if 5 restarts within 5 cycles then timeout
#
check process named with pidfile /var/run/named/named.pid
 start program = "/usr/sbin/service bind9 start"
 stop program = "/usr/sbin/service 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
#
#check process ntpd with pidfile /var/run/ntpd.pid
# start program = "/usr/sbin/service ntp start"
# stop program = "/usr/sbin/service ntp stop"
# if failed host 127.0.0.1 port 123 type udp then restart
# if 5 restarts within 5 cycles then timeout
#
#check process amavisd with pidfile /var/run/amavis/amavisd.pid
# group mail
# start program = "/usr/sbin/service amavis start"
# stop program = "/usr/sbin/service amavis stop"
# if failed port 10024 protocol smtp then restart
# if 5 restarts within 5 cycles then timeout
#
#check process courier-imap with pidfile /var/run/courier/imapd.pid
# group mail
# start program = "/usr/sbin/service courier-imap start"
# stop program = "/usr/sbin/service courier-imap stop"
# if failed host localhost port 143 type tcp protocol imap then restart
# if 5 restarts within 5 cycles then timeout
#
#check process courier-imap-ssl with pidfile /var/run/courier/imapd-ssl.pid
# group mail
# start program = "/usr/sbin/service courier-imap-ssl start"
# stop program = "/usr/sbin/service courier-imap-ssl stop"
# if failed host localhost port 993 type tcpssl sslauto protocol imap then restart
# if 5 restarts within 5 cycles then timeout
#
#check process courier-pop3 with pidfile /var/run/courier/pop3d.pid
# group mail
# start program = "/usr/sbin/service courier-pop start"
# stop program = "/usr/sbin/service courier-pop stop"
# if failed host localhost port 110 type tcp protocol pop then restart
# if 5 restarts within 5 cycles then timeout
#
#check process courier-pop3-ssl with pidfile /var/run/courier/pop3d-ssl.pid
# group mail
# start program = "/usr/sbin/service courier-pop-ssl start"
# stop program = "/usr/sbin/service courier-pop-ssl stop"
# if failed host localhost port 995 type tcpssl sslauto protocol pop then restart
# if 5 restarts within 5 cycles then timeout
#
#check process dovecot with pidfile /var/run/dovecot/master.pid
# group mail
# start program = "/usr/sbin/service dovecot start"
# stop program = "/usr/sbin/service dovecot stop"
# if failed host localhost port 143 type tcp protocol imap then restart
# if 5 restarts within 5 cycles then timeout

check process php5.6-fpm with pidfile /var/run/php/php5.6-fpm.pid
group php-fpm #change accordingly
start program = "/etc/init.d/php5.6-fpm start"
stop program  = "/etc/init.d/php5.6-fpm stop"
if failed unixsocket /var/run/php/php5.6-fpm.sock then restart
if 3 restarts within 5 cycles then timeout

check process php7.2-fpm with pidfile /var/run/php/php7.2-fpm.pid
group php-fpm #change accordingly
start program = "/etc/init.d/php7.2-fpm start"
stop program  = "/etc/init.d/php7.2-fpm stop"
if failed unixsocket /var/run/php/php7.2-fpm.sock then restart
if 3 restarts within 5 cycles then timeout

check process php7.3-fpm with pidfile /var/run/php/php7.3-fpm.pid
group php-fpm #change accordingly
start program = "/etc/init.d/php7.3-fpm start"
stop program  = "/etc/init.d/php7.3-fpm stop"
if failed unixsocket /var/run/php/php7.3-fpm.sock then restart
if 3 restarts within 5 cycles then timeout

check process php7.4-fpm with pidfile /var/run/php/php7.4-fpm.pid
group php-fpm #change accordingly
start program = "/etc/init.d/php7.4-fpm start"
stop program  = "/etc/init.d/php7.4-fpm stop"
if failed unixsocket /var/run/php/php7.4-fpm.sock then restart
if 3 restarts within 5 cycles then timeout

check process php8.0-fpm with pidfile /var/run/php/php8.0-fpm.pid
group php-fpm #change accordingly
start program = "/etc/init.d/php8.0-fpm start"
stop program  = "/etc/init.d/php8.0-fpm stop"
if failed unixsocket /var/run/php/php8.0-fpm.sock then restart
if 3 restarts within 5 cycles then timeout

check process php8.1-fpm with pidfile /var/run/php/php8.1-fpm.pid
group php-fpm #change accordingly
start program = "/etc/init.d/php8.1-fpm start"
stop program  = "/etc/init.d/php8.1-fpm stop"
if failed unixsocket /var/run/php/php8.1-fpm.sock then restart
if 3 restarts within 5 cycles then timeout

check process php8.2-fpm with pidfile /var/run/php/php8.2-fpm.pid
group php-fpm #change accordingly
start program = "/etc/init.d/php8.2-fpm start"
stop program  = "/etc/init.d/php8.2-fpm stop"
if failed unixsocket /var/run/php/php8.2-fpm.sock then restart
if 3 restarts within 5 cycles then timeout

check process php8.3-fpm with pidfile /var/run/php/php8.3-fpm.pid
group php-fpm #change accordingly
start program = "/etc/init.d/php8.3-fpm start"
stop program  = "/etc/init.d/php8.3-fpm stop"
if failed unixsocket /var/run/php/php8.3-fpm.sock then restart
if 3 restarts within 5 cycles then timeout

# Monitorowanie Samby
check process smbd with pidfile /var/run/samba/smbd.pid
group samba
start program = "/etc/init.d/smbd start"
stop  program = "/etc/init.d/smbd stop"
if failed host localhost port 445 type TCP then restart

# Monitorowanie NFS
check process NFS with pidfile /var/run/rpc.statd.pid
start program = "/etc/init.d/nfs-kernel-server start"
stop program  = "/etc/init.d/nfs-kernel-server stop"
if failed host 127.0.0.1 port 2049 type tcp for 2 cycles then restart
if 2 restarts within 3 cycles then unmonitor

# Monitorowanie temperatury CPU
check program cpu_temperature with path "/etc/monit/customscripts/picputemp.sh"
with timeout 5 seconds
if status != 0 then alert

# Monitorowanie miejsca na dysku
check filesystem rootfs with path /
if space usage > 95% then alert

plik konfiguracyjny jaki należy zmodyfikować to: /etc/monit/monitrc

Ustaw konfigurację serwera mailowego z jakiego mają być wysyłane alerty. Kolejno Nazwa serwera poczty wychodzącej, użytkownik i hasło, a następnie adres email na jaki mają dochodzić alerty.

Zmień także admin:twojehasło w powyższym pliku konfiguracyjnym, aby ustawić własny login i hasło do panelu Monit. Pierwsza fraza przed dwukropkiem to login, druga fraza za dwukropkiem to hasło jakim się będziesz logowal do Monit.

Następnie utwórz skrypt, który pobiera wartości temperatury procesora

W katalogu /etc/monit/customscripts/ utwórz plik poniższym poleceniem

touch picputemp.sh

i wpisz do niego poniższą zawartość

#!/bin/bash

# Configuration
templimit="70" # CPU temperature must be less than this value

# Get output
output=$(vcgencmd measure_temp | egrep -o '[0-9]*\.[0-9]*')

# If CPU Temperature is below templimit
if (( $(echo "$output < $templimit" |bc) -l )); then
  echo "CPU Temperature: $output°C"
  exit 0
fi

# If CPU temperature is at or above temp limit
if (( $(echo "$output >= $templimit" |bc) -l )); then
  echo "CPU temperature is above limit threshold of $templimit°C, current temp is: $output°C" >&2
  exit 1
fi

zapisz plik i nadaj mu prawa wykonania

chmod 755 picputemp.sh

Wygeneruj certyfikat ssl

Wykonaj poniższe polecenia

mkdir /var/certs
cd /var/certs
touch monit.cnf

Następnie do monit.cnf wpisz poniższą zawartość

# create RSA certs - Server

RANDFILE = ./openssl.rnd

[ req ]
default_bits = 2048
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

Zapisz plik i w konsoli wpisz

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

Zrestartuj Monit wpisując w konsoli

systemctl stop monit
systemctl start monit

Na koniec odblokuj port 2812 na swoim firewallu tak jak to opisywałem w poprzednich częściach tutoriala.

Do panelu Monit możesz wejść wpisując w przeglądarce https://twój-zewnętrzny-ip:2812

Podsumowanie

Monit to potężne i wszechstronne narzędzie do monitorowania i zarządzania systemami Linux. Dzięki swojej elastyczności, łatwości konfiguracji i zdolności do automatycznego naprawiania problemów, Monit jest niezastąpionym elementem infrastruktury IT. Niezależnie od tego, czy zarządzasz małym serwerem, czy dużą infrastrukturą, Monit może znacząco poprawić niezawodność i dostępność Twoich usług.

Autor

  • gielo

    Witam! Jestem pasjonatem technologii z wykształceniem matematycznym i informatycznym. W kręgu moich zainteresowań leży między innymi elektronika, linuks i technologie serwerowe.

    View all posts