[ADD]Uptime check

This commit is contained in:
Fabien BOURGEOIS 2021-05-13 16:32:05 +02:00
parent 1c41f871a0
commit 9d7e05afe2
5 changed files with 72 additions and 0 deletions

7
uptime/base.yml Normal file
View File

@ -0,0 +1,7 @@
version: '2.4'
services:
check:
build: ./check
image: registry.yaltik.net/uptimecheck:yaltik

10
uptime/check/Dockerfile Normal file
View File

@ -0,0 +1,10 @@
FROM oraclelinux:8
MAINTAINER Yaltik - Fabien Bourgeois <fabien@yaltik.com>
RUN dnf -y install cronie python3 python3-requests
COPY check.py ./
COPY crontab ./
COPY launch.sh ./
CMD bash launch.sh

47
uptime/check/check.py Normal file
View File

@ -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))

4
uptime/check/crontab Normal file
View File

@ -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

4
uptime/check/launch.sh Normal file
View File

@ -0,0 +1,4 @@
#!/bin/bash
printenv >> /etc/environment
crontab ./crontab && touch /var/log/cron.log && crond && tail -f /var/log/cron.log