Gli EventHandlers in Nagios sono un mezzo attraverso il quale è possibile ottenere l’esecuzione di script da parte di Nagios a seconda del rilevamento o meno di un certo evento, per esempio, se viene rilevato che il servizio apache (apache2) è DOWN, possiamo impostare che nagios esegua il comando per riavviarlo. In questa maniera è possibile essere pro attivi a fronte di un disservizio di lunga durata.
Il sistema Nagios viene solitamente eseguito con l’utente di sistema nagios, per questo alcuni comandi di sistema non potranno essere eseguiti e servirebbero dei privilegi elevati. Tramite sudo potremo permettere all’utente nagios di eseguirli con privilegi di root.
I passi per la configurazione sono i seguenti:
1. Configurazione dell’accesso ssh tra il server Nagios e l’host (in questo caso nagiosserver e nagiosmonitored)
2. Creazione dello script di gestore degli eventi sul server nagios (nagiosserver)
3. Creazione dello script contente il comando che verrà lanciato sulla macchina host (nagiosmonitored)
4. Impostazione dell’accesso tramite sudo (nagiosmonitored)
5. Aggiunta del servizio EventHandler sul sistema Nagios (nagiosserver)
6. Aggiunta del comando al sistema Nagios (nagiosserver)
7. Test
1. Configurazione dell’accesso ssh tra il server Nagios e l’host (nagiosserver e nagiosmonitored):
Come utente nagios sul server nagios (nagiosserver) eseguire il seguente comando:
# ssh-keygen -t rsa
Rispondere in modo di ‘default’ a tutte le domande proposte.
Al termine della generazione delle chiavi, in /home/nagios/.ssh vi troverete la coppia di chiavi pubblica e privata.
id_rsa id_rsa.pub
ssh [email protected]
2. Creazione dello script di gestore degli eventi sul server nagios (nagiosserver)
Creiamo sul nostro nagiosserver lo script di eventhandler che verrà chiamato dal sistema Nagios in caso di evento.
Lo script andrà messo nella cartella /usr/local/nagios/libexec/eventhandlers (se non presente, crearla).
cd /usr/local/nagios/libexec/eventhandlers
Creiamo lo script, in questo caso si chiamerà restart-apache-eh.sh in quanto vogliamo che riavvii remotamente il servizio Apache.
restart-apache-eh.sh
#!/bin/sh # # Event handler script for restarting the apache (httpd) on the remote machine # # Matthew Harman May 2012 - [email protected] # # Note: This script will only restart if the service is # retried 3 times (in a "soft" state) or if the service somehow # manages to fall into a "hard" error state. # # What state is the httpd check 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 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) # Check if the host or service is in a # period of downtime servicestatus="$5""$6"; case "$servicestatus" in 00) echo -n "Restarting service (3rd soft critical state)..." # Call the script to restart the process ssh -f -T "$4" /etc/SCRIPT/restart-apache.sh sleep 20 ;; esac ;; esac ;; # The 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. Lets 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) # Check if the host or service is in a period of downtime servicestatus="$5""$6"; case "$servicestatus" in 00) echo -n "Restarting Service..." # Call the script to restart the server ssh -f -T "$4" /ec/SCRIPT/restart-apache.sh sleep 20 ;; esac ;; esac ;; esac exit 0
Guardando lo script possiamo capirne il suo funzionamento. Se sarà rilevato uno di questi due stati, SOFT 3 o HARD, e non sarà presente uno stato di Downtime impostato sul servizio, lo script si collegherà in SSH al nagiosmonitored e lancerà lo script restart-apache.sh contenuto in /etc/SCRIPT
3. Creazione dello script contente il comando che verrà lanciato sulla macchina host (nagiosmonitored)
Ora creiamo lo script nel nagiosmonitored, Creaimo la cartella /etc/SCRIPT e creiamo il file restart.apache.sh contenente:
sudo /sbin/serviceapache2 restart
il comando di restart è preceduto dal comando sudo perchè, come dicevamo prima, lo script verrebbe lanciato con l’utenza nagios di connessione SSH e un riavvio di un servizio non può essere fatto da una utenza di tipo normale. Per questo ci vogliono i privilegi di root.
4. Impostazione dell’accesso tramite sudo (nagiosmonitored)
Editiamo il file /etc/sudoers nell’host (nagiosmonitored) in modo che contenga le seguenti voci:
# Nagios commands Cmnd_Alias SERVICE=/sbin/service User_Alias NAGIOSUSERS = nagios NAGIOSUSERS ALL = NOPASSWD: SERVICE
5. Aggiunta del servizio EventHandler sul sistema Nagios (nagiosserver)
define service {
use generic-service
host_name nagiosmonitored
service_description check apache is running
check_command check_nrpe!check_http
contact_groups admins
max_check_attempts 4
event_handler restart-apache-eh
}
6. Aggiunta del comando al sistema Nagios (nagiosserver)
########################################################### # Restart apache ########################################################### define command{ command_name restart-apache-eh command_line /usr/local/nagios/libexec/eventhandlers/restart-httpd-eh.sh $SERVICESTATE$ $SERVICESTATETYPE$ $SERVICEATTEMPT$ $HOSTADDRESS$ $HOSTDOWNTIME$ $SERVICEDOWNTIME$ }