-
Notifications
You must be signed in to change notification settings - Fork 0
/
heartbeat.py
87 lines (71 loc) · 2.83 KB
/
heartbeat.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
#!/usr/bin/python
import os, sys
import urllib2
from time import gmtime, strftime, time
try:
from argparse import OptionParser
except ImportError:
from optparse import OptionParser
def log_entry(logfile, response=None):
if response and response.getcode() == 200:
if response.time > 1:
stopwatch = '%ss' % round(response.time, 3)
else:
stopwatch = '%sms' % int(response.time * 1000)
msg = 'OK! HTTP 200 after %s' % stopwatch
else:
msg = 'FAIL!'
print msg
if logfile:
lf = open(logfile, 'a+')
lf.write('%s: ' % strftime("%a, %d %b %Y %X GMT", gmtime()))
lf.write('%s\n' % msg)
lf.close()
def heartbeat():
# Change chdir to current file loation, then add it to pythonpath
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
os.chdir(os.path.dirname(os.path.abspath(__file__)))
# Parse options
parser = OptionParser()
parser.add_option("--timeout", dest="timeout", default=60, type="int",
help="Number of seconds after which heartbeat timeouts.")
parser.add_option("--path", dest="pypath",
help="Add extra entry to python-path.")
parser.add_option("--log", dest="logfile",
help="Log responses to file.", metavar="FILE")
(options, argv) = parser.parse_args(sys.argv)
# Set extra pythonpath?
if options.pypath:
sys.path.insert(0, options.pypath)
# Validate timeout
if options.timeout < 5 or options.timeout > 300:
raise ValueError("Timeout cannot be lower than 5 seconds and greater than 5 minutes (300 seconds).")
try:
# Read Misago settings
settings = __import__(argv[1]).settings
BOARD_ADDRESS = settings.BOARD_ADDRESS
HEARTBEAT_PATH = settings.HEARTBEAT_PATH
# Validate
if not BOARD_ADDRESS:
raise ValueError('"BOARD_ADDRESS" setting is not set.')
if not HEARTBEAT_PATH:
raise ValueError('"HEARTBEAT_PATH" setting is not set.')
request_url = '%s/%s' % (BOARD_ADDRESS, HEARTBEAT_PATH)
# Send and handle request
try:
stopwatch = time()
response = urllib2.urlopen(request_url, timeout=options.timeout)
body = response.read()
response.close()
response.time = time() - stopwatch
log_entry(options.logfile, response)
except urllib2.URLError:
log_entry(options.logfile)
except IndexError:
raise ValueError("You have to specify name of Misago's settings module used by your forum.")
except ImportError:
raise ValueError('"%s" could not be imported.' % argv[1])
except AttributeError as e:
raise ValueError('"%s" is not correct settings module.' % argv[1])
if __name__ == '__main__':
heartbeat()