solved nginx health bugs

This commit is contained in:
Kevin Veen-Birkenbach 2023-11-17 10:32:15 +01:00
parent 0b895e5694
commit 2e3e54f11f

View File

@ -1,6 +1,7 @@
import os import os
import requests import requests
import sys import sys
import re
# Define the path to the nginx configuration directory # Define the path to the nginx configuration directory
config_path = '/etc/nginx/conf.d/' config_path = '/etc/nginx/conf.d/'
@ -8,9 +9,12 @@ config_path = '/etc/nginx/conf.d/'
# Initialize the error counter # Initialize the error counter
error_counter = 0 error_counter = 0
# Regex pattern to match domain.tld or subdomain.domain.tld
pattern = re.compile(r"^(?:[\w-]+\.)?[\w-]+\.[\w-]+\.conf$")
# Iterate over each file in the configuration directory # Iterate over each file in the configuration directory
for filename in os.listdir(config_path): for filename in os.listdir(config_path):
if filename.endswith('.conf'): if filename.endswith('.conf') and pattern.match(filename):
# Extract the domain and subdomain from the filename # Extract the domain and subdomain from the filename
name = filename.replace('.conf', '') name = filename.replace('.conf', '')
parts = name.split('.') parts = name.split('.')
@ -19,13 +23,16 @@ for filename in os.listdir(config_path):
url = f"http://{name}" url = f"http://{name}"
# Determine expected status codes based on subdomain # Determine expected status codes based on subdomain
if parts[0] == 'www': if len(parts) == 3 and parts[0] == 'www':
expected_statuses = [301] expected_statuses = [200,301]
elif parts[0] == 's': elif len(parts) == 3 and parts[0] == 's':
expected_statuses = [403] expected_statuses = [403]
else: elif len(parts) <= 3:
# For domain.tld where no specific subdomain is present # For domain.tld where no specific subdomain is present
expected_statuses = [200, 301] expected_statuses = [200, 301]
else:
# Skip files that don't match the schema
continue
try: try:
# Send a HEAD request to get only the response header # Send a HEAD request to get only the response header