123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- #!/bin/bash
- ##################################################################
- # check-sites: comprueba los dominios de un servidor web y #
- # envía un email si alguno no se encuentra Online. #
- # Los dominios pueden provenir de una lista proporcionada u #
- # obtenidos de los sitios web configurados en Nginx o en Apache. #
- # #
- # Autor: Guzmán Castanedo Villalba #
- # Email: guzman@castanedo.es #
- # Licencia: GPLv3 (https://www.gnu.org/licenses/gpl-3.0.en.html) #
- # Agosto 2018 #
- ##################################################################
- usage() {
- echo -en "Uso: "$(basename $0)" [OPCIONES]\n\n"
- echo -en " -h, --help: muestra mensaje de error y finaliza.\n"
- echo -en " -t, --to <email>: email destinatario.\n"
- echo -en " -f, --from <email>: email remitente.\n"
- echo -en " -P, --password <pass>: contraseña del remitente.\n"
- echo -en " -s, --subject <subject>: concepto del email.\n"
- echo -en " -H, --host <host>: dominio o IP del servidor SMTP (necesita STARTTLS).\n"
- echo -en " -p, --port <port>: puerto TCP/IP del servidor SMTP (por defecto: $port).\n"
- echo -en " -d, --domains <domain1[,domain2[,...]]>: dominios a comprobar.\n"
- echo -en " --nginx: obtiene los dominios de \"/etc/nginx/sites-enabled/\".\n"
- echo -en " --apache: obtiene los dominios de \"/etc/apache2/sites-enabled/\".\n"
- echo -en " --no-email: desactiva el envio de emails.\n"
- }
- sendEmail() {
- # sendEmail
- echo -en "$message" | s-nail -Ssendwait -s "$subject" -S smtp-use-starttls -S smtp-auth=login -S smtp=smtp://$host:$port -S from="$from" -S smtp-auth-user="$from" -S smtp-auth-password="$pass" $to > /dev/null 2>&1
- return $?
- }
- getVirtualHostsNginx() {
- # getVirtualHostNginx $virtualHostPath
- if [ ! -d $1 ];then
- echo -en "ERROR:\tEl directorio \"$1\" no existe.\n"
- exit 1
- fi
- cont=1
- for virtualHost in $(find -L "$1" -type f);do
- grep '^[[:space:]]*auth_basic' "$virtualHost" > /dev/null 2>&1
- if [ $? -eq 0 ];then
- # Si tiene la directiva "auth_basic" activada no lo comprobamos
- continue
- fi
- linea=$(grep '^[[:space:]]*server_name' "$virtualHost")
- if [ $? -ne 0 ];then
- # Si no tiene la directiva "server_name" no lo comprobamos
- continue
- fi
- for hostname in $linea;do
- hostname=$(echo "$hostname" | sed 's/;//g')
- if [ $hostname != "server_name" ] && [ $hostname != "localhost" ];then
- hostnames[$cont]=$hostname
- cont=$((cont+1))
- fi
- done
- done
- # Eliminar repetidos
- hostnames=($(echo "${hostnames[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' '))
- unset linea cont hostname
- }
- getVirtualHostsApache() {
- # getVirtualHostsApache $virtualHostPath
- if [ ! -d $1 ];then
- echo -en "ERROR:\tEl directorio \"$1\" no existe.\n"
- exit 1
- fi
- cont=0
- for virtualHost in $(find -L "$1" -type f);do
- grep '^[[:space:]]*AuthType' "$virtualHost" > /dev/null 2>&1
- if [ $? -eq 0 ];then
- # Si tiene la directiva "AuthType" activada no lo comprobamos
- continue
- fi
- linea=$(grep '^[[:space:]]*ServerName' "$virtualHost")
- if [ $? -eq 0 ];then
- for hostname in $linea;do
- if [ $hostname != "ServerName" ] && [ $hostname != "localhost" ];then
- hostnames[$cont]=$hostname
- cont=$((cont+1))
- fi
- done
- else
- # Si no tiene la directiva "ServerName" no lo comprobamos
- continue
- fi
- linea2=$(grep '^[[:space:]]*ServerAlias' "$virtualHost")
- if [ $? -eq 0 ];then
- # Si tiene la directiva "ServerAlias" las añadimos
- for hostname in $linea2;do
- if [ $hostname != "ServerAlias" ] && [ $hostname != "localhost" ];then
- hostnames[$cont]=$hostname
- cont=$((cont+1))
- fi
- done
- fi
- done
- # Eliminar repetidos
- hostnames=($(echo "${hostnames[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' '))
- unset linea linea2 cont hostname
- }
- checkHostname() {
- # checkHostname $domain
- if [ $# -ne 1 ];then
- echo -en "ERROR:\tError interno en checkHostname.\n"
- exit 1
- fi
- local domain=$1
- local httpCode=$(curl -L -s -o /dev/null -w "%{http_code}" "$domain")
- case $httpCode in
- 2[0-9][0-9])
- # Sitio Disponible
- return 0
- ;;
- *)
- # Se ha producido algún error
- return 1
- ;;
- esac
- }
- # Variables iniciales
- hostnames[0]=""
- hostnamesFail=""
- email=true
- nginx=true
- apache=false
- to=""
- from=""
- pass=""
- subject="WARNING: Virtual Host NO DISPONIBLE"
- host=""
- port="587"
- domains=""
- # Leer Opciones
- TEMP=$(getopt -q -o ht:f:P:s:H:p:d: --longoptions help,to:,from:,password:,subject:,host:,port:,domains:,nginx,apache,no-email --name $(basename $0) -- "$@")
- eval set -- $TEMP
- unset TEMP
- while true; do
- case $1 in
- -h|--help)
- usage
- exit 0
- ;;
- -t|--to)
- to=$2
- shift 2
- ;;
- -f|--from)
- from=$2
- shift 2
- ;;
- -P|--password)
- pass=$2
- shift 2
- ;;
- -s|--subject)
- subject=$2
- shift 2
- ;;
- -H|--host)
- host=$2
- shift 2
- ;;
- -p|--port)
- port=$2
- shift 2
- ;;
- -d|--domains)
- domains=$(echo "$2" | sed 's/,/ /g')
- shift 2
- nginx=false
- apache=false
- ;;
- --nginx)
- nginx=true
- apache=false
- ;;
- --apache)
- nginx=false
- apache=true
- ;;
- --no-email)
- email=false
- shift
- ;;
- --)
- # Ultimo
- shift
- break
- ;;
- *)
- # Inesperado
- help
- exit 1
- ;;
- esac
- done
- which s-nail > /dev/null 2>&1
- if [ $? -ne 0 ];then
- echo -en "WARNING(S-NAIL): El envio de correo está desactivado (Compruebe comfiguración).\n"
- email=false
- fi
- if [ -z $to ] && [ $email = true ];then
- echo -en "WARNING(TO): El envio de correo está desactivado (Compruebe comfiguración).\n"
- email=false
- fi
- if [ -z $from ] && [ $mail = true ];then
- echo -en "WARNING(FROM): El envio de correo está desactivado (Compruebe comfiguración).\n"
- email=false
- fi
- if [ -z $pass ] && [ $email = true ];then
- echo -en "WARNING(PASSWORD): El envio de correo está desactivado (Compruebe comfiguración).\n"
- email=false
- fi
- if [ -z $host ] && [ $email = true ];then
- host=$(echo "$from" | cut -d '@' -f 2)
- host=$(dig -t MX "$host" +short | cut -d ' ' -f 2 | sed 's/.$//' | head -1)
- if [ -z $host ];then
- echo -en "WARNING(HOST): El envio de correo está desactivado (Compruebe comfiguración).\n"
- email=false
- fi
- fi
- # Obtenemos dominios
- echo -en $(date +'[%Y-%m-%d] %H:%M:%S')" Comprobando Virtual Hosts...\n"
- if [ $nginx = true ];then
- # Comprobamos sites-enabled de Nginx
- getVirtualHostsNginx "/etc/nginx/sites-enabled/"
- elif [ $apache = true ];then
- # Comprobamos sites-enabled de Apache
- getVirtualHostsApache "/etc/apache2/sites-enabled/"
- else
- cont=0
- for domain in $domains;do
- hostnames[$cont]=$domain
- cont=$((cont+1))
- done
- unset cont domain
- fi
- # Comprobamos dominios
- if [ ${#hostnames[@]} -eq 0 ];then
- echo -en "ERROR:\tNingún dominio encontrado.\n"
- usage
- exit 1
- fi
- for hostname in ${hostnames[@]};do
- checkHostname $hostname
- if [ $? -ne 0 ];then
- echo -en "* \"$hostname\" NO ESTÁ DISPONIBLE.\n"
- hostnamesFail=$hostnamesFail"* ""$hostname"" NO ESTÁ DISPONIBLE.\n"
- else
- echo -en "* \"$hostname\" ESTÁ DISPONIBLE.\n"
- fi
- done
- # Enviar email
- if [ ! -z "$hostnamesFail" ] && [ $email = true ];then
- echo -en "Enviando informe por email a $to..."
- message="Envío automático de "$(basename $0)".\nEl sistema presenta Virtual Hosts NO DISPONIBLES.\n""$hostnamesFail""\nCOMPRUEBE EL SERVIDOR.\n"
- sendEmail
- if [ $? -ne 0 ];then
- echo -en "\nWARNING:\tNo ha sido posible enviar email (REVISE CONFIGURACIÓN).\n"
- else
- echo -en " OK.\n"
- fi
- fi
- echo -en "\n"
|