From b53a107471783a0a4b9e9d042447765f7520d307 Mon Sep 17 00:00:00 2001 From: Fabien BOURGEOIS Date: Sat, 23 Oct 2021 14:36:49 +0200 Subject: [PATCH] [REF]Uptime now uses Python config file Because of env limit with cronie on OL8 --- uptime/check/Dockerfile | 1 + uptime/check/check.py | 24 +++++++----------------- uptime/check/config.py | 10 ++++++++++ uptime/check/launch.sh | 1 - 4 files changed, 18 insertions(+), 18 deletions(-) create mode 100644 uptime/check/config.py diff --git a/uptime/check/Dockerfile b/uptime/check/Dockerfile index 5a9adb8..a9599b3 100644 --- a/uptime/check/Dockerfile +++ b/uptime/check/Dockerfile @@ -4,6 +4,7 @@ MAINTAINER Yaltik - Fabien Bourgeois RUN dnf -y install cronie python3 python3-requests COPY check.py ./ +COPY config.py ./ COPY crontab ./ COPY launch.sh ./ diff --git a/uptime/check/check.py b/uptime/check/check.py index 8181808..f778a4b 100644 --- a/uptime/check/check.py +++ b/uptime/check/check.py @@ -2,23 +2,12 @@ """ Uptime check, at the moment HTTPS only domains -Usage of own healthchecks.io instance and ENV +Usage of own healthchecks.io instance and config file """ -from os import environ from requests import get, post +from config import HC_ROOT, CONFIG -hc_root = environ.get('HC_ROOT', '') -# HOSTS are in form hostname|uuid:hostname2|uuid... -hosts = environ.get('HOSTS', '').split(':') -# Domains are in form of example.org,example.net:hostname.org:another.net -domains = environ.get('DOMAINS', '').split(':') - -config = {} -for host in zip(hosts, domains): - host_data = host[0].split('|') - config[host_data[0]] = {'url': '%s/ping/%s' % (hc_root, host_data[1]), - 'domains': host[1].split(',')} def check_domain(domain): try: @@ -35,13 +24,14 @@ def ping_with_info(url, fail=False, data=None): except Exception as e: print("Ping failed: %s" % e) -for host, settings in config.items(): - print('Checking %s : %s' % (', '.join(settings['domains']), host)) +for check_item in CONFIG: + print('Checking %s : %s' % (check_item['host'], ', '.join(check_item['domains']))) fail = False data = [] - for domain in settings['domains']: + for domain in check_item['domains']: status = check_domain(domain) if not fail and status != 200: fail = True data.append('%s : %s' % (domain, status)) - ping_with_info(settings['url'], fail=fail, data='\n'.join(data)) + url = '%s/ping/%s' % (HC_ROOT, check_item['uuid']) + ping_with_info(url, fail=fail, data='\n'.join(data)) diff --git a/uptime/check/config.py b/uptime/check/config.py new file mode 100644 index 0000000..2026fc6 --- /dev/null +++ b/uptime/check/config.py @@ -0,0 +1,10 @@ +# -*- coding: utf-8 -*- + +""" +Configuration file +""" + +HC_ROOT = 'https://hc.yaltik.net' +CONFIG = [{'host': 'server_name', + 'uuid': 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', + 'domains': ['www.example.com', 'beta.example.com', 'another.sample.net']}] diff --git a/uptime/check/launch.sh b/uptime/check/launch.sh index b7e5b74..1f32743 100644 --- a/uptime/check/launch.sh +++ b/uptime/check/launch.sh @@ -1,4 +1,3 @@ #!/bin/bash -printenv >> /etc/environment crontab ./crontab && touch /var/log/cron.log && crond && tail -f /var/log/cron.log