#!/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 destinatario.\n" echo -en " -f, --from : email remitente.\n" echo -en " -P, --password : contraseña del remitente.\n" echo -en " -s, --subject : concepto del email.\n" echo -en " -H, --host : dominio o IP del servidor SMTP (necesita STARTTLS).\n" echo -en " -p, --port : puerto TCP/IP del servidor SMTP (por defecto: $port).\n" echo -en " -d, --domains : 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="" 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 shift ;; --apache) nginx=false apache=true shift ;; --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"