forked from Overv/openstreetmap-tile-server
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhealthcheck.py
executable file
·75 lines (61 loc) · 1.88 KB
/
healthcheck.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
#!/usr/bin/python3
# -----------------------------------------------------------------
# Check health of tileserver by retrieving few tiles and checking
# that their size indicates expected data (i.e. complex image rather
# than just grey landarea or coastline)
# -----------------------------------------------------------------
import urllib.request
import urllib.error
import socket
import random
site = 'localhost'
protocol = 'http://'
tile_dir = 'tiles'
zoom_level = '16'
min_x = 36000
max_x = 38000
min_y = 15000
max_y = 18000
image_type = '.png'
min_tile_size = 100
n_tiles_to_check = 1
def random_tiles(nbr, site, image_type):
tiles = []
for i in range(nbr):
tiles.append((random_tile(site, image_type), min_tile_size))
return tiles
def random_tile(site, image_type):
url = protocol + site + '/' + tile_dir + '/' + zoom_level
x = random.randrange(min_x, max_x)
y = random.randrange(min_y, max_y)
return url + '/' + str(x) + '/' + str(y) + image_type
health_check = [
(protocol + site + '/tiles/15/18376/9243.png', 7000),
(protocol + site + '/tiles/14/9187/4625.png', 20000),
(protocol + site + '/tiles_winter/14/9272/4618.png', 30000)
]
tile_test_set = random_tiles(n_tiles_to_check, site, image_type)
tile_test_set = tile_test_set + (health_check)
health_status = True
exit
for tile_url in tile_test_set:
try:
res = urllib.request.urlopen(tile_url[0], None, 30)
len = int(res.getheader('Content-Length'))
if res.getheader('Content-Type') != 'image/png' or len < tile_url[1]:
health_status = False
except urllib.error.HTTPError as e:
health_status = False
except urllib.error.URLError as e:
health_status = False
except socket.error as e:
health_status = False
if health_status:
print('Content-Type: text/plain;charset=utf-8')
print()
print('OK')
else:
print('Status: 500 Internal Server Error')
print('Content-Type: text/plain;charset=utf-8')
print()
print('ERROR')