Skip to content

Instantly share code, notes, and snippets.

@Zeokat
Created August 30, 2014 02:20
Show Gist options
  • Save Zeokat/b7e5d6610521b089d7ff to your computer and use it in GitHub Desktop.
Save Zeokat/b7e5d6610521b089d7ff to your computer and use it in GitHub Desktop.
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="[email protected]"
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