Explorar el Código

* Versión 0.6

Guzmán Castanedo Villalba hace 5 años
padre
commit
4b9e96bc84
Se han modificado 2 ficheros con 110 adiciones y 16 borrados
  1. 5 1
      README.md
  2. 105 15
      check-sites

+ 5 - 1
README.md

@@ -1,6 +1,6 @@
 # check-sites
 
-Comprueba si todos los Virtual Hosts de Nginx están online y envía un email de error en caso contrario.
+Comprueba si todos los Virtual Hosts de Nginx o de Apache están online y envía un email de error en caso contrario.
 
 ## Sinopsis
 * `check-sites [OPCIONES]`
@@ -13,6 +13,10 @@ Comprueba si todos los Virtual Hosts de Nginx están online y envía un email de
 * `-s, --subject`: concepto del email.
 * `-H, --host <host>`: dominio o IP del servidor SMTP (necesita STARTTLS).
 * `-p, --port <port>`: puerto TCP/IP del servidor SMTP (por defecto: 587).
+* `-d, --domains <domain1[,domain2[,...]]>`: dominios a comprobar.
+* `--nginx`: obtiene los dominios de `/etc/nginx/sites-enabled`.
+* `--apache`: obtiene los dominios de `/etc/apache2/sites-enabled`.
+* `--no-email`: desactiva el envio de emails.
 
 ## Instalación
 * `git clone https://code.castanedo.es/guzman/check-sites.git`

+ 105 - 15
check-sites

@@ -1,8 +1,10 @@
 #!/bin/bash
 
 ##################################################################
-# check-sites: comprueba todos los Virtual Hosts de Nginx y      #
+# check-sites: comprueba los dominios de un servidor web y       #
 # envía un email si alguno no se encuentra Online.               #
+# Los dominios pueden provenir de una lista proporcionada u      #
+# obtenidos de los sitios web configurados en Nginx o en Apache. #
 #                                                                #
 # Autor: Guzmán Castanedo Villalba                               #
 # Email: guzman@castanedo.es                                     #
@@ -10,7 +12,7 @@
 # Agosto 2018                                                    #
 ##################################################################
 
-help() {
+usage() {
 	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"
@@ -19,6 +21,10 @@ help() {
 	echo -en "  -s, --subject <subject>: concepto del email.\n"
 	echo -en "  -H, --host <host>: dominio o IP del servidor SMTP (necesita STARTTLS).\n"
 	echo -en "  -p, --port <port>: puerto TCP/IP del servidor SMTP (por defecto: $port).\n"
+	echo -en "  -d, --domains <domain1[,domain2[,...]]>: dominios a comprobar.\n"
+	echo -en "  --nginx: obtiene los dominios de \"/etc/nginx/sites-enabled/\".\n"
+	echo -en "  --apache: obtiene los dominios de \"/etc/apache2/sites-enabled/\".\n"
+	echo -en "  --no-email: desactiva el envio de emails.\n"
 }
 
 sendEmail() {
@@ -27,20 +33,20 @@ sendEmail() {
 	return $?
 }
 
-getVirtualHosts() {
-# getVirtualHost $virtualHostPath
+getVirtualHostsNginx() {
+# getVirtualHostNginx $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
+		grep '^[[:space:]]*auth_basic' "$virtualHost" > /dev/null 2>&1
 		if [ $? -eq 0 ];then
 			# Si tiene la directiva "auth_basic" activada no lo comprobamos
 			continue
 		fi
-		linea=$(grep ^[[:space:]]*server_name "$virtualHost")
+		linea=$(grep '^[[:space:]]*server_name' "$virtualHost")
 		if [ $? -ne 0 ];then
 			# Si no tiene la directiva "server_name" no lo comprobamos
 			continue
@@ -58,14 +64,55 @@ getVirtualHosts() {
 	unset linea cont hostname
 }
 
+getVirtualHostsApache() {
+# getVirtualHostsApache $virtualHostPath
+	if [ ! -d $1 ];then
+		echo -en "ERROR:\tEl directorio \"$1\" no existe.\n"
+		exit 1
+	fi
+	cont=0
+	for virtualHost in $(find -L "$1" -type f);do
+		grep '^[[:space:]]*AuthType' "$virtualHost" > /dev/null 2>&1
+		if [ $? -eq 0 ];then
+			# Si tiene la directiva "AuthType" activada no lo comprobamos
+			continue
+		fi
+		linea=$(grep '^[[:space:]]*ServerName' "$virtualHost")
+		if [ $? -eq 0 ];then
+			for hostname in $linea;do
+				if [ $hostname != "ServerName" ] && [ $hostname != "localhost" ];then
+					hostnames[$cont]=$hostname
+					cont=$((cont+1))
+				fi
+			done
+		else
+			# Si no tiene la directiva "ServerName" no lo comprobamos
+			continue
+		fi
+		linea2=$(grep '^[[:space:]]*ServerAlias' "$virtualHost")
+		if [ $? -eq 0 ];then
+			# Si tiene la directiva "ServerAlias" las añadimos
+			for hostname in $linea2;do
+				if [ $hostname != "ServerAlias" ] && [ $hostname != "localhost" ];then
+					hostnames[$cont]=$hostname
+					cont=$((cont+1))
+				fi
+			done
+		fi
+	done
+	# Eliminar repetidos
+	hostnames=($(echo "${hostnames[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' '))
+	unset linea linea2 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")
+	local domain=$1
+	local httpCode=$(curl -L -s -o /dev/null -w "%{http_code}" "$domain")
 	case $httpCode in
 		2[0-9][0-9])
 			# Sitio Disponible
@@ -82,21 +129,24 @@ checkHostname() {
 hostnames=""
 hostnamesFail=""
 email=true
+nginx=true
+apache=false
 to=""
 from=""
 pass=""
 subject="WARNING: Virtual Host NO DISPONIBLE"
 host=""
 port="587"
+domains=""
 
 # Leer Opciones
-TEMP=$(getopt -q -o ht:f:P:s:H:p: --longoptions help,to:,from:,password:,subject:,host:,port: --name $(basename $0) -- "$@")
+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) -- "$@")
 eval set -- $TEMP
 unset TEMP
 while true; do
 	case $1 in
 		-h|--help)
-			help
+			usage
 			exit 0
 			;;
 		-t|--to)
@@ -123,6 +173,24 @@ while true; do
 			port=$2
 			shift 2
 			;;
+		-d|--domains)
+			domains=$(echo "$2" | sed 's/,/ /g')
+			shift 2
+			nginx=false
+			apache=false
+			;;
+		--nginx)
+			nginx=true
+			apache=false
+			;;
+		--apache)
+			nginx=false
+			apache=true
+			;;
+		--no-email)
+			email=false
+			shift
+			;;
 		--)
 			# Ultimo
 			shift
@@ -161,22 +229,43 @@ if [ -z $host ] && [ $email = true ];then
 	fi
 fi
 
-# Comienza el programa
+# Obtenemos dominios
 echo -en $(date +'[%Y-%m-%d] %H:%M:%S')" Comprobando Virtual Hosts...\n"
-getVirtualHosts "/etc/nginx/sites-enabled/"
+if [ $nginx = true ];then
+	# Comprobamos sites-enabled de Nginx
+	getVirtualHostsNginx "/etc/nginx/sites-enabled/"
+elif [ $apache = true ];then
+	# Comprobamos sites-enabled de Apache
+	getVirtualHostsApache "/etc/apache2/sites-enabled/"
+else
+	cont=0
+	for domain in $domains;do
+		hostnames[$cont]=$domain
+		cont=$((cont+1))
+	done
+	unset cont domain
+fi
+
+# Comprobamos dominios
+if [ -z ${hostnames[@]} ];then
+	echo -en "ERROR:\tNingún dominio encontrado.\n"
+	usage
+	exit 1
+fi
 for hostname in ${hostnames[@]};do
 	checkHostname $hostname
 	if [ $? -ne 0 ];then
-		echo -en "* \"$hostname\"\tNO ESTÁ DISPONIBLE.\n"
+		echo -en "* \"$hostname\" NO ESTÁ DISPONIBLE.\n"
 		hostnamesFail=$hostnamesFail"* ""$hostname"" NO ESTÁ DISPONIBLE.\n"
 	else
-		echo -en "* \"$hostname\"\t ESTÁ DISPONIBLE.\n"
+		echo -en "* \"$hostname\" ESTÁ DISPONIBLE.\n"
 	fi
 done
 
+# Enviar email
 if [ ! -z "$hostnamesFail" ] && [ $email = true ];then
 	echo -en "Enviando informe por email a $to..."
-	message="Envío automático de "$(basename $0)".\nEl sistema presenta Virtual Hosts NO DISPONIBLES.\n""$hostnamesFail""\nCOMPRUEBE QUE EL SERVIDOR.\n"
+	message="Envío automático de "$(basename $0)".\nEl sistema presenta Virtual Hosts NO DISPONIBLES.\n""$hostnamesFail""\nCOMPRUEBE EL SERVIDOR.\n"
 	sendEmail
 	if [ $? -ne 0 ];then
 		echo -en "\nWARNING:\tNo ha sido posible enviar email (REVISE CONFIGURACIÓN).\n"
@@ -184,3 +273,4 @@ if [ ! -z "$hostnamesFail" ] && [ $email = true ];then
 		echo -en " OK.\n"
 	fi
 fi
+echo -en "\n"