check-sites 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. shift
  178. ;;
  179. --apache)
  180. nginx=false
  181. apache=true
  182. shift
  183. ;;
  184. --no-email)
  185. email=false
  186. shift
  187. ;;
  188. --)
  189. # Ultimo
  190. shift
  191. break
  192. ;;
  193. *)
  194. # Inesperado
  195. help
  196. exit 1
  197. ;;
  198. esac
  199. done
  200. which s-nail > /dev/null 2>&1
  201. if [ $? -ne 0 ];then
  202. echo -en "WARNING(S-NAIL): El envio de correo está desactivado (Compruebe comfiguración).\n"
  203. email=false
  204. fi
  205. if [ -z $to ] && [ $email = true ];then
  206. echo -en "WARNING(TO): El envio de correo está desactivado (Compruebe comfiguración).\n"
  207. email=false
  208. fi
  209. if [ -z $from ] && [ $mail = true ];then
  210. echo -en "WARNING(FROM): El envio de correo está desactivado (Compruebe comfiguración).\n"
  211. email=false
  212. fi
  213. if [ -z $pass ] && [ $email = true ];then
  214. echo -en "WARNING(PASSWORD): El envio de correo está desactivado (Compruebe comfiguración).\n"
  215. email=false
  216. fi
  217. if [ -z $host ] && [ $email = true ];then
  218. host=$(echo "$from" | cut -d '@' -f 2)
  219. host=$(dig -t MX "$host" +short | cut -d ' ' -f 2 | sed 's/.$//' | head -1)
  220. if [ -z $host ];then
  221. echo -en "WARNING(HOST): El envio de correo está desactivado (Compruebe comfiguración).\n"
  222. email=false
  223. fi
  224. fi
  225. # Obtenemos dominios
  226. echo -en $(date +'[%Y-%m-%d] %H:%M:%S')" Comprobando Virtual Hosts...\n"
  227. if [ $nginx = true ];then
  228. # Comprobamos sites-enabled de Nginx
  229. getVirtualHostsNginx "/etc/nginx/sites-enabled/"
  230. elif [ $apache = true ];then
  231. # Comprobamos sites-enabled de Apache
  232. getVirtualHostsApache "/etc/apache2/sites-enabled/"
  233. else
  234. cont=0
  235. for domain in $domains;do
  236. hostnames[$cont]=$domain
  237. cont=$((cont+1))
  238. done
  239. unset cont domain
  240. fi
  241. # Comprobamos dominios
  242. if [ ${#hostnames[@]} -eq 0 ];then
  243. echo -en "ERROR:\tNingún dominio encontrado.\n"
  244. usage
  245. exit 1
  246. fi
  247. for hostname in ${hostnames[@]};do
  248. checkHostname $hostname
  249. if [ $? -ne 0 ];then
  250. echo -en "* \"$hostname\" NO ESTÁ DISPONIBLE.\n"
  251. hostnamesFail=$hostnamesFail"* ""$hostname"" NO ESTÁ DISPONIBLE.\n"
  252. else
  253. echo -en "* \"$hostname\" ESTÁ DISPONIBLE.\n"
  254. fi
  255. done
  256. # Enviar email
  257. if [ ! -z "$hostnamesFail" ] && [ $email = true ];then
  258. echo -en "Enviando informe por email a $to..."
  259. message="Envío automático de "$(basename $0)".\nEl sistema presenta Virtual Hosts NO DISPONIBLES.\n""$hostnamesFail""\nCOMPRUEBE EL SERVIDOR.\n"
  260. sendEmail
  261. if [ $? -ne 0 ];then
  262. echo -en "\nWARNING:\tNo ha sido posible enviar email (REVISE CONFIGURACIÓN).\n"
  263. else
  264. echo -en " OK.\n"
  265. fi
  266. fi
  267. echo -en "\n"