Bash script for monitoring services
#!/bin/bash | |
# Lista de servicios a monitorear. | |
servicios='mysql apache2 varnish' | |
# Aqui se guarda un log de servicios caídos. | |
report=/root/servicesmonitor_log.txt | |
sendmail () { | |
SUBJECT="Servicio $1 caido $(date)" | |
EMAIL="write_your_mail_here@mail.com" | |
echo "$SUBJECT" | mail -s "$SUBJECT" "$EMAIL" | |
} | |
checkstatus () { | |
#Redireccion al vacio | |
service $1 status > /dev/null 2>&1 | |
#$? devuelve el codigo de estado del ultimo comando o script ejecutado (0=exito, otr numero significa error) | |
status="$?" | |
if [ $status -ne "0" ]; then | |
#escribimos en archivo del log | |
echo "$1 esta caído" $(date) >> $report | |
#enviamos un mail | |
sendmail $1 | |
#como está caido intentamos iniciarlo y logeamos los posibles errores | |
/etc/init.d/$1 start >> $report | |
fi | |
} | |
existe () { | |
if [ -e /etc/init.d/$1 ]; then | |
checkstatus $1 | |
fi | |
} | |
for servicio in $servicios | |
do | |
existe $servicio | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment