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
This commit is contained in:
friendly Friend 2023-07-22 21:50:59 +02:00
parent f5f3926e0f
commit 7601be0f1b
3 changed files with 76 additions and 16 deletions

View file

@ -39,6 +39,7 @@ class ImmichMetricsCollector:
yield prom_metric yield prom_metric
def get_immich_metrics(self): def get_immich_metrics(self):
metrics = [] metrics = []
metrics.extend(self.get_immich_server_version_number()) metrics.extend(self.get_immich_server_version_number())
metrics.extend(self.get_immich_server_info()) metrics.extend(self.get_immich_server_info())
@ -47,6 +48,7 @@ class ImmichMetricsCollector:
return metrics return metrics
def get_immich_users_stat_growth(self): def get_immich_users_stat_growth(self):
try: try:
@ -210,25 +212,29 @@ class ImmichMetricsCollector:
] ]
def get_immich_server_version_number(self): 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" server_version_endpoint = "/api/server-info/version"
response_server_version = "" response_server_version = ""
for i in range(0, 360): while True:
while True: try:
try:
response_server_version = requests.request( response_server_version = requests.request(
"GET", "GET",
self.combine_url(server_version_endpoint), self.combine_url(server_version_endpoint),
headers={'Accept': 'application/json', headers={'Accept': 'application/json',
"x-api-key": self.config["token"]} "x-api-key": self.config["token"]}
) )
except requests.exceptions.RequestException as e: except requests.exceptions.RequestException as e:
logger.error(f"Couldn't get server version: {e}") logger.error(f"Couldn't get server version")
continue continue
break break
logger.info(f"immich is up and running.")
server_version_number = (str(response_server_version.json()["major"]) + "." + server_version_number = (str(response_server_version.json()["major"]) + "." +
str(response_server_version.json()["minor"]) + "." + str(response_server_version.json()["minor"]) + "." +
str(response_server_version.json()["patch"]) str(response_server_version.json()["patch"])
@ -253,6 +259,7 @@ class ImmichMetricsCollector:
return combined_url return combined_url
# test
class SignalHandler(): class SignalHandler():
def __init__(self): def __init__(self):
self.shutdownCount = 0 self.shutdownCount = 0
@ -284,6 +291,53 @@ def get_config_value(key, default=""):
return os.environ.get(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(): def main():
# Init logger so it can be used # Init logger so it can be used
logHandler = logging.StreamHandler() logHandler = logging.StreamHandler()
@ -321,15 +375,21 @@ def main():
# Register our custom collector # Register our custom collector
logger.info("Exporter is starting up") 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)) REGISTRY.register(ImmichMetricsCollector(config))
# Start server # Start server
start_http_server(config["exporter_port"]) start_http_server(config["exporter_port"])
logger.info( logger.info(
f"Exporter listening on port {config['exporter_port']}" f"Exporter listening on port {config['exporter_port']}"
) )
while not signal_handler.is_shutting_down(): while not signal_handler.is_shutting_down():
time.sleep(1) time.sleep(1)
logger.info("Exporter has shutdown") logger.info("Exporter has shutdown")

View file

@ -6,7 +6,7 @@ with open("README.md", "r") as fh:
setup( setup(
name='prometheus-immich-exporter', name='prometheus-immich-exporter',
packages=['immich_exporter'], packages=['immich_exporter'],
version='1.0.1', version='1.0.4',
long_description=long_description, long_description=long_description,
long_description_content_type="text/markdown", long_description_content_type="text/markdown",
description='Prometheus exporter for immich', description='Prometheus exporter for immich',

View file

@ -18,11 +18,11 @@
<PostArgs/> <PostArgs/>
<CPUset/> <CPUset/>
<DateInstalled>1678876858</DateInstalled> <DateInstalled>1678876858</DateInstalled>
<DonateText>If I made your day a little bit brighter, consider donating.</DonateText> <DonateText>If you like my work, consider supporting me.</DonateText>
<DonateLink>https://www.paypal.com/donate/?hosted_button_id=DPDKED3T3BFV8</DonateLink> <DonateLink>https://www.paypal.com/donate/?hosted_button_id=DPDKED3T3BFV8</DonateLink>
<Requires/> <Requires/>
<Config Name="Host Port 1" Target="8000" Default="8028" Mode="tcp" Description="" Type="Port" Display="always" Required="false" Mask="false">8028</Config> <Config Name="Host Port 1" Target="8000" Default="8028" Mode="tcp" Description="" Type="Port" Display="always" Required="false" Mask="false">8028</Config>
<Config Name="immich api token" Target="IMMICH_API_TOKEN" Default="" Mode="" Description="You need to log into to your immich admin account&#13;&#10;and create an api token in your settings" Type="Variable" Display="always" Required="false" Mask="false"/> <Config Name="immich api token" Target="IMMICH_API_TOKEN" Default="" Mode="" Description="You need to log into to your immich admin account&#13;&#10;and create an api token in your settings" Type="Variable" Display="always" Required="false" Mask="false"/>
<Config Name="immich_host" Target="IMMICH_HOST" Default="" Mode="" Description="example: 192.168.178.2" Type="Variable" Display="always" Required="false" Mask="false"/> <Config Name="immich_host" Target="IMMICH_HOST" Default="" Mode="" Description="internal ip of you rimmich instance - example: 192.168.178.2" Type="Variable" Display="always" Required="false" Mask="false"/>
<Config Name="immich port" Target="IMMICH_PORT" Default="" Mode="" Description="" Type="Variable" Display="always" Required="false" Mask="false"/> <Config Name="immich port" Target="IMMICH_PORT" Default="" Mode="" Description="" Type="Variable" Display="always" Required="false" Mask="false"/>
</Container> </Container>