forked from ccpgames/eve-metrics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatchdog.py
executable file
·132 lines (118 loc) · 3.98 KB
/
watchdog.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
131
132
import sys, os, pyodbc, urllib, datetime, traceback, time, smtplib
import win32serviceutil
from pprint import pprint
RECIPIENT_EMAIL_ADDRESSES = ["[email protected]"]
SMTP_SERVER = "smtp.ccpgames.com"
SENDER_EMAIL_ADDRESS = "[email protected]"
PID_FILE = ".WATCHDOG"
def FmtTime(dt):
return dt.strftime("%Y-%m-%d %H:%M")
def SendEmail(title, body):
sender = SENDER_EMAIL_ADDRESS
receivers = RECIPIENT_EMAIL_ADDRESSES
message = """From: Eve Metrics <%(fromemail)s>
To: %(email)s
MIME-Version: 1.0
Content-type: text
Subject: %(title)s
%(body)s
""" % {"fromemail": sender, "body": body, "email": str(receivers[0]), "title": title}
try:
smtpObj = smtplib.SMTP(SMTP_SERVER)
smtpObj.set_debuglevel(True)
smtpObj.sendmail(sender, receivers, message)
print " Successfully sent email to %s" % receivers[0]
except Exception, e:
print "Error: unable to send email to %s: %s" % (receivers[0], e)
raise
def TestWebsite(url):
sys.stdout.write("TestWebsite %s... " % url)
t = time.time()
ret = ""
url = "http://%s/Test" % url
try:
data = urllib.urlopen(url).read()
if data != "OK":
ret = "Error talking to website at %s. Response was %s" % (url, data[:128])
else:
diff = time.time()-t
if diff > 60.0:
ret = "It took %.1f seconds to get a response from %s" % (diff, url)
except:
ret = "Unable to reach website at %s" % url
if ret:
print "\n%s" % ret
else:
sys.stdout.write("OK! ")
return ret
def RestartService(svc, retry=0):
print "Restarting service %s..." % svc
try:
win32serviceutil.RestartService(svc)
except:
print "Exception trying to restart service: %s" % traceback.format_exc()
if not retry:
print "Retrying..."
RestartService(svc, 1)
else:
raise
print "Done restarting service"
def main():
try:
f = open(PID_FILE, "rb")
oldPid = int(f.read())
print "Found old Pid, WTF???", oldPid
# restart both services
err = "I have restarted the web servers."
try:
RestartService("web2py_evemetrics")
except Exception as e:
err = "I encountered an exception trying to restart the services:\n\n%s" % traceback.format_exc()
body = "Error report\n\nPrevious watchdog run has not completed! This might mean that a website is locked up!\n\n%s" % err
SendEmail("Evelogs watchdog reports a problem!", body)
f.close()
time.sleep(2.0)
try:
os.remove(PID_FILE)
except:
pass
return
except IOError:
pass
f = open(PID_FILE, "w")
f.write(repr(os.getpid()))
f.close()
try:
results = []
for url in ("evemetrics", ):
try:
result = TestWebsite(url)
if result: results.append("TestWebsite - %s\n%s" % (url, result))
except Exception as e:
results.append("Exception running TestWebsite - %s:\n%s" % (url, traceback.format_exc()))
if results:
print "Results:"
pprint(results)
body = "Error report\n\n%s" % "\n\n".join(results)
SendEmail("Evelogs watchdog reports a problem!", body)
finally:
try:
os.remove(PID_FILE)
except:
pass
while 1:
startTime = time.time()
try:
print datetime.datetime.now()
main()
print
except:
print "Exception: ", traceback.format_exc()
finally:
diff = time.time()-startTime
if diff > 10:
print "TIME WARNING!!! Done in %.2f seconds" % (diff)
else:
print "Done in %.2f seconds" % (diff)
sys.exit(0)
time.sleep(60)