[ADD]Uptime check
This commit is contained in:
parent
1c41f871a0
commit
9d7e05afe2
|
@ -0,0 +1,7 @@
|
||||||
|
version: '2.4'
|
||||||
|
|
||||||
|
services:
|
||||||
|
|
||||||
|
check:
|
||||||
|
build: ./check
|
||||||
|
image: registry.yaltik.net/uptimecheck:yaltik
|
|
@ -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
|
|
@ -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))
|
|
@ -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
|
|
@ -0,0 +1,4 @@
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
printenv >> /etc/environment
|
||||||
|
crontab ./crontab && touch /var/log/cron.log && crond && tail -f /var/log/cron.log
|
Loading…
Reference in New Issue