From 4c462a89719b5012d353d28a3c308dda8f07df23 Mon Sep 17 00:00:00 2001 From: Paige Patton Date: Wed, 4 Jun 2025 14:40:26 -0400 Subject: [PATCH] updating health checks Signed-off-by: Paige Patton --- krkn/utils/HealthChecker.py | 84 +++++++++++++++++++------------------ 1 file changed, 44 insertions(+), 40 deletions(-) diff --git a/krkn/utils/HealthChecker.py b/krkn/utils/HealthChecker.py index a233c2ad..6317293a 100644 --- a/krkn/utils/HealthChecker.py +++ b/krkn/utils/HealthChecker.py @@ -13,16 +13,15 @@ class HealthChecker: def make_request(self, url, auth=None, headers=None, verify=True): response_data = {} - response = requests.get(url, auth=auth, headers=headers, verify=verify) + response = requests.get(url, auth=auth, headers=headers, verify=verify, timeout=3) response_data["url"] = url response_data["status"] = response.status_code == 200 response_data["status_code"] = response.status_code return response_data - def run_health_check(self, health_check_config, health_check_telemetry_queue: queue.Queue): + def run_health_check(self, health_check_config, health_check_telemetry_queue: queue.Queue): if health_check_config and health_check_config["config"] and any(config.get("url") for config in health_check_config["config"]): - health_check_start_time_stamp = datetime.now() health_check_telemetry = [] health_check_tracker = {} interval = health_check_config["interval"] if health_check_config["interval"] else 2 @@ -37,49 +36,54 @@ class HealthChecker: if config["bearer_token"]: bearer_token = "Bearer " + config["bearer_token"] headers = {"Authorization": bearer_token} - if config["auth"]: auth = tuple(config["auth"].split(',')) - response = self.make_request(url, auth, headers, verify_url) - - if response["status_code"] != 200: - if config["url"] not in health_check_tracker: - start_timestamp = datetime.now() - health_check_tracker[config["url"]] = { - "status_code": response["status_code"], - "start_timestamp": start_timestamp - } + try: + response = self.make_request(url, auth, headers, verify_url) + except Exception: + response = {} + response['status_code'] = 500 + + if config["url"] not in health_check_tracker: + start_timestamp = datetime.now() + health_check_tracker[config["url"]] = { + "status_code": response["status_code"], + "start_timestamp": start_timestamp + } + if response["status_code"] != 200: if response_tracker[config["url"]] != False: response_tracker[config["url"]] = False if config["exit_on_failure"] and config["exit_on_failure"] == True and self.ret_value==0: self.ret_value = 2 else: - if config["url"] in health_check_tracker: - end_timestamp = datetime.now() - start_timestamp = health_check_tracker[config["url"]]["start_timestamp"] - previous_status_code = str(health_check_tracker[config["url"]]["status_code"]) - duration = (end_timestamp - start_timestamp).total_seconds() - downtime_record = { - "url": config["url"], - "status": False, - "status_code": previous_status_code, - "start_timestamp": start_timestamp.isoformat(), - "end_timestamp": end_timestamp.isoformat(), - "duration": duration - } - health_check_telemetry.append(HealthCheck(downtime_record)) - del health_check_tracker[config["url"]] + if response["status_code"] != health_check_tracker[config["url"]]["status_code"]: + end_timestamp = datetime.now() + start_timestamp = health_check_tracker[config["url"]]["start_timestamp"] + previous_status_code = str(health_check_tracker[config["url"]]["status_code"]) + duration = (end_timestamp - start_timestamp).total_seconds() + change_record = { + "url": config["url"], + "status": False, + "status_code": previous_status_code, + "start_timestamp": start_timestamp.isoformat(), + "end_timestamp": end_timestamp.isoformat(), + "duration": duration + } + + health_check_telemetry.append(HealthCheck(change_record)) + if response_tracker[config["url"]] != True: response_tracker[config["url"]] = True + del health_check_tracker[config["url"]] time.sleep(interval) health_check_end_time_stamp = datetime.now() - for url, status in response_tracker.items(): - if status == True: - duration = (health_check_end_time_stamp - health_check_start_time_stamp).total_seconds() - success_response = { - "url": url, - "status": True, - "status_code": 200, - "start_timestamp": health_check_start_time_stamp.isoformat(), - "end_timestamp": health_check_end_time_stamp.isoformat(), - "duration": duration - } - health_check_telemetry.append(HealthCheck(success_response)) + for url in health_check_tracker.keys(): + duration = (health_check_end_time_stamp - health_check_tracker[url]["start_timestamp"]).total_seconds() + success_response = { + "url": url, + "status": True, + "status_code": response["status_code"], + "start_timestamp": health_check_tracker[url]["start_timestamp"].isoformat(), + "end_timestamp": health_check_end_time_stamp.isoformat(), + "duration": duration + } + health_check_telemetry.append(HealthCheck(success_response)) + health_check_telemetry_queue.put(health_check_telemetry) else: logging.info("health checks config is not defined, skipping them") \ No newline at end of file