forked from sysown/proxysql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxysql_ping_thread.py
87 lines (75 loc) · 2.88 KB
/
proxysql_ping_thread.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
from email.mime.text import MIMEText
import smtplib
from threading import Thread
import time
import MySQLdb
class ProxySQL_Ping_Thread(Thread):
"""ProxySQL_Ping_Thread's purpose is to do a continuous health check of the
ProxySQL daemon when tests are running against it. When it has crashed
or it's simply not responding anymore, it will send an e-mail to draw the
attention of the developer so that he or she will examine the situation.
This is because the test suite is designed to be long running and we want
to find out as quickly as possible when the tests ran into trouble without
continuously keeping an eye on the tests.
"""
def __init__(self, config, **kwargs):
self.username = config.get('ProxySQL', 'username')
self.password = config.get('ProxySQL', 'password')
self.hostname = config.get('ProxySQL', 'hostname')
self.port = int(config.get('ProxySQL', 'port'))
self.db = config.get('Ping', 'db')
self.ping_command = config.get('Ping', 'ping_command')
self.interval = int(config.get('Ping', 'ping_interval'))
self.max_failed_connections = int(config.get('Ping', 'failed_connections_before_alert'))
self.config=config
self.running = True
self.failed_connections = 0
super(ProxySQL_Ping_Thread, self).__init__(**kwargs)
def run(self):
while self.running:
time.sleep(self.interval)
if not self.running:
return
try:
connection = MySQLdb.connect(self.hostname,
self.username,
self.password,
port=self.port,
db=self.db,
connect_timeout=30)
cursor = connection.cursor()
cursor.execute(self.ping_command)
rows = cursor.fetchall()
cursor.close()
connection.close()
print("ProxySQL server @ %s:%d responded to query %s with %r" %
(self.hostname, self.port, self.ping_command, rows))
self.failed_connections = 0
except:
self.failed_connections = self.failed_connections + 1
if self.failed_connections >= self.max_failed_connections:
self.send_error_email()
self.running = False
return
def stop(self):
self.running = False
def send_error_email(self):
msg = MIMEText("ProxySQL daemon stopped responding during tests.\n"
"Please check if it has crashed and you have been left with a gdb console on!")
# me == the sender's email address
# you == the recipient's email address
msg['Subject'] = 'Daemon has stopped responding'
msg['From'] = self.config.get('Email', 'from')
msg['To'] = self.config.get('Email', 'to')
# Send the message via our own SMTP server, but don't include the
# envelope header.
s = smtplib.SMTP(self.config.get('Email', 'smtp_server'),
int(self.config.get('Email', 'smtp_port')))
s.ehlo()
s.starttls()
s.login(self.config.get('Email', 'username'),
self.config.get('Email', 'password'))
s.sendmail(self.config.get('Email', 'from'),
[self.config.get('Email', 'to')],
msg.as_string())
s.quit()