From 4f572b655d3984894ecaaea9143d78e5c4132c8c Mon Sep 17 00:00:00 2001 From: Eugene Amos Date: Thu, 7 Dec 2023 23:14:15 -0800 Subject: [PATCH] Inspector > Updated the `check_container_health(container_name)` function. Included to check if container is running and any other exceptions. If it finds any of those the functions will return messages that are not "Healthy" which will trigger the error count. --- inspector/inspector_qbitt.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/inspector/inspector_qbitt.py b/inspector/inspector_qbitt.py index 54920e2..5eb7bb6 100755 --- a/inspector/inspector_qbitt.py +++ b/inspector/inspector_qbitt.py @@ -24,6 +24,7 @@ if not os.path.exists(logs_folder): def get_log_file_path(): return os.path.join(logs_folder, 'inspector_qbitt.log') +# Rotate the log files at 12am daily def rotate_log_files(log_file): now = datetime.datetime.now() timestamp = now.strftime('%Y-%m-%d') @@ -35,17 +36,35 @@ def rotate_log_files(log_file): os.rename(log_file, rotated_log_path) +# Formating time def format_remaining_time(remaining_time): minutes, seconds = divmod(remaining_time.seconds, 60) return f'{minutes} minutes {seconds} seconds' +# Checking the health of the container def check_container_health(container_name): client = docker.from_env() - container = client.containers.get(container_name) - return container.attrs['State']['Health']['Status'].upper() + try: + container = client.containers.get(container_name) + # Check if container is running + if container.status != 'running': + return 'NOT RUNNING' + + health_data = container.attrs['State'].get('Health') + if health_data is None: + return 'NO HEALTH CHECK' + + health_status = health_data.get('Status', 'UNKNOWN') + return health_status.upper() + + except docker.errors.NotFound: + return 'CONTAINER NOT FOUND' + except Exception as e: + return f'ERROR: {str(e)}' +# Restaring the container def restart_container(container_name): client = docker.from_env() container = client.containers.get(container_name)