From 7601be0f1bbd03614753dbbbca48b6946a3ffde5 Mon Sep 17 00:00:00 2001 From: friendly Friend Date: Sat, 22 Jul 2023 21:50:59 +0200 Subject: [PATCH] what's new: easier console log output to troubleshoot errors fixed issue where exporter did not start when immich wasn't running after CA backup --- immich_exporter/exporter.py | 86 +++++++++++++++++++++++---- setup.py | 2 +- unraid/prometheus-immich-exporter.xml | 4 +- 3 files changed, 76 insertions(+), 16 deletions(-) diff --git a/immich_exporter/exporter.py b/immich_exporter/exporter.py index f752ce8..9426ffc 100644 --- a/immich_exporter/exporter.py +++ b/immich_exporter/exporter.py @@ -39,6 +39,7 @@ class ImmichMetricsCollector: yield prom_metric def get_immich_metrics(self): + metrics = [] metrics.extend(self.get_immich_server_version_number()) metrics.extend(self.get_immich_server_info()) @@ -47,6 +48,7 @@ class ImmichMetricsCollector: return metrics + def get_immich_users_stat_growth(self): try: @@ -210,25 +212,29 @@ class ImmichMetricsCollector: ] def get_immich_server_version_number(self): + # Requesting immich_server_number serves two purposes. As the name says it returns the version number + # 1. get version the full server version number + # 2. check if immich api key is correct + # throwing connectionRefused exception usually means that immich isn't running server_version_endpoint = "/api/server-info/version" response_server_version = "" - for i in range(0, 360): - while True: - try: + while True: + try: - response_server_version = requests.request( - "GET", - self.combine_url(server_version_endpoint), - headers={'Accept': 'application/json', - "x-api-key": self.config["token"]} - ) - except requests.exceptions.RequestException as e: - logger.error(f"Couldn't get server version: {e}") - continue - break + response_server_version = requests.request( + "GET", + self.combine_url(server_version_endpoint), + headers={'Accept': 'application/json', + "x-api-key": self.config["token"]} + ) + except requests.exceptions.RequestException as e: + logger.error(f"Couldn't get server version") + continue + break + logger.info(f"immich is up and running.") server_version_number = (str(response_server_version.json()["major"]) + "." + str(response_server_version.json()["minor"]) + "." + str(response_server_version.json()["patch"]) @@ -253,6 +259,7 @@ class ImmichMetricsCollector: return combined_url +# test class SignalHandler(): def __init__(self): self.shutdownCount = 0 @@ -284,6 +291,53 @@ def get_config_value(key, default=""): return os.environ.get(key, default) +def check_server_up(immichHost, immichPort): + + # + counter = 0 + + + while True: + counter = counter + 1 + try: + + requests.request( + "GET", + "http://" + immichHost + ":" + immichPort + "/api/server-info/ping", + headers={'Accept': 'application/json'} + ) + except requests.exceptions.RequestException as e: + logger.error(f"CONNECTION ERROR. Cannot reach immich at " + immichHost + ":" + immichPort + "." + f"Is immich up and running?") + if 0 <= counter <= 60: + time.sleep(1) + elif 11 <= counter <= 300: + time.sleep(15) + elif counter > 300: + time.sleep(60) + continue + break + logger.info(f"Found immich up and running at " + immichHost + ":" + immichPort + ".") + time.sleep(5) + +def check_immich_api_key(immichHost, immichPort, immichApiKey): + + while True: + try: + + response_server_version = requests.request( + "GET", + "https://"+immichHost+":"+immichPort+"/api/server-info/version", + headers={'Accept': 'application/json', + "x-api-key": immichApiKey} + ) + except requests.exceptions.RequestException as e: + logger.error(f"CONNECTION ERROR. Is the api key correct? You may have to delete the entry and copypaste it anew.") + time.sleep(3) + continue + break + logger.error(f"Immich API key matches") + def main(): # Init logger so it can be used logHandler = logging.StreamHandler() @@ -321,15 +375,21 @@ def main(): # Register our custom collector logger.info("Exporter is starting up") + + check_server_up(config["immich_host"], config["immich_port"]) + check_immich_api_key(config["immich_host"], config["immich_port"], config["token"]) REGISTRY.register(ImmichMetricsCollector(config)) # Start server start_http_server(config["exporter_port"]) + logger.info( f"Exporter listening on port {config['exporter_port']}" ) + while not signal_handler.is_shutting_down(): time.sleep(1) logger.info("Exporter has shutdown") + diff --git a/setup.py b/setup.py index c909ea2..5a1e948 100644 --- a/setup.py +++ b/setup.py @@ -6,7 +6,7 @@ with open("README.md", "r") as fh: setup( name='prometheus-immich-exporter', packages=['immich_exporter'], - version='1.0.1', + version='1.0.4', long_description=long_description, long_description_content_type="text/markdown", description='Prometheus exporter for immich', diff --git a/unraid/prometheus-immich-exporter.xml b/unraid/prometheus-immich-exporter.xml index 41bc295..f04cba9 100644 --- a/unraid/prometheus-immich-exporter.xml +++ b/unraid/prometheus-immich-exporter.xml @@ -18,11 +18,11 @@ 1678876858 - If I made your day a little bit brighter, consider donating. + If you like my work, consider supporting me. https://www.paypal.com/donate/?hosted_button_id=DPDKED3T3BFV8 8028 - + \ No newline at end of file