mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2025-08-29 15:06:26 +02:00
solved health-nginx bugs
This commit is contained in:
55
roles/health-nginx/templates/health-nginx.py.j2
Normal file
55
roles/health-nginx/templates/health-nginx.py.j2
Normal file
@@ -0,0 +1,55 @@
|
||||
import os
|
||||
import requests
|
||||
import sys
|
||||
import re
|
||||
|
||||
# file in which fqdn server configs are deposit
|
||||
config_path = {{nginx_servers_directory}}
|
||||
|
||||
# Initialize the error counter
|
||||
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
|
||||
for filename in os.listdir(config_path):
|
||||
if filename.endswith('.conf') and pattern.match(filename):
|
||||
# Extract the domain and subdomain from the filename
|
||||
name = filename.replace('.conf', '')
|
||||
parts = name.split('.')
|
||||
|
||||
# Prepare the URL and expected status codes
|
||||
url = f"https://{name}"
|
||||
|
||||
# Default: Expect status code 200 for a domain
|
||||
expected_statuses = [200]
|
||||
|
||||
# Determine expected status codes based on subdomain
|
||||
if len(parts) == 3:
|
||||
if parts[0] == '{{domain_listmonk}}':
|
||||
expected_statuses = [401]
|
||||
{% if nginx_matomo_tracking | bool %}
|
||||
elif parts[0] == '{{nginx_www_redirect}}':
|
||||
expected_statuses = [200,301]
|
||||
{% endif %}
|
||||
elif parts[0] == '{{domain_yourls}}':
|
||||
expected_statuses = [403]
|
||||
|
||||
try:
|
||||
# Send a HEAD request to get only the response header
|
||||
response = requests.head(url, allow_redirects=True)
|
||||
|
||||
# Check if the status code matches the expected statuses
|
||||
if response.status_code in expected_statuses:
|
||||
print(f"{name}: ok")
|
||||
else:
|
||||
print(f"{name}: error")
|
||||
error_counter += 1
|
||||
except requests.RequestException as e:
|
||||
# Handle exceptions for requests like connection errors
|
||||
print(f"{name}: error due to {e}")
|
||||
error_counter += 1
|
||||
|
||||
# Exit the script with the number of errors as the exit code
|
||||
sys.exit(error_counter)
|
@@ -4,4 +4,4 @@ OnFailure=systemd-notifier@%n.service
|
||||
|
||||
[Service]
|
||||
Type=oneshot
|
||||
ExecStart=/usr/bin/python3 {{ health_nginx_folder }}health-nginx.py {{nginx_servers_directory}}
|
||||
ExecStart=/usr/bin/python3 {{ health_nginx_folder }}health-nginx.py
|
||||
|
Reference in New Issue
Block a user