From 5a07ffdf0d498bb152efcac002e118d5f1bd64cf Mon Sep 17 00:00:00 2001 From: friendly Friend Date: Sat, 22 Jul 2023 21:50:59 +0200 Subject: [PATCH 1/5] 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 From 7601be0f1bbd03614753dbbbca48b6946a3ffde5 Mon Sep 17 00:00:00 2001 From: friendly Friend Date: Sat, 22 Jul 2023 21:50:59 +0200 Subject: [PATCH 2/5] 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 From e07e9fb45982342a3bc77630b9e84fb1eebcf59a Mon Sep 17 00:00:00 2001 From: friendly Friend Date: Wed, 26 Jul 2023 08:22:58 +0200 Subject: [PATCH 3/5] -fixed ssl error. -more transparent error codes --- immich_exporter/exporter.py | 16 ++++++++++------ setup.py | 2 +- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/immich_exporter/exporter.py b/immich_exporter/exporter.py index 9426ffc..2af4970 100644 --- a/immich_exporter/exporter.py +++ b/immich_exporter/exporter.py @@ -234,7 +234,6 @@ class ImmichMetricsCollector: 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"]) @@ -318,25 +317,30 @@ def check_server_up(immichHost, immichPort): continue break logger.info(f"Found immich up and running at " + immichHost + ":" + immichPort + ".") - time.sleep(5) + logger.info(f"Attempting to connect") + time.sleep(1) + logger.info(".") + def check_immich_api_key(immichHost, immichPort, immichApiKey): while True: try: - response_server_version = requests.request( + requests.request( "GET", - "https://"+immichHost+":"+immichPort+"/api/server-info/version", + "http://"+immichHost+":"+immichPort+"/api/server-info/", 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.") + logger.error(f"CONNECTION ERROR. Possible API key error") + logger.error({e}) time.sleep(3) continue + logger.info(f"Connected to immich successfully") break - logger.error(f"Immich API key matches") + def main(): # Init logger so it can be used diff --git a/setup.py b/setup.py index 5a1e948..c980f1c 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.4', + version='1.0.5', long_description=long_description, long_description_content_type="text/markdown", description='Prometheus exporter for immich', From edc87014199370b304c37769bcbbba18bbcfa78e Mon Sep 17 00:00:00 2001 From: friendly Friend Date: Wed, 26 Jul 2023 08:24:24 +0200 Subject: [PATCH 4/5] -fixed ssl error. -more transparent error codes --- .idea/.gitignore | 8 ++++++++ .idea/deployment.xml | 14 ++++++++++++++ .idea/inspectionProfiles/profiles_settings.xml | 6 ++++++ .idea/misc.xml | 4 ++++ .idea/modules.xml | 8 ++++++++ .idea/prometheus-immich-exporter.iml | 8 ++++++++ .idea/vcs.xml | 6 ++++++ 7 files changed, 54 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/deployment.xml create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/prometheus-immich-exporter.iml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/deployment.xml b/.idea/deployment.xml new file mode 100644 index 0000000..1f3f6d1 --- /dev/null +++ b/.idea/deployment.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..ab530bf --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..0e66d6a --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/prometheus-immich-exporter.iml b/.idea/prometheus-immich-exporter.iml new file mode 100644 index 0000000..d870a4a --- /dev/null +++ b/.idea/prometheus-immich-exporter.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file From 3c6d7ef8ae9d3b75d8e6430f050673ad33c53345 Mon Sep 17 00:00:00 2001 From: friendly Friend Date: Fri, 28 Jul 2023 17:23:59 +0200 Subject: [PATCH 5/5] -display exporter version number on startup --- immich_exporter/exporter.py | 2 +- setup.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/immich_exporter/exporter.py b/immich_exporter/exporter.py index 2af4970..a36d390 100644 --- a/immich_exporter/exporter.py +++ b/immich_exporter/exporter.py @@ -319,7 +319,7 @@ def check_server_up(immichHost, immichPort): logger.info(f"Found immich up and running at " + immichHost + ":" + immichPort + ".") logger.info(f"Attempting to connect") time.sleep(1) - logger.info(".") + logger.info("Exporter v1.0.6") def check_immich_api_key(immichHost, immichPort, immichApiKey): diff --git a/setup.py b/setup.py index c980f1c..c86a61f 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.5', + version='1.0.6', long_description=long_description, long_description_content_type="text/markdown", description='Prometheus exporter for immich',