Skip to content

Instantly share code, notes, and snippets.

@plencovich
Created March 18, 2018 12:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plencovich/e2e95beb4f5e48c77a43437e71c84d97 to your computer and use it in GitHub Desktop.
Save plencovich/e2e95beb4f5e48c77a43437e71c84d97 to your computer and use it in GitHub Desktop.
Monitor de Servicio WEB / Web Service Monitor. Archivo bash para comprobar el estado de un servicio de un servidor; en caso que esté inactivo, intenta iniciarlo y notifica vía email.

Plen.co

Monitor de Servicio / Service Monitor

Configuración

  1. Copiar el siguiente contenido en un archivo bash check_services.sh
  2. Modificar el contenido de las variables SERVICE MAILBOX FILELOG HOSTNAME
  3. Crear un CRON cada 5' o el tiempo que desean: */5 * * * * bash /root/check_services.sh> /dev/null

Archivo BASH

#!/bin/bash
SERVICE=nombre-del-servicio
MAILBOX=miemail@dominio.com
FILELOG=/root/content_mail.log
HOSTNAME=hostname-del-servidor

if [ "`systemctl is-active $SERVICE`" != "active" ] 
then 
    touch $FILELOG
    echo "$SERVICE no está ejecutandose, intento reiniciar" >> $FILELOG
    systemctl restart $SERVICE >> $FILELOG
    echo "$SERVICE estado actual" >> $FILELOG
    systemctl status $SERVICE >> $FILELOG
    mail -s "[$HOSTNAME] $SERVICE Report" $MAILBOX < $FILELOG
    rm -f $FILELOG
    exit 0
fi 
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment