This repository has been archived by the owner on Jul 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
docker_status.py
130 lines (109 loc) · 3.9 KB
/
docker_status.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
from flask import Flask
from multiprocessing import Process, Value
from time import sleep
import datetime
import graypy
import logging
import os
import re
import urllib2
import sys
import traceback
OK_STATUSES = [ 200, 301, 302 ]
HTTP_TIMEOUT = os.environ.get('HTTP_TIMEOUT', 30)
TEST_INTERVAL = os.environ.get('TEST_INTERVAL', 30)
DEBUG = os.environ.get('DEBUG', False) == "true"
LISTEN_HOST = os.environ.get('LISTEN_HOST', '0.0.0.0')
LISTEN_PORT = os.environ.get('LISTEN_PORT', 80)
DELAY_START = os.environ.get('DELAY_START', False) == "true"
GRAYLOG_HOST = os.environ.get('GRAYLOG_HOST', None)
GRAYLOG_PORT = int(os.environ.get('GRAYLOG_PORT', 12201))
GRAYLOG_LOCALNAME = os.environ.get('GRAYLOG_LOCALNAME', 'docker-status')
app = Flask(__name__)
checks = {}
class NoRedirectHTTPErrorProcessor(urllib2.HTTPErrorProcessor):
def http_response(self, request, response):
return response
https_response = http_response
url_opener = urllib2.build_opener(NoRedirectHTTPErrorProcessor)
if GRAYLOG_HOST is not None:
logger = logging.getLogger('status_logger')
if DEBUG:
logger.setLevel(logging.DEBUG)
else:
logger.setLevel(logging.INFO)
graylog_handler = graypy.GELFHandler(
GRAYLOG_HOST,
GRAYLOG_PORT,
localname=GRAYLOG_LOCALNAME
)
logger.addHandler(graylog_handler)
@app.route("/")
def status():
for (host, (status, timestamp, process)) in checks.items():
if (status.value not in OK_STATUSES or
timestamp.value < int(
(datetime.datetime.now() - datetime.timedelta(
seconds=TEST_INTERVAL*2
)).strftime("%s")
)
):
return "Fail", 500
return "OK", 200
def checker(host, status, timestamp):
get_path = os.environ.get("%s_GET_PATH" % host, "/")
use_host_ip = os.environ.get("%s_CONNECT_IP" % host, False) == 'true'
if use_host_ip:
check_host = os.environ.get("%s_PORT_80_TCP_ADDR" % host)
else:
check_host = host.lower()
log_adapter = logging.LoggerAdapter(
logging.getLogger('status_logger'),
{
'status_host': host,
'status_check_host': check_host
}
)
while True:
try:
result = url_opener.open("http://%s%s" % (check_host, get_path),
timeout=HTTP_TIMEOUT)
status.value = result.getcode()
except Exception as e:
status.value = getattr(e, 'code', -1)
log_str = "Failed check. Host: %s, path: %s" % (
check_host,
get_path
)
log_adapter.error(log_str, exc_info=1)
print log_str
traceback.print_exc()
timestamp.value = int(datetime.datetime.now().strftime('%s'))
log_str = "%s %s %s" % (datetime.datetime.now(), host, status.value)
if status.value in OK_STATUSES:
log_adapter.info(log_str)
else:
log_adapter.error(log_str)
print(log_str)
sys.stdout.flush()
sleep(TEST_INTERVAL)
if __name__ == "__main__":
hosts = [ var.split('_PORT_80_TCP')[0] for var in os.environ
if re.match('[A-Z0-9_]+_PORT_80_TCP$', var) ]
# De-dupe hosts. We take the one with the shortest name for each IP address
host_ips = {}
for host in hosts:
host_ips.setdefault(os.environ.get("%s_PORT_80_TCP_ADDR" % host),
[]).append(host)
hosts = []
for ip, ip_hosts in host_ips.items():
hosts.append(sorted(ip_hosts, key=len)[0])
for host in hosts:
status = Value('i', -1)
timestamp = Value('i', -1)
process = Process(target=checker, args=(host, status, timestamp))
process.start()
checks[host] = (status, timestamp, process)
if DELAY_START:
sleep(TEST_INTERVAL)
app.run(host=LISTEN_HOST, port=LISTEN_PORT, debug=DEBUG)