Compare commits

..

No commits in common. "main" and "v0.1" have entirely different histories.
main ... v0.1

2 changed files with 13 additions and 37 deletions

View file

@ -1,7 +1,5 @@
Script intented to check a domain expiration with a query to the corresponding
RDAP server. The RDAP server for the TLD is extracted from the IANA JSON RDAP.
If the expiration field isnt found on the TLD server, the script falls back to
the registrar server.
RDAP server.
This script is inspired from
https://raw.githubusercontent.com/buanzo/check_expiration_rdap/main/src/nagios_check_domain_expiration_rdap/nagios_check_domain_expiration_rdap.py
@ -10,9 +8,9 @@ and `/usr/lib/python3.11/site-packages/nagiosplugin/examples/`
The script assumes that the TLD has only one label while looking for the RDAP
server from the IANA JSON. If its not the case it will fail.
Have fun.
I dont understand half of what I wrote
For all the options, run `./check_domain_expiration_rdap.py -h`
Have fun.
Here are the tested cases:
```shell

View file

@ -7,7 +7,6 @@ import argparse
import datetime
import logging
import os
import pathlib
import requests
import nagiosplugin
@ -19,20 +18,10 @@ __version__ = '0.1'
_log = logging.getLogger('nagiosplugin')
# cache session for json and csv storage
uid = os.getuid()
home = pathlib.Path.home()
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 = ''
session = requests_cache.CachedSession(
'/tmp/iana_rdap_cache',
cache_control=True
)
def find_rdap_server(domain):
"""Find the TLD rdap server."""
@ -52,7 +41,7 @@ def find_rdap_server(domain):
# no rdap on tld
except IndexError:
raise nagiosplugin.CheckError(
f'The TLD {tld} does not have an RDAP server, try forcing the registrar server with --server. It can be found on https://www.iana.org/assignments/registrar-ids/registrar-ids.xhtml'
f'The TLD {tld} does not have an RDAP server'
)
_log.debug(f'The used RDAP server is {url}')
@ -64,10 +53,6 @@ def parse_ldap(domain, rdap_server):
req_rdap = requests.get(f'{rdap_server}domain/{domain}')
match req_rdap.status_code:
case 400:
raise nagiosplugin.CheckError(
f'Got {req_rdap.status_code}, the RDAP server {rdap_server} interprets this domain query as a bad request'
)
case 403:
raise nagiosplugin.CheckError(
f'Got {req_rdap.status_code}, the RDAP server {rdap_server} refused to reply'
@ -112,8 +97,7 @@ def parse_ldap(domain, rdap_server):
raw_expiration.append(line[3])
elif len(raw_expiration) == 1:
fecha = raw_expiration[0].split('T')[0].strip().split()
fecha = fecha[0]
fecha = raw_expiration[0].split('T')[0]
today = datetime.datetime.now()
delta = datetime.datetime.strptime(fecha, '%Y-%m-%d') - today
raw_expiration[0] = delta.days
@ -140,11 +124,10 @@ def expiration(domain, server):
elif isinstance(raw_expiration[0], str):
import csv
# fetch csv
iana_registrars_req = session.get(
iana_registrars_csv = session.get(
'https://www.iana.org/assignments/registrar-ids/registrar-ids-1.csv',
timeout=120
)
iana_registrars_csv = iana_registrars_req.content.decode('utf-8')
).content.decode('utf-8')
# parse csv
registrar_rdap_found = False
for registrar_row in csv.reader(
@ -165,7 +148,7 @@ def expiration(domain, server):
)
if not(registrar_rdap_found):
raise nagiosplugin.CheckError(
f'The registrar {raw_expiration[0]} is not found from {iana_registrars_req.url}'
f'The registrar {raw_expiration[0]} is not fond from {iana_registrars_csv.url}'
)
else:
@ -259,13 +242,8 @@ def main():
)
domain = pyunycode.convert(args.domain)
# be sure that the provided server url ends with / for future concat
if (isinstance(args.server, str) and args.server[-1] != '/'):
server = args.server + '/'
else:
server = args.server
check = nagiosplugin.Check(
Expiration(domain, server),
Expiration(domain, args.server),
nagiosplugin.ScalarContext(
'daystoexpiration',
warning=wrange,