check-virtualhosts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #!/bin/bash
  2. help() {
  3. echo -en "Uso: "$(basename $0)" [OPCIONES]\n\n"
  4. echo -en " -h, --help: muestra mensaje de error y finaliza.\n"
  5. echo -en " -t, --to <email>: email destinatario.\n"
  6. echo -en " -f, --from: email remitente.\n"
  7. echo -en " -P, --password: contraseña del remitente.\n"
  8. echo -en " -s, --subject: Concepto del email.\n"
  9. echo -en " -H, --host: dominio o IP del servidor SMTP (necesita STARTTLS).\n"
  10. echo -en " -p, --port: puerto TCP/IP del servidor SMTP (por defecto: $port).\n"
  11. }
  12. sendEmail() {
  13. # sendEmail
  14. echo -en "$message" | mailx -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
  15. return $?
  16. }
  17. getVirtualHosts() {
  18. # getVirtualHost $virtualHostPath
  19. if [ ! -d $1 ];then
  20. echo -en "ERROR:\tEl directorio \"$1\" no existe.\n"
  21. exit 1
  22. fi
  23. cont=1
  24. for virtualHost in $(find -L "$1" -type f);do
  25. grep ^[[:space:]]*auth_basic "$virtualHost" > /dev/null 2>&1
  26. if [ $? -eq 0 ];then
  27. # Si tiene la directiva "auth_basic" activada no lo comprobamos
  28. continue
  29. fi
  30. linea=$(grep ^[[:space:]]*server_name "$virtualHost")
  31. if [ $? -ne 0 ];then
  32. # Si no tiene la directiva "server_name" no lo comprobamos
  33. continue
  34. fi
  35. for hostname in $linea;do
  36. hostname=$(echo "$hostname" | sed 's/;//g')
  37. if [ $hostname != "server_name" ] && [ $hostname != "localhost" ];then
  38. hostnames[$cont]=$hostname
  39. cont=$((cont+1))
  40. fi
  41. done
  42. done
  43. # Eliminar repetidos
  44. hostnames=($(echo "${hostnames[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' '))
  45. unset linea cont hostname
  46. }
  47. checkHostname() {
  48. # checkHostname $domain
  49. if [ $# -ne 1 ];then
  50. echo -en "ERROR:\tError interno en checkHostname.\n"
  51. exit 1
  52. fi
  53. domain=$1
  54. httpCode=$(curl -L -s -o /dev/null -w "%{http_code}" "$domain")
  55. case $httpCode in
  56. 2[0-9][0-9])
  57. # Sitio Disponible
  58. return 0
  59. ;;
  60. *)
  61. # Se ha producido algún error
  62. return 1
  63. ;;
  64. esac
  65. }
  66. # Variables iniciales
  67. hostnames=""
  68. hostnamesFail=""
  69. email=true
  70. to=""
  71. from=""
  72. pass=""
  73. subject="WARNING: Virtual Host NO DISPONIBLE"
  74. host=""
  75. port="587"
  76. # Leer Opciones
  77. TEMP=$(getopt -q -o ht:f:P:s:H:p: --longoptions help,to:,from:,password:,subject:,host:,port: --name $(basename $0) -- "$@")
  78. eval set -- $TEMP
  79. unset TEMP
  80. while true; do
  81. case $1 in
  82. -h|--help)
  83. help
  84. exit 0
  85. ;;
  86. -t|--to)
  87. to=$2
  88. shift 2
  89. ;;
  90. -f|--from)
  91. from=$2
  92. shift 2
  93. ;;
  94. -P|--password)
  95. pass=$2
  96. shift 2
  97. ;;
  98. -s|--subject)
  99. subject=$2
  100. shift 2
  101. ;;
  102. -H|--host)
  103. host=$2
  104. shift 2
  105. ;;
  106. -p|--port)
  107. port=$2
  108. shift 2
  109. ;;
  110. --)
  111. # Ultimo
  112. shift
  113. break
  114. ;;
  115. *)
  116. # Inesperado
  117. help
  118. exit 1
  119. ;;
  120. esac
  121. done
  122. if [ -z $to ] && [ $email = true ];then
  123. echo -en "WARNING(TO): El envio de correo está desactivado (Compruebe comfiguración).\n"
  124. email=false
  125. fi
  126. if [ -z $from ] && [ $mail = true ];then
  127. echo -en "WARNING(FROM): El envio de correo está desactivado (Compruebe comfiguración).\n"
  128. email=false
  129. fi
  130. if [ -z $pass ] && [ $email = true ];then
  131. echo -en "WARNING(PASSWORD): El envio de correo está desactivado (Compruebe comfiguración).\n"
  132. email=false
  133. fi
  134. if [ -z $host ] && [ $email = true ];then
  135. host=$(echo "$from" | cut -d '@' -f 2)
  136. host=$(dig -t MX "$host" +short | cut -d ' ' -f 2 | sed 's/.$//' | head -1)
  137. if [ -z $host ];then
  138. echo -en "WARNING(HOST): El envio de correo está desactivado (Compruebe comfiguración).\n"
  139. email=false
  140. fi
  141. fi
  142. # Comienza el programa
  143. echo -en $(date +'[%Y-%m-%d] %H:%M:%S')" Comprobando Virtual Hosts...\n"
  144. getVirtualHosts "/etc/nginx/sites-enabled/"
  145. for hostname in ${hostnames[@]};do
  146. checkHostname $hostname
  147. if [ $? -ne 0 ];then
  148. echo -en "* \"$hostname\"\tNO ESTÁ DISPONIBLE.\n"
  149. hostnamesFail=$hostnameFail"* ""$hostname"" NO ESTÁ DISPONIBLE.\n"
  150. else
  151. echo -en "* \"$hostname\"\t ESTÁ DISPONIBLE.\n"
  152. fi
  153. done
  154. if [ ! -z $hostnamesFail ] && [ $email = true ];then
  155. echo -en "Enviando informe por email a \$TO..."
  156. message="Envío automático de "$(basename $0)".\nEl sistema presenta Virtual Hosts NO DISPONIBLES.\n""$hostnamesFail""\nCOMPRUEBE QUE EL SERVIDOR.\n"
  157. sendEmail
  158. if [ $? -ne 0 ];then
  159. echo -en "WARNING:\tNo ha sido posible enviar email (REVISE CONFIGURACIÓN).\n"
  160. fi
  161. fi