Check for several cache paths and fallback to requests if needed

Signed-off-by: Alarig Le Lay <alarig@swordarmor.fr>
This commit is contained in:
Alarig Le Lay 2024-07-28 01:08:33 +02:00
parent 559cb97cd4
commit f77ab3348c
Signed by: alarig
GPG key ID: 7AFE62C6DF8BCDEC

View file

@ -7,6 +7,7 @@ import argparse
import datetime import datetime
import logging import logging
import os import os
import pathlib
import requests import requests
import nagiosplugin import nagiosplugin
@ -18,10 +19,20 @@ __version__ = '0.1'
_log = logging.getLogger('nagiosplugin') _log = logging.getLogger('nagiosplugin')
# cache session for json and csv storage # cache session for json and csv storage
session = requests_cache.CachedSession( uid = os.getuid()
'/tmp/iana_rdap_cache', home = pathlib.Path.home()
cache_control=True for possible_dir in [f'/run/{uid}', home, '/tmp']:
) iana_rdap_cache = f'{possible_dir}/iana_rdap_cache'
try:
cache = open(f'{iana_rdap_cache}.sqlite', 'a')
cache.close()
session = requests_cache.CachedSession(iana_rdap_cache, cache_control=True)
_log.debug(f'Caching to {iana_rdap_cache}.sqlite')
break
except IOError:
_log.debug(f'{iana_rdap_cache}.sqlite is not writtable')
session = requests
iana_rdap_cache = ''
def find_rdap_server(domain): def find_rdap_server(domain):
"""Find the TLD rdap server.""" """Find the TLD rdap server."""