diff --git a/main.py b/main.py index 7f5cf5e..610f0d0 100644 --- a/main.py +++ b/main.py @@ -19,10 +19,12 @@ except FileNotFoundError: logging.error("hosts.json not found. Please copy hosts.json.example to hosts.json and configure it.") exit(1) -def alert_tanner(message): +async def alert_tanner(message): try: params = {'ssl-monitor': message} - requests.get('https://tbot.tannercollin.com/message', params=params, timeout=4) + async with aiohttp.ClientSession() as session: + async with session.get('https://tbot.tannercollin.com/message', params=params, timeout=4) as response: + response.raise_for_status() except BaseException as e: logging.error('Problem alerting Tanner: ' + str(e)) @@ -43,7 +45,7 @@ async def check_host_cert(host, port, seen_serials): # this case should be rare if handshake succeeded msg = f"Could not get certificate for {host}:{port}" logging.warning(msg) - alert_tanner(msg) + await alert_tanner(msg) return serial_number = cert.get('serialNumber') @@ -61,18 +63,18 @@ async def check_host_cert(host, port, seen_serials): if time_left < timedelta(days=7): msg = f"Certificate for {host}:{port} expires in less than a week: {expiry_date}" logging.warning(msg) - alert_tanner(msg) + await alert_tanner(msg) else: logging.info(f"Certificate for {host}:{port} is valid until {expiry_date} ({time_left.days} days left)") except ssl.SSLCertVerificationError as e: msg = f"Certificate verification error for {host}:{port}: {e.reason}" logging.error(msg) - alert_tanner(msg) + await alert_tanner(msg) except ssl.SSLError as e: msg = f"SSL error for {host}:{port}: {e}" logging.error(msg) - alert_tanner(msg) + await alert_tanner(msg) except (asyncio.TimeoutError, OSError) as e: # Per instructions: log and move on for connection errors logging.error(f"Connection error for {host}:{port}: {e}") @@ -80,7 +82,7 @@ async def check_host_cert(host, port, seen_serials): # Catchall for other things msg = f"An unexpected error occurred for {host}:{port}: {e}" logging.error(msg) - alert_tanner(msg) + await alert_tanner(msg) async def main():