Explorar el Código

* Versión 0.2

Guzmán Castanedo Villalba hace 5 años
padre
commit
3ced9fa0f8
Se han modificado 1 ficheros con 43 adiciones y 3 borrados
  1. 43 3
      check-virtualhosts

+ 43 - 3
check-virtualhosts

@@ -19,12 +19,17 @@ sendEmail() {
 	port=$1
 	shift
 	message=$@
-	echo -en "$message" | mailx -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 >> $logFile 2>&1
+	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
+	return $?
 	unset to from pass subject host port message
 }
 
 getVirtualHosts() {
 # getVirtualHost $virtualHostPath
+	if [ ! -d $1 ];then
+		echo -en "ERROR:\tEl directorio \"$1\" no existe.\n"
+		exit 1
+	fi
 	cont=1
 	for virtualHost in $(find -L "$1" -type f);do
 		grep ^[[:space:]]*auth_basic "$virtualHost" > /dev/null 2>&1
@@ -47,12 +52,47 @@ getVirtualHosts() {
 	done
 	# Eliminar repetidos
 	hostnames=($(echo "${hostnames[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' '))
-	echo -en "Hostnames: ""${hostnames[@]}""\n"
 	unset linea cont hostname
 }
 
+checkHostname() {
+# checkHostname $domain
+	if [ $# -ne 1 ];then
+		echo -en "ERROR:\tError interno en checkHostname.\n"
+		exit 1
+	fi
+	domain=$1
+	httpCode=$(curl -L -s -o /dev/null -w "%{http_code}" "$domain")
+	case $httpCode in
+		2[0-9][0-9])
+			# Sitio Disponible
+			return 0
+			;;
+		*)
+			# Se ha producido algún error
+			return 1
+			;;
+	esac
+}
+
 # Variables iniciales
-logFile="."$(basename $0)".log"
 hostnames=""
+hostnamesFail=""
 
+echo -en $(date +'[%Y-%m-%d] %H:%M:%S')" Comprobando Virtual Hosts...\n"
 getVirtualHosts "/etc/nginx/sites-enabled/"
+for hostname in $hostnames;do
+	checkHostname $hostname
+	if [ $? -ne 0 ];then
+		# Este dominio no está disponible
+		echo -en "* \"$hostname\"\t\tNO ESTÁ DISPONIBLE.\n"
+		hostnamesFail=$hostnameFail"* ""$hostname"" NO ESTÁ DISPONIBLE.\n"
+	else
+		echo -en "* \"$hostname\"\t\t ESTÁ DISPONIBLE.\n"
+	fi
+done
+
+if [ ! -z $hostnamesFail ];then
+	echo -en "Enviando informe por email a \$TO..."
+	#sendEmail
+fi