From 9d7e05afe2e73dcc67030dd6080cc92e3400ebdc Mon Sep 17 00:00:00 2001 From: Fabien BOURGEOIS Date: Thu, 13 May 2021 16:32:05 +0200 Subject: [PATCH] [ADD]Uptime check --- uptime/base.yml | 7 ++++++ uptime/check/Dockerfile | 10 +++++++++ uptime/check/check.py | 47 +++++++++++++++++++++++++++++++++++++++++ uptime/check/crontab | 4 ++++ uptime/check/launch.sh | 4 ++++ 5 files changed, 72 insertions(+) create mode 100644 uptime/base.yml create mode 100644 uptime/check/Dockerfile create mode 100644 uptime/check/check.py create mode 100644 uptime/check/crontab create mode 100644 uptime/check/launch.sh diff --git a/uptime/base.yml b/uptime/base.yml new file mode 100644 index 0000000..639559c --- /dev/null +++ b/uptime/base.yml @@ -0,0 +1,7 @@ +version: '2.4' + +services: + + check: + build: ./check + image: registry.yaltik.net/uptimecheck:yaltik diff --git a/uptime/check/Dockerfile b/uptime/check/Dockerfile new file mode 100644 index 0000000..5a9adb8 --- /dev/null +++ b/uptime/check/Dockerfile @@ -0,0 +1,10 @@ +FROM oraclelinux:8 +MAINTAINER Yaltik - Fabien Bourgeois + +RUN dnf -y install cronie python3 python3-requests + +COPY check.py ./ +COPY crontab ./ +COPY launch.sh ./ + +CMD bash launch.sh diff --git a/uptime/check/check.py b/uptime/check/check.py new file mode 100644 index 0000000..5f3394b --- /dev/null +++ b/uptime/check/check.py @@ -0,0 +1,47 @@ +# -*- coding: utf-8 -*- + +""" +Uptime check, at the moment HTTPS only domains +Usage of own healthchecks.io instance and ENV +""" + +from os import environ +from requests import get, post, RequestException + +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: + r = get('https://%s' % domain) + except Exception as e: + return e + return r.status_code + +def ping_with_info(url, fail=False, data=None): + try: + if fail: + url = '%s/fail' % url + post(url, timeout=10, data=data) + except RequestException as e: + print("Ping failed: %s" % e) + +for host, settings in config.items(): + print('Checking %s : %s' % (', '.join(settings['domains']), host)) + fail = False + data = [] + for domain in settings['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)) diff --git a/uptime/check/crontab b/uptime/check/crontab new file mode 100644 index 0000000..5a2c8cd --- /dev/null +++ b/uptime/check/crontab @@ -0,0 +1,4 @@ +# uptime check, default every 15 minutes +# m h dom mon dow user command +*/15 * * * * /usr/bin/python3 /check.py >> /var/log/cron.log 2>&1 +# empty line needed diff --git a/uptime/check/launch.sh b/uptime/check/launch.sh new file mode 100644 index 0000000..b7e5b74 --- /dev/null +++ b/uptime/check/launch.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +printenv >> /etc/environment +crontab ./crontab && touch /var/log/cron.log && crond && tail -f /var/log/cron.log