The health_check.py
script performs a HTTP health check on given URLs and outputs the health status in JSON format. It checks the URLs using a multi-threaded approach and supports both full and simple reporting.
This script was tested with the following software and system
- Python 3
- Ubuntu 22.04
- nginx
- uwsgi-plugin-python3
- uwsgi
- uwsgi-emperor
The script accepts the following arguments:
--report
: Output full status in JSON format.--simple
: Output simple status in JSON format. (Note: If no argument is specified, the default behaviour is to output a Healthy or Unhealthy status in plain text.)
The URLs to be checked and other configurations are specified in the config.json
file. The configuration options are as follows:
urls
: A list of URLs to be checked.timeout
: The timeout (in seconds) for each HTTP request.success_codes
: A list of HTTP status codes considered as successful responses.verify_tls
: A boolean value indicating whether TLS verification should be enabled or disabled.
Example config.json
:
{
"urls": [
"https://google.com",
"https://apple.com"
],
"timeout": 5,
"success_codes": [200, 301, 401, 404, 502],
"verify_tls": true
}
sudo apt-get update
sudo apt-get install nginx uwsgi uwsgi-plugin-python3 uwsgi-emperor
-
Create the
/opt/healthcheck
directorymkdir /opt/healthcheck
-
Copy the script and example configuration
cp health_check.py /opt/healthcheck cp config.json /opt/healthcheck
-
Set up permissions
chown -R www-data:www-data /opt/healthcheck chmod 755 /opt/healthcheck/health_check.py chmod 644 /opt/healthcheck/config.json
To run the script with its default behaviour:
/opt/healthcheck/health_check.py
To get the full status in JSON format:
/opt/healthcheck/health_check.py --report
To get the simple status in JSON format:
/opt/healthcheck/health_check.py --simple
-
Copy the
health_check.ini
uWSGI app configurationcp health_check.ini /etc/uwsgi-emperor/vassals
-
Set up permissions
chown root:root /etc/uwsgi-emperor/vassals/health_check.ini chmod 644 /etc/uwsgi-emperor/vassals/health_check.ini
Below is an NGINX proxy configuration to the uWSGI app:
server {
listen 80 default_server;
server_name _;
location / {
return 200 '';
add_header Content-Type text/html;
}
location /healthcheck {
include uwsgi_params;
uwsgi_pass unix:/opt/healthcheck/health_check.sock;
}
}