check-sites 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. #!/bin/bash
  2. ##################################################################
  3. # check-sites: comprueba los dominios de un servidor web y #
  4. # envía un email si alguno no se encuentra Online. #
  5. # Los dominios pueden provenir de una lista proporcionada u #
  6. # obtenidos de los sitios web configurados en Nginx o en Apache. #
  7. # #
  8. # Autor: Guzmán Castanedo Villalba #
  9. # Email: guzman@castanedo.es #
  10. # Licencia: GPLv3 (https://www.gnu.org/licenses/gpl-3.0.en.html) #
  11. # Agosto 2018 #
  12. ##################################################################
  13. usage() {
  14. echo -en "Uso: "$(basename $0)" [OPCIONES]\n\n"
  15. echo -en " -h, --help: muestra mensaje de error y finaliza.\n"
  16. echo -en " -t, --to <email>: email destinatario.\n"
  17. echo -en " -f, --from <email>: email remitente.\n"
  18. echo -en " -P, --password <pass>: contraseña del remitente.\n"
  19. echo -en " -s, --subject <subject>: concepto del email.\n"
  20. echo -en " -H, --host <host>: dominio o IP del servidor SMTP (necesita STARTTLS).\n"
  21. echo -en " -p, --port <port>: puerto TCP/IP del servidor SMTP (por defecto: $port).\n"
  22. echo -en " -d, --domains <domain1[,domain2[,...]]>: dominios a comprobar.\n"
  23. echo -en " --nginx: obtiene los dominios de \"/etc/nginx/sites-enabled/\".\n"
  24. echo -en " --apache: obtiene los dominios de \"/etc/apache2/sites-enabled/\".\n"
  25. echo -en " --no-email: desactiva el envio de emails.\n"
  26. }
  27. sendEmail() {
  28. # sendEmail
  29. 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
  30. return $?
  31. }
  32. getVirtualHostsNginx() {
  33. # getVirtualHostNginx $virtualHostPath
  34. if [ ! -d $1 ];then
  35. echo -en "ERROR:\tEl directorio \"$1\" no existe.\n"
  36. exit 1
  37. fi
  38. cont=1
  39. for virtualHost in $(find -L "$1" -type f);do
  40. grep '^[[:space:]]*auth_basic' "$virtualHost" > /dev/null 2>&1
  41. if [ $? -eq 0 ];then
  42. # Si tiene la directiva "auth_basic" activada no lo comprobamos
  43. continue
  44. fi
  45. linea=$(grep '^[[:space:]]*server_name' "$virtualHost")
  46. if [ $? -ne 0 ];then
  47. # Si no tiene la directiva "server_name" no lo comprobamos
  48. continue
  49. fi
  50. for hostname in $linea;do
  51. hostname=$(echo "$hostname" | sed 's/;//g')
  52. if [ $hostname != "server_name" ] && [ $hostname != "localhost" ];then
  53. hostnames[$cont]=$hostname
  54. cont=$((cont+1))
  55. fi
  56. done
  57. done
  58. # Eliminar repetidos
  59. hostnames=($(echo "${hostnames[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' '))
  60. unset linea cont hostname
  61. }
  62. getVirtualHostsApache() {
  63. # getVirtualHostsApache $virtualHostPath
  64. if [ ! -d $1 ];then
  65. echo -en "ERROR:\tEl directorio \"$1\" no existe.\n"
  66. exit 1
  67. fi
  68. cont=0
  69. for virtualHost in $(find -L "$1" -type f);do
  70. grep '^[[:space:]]*AuthType' "$virtualHost" > /dev/null 2>&1
  71. if [ $? -eq 0 ];then
  72. # Si tiene la directiva "AuthType" activada no lo comprobamos
  73. continue
  74. fi
  75. linea=$(grep '^[[:space:]]*ServerName' "$virtualHost")
  76. if [ $? -eq 0 ];then
  77. for hostname in $linea;do
  78. if [ $hostname != "ServerName" ] && [ $hostname != "localhost" ];then
  79. hostnames[$cont]=$hostname
  80. cont=$((cont+1))
  81. fi
  82. done
  83. else
  84. # Si no tiene la directiva "ServerName" no lo comprobamos
  85. continue
  86. fi
  87. linea2=$(grep '^[[:space:]]*ServerAlias' "$virtualHost")
  88. if [ $? -eq 0 ];then
  89. # Si tiene la directiva "ServerAlias" las añadimos
  90. for hostname in $linea2;do
  91. if [ $hostname != "ServerAlias" ] && [ $hostname != "localhost" ];then
  92. hostnames[$cont]=$hostname
  93. cont=$((cont+1))
  94. fi
  95. done
  96. fi
  97. done
  98. # Eliminar repetidos
  99. hostnames=($(echo "${hostnames[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' '))
  100. unset linea linea2 cont hostname
  101. }
  102. checkHostname() {
  103. # checkHostname $domain
  104. if [ $# -ne 1 ];then
  105. echo -en "ERROR:\tError interno en checkHostname.\n"
  106. exit 1
  107. fi
  108. local domain=$1
  109. local httpCode=$(curl -L -s -o /dev/null -w "%{http_code}" "$domain")
  110. case $httpCode in
  111. 2[0-9][0-9])
  112. # Sitio Disponible
  113. return 0
  114. ;;
  115. *)
  116. # Se ha producido algún error
  117. return 1
  118. ;;
  119. esac
  120. }
  121. # Variables iniciales
  122. #hostnames=""
  123. hostnamesFail=""
  124. email=true
  125. nginx=true
  126. apache=false
  127. to=""
  128. from=""
  129. pass=""
  130. subject="WARNING: Virtual Host NO DISPONIBLE"
  131. host=""
  132. port="587"
  133. domains=""
  134. # Leer Opciones
  135. 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) -- "$@")
  136. eval set -- $TEMP
  137. unset TEMP
  138. while true; do
  139. case $1 in
  140. -h|--help)
  141. usage
  142. exit 0
  143. ;;
  144. -t|--to)
  145. to=$2
  146. shift 2
  147. ;;
  148. -f|--from)
  149. from=$2
  150. shift 2
  151. ;;
  152. -P|--password)
  153. pass=$2
  154. shift 2
  155. ;;
  156. -s|--subject)
  157. subject=$2
  158. shift 2
  159. ;;
  160. -H|--host)
  161. host=$2
  162. shift 2
  163. ;;
  164. -p|--port)
  165. port=$2
  166. shift 2
  167. ;;
  168. -d|--domains)
  169. domains=$(echo "$2" | sed 's/,/ /g')
  170. shift 2
  171. nginx=false
  172. apache=false
  173. ;;
  174. --nginx)
  175. nginx=true
  176. apache=false
  177. ;;
  178. --apache)
  179. nginx=false
  180. apache=true
  181. ;;
  182. --no-email)
  183. email=false
  184. shift
  185. ;;
  186. --)
  187. # Ultimo
  188. shift
  189. break
  190. ;;
  191. *)
  192. # Inesperado
  193. help
  194. exit 1
  195. ;;
  196. esac
  197. done
  198. which s-nail > /dev/null 2>&1
  199. if [ $? -ne 0 ];then
  200. echo -en "WARNING(S-NAIL): El envio de correo está desactivado (Compruebe comfiguración).\n"
  201. email=false
  202. fi
  203. if [ -z $to ] && [ $email = true ];then
  204. echo -en "WARNING(TO): El envio de correo está desactivado (Compruebe comfiguración).\n"
  205. email=false
  206. fi
  207. if [ -z $from ] && [ $mail = true ];then
  208. echo -en "WARNING(FROM): El envio de correo está desactivado (Compruebe comfiguración).\n"
  209. email=false
  210. fi
  211. if [ -z $pass ] && [ $email = true ];then
  212. echo -en "WARNING(PASSWORD): El envio de correo está desactivado (Compruebe comfiguración).\n"
  213. email=false
  214. fi
  215. if [ -z $host ] && [ $email = true ];then
  216. host=$(echo "$from" | cut -d '@' -f 2)
  217. host=$(dig -t MX "$host" +short | cut -d ' ' -f 2 | sed 's/.$//' | head -1)
  218. if [ -z $host ];then
  219. echo -en "WARNING(HOST): El envio de correo está desactivado (Compruebe comfiguración).\n"
  220. email=false
  221. fi
  222. fi
  223. # Obtenemos dominios
  224. echo -en $(date +'[%Y-%m-%d] %H:%M:%S')" Comprobando Virtual Hosts...\n"
  225. if [ $nginx = true ];then
  226. # Comprobamos sites-enabled de Nginx
  227. getVirtualHostsNginx "/etc/nginx/sites-enabled/"
  228. elif [ $apache = true ];then
  229. # Comprobamos sites-enabled de Apache
  230. getVirtualHostsApache "/etc/apache2/sites-enabled/"
  231. else
  232. cont=0
  233. for domain in $domains;do
  234. hostnames[$cont]=$domain
  235. cont=$((cont+1))
  236. done
  237. unset cont domain
  238. fi
  239. # Comprobamos dominios
  240. if [ ${#hostnames[@]} -eq 0 ];then
  241. echo -en "ERROR:\tNingún dominio encontrado.\n"
  242. usage
  243. exit 1
  244. fi
  245. for hostname in ${hostnames[@]};do
  246. checkHostname $hostname
  247. if [ $? -ne 0 ];then
  248. echo -en "* \"$hostname\" NO ESTÁ DISPONIBLE.\n"
  249. hostnamesFail=$hostnamesFail"* ""$hostname"" NO ESTÁ DISPONIBLE.\n"
  250. else
  251. echo -en "* \"$hostname\" ESTÁ DISPONIBLE.\n"
  252. fi
  253. done
  254. # Enviar email
  255. if [ ! -z "$hostnamesFail" ] && [ $email = true ];then
  256. echo -en "Enviando informe por email a $to..."
  257. message="Envío automático de "$(basename $0)".\nEl sistema presenta Virtual Hosts NO DISPONIBLES.\n""$hostnamesFail""\nCOMPRUEBE EL SERVIDOR.\n"
  258. sendEmail
  259. if [ $? -ne 0 ];then
  260. echo -en "\nWARNING:\tNo ha sido posible enviar email (REVISE CONFIGURACIÓN).\n"
  261. else
  262. echo -en " OK.\n"
  263. fi
  264. fi
  265. echo -en "\n"