check-sites 5.0 KB

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