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 diff --git a/immich_exporter/exporter.py b/immich_exporter/exporter.py index f752ce8..a36d390 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,24 +212,27 @@ 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 server_version_number = (str(response_server_version.json()["major"]) + "." + str(response_server_version.json()["minor"]) + "." + @@ -253,6 +258,7 @@ class ImmichMetricsCollector: return combined_url +# test class SignalHandler(): def __init__(self): self.shutdownCount = 0 @@ -284,6 +290,58 @@ 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 + ".") + logger.info(f"Attempting to connect") + time.sleep(1) + logger.info("Exporter v1.0.6") + + +def check_immich_api_key(immichHost, immichPort, immichApiKey): + + while True: + try: + + requests.request( + "GET", + "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. Possible API key error") + logger.error({e}) + time.sleep(3) + continue + logger.info(f"Connected to immich successfully") + break + + def main(): # Init logger so it can be used logHandler = logging.StreamHandler() @@ -321,15 +379,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..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.1', + version='1.0.6', 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 6953cb7..706bf0e 100644 --- a/unraid/prometheus-immich-exporter.xml +++ b/unraid/prometheus-immich-exporter.xml @@ -18,7 +18,7 @@ 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 8000