2023-11-17 09:36:40 +01:00
|
|
|
import os
|
|
|
|
import requests
|
|
|
|
import sys
|
2023-11-17 10:32:15 +01:00
|
|
|
import re
|
2023-11-17 09:36:40 +01:00
|
|
|
|
2023-12-12 15:21:02 +01:00
|
|
|
# file in which fqdn server configs are deposit
|
2023-12-12 17:43:13 +01:00
|
|
|
config_path = '{{nginx_servers_directory}}'
|
2023-11-17 09:36:40 +01:00
|
|
|
|
|
|
|
# Initialize the error counter
|
|
|
|
error_counter = 0
|
|
|
|
|
2023-12-12 17:43:13 +01:00
|
|
|
# Regex pattern to match domain.tld or *.domain.tld
|
|
|
|
pattern = re.compile(r"^(?:[\w-]+\.)*[\w-]+\.[\w-]+\.conf$")
|
2023-11-17 10:32:15 +01:00
|
|
|
|
2023-11-17 09:36:40 +01:00
|
|
|
# Iterate over each file in the configuration directory
|
|
|
|
for filename in os.listdir(config_path):
|
2023-11-17 10:32:15 +01:00
|
|
|
if filename.endswith('.conf') and pattern.match(filename):
|
2023-11-17 09:36:40 +01:00
|
|
|
# Extract the domain and subdomain from the filename
|
2023-12-12 17:43:13 +01:00
|
|
|
domain = filename.replace('.conf', '')
|
|
|
|
parts = domain.split('.')
|
2023-11-17 09:36:40 +01:00
|
|
|
|
|
|
|
# Prepare the URL and expected status codes
|
2023-12-12 17:43:13 +01:00
|
|
|
url = f"https://{domain}"
|
2023-12-06 12:31:39 +01:00
|
|
|
|
2023-12-12 17:43:13 +01:00
|
|
|
# Default: Expect status code 200 or 302 for a domain
|
|
|
|
expected_statuses = [200,302]
|
2023-11-17 09:36:40 +01:00
|
|
|
|
2023-12-12 17:43:13 +01:00
|
|
|
redirected_domains = [domain['source'] for domain in {{redirect_domain_mappings}}]
|
|
|
|
redirected_domains.append("{{domain_mailu}}")
|
|
|
|
|
|
|
|
# Determine expected status codes based on the domain
|
|
|
|
if domain == '{{domain_listmonk}}':
|
|
|
|
expected_statuses = [401]
|
|
|
|
{% if nginx_matomo_tracking | bool %}
|
|
|
|
elif parts[0] == 'www' or domain in redirected_domains:
|
|
|
|
expected_statuses = [301]
|
|
|
|
{% endif %}
|
|
|
|
elif domain == '{{domain_yourls}}':
|
|
|
|
expected_statuses = [403]
|
2023-11-17 09:36:40 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
# Send a HEAD request to get only the response header
|
2023-12-12 17:43:13 +01:00
|
|
|
response = requests.head(url)
|
2023-11-17 09:36:40 +01:00
|
|
|
|
|
|
|
# Check if the status code matches the expected statuses
|
|
|
|
if response.status_code in expected_statuses:
|
2023-12-12 17:43:13 +01:00
|
|
|
print(f"{domain}: OK")
|
2023-11-17 09:36:40 +01:00
|
|
|
else:
|
2023-12-12 17:43:13 +01:00
|
|
|
print(f"{domain}: ERROR: Expected {expected_statuses}. Got {response.status_code}.")
|
2023-11-17 09:36:40 +01:00
|
|
|
error_counter += 1
|
|
|
|
except requests.RequestException as e:
|
|
|
|
# Handle exceptions for requests like connection errors
|
2023-12-12 17:43:13 +01:00
|
|
|
print(f"{domain}: error due to {e}")
|
2023-11-17 09:36:40 +01:00
|
|
|
error_counter += 1
|
|
|
|
|
2023-12-12 17:43:13 +01:00
|
|
|
if error_counter > 0:
|
|
|
|
print(f"Warning: {error_counter} domains responded with an unexpected https status code.")
|
|
|
|
|
2023-11-17 09:36:40 +01:00
|
|
|
# Exit the script with the number of errors as the exit code
|
|
|
|
sys.exit(error_counter)
|