Monitorare un server con Munin e Monit: differenze tra le versioni

Vai alla navigazione Vai alla ricerca
nessun oggetto della modifica
Nessun oggetto della modifica
(13 versioni intermedie di 2 utenti non mostrate)
Riga 1: Riga 1:
{{Versioni compatibili | Squeeze | Wheezy}}
{{Warningbox | Per Debian Stretch e successive si veda la guida aggiornata: [[Monitorare un server con Munin e Monit - Stretch]]}}
== Introduzione ==
== Introduzione ==
In questa guida vedremo come monitorare un server Debian Wheezy attraverso gli strumenti ''munin'' e ''monit''.
In questa guida vedremo come monitorare un server Debian Wheezy attraverso gli strumenti ''munin'' e ''monit''.
Riga 33: Riga 35:
* decommentiamo le linee ''dbdir, htmldir, logdir, rundir, tmpldir''
* decommentiamo le linee ''dbdir, htmldir, logdir, rundir, tmpldir''
* sostituiamo ''localhost.localdomain'' con il FQDN del nostro server, ad esempio ''server1.example.com'', nella sezione ''simple host tree''
* sostituiamo ''localhost.localdomain'' con il FQDN del nostro server, ad esempio ''server1.example.com'', nella sezione ''simple host tree''
La configurazione di Munin per Apache si trova nel file <tt>/etc/apache2/conf.d/munin</tt> (che attualmente è un symlink verso <tt>/etc/munin/apache.conf</tt>): tra le altre cose definisce un alias chiamato ''munin'' alla directory di output dei report HTML di Munin <tt>/var/cache/munin/www</tt>; questo significa che possiamo accedere alle statistiche di Munin da tutti i Virtual Host del nostro webserver attraverso il percorso relativo <tt>/munin</tt> (ad esempio ''http://www.example.com/munin'').
La configurazione di Munin per Apache si trova nel file <code>/etc/apache2/conf.d/munin</code> (che attualmente è un [[symlink]] verso <code>/etc/munin/apache.conf</code>): tra le altre cose definisce un alias chiamato ''munin'' alla directory di output dei report HTML di Munin <code>/var/cache/munin/www</code>; questo significa che possiamo accedere alle statistiche di Munin da tutti i Virtual Host del nostro webserver attraverso il percorso relativo <code>/munin</code> (ad esempio ''http://www.example.com/munin'').
<br/>
<br/>
Apriamo il file in questione:
Apriamo il file in questione:
Riga 51: Riga 53:
Allow from 192.168.0.0/24
Allow from 192.168.0.0/24
</pre>
</pre>
altrimenti Munin sarà accessibile solo da localhost.
altrimenti Munin sarà accessibile solo da [[localhost]].
<br/><br/>
<br/><br/>
Riavviamo Apache:
Riavviamo Apache:
Riga 61: Riga 63:
# /etc/init.d/munin-node restart
# /etc/init.d/munin-node restart
</pre>
</pre>
Attendiamo qualche minuto, per dare a Munin il tempo di generare le prime statistiche, e rechiamoci all'indirizzo http://ip.del.mio.server/munin per vedere i primi grafici.
Attendiamo qualche minuto, per dare a Munin il tempo di generare le prime statistiche, e rechiamoci all'indirizzo '''<nowiki>http://ip.del.mio.server/munin</nowiki>''' per vedere i primi grafici.


== Protezione di Munin ==
== Protezione di Munin ==
E' una buona idea proteggere tramite password l'accesso ai grafici HTML prodotti da Munin; vogliamo forse che tutto il mondo veda come sta funzionando il nostro server?
È una buona idea proteggere tramite password l'accesso ai grafici HTML prodotti da Munin; vogliamo forse che tutto il mondo veda come sta funzionando il nostro server?
<br/>
<br/>
Per proteggere Munin dobbiamo creare un file di password <tt>/etc/munin/munin-htpasswd</tt> e abilitare un utente chiamato ''admin'':
Per proteggere Munin dobbiamo creare un file di password <code>/etc/munin/munin-htpasswd</code> e abilitare un utente chiamato ''admin'':
<pre>
<pre>
# htpasswd -c /etc/munin/munin-htpasswd admin
# htpasswd -c /etc/munin/munin-htpasswd admin
Riga 83: Riga 85:
[...]
[...]
</pre>
</pre>
Infine riavviamo Apache:
Apportiamo ancora una modifica, aggiungendo all'inizio del file la sezione seguente, per abilitare automaticamente HTTPS sulle pagine di Munin:
<pre>
<IfModule mod_rewrite.c>
<IfModule mod_ssl.c>
        <Location /munin>
        RewriteEngine on
        RewriteCond %{HTTPS} off
        RewriteRule ^(.*)$ https://%{HTTP_HOST}/munin [R]
        </Location>
</IfModule>
</IfModule>
</pre>
Infine riavviamo Apache e Munin:
<pre>
<pre>
# /etc/init.d/apache2 restart
# /etc/init.d/apache2 restart
# /etc/init.d/munin-node restart
</pre>
== Installazione di Monit ==
Installare Monit in Debian è semplice:
<pre>
# apt-get install monit
</pre>
Il file di configurazione è <code>/etc/monit/monitrc</code>. Il file di default contiene un sacco di esempi. Nel nostro caso noi vogliamo:
* monitorare proftpd, sshd, mysql, apache, postfix
* abilitare l'interfaccia di monit sulla porta 2812
* abilitare HTTPS sull'interfaccia
* proteggere le pagine con una username e una password
* inviare degli alert vie email all'indirizzo root@localhost
Salviamo la configurazione originale e creiamo quindi il nostro file di configurazione, secondo le specifiche stabilite:
<pre>
# cp /etc/monit/monitrc /etc/monit/monitrc_orig
# cat /dev/null > /etc/monit/monitrc
# nano /etc/monit/monitrc
</pre>
</pre>
dandogli questo contenuto:
<pre>
set daemon  60
set logfile syslog facility log_daemon
set mailserver localhost
set mail-format { from: monit@server1.example.com }
set alert root@localhost
set httpd port 2812 and
    use address IP.DEL.NOSTRO.SERVER
    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/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
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
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 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
check process munin-node with pidfile /var/run/munin/munin-node.pid
    group system
    start program = "/etc/init.d/munin-node start"
    stop program  = "/etc/init.d/munin-node stop"
    if failed host localhost port 4949 then restart
    if 5 restarts within 5 cycles then timeout
#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 host 127.0.0.1 port 80 then restart
#
#check process memcached with pidfile /var/run/memcached.pid
#  start program = "/etc/init.d/memcached start"
#  stop  program = "/etc/init.d/memcached stop"
#  if failed host 127.0.0.1 port 11211  then restart
#
#check process pureftpd 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 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 = "/etc/init.d/bind9 start"
#  stop program = "/etc/init.d/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 = "/etc/init.d/ntp start"
#  stop  program = "/etc/init.d/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 mailman with pidfile /var/run/mailman/mailman.pid
#  group mail
#  start program = "/etc/init.d/mailman start"
#  stop  program = "/etc/init.d/mailman stop"
#
#check process amavisd with pidfile /var/run/amavis/amavisd.pid
#  group mail
#  start program = "/etc/init.d/amavis start"
#  stop  program = "/etc/init.d/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 = "/etc/init.d/courier-imap start"
#  stop program = "/etc/init.d/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 = "/etc/init.d/courier-imap-ssl start"
#  stop program = "/etc/init.d/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 = "/etc/init.d/courier-pop start"
#  stop program = "/etc/init.d/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 = "/etc/init.d/courier-pop-ssl start"
#  stop program = "/etc/init.d/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 = "/etc/init.d/dovecot start"
#  stop program = "/etc/init.d/dovecot stop"
#  if failed host localhost port 993 type tcpssl sslauto protocol imap then restart
#  if 5 restarts within 5 cycles then timeout
#
#check process clamav with pidfile /var/run/clamav/clamd.pid
#    group virus
#    start program = "/etc/init.d/clamav-daemon start"
#    stop  program = "/etc/init.d/clamav-daemon stop"
#    if failed host localhost port 3310 then restart
#    if 5 restarts within 5 cycles then timeout
#
#check process freshclam with pidfile /var/run/clamav/freshclam.pid
#    group virus
#    start program = "/etc/init.d/clamav-freshclam start"
#    stop  program = "/etc/init.d/clamav-freshclam stop"
#    if 5 restarts within 5 cycles then timeout
#
#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
#
#check process dspam with pidfile /var/run/dspam/dspam.pid
#    group mail
#    start program = "/etc/init.d/dspam start"
#    stop program = "/etc/init.d/dspam stop"
#    if 5 restarts within 5 cycles then timeout
#
#check process fail2ban with pidfile /var/run/fail2ban/fail2ban.pid
#    start program = "/etc/init.d/fail2ban 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 process vsftpd with pidfile /var/run/vsftpd/vsftpd.pid
#    start program = "/etc/init.d/vsftpd start"
#    stop program  = "/etc/init.d/vsftpd stop"
#    if failed port 21 protocol ftp then restart
#    if 5 restarts within 5 cycles then timeout
</pre>
Nella configurazione sono stati lasciati, commentati, anche i parametri per monitorare servizi diversi da quelli impostati nel nostro caso specifico.
<br/>
Nella parte di configurazione dedicata ad Apache si trova:
<pre>
  if failed host www.example.com port 80 protocol http
      and request "/monit/token" then restart
</pre>
che significa che Monit prova a connettersi a www.example.com sulla porta 80 e cerca di accedere al file <code>/monit/token</code>, il cui path completo è <code>/var/www/www.example.com/web/monit/token</code>, supponendo che la root del nostro sito web sia <code>/var/www/www.example.com/web</code>. Di conseguenza dobbiamo creare il file:
<pre>
# mkdir /var/www/www.example.com/web/monit
# echo "hello" > /var/www/www.example.com/web/monit/token
</pre>
== Protezione di Monit ==
Adesso occorre creare il certificato pem (<code>/var/certs/monit.pem</code>) di cui ci serviremo per proteggere via SSL l'interfaccia web di Monit:
<pre>
# mkdir /var/certs
# cd /var/certs
</pre>
Abbiamo bisogno di una configurazione OpenSSL per creare il nostro certificato. Il file di configurazione può essere come il seguente:
<pre>
# nano /var/certs/monit.cnf
</pre>
<pre>
# 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            = root@monit.mo
[ cert_type ]
nsCertType = server
</pre>
Ora possiamo creare il certificato:
<pre>
# openssl req -new -x509 -days 3650 -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
</pre>
Finalmente, possiamo avviare Monit:
<pre>
# /etc/init.d/monit start
</pre>
Puntando il browser all'indirizzo '''<nowiki>https://www.example.com:2812</nowiki>''' e facendo login con le credenziali impostate in precedenza nella configurazione (admin / test) raggiungeremo l'interfaccia web di Monit.
{{Autori
|Autore = [[Utente:Ferdybassi|Ferdybassi]] 15:31, 09 mar 2014 (CEST)
}}
[[Categoria: Monitoraggio]]

Menu di navigazione