optimized healh-nginx logic

This commit is contained in:
Kevin Veen-Birkenbach 2023-12-06 12:31:39 +01:00
parent c43481f8e7
commit 06013413e9
2 changed files with 49 additions and 15 deletions

View File

@ -1,7 +1,41 @@
# health-nginx # health-nginx
Sends a health report for nginx configurations. This role was created with the help of ChatGPT. The conversation you will find [here](https://chat.openai.com/share/4033be29-12a6-40a3-bf3c-fc5d57dba8cb). ## Overview
`health-nginx` is an Ansible role designed to send health reports for nginx configurations. It leverages Python scripting to check the status of nginx server configurations and reports back any issues. This role is especially useful for maintaining the health of nginx servers in a dynamic environment.
## see ## Requirements
- https://nginx.org/en/docs/ - Ansible
- https://docs.ansible.com/ansible/latest/modules/uri_module.html - Python with the `requests` module
- Access to the nginx configuration files
## Role Variables
- `health_nginx_folder`: The folder where the `health-nginx` script and related files are stored. Defaults to `"{{ path_administrator_scripts }}health-nginx/"`.
## Dependencies
This role depends on:
- `python-pip`: For installing Python packages.
- `systemd-notifier`: For notifying systemd in case of any failures.
## Example Playbook
```yaml
- hosts: servers
roles:
- { role: health-nginx }
```
## Usage
1. **Installation of Python Modules**: The role installs the required Python `requests` module.
2. **File and Directory Management**: It creates the necessary directories and files, including the `health-nginx.py` script.
3. **Service and Timer Templates**: Templates for `health-nginx.service` and `health-nginx.timer` are set up to automate the health checks.
4. **Running the Health Check**: The `health-nginx.py` script is executed to perform the health check. It iterates over nginx configuration files and sends a HEAD request to each domain/subdomain to verify its status. The script considers different expected status codes based on the domain or subdomain.
## Handler Details
- **reload health-nginx.service**: Reloads the `health-nginx.service` if there are any changes to the service file.
- **restart health-nginx.timer**: Restarts and enables the `health-nginx.timer` to schedule regular health checks.
## Additional Information
- For more details on nginx configurations, visit [nginx documentation](https://nginx.org/en/docs/).
- Learn more about Ansible's `uri_module` [here](https://docs.ansible.com/ansible/latest/modules/uri_module.html).
## Contributions
This role was created with the assistance of ChatGPT. The conversation can be found [here](https://chat.openai.com/share/4033be29-12a6-40a3-bf3c-fc5d57dba8cb) and [here](https://chat.openai.com/share/7f3766d1-9db7-4976-8fe9-68d1142c0a78).

View File

@ -20,19 +20,19 @@ for filename in os.listdir(config_path):
parts = name.split('.') parts = name.split('.')
# Prepare the URL and expected status codes # Prepare the URL and expected status codes
url = f"http://{name}" url = f"https://{name}"
# Default: Expect status code 200 for a domain
expected_statuses = [200]
# Determine expected status codes based on subdomain # Determine expected status codes based on subdomain
if len(parts) == 3 and parts[0] == 'www': if len(parts) == 3:
if parts[0] == 'listmonk':
expected_statuses = [401]
elif parts[0] == 'www':
expected_statuses = [200,301] expected_statuses = [200,301]
elif len(parts) == 3 and parts[0] == 's': elif parts[0] == 's':
expected_statuses = [403] expected_statuses = [403]
elif len(parts) <= 3:
# For domain.tld where no specific subdomain is present
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