Tomcat monitor con Nagios – Riavviare il servizio da remoto

Il nome Nagios è sicuramente noto agli addetti ai lavori nell’ambito del monitoraggio di server e servizi. Ho recentemente configurato il monitoraggio remoto di un Apache Tomcat con la possibilità di riavvio del servizio in caso di down. Vediamo come.

Nagios

 

 

 

 

Nel caso in esame le componenti sono le seguenti:

  • server di monitoraggio centralizzato con servizio Nagios 3
  • servizio monitorato Apache Tomcat 7 installato su server remoto, con client nrpe

Sul server Nagios gli interventi sono due. Bisogna innanzitutto configurare un event handler per il riavvio del servizio remoto. Per fare questo, ho aggiunto al file commands.cfg le seguenti righe

#tomcat restart nrpe
define command{
command_name    restart_tomcat
command_line    $USER1$/check_nrpe -H $HOSTADDRESS$ -c restart-tomcat -a $SERVICESTATE$ $SERVICESTATETYPE$ $SERVICEATTEMPT$
}

che definiscono un nuovo comando, chiamato restart_tomcat, che di fatto richiede al client nrpe l’esecuzione del comando ‘restart-tomcat’, passando come argomenti lo stato del servizio monitorato. Va poi configurato il monitoraggio specifico per il tomcat, che ho gestito con una richiesta http con verifica di un testo nella risposta:

#service to monitor tomcat
define service{
use                             generic-service         ; Name of service template to use
host_name                       HOSTNAME
service_description             HTTP
check_command            check_url!IPADDRESS!/REQUEST_URI!”VERIFY_TEXT”!30
notifications_enabled           1
event_handler restart_tomcat
}

dove vanno adattate in base alla situazione i valori di HOSTNAME, IPADDRESS, REQUEST_URI e il VERIFY_TEXT da cercare nella pagina restituita. A questo punto verificare la configurazione del server con il comando

nagios3 -v /etc/nagios3/nagios.cfg

per essere sicuri che non ci siano errori di sintatti.

Sul server dove è installato Tomcat ci sono varie operazioni da eseguire. Innanzitutto occorre aver installato il client NRPE. Le configurazioni da fare sul client sono le seguenti:

  • allowed_hosts=127.0.0.1,NAGIOS_SERVER_IPADDRESS
    qui dovrete configurare l’ip del server nagios, per consentire la comunicazione da remoto (attenzione ad eventuali firewall)
  • dont_blame_nrpe=1
    per consentire ad nrpe di eseguire comandi sul server locale richiesti dal server remoto
  • command[restart-tomcat]=/usr/lib64/nagios/eventhandlers/tomcat-restart.sh $ARG1$ $ARG2$ $ARG3$
    per aggiungere il comando di riavvio del servizio tomcat locale (il valore restart-tomcat deve coincidere con quello specificato nel commands.cfg)

Fatto questo, va creato lo script /usr/lib64/nagios/eventhandlers/tomcat-restart.sh, che andrà adattato al servizio da riavviare:

#!/bin/sh
#
# Event handler script for restarting the web server on the local machine
#
# Note: This script will only restart the web server if the service is
#       retried 3 times (in a “soft” state) or if the web service somehow
#       manages to fall into a “hard” error state.
#

# What state is the HTTP service in?
case “$1” in
OK)
# The service just came back up, so don’t do anything…
;;
WARNING)
# We don’t really care about warning states, since the service is probably still running…
;;
UNKNOWN)
# We don’t know what might be causing an unknown error, so don’t do anything…
;;
CRITICAL)
# Aha!  The HTTP service appears to have a problem – perhaps we should restart the server…
# Is this a “soft” or a “hard” state?
case “$2” in

# We’re in a “soft” state, meaning that Nagios is in the middle of retrying the
# check before it turns into a “hard” state and contacts get notified…
SOFT)
# What check attempt are we on?  We don’t want to restart the web server on the first
# check, because it may just be a fluke!
case “$3” in

# Wait until the check has been tried 3 times before restarting the web server.
# If the check fails on the 4th time (after we restart the web server), the state
# type will turn to “hard” and contacts will be notified of the problem.
# Hopefully this will restart the web server successfully, so the 4th check will
# result in a “soft” recovery.  If that happens no one gets notified because we
# fixed the problem!
3)
echo -n “Restarting HTTP service (3rd soft critical state)…”
# Call the init script to restart the HTTPD server
sudo /etc/init.d/tomcat restart
;;
esac
;;

# The HTTP service somehow managed to turn into a hard error without getting fixed.
# It should have been restarted by the code above, but for some reason it didn’t.
# Let’s give it one last try, shall we?
# Note: Contacts have already been notified of a problem with the service at this
# point (unless you disabled notifications for this service)
HARD)
echo -n “Restarting $4 service…”
# Call the init script to restart the HTTPD server
sudo /etc/init.d/tomcat restart
;;
esac
;;
esac
exit 0

Questo script è quello standard fornito da Nagios per gli configurare gli event handler. Di fatto esegue il restart del servizio al terzo o al quarto KO consecutivo ricevuto dal monitoraggio. Nel mio caso, il servizio tomcat viene gestito con lo script di init /etc/init.d/tomcat. La presenza del comando ‘sudo’ davanti è dovuta al fatto che il demone nrpe viene eseguito con un proprio utente (tipicamente nrpe), che quindi ha diritti limitati. Bisogna quindi consentire a tale utente di poter eseguire il comando di restart, aggiungendo appunto il comando al sudoers. Ho eseguito quindi visudo ed aggiunto la seguente riga:

#allow user nrpe to restart tomcat without password
nrpe ALL= NOPASSWD: /etc/init.d/tomcat restart

A questo punto tutte le componenti sono al loro posto. Riavviate il server nagios e il servizio nrpe per applicare le modifiche.

/etc/init.d/nagios3 restart

/etc/init.d/nrpe restart

 

Lascia un commento