Merge branch 'master' into master

This commit is contained in:
friendlyFriend4000 2023-08-22 22:01:18 +02:00 committed by GitHub
commit f6d95209c0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 133 additions and 15 deletions

8
.idea/.gitignore vendored Normal file
View file

@ -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

14
.idea/deployment.xml Normal file
View file

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="PublishConfigData" remoteFilesAllowedToDisappearOnAutoupload="false">
<serverData>
<paths name="//TOWER">
<serverdata>
<mappings>
<mapping local="$PROJECT_DIR$" web="/" />
</mappings>
</serverdata>
</paths>
</serverData>
</component>
</project>

View file

@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>

4
.idea/misc.xml Normal file
View file

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.10 (2)" project-jdk-type="Python SDK" />
</project>

8
.idea/modules.xml Normal file
View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/prometheus-immich-exporter.iml" filepath="$PROJECT_DIR$/.idea/prometheus-immich-exporter.iml" />
</modules>
</component>
</project>

View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="jdk" jdkName="Python 3.10 (2)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

6
.idea/vcs.xml Normal file
View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

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,11 +212,14 @@ 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:
@ -225,7 +230,7 @@ class ImmichMetricsCollector:
"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
@ -253,6 +258,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 +290,58 @@ 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 + ".")
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(): def main():
# Init logger so it can be used # Init logger so it can be used
logHandler = logging.StreamHandler() logHandler = logging.StreamHandler()
@ -321,15 +379,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.6',
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,7 +18,7 @@
<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="Immich_exporter_port" Target="8000" Default="8000" Mode="tcp" Description="" Type="Port" Display="always" Required="false" Mask="false">8000</Config> <Config Name="Immich_exporter_port" Target="8000" Default="8000" Mode="tcp" Description="" Type="Port" Display="always" Required="false" Mask="false">8000</Config>