Actualizado el sábado, 11 enero, 2020
Script bash que alerta por mail y sms cuando el servidor está caido
En este articulo vemos un sencillo (pero a la vez muy útil y necesario) script bash, con el cual nos mantendremos informados vía mail y sms, de cualquier caída o perdida de conexión de nuestro servidor.
Para el tema de las pasarelas SMS, no te preocupes. Aquí tienes una lista: https://en.wikipedia.org/wiki/SMS_gateway
Así que abrimos nuestra consola / terminal, y escribimos:
nano conect.sh
Copiamos y pegamos lo siguiente:
#!/bin/bash NOTIFYEMAIL=<mimail@midominio.es> #En sms-gateway debes colocar el usuario SMSEMAIL=<numero telefono @ sms-gateway> SENDEREMAIL=alerta@localhost SERVER=http://127.0.0.1/ PAUSE=60 FAILED=0 DEBUG=0 while true do /usr/bin/curl -sSf $SERVER > /dev/null 2>&1 CS=$? # For debugging purposes if [ $DEBUG -eq 1 ] then echo "STATUS = $CS" echo "FAILED = $FAILED" if [ $CS -ne 0 ] then echo "$SERVER is down" elif [ $CS -eq 0 ] then echo "$SERVER is up" fi fi # If the server is down and no alert is sent - alert if [ $CS -ne 0 ] && [ $FAILED -eq 0 ] then FAILED=1 if [ $DEBUG -eq 1 ] then echo "$SERVER failed" fi if [ $DEBUG = 0 ] then echo "$SERVER went down $(date)" | /usr/bin/mailx -s "$SERVER went down" -r "$SENDEREMAIL" "$SMSEMAIL" echo "$SERVER went down $(date)" | /usr/bin/mailx -s "$SERVER went down" -r "$SENDEREMAIL" "$NOTIFYEMAIL" fi # If the server is back up and no alert is sent - alert elif [ $CS -eq 0 ] && [ $FAILED -eq 1 ] then FAILED=0 if [ $DEBUG -eq 1 ] then echo "$SERVER is back up" fi if [ $DEBUG = 0 ] then echo "$SERVER is back up $(date)" | /usr/bin/mailx -s "$SERVER is back up again" -r "$SENDEREMAIL" "$SMSEMAIL" echo "$SERVER is back up $(date)" | /usr/bin/mailx -s "$SERVER is back up again" -r "$SENDEREMAIL" "$NOTIFYEMAIL" fi fi sleep $PAUSE done
Guarda y cierra.
Creas una tarea cron. Cuando el servidor este caído recibirás un mail y un sms.