Explorar el Código

* Versión 0.3

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

+ 94 - 24
check-virtualhosts

@@ -1,27 +1,20 @@
 #!/bin/bash
 #!/bin/bash
 
 
+help() {
+	echo -en "Uso: "$(basename $0)" [OPCIONES]\n\n"
+	echo -en "  -h, --help: muestra mensaje de error y finaliza.\n"
+	echo -en "  -t, --to <email>: email destinatario.\n"
+	echo -en "  -f, --from: email remitente.\n"
+	echo -en "  -P, --password: contraseña del remitente.\n"
+	echo -en "  -s, --subject: Concepto del email.\n"
+	echo -en "  -H, --host: dominio o IP del servidor SMTP (necesita STARTTLS).\n"
+	echo -en "  -p, --port: puerto TCP/IP del servidor SMTP (por defecto: $port).\n"
+}
+
 sendEmail() {
 sendEmail() {
-# sendEmail $to $from $pass $subject $host $port $message
-	if [ $# -ne 7 ]; then
-		echo -en "ERROR:\tError interno en sendEmail.\n"
-		exit 1
-	fi
-	to=$1
-	shift
-	from=$1
-	shift
-	pass=$1
-	shift
-	subject=$1
-	shift
-	host=$1
-	shift
-	port=$1
-	shift
-	message=$@
+# sendEmail
 	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
 	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 $?
 	return $?
-	unset to from pass subject host port message
 }
 }
 
 
 getVirtualHosts() {
 getVirtualHosts() {
@@ -78,21 +71,98 @@ checkHostname() {
 # Variables iniciales
 # Variables iniciales
 hostnames=""
 hostnames=""
 hostnamesFail=""
 hostnamesFail=""
+email=true
+to=""
+from=""
+pass=""
+subject="WARNING: Virtual Host NO DISPONIBLE"
+host=""
+port="587"
+
+# Leer Opciones
+TEMP=$(getopt -q -o ht:f:P:s:H:p: --longoptions help,to:,from:,password:,subject:,host:,port: --name $(basename $0) -- "$@")
+eval set -- $TEMP
+unset TEMP
+while true; do
+	case $1 in
+		-h|--help)
+			help
+			exit 0
+			;;
+		-t|--to)
+			to=$2
+			shift 2
+			;;
+		-f|--from)
+			from=$2
+			shift 2
+			;;
+		-P|--password)
+			pass=$2
+			shift 2
+			;;
+		-s|--subject)
+			subject=$2
+			shift 2
+			;;
+		-H|--host)
+			host=$2
+			shift 2
+			;;
+		-p|--port)
+			port=$2
+			shift 2
+			;;
+		--)
+			# Ultimo
+			shift
+			break
+			;;
+		*)
+			# Inesperado
+			help
+			exit 1
+			;;
+	esac
+done
+if [ -z $to ];then
+	echo -en "WARNING: El envio de correo está desactivado (Compruebe comfiguración).\n"
+	email=false
+fi
+if [ -z $from ];then
+	echo -en "WARNING: El envio de correo está desactivado (Compruebe comfiguración).\n"
+	email=false
+fi
+if [ -z $password ];then
+	echo -en "WARNING: El envio de correo está desactivado (Compruebe comfiguración).\n"
+	email=false
+fi
+if [ -z $host ];then
+	host=$(echo "$from" | cut -d '@' -f 2)
+	host=$(dig -t MX "$host" +short | cut -d ' ' -f 2 | sed 's/.$//' | head -1)
+	if [ -z $host];then
+		echo -en "WARNING: El envio de correo está desactivado (Compruebe comfiguración).\n"
+		email=false
+	fi
+fi
 
 
+# Comienza el programa
 echo -en $(date +'[%Y-%m-%d] %H:%M:%S')" Comprobando Virtual Hosts...\n"
 echo -en $(date +'[%Y-%m-%d] %H:%M:%S')" Comprobando Virtual Hosts...\n"
 getVirtualHosts "/etc/nginx/sites-enabled/"
 getVirtualHosts "/etc/nginx/sites-enabled/"
 for hostname in ${hostnames[@]};do
 for hostname in ${hostnames[@]};do
 	checkHostname $hostname
 	checkHostname $hostname
 	if [ $? -ne 0 ];then
 	if [ $? -ne 0 ];then
-		# Este dominio no está disponible
-		echo -en "* \"$hostname\"\t\tNO ESTÁ DISPONIBLE.\n"
+		echo -en "* \"$hostname\"\tNO ESTÁ DISPONIBLE.\n"
 		hostnamesFail=$hostnameFail"* ""$hostname"" NO ESTÁ DISPONIBLE.\n"
 		hostnamesFail=$hostnameFail"* ""$hostname"" NO ESTÁ DISPONIBLE.\n"
 	else
 	else
-		echo -en "* \"$hostname\"\t\t ESTÁ DISPONIBLE.\n"
+		echo -en "* \"$hostname\"\t ESTÁ DISPONIBLE.\n"
 	fi
 	fi
 done
 done
 
 
-if [ ! -z $hostnamesFail ];then
+if [ ! -z $hostnamesFail ] && [ $email = true ];then
 	echo -en "Enviando informe por email a \$TO..."
 	echo -en "Enviando informe por email a \$TO..."
-	#sendEmail
+	sendEmail
+	if [ $? -ne 0 ];then
+		echo -en "WARNING:\tNo ha sido posible enviar email (REVISE CONFIGURACIÓN).\n"
+	fi
 fi
 fi