check-virtualhosts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/bin/bash
  2. sendEmail() {
  3. # sendEmail $to $from $pass $subject $host $port $message
  4. if [ $# -ne 7 ]; then
  5. echo -en "ERROR:\tError interno en sendEmail.\n"
  6. exit 1
  7. fi
  8. to=$1
  9. shift
  10. from=$1
  11. shift
  12. pass=$1
  13. shift
  14. subject=$1
  15. shift
  16. host=$1
  17. shift
  18. port=$1
  19. shift
  20. message=$@
  21. 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
  22. return $?
  23. unset to from pass subject host port message
  24. }
  25. getVirtualHosts() {
  26. # getVirtualHost $virtualHostPath
  27. if [ ! -d $1 ];then
  28. echo -en "ERROR:\tEl directorio \"$1\" no existe.\n"
  29. exit 1
  30. fi
  31. cont=1
  32. for virtualHost in $(find -L "$1" -type f);do
  33. grep ^[[:space:]]*auth_basic "$virtualHost" > /dev/null 2>&1
  34. if [ $? -eq 0 ];then
  35. # Si tiene la directiva "auth_basic" activada no lo comprobamos
  36. continue
  37. fi
  38. linea=$(grep ^[[:space:]]*server_name "$virtualHost")
  39. if [ $? -ne 0 ];then
  40. # Si no tiene la directiva "server_name" no lo comprobamos
  41. continue
  42. fi
  43. for hostname in $linea;do
  44. hostname=$(echo "$hostname" | sed 's/;//g')
  45. if [ $hostname != "server_name" ] && [ $hostname != "localhost" ];then
  46. hostnames[$cont]=$hostname
  47. cont=$((cont+1))
  48. fi
  49. done
  50. done
  51. # Eliminar repetidos
  52. hostnames=($(echo "${hostnames[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' '))
  53. unset linea cont hostname
  54. }
  55. checkHostname() {
  56. # checkHostname $domain
  57. if [ $# -ne 1 ];then
  58. echo -en "ERROR:\tError interno en checkHostname.\n"
  59. exit 1
  60. fi
  61. domain=$1
  62. httpCode=$(curl -L -s -o /dev/null -w "%{http_code}" "$domain")
  63. case $httpCode in
  64. 2[0-9][0-9])
  65. # Sitio Disponible
  66. return 0
  67. ;;
  68. *)
  69. # Se ha producido algún error
  70. return 1
  71. ;;
  72. esac
  73. }
  74. # Variables iniciales
  75. hostnames=""
  76. hostnamesFail=""
  77. echo -en $(date +'[%Y-%m-%d] %H:%M:%S')" Comprobando Virtual Hosts...\n"
  78. getVirtualHosts "/etc/nginx/sites-enabled/"
  79. for hostname in ${hostnames[@]};do
  80. checkHostname $hostname
  81. if [ $? -ne 0 ];then
  82. # Este dominio no está disponible
  83. echo -en "* \"$hostname\"\t\tNO ESTÁ DISPONIBLE.\n"
  84. hostnamesFail=$hostnameFail"* ""$hostname"" NO ESTÁ DISPONIBLE.\n"
  85. else
  86. echo -en "* \"$hostname\"\t\t ESTÁ DISPONIBLE.\n"
  87. fi
  88. done
  89. if [ ! -z $hostnamesFail ];then
  90. echo -en "Enviando informe por email a \$TO..."
  91. #sendEmail
  92. fi