-
Notifications
You must be signed in to change notification settings - Fork 1
/
galeranode.py
152 lines (127 loc) · 4.84 KB
/
galeranode.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/python
import subprocess
import sys
import argparse
from tabulate import tabulate
import logging
from threading import Thread
import mysql.connector
from mysql.connector import Error
logging.basicConfig(level=logging.INFO,
format='%(asctime)s (%(threadName)-2s) %(message)s',)
logger = logging.getLogger(__name__)
class GaleraNode:
host = ""
port = ""
username = ""
password = ""
state = []
s_queue = []
s_queue_avg = []
r_queue = []
r_queue_avg = []
sent = []
received = []
paused = []
committed = []
def __init__(self, host, port, username, password):
self.host = host
self.port = port
self.username = username
self.password = password
def set_state(self):
"""Set galera state from node."""
self.state = self.query_node(
"SHOW GLOBAL STATUS LIKE 'wsrep_local_state_comment';")
def set_squeue(self):
"""Set sent queue from node."""
self.s_queue = self.query_node(
"SHOW GLOBAL STATUS LIKE 'wsrep_local_send_queue';")
def set_squeue_avg(self):
"""Set sent queue average from node."""
self.s_queue_avg = self.query_node(
"SHOW GLOBAL STATUS LIKE 'wsrep_local_send_queue_avg';")
def set_rqueue(self):
"""Set receive queue from node."""
self.r_queue = self.query_node(
"SHOW GLOBAL STATUS LIKE 'wsrep_local_recv_queue';")
def set_rqueue_avg(self):
"""Set receive queue average from node."""
self.r_queue_avg = self.query_node(
"SHOW GLOBAL STATUS LIKE 'wsrep_local_recv_queue_avg';")
def set_fsent(self):
"""Set flow control sent from node."""
self.sent = self.query_node(
"SHOW GLOBAL STATUS LIKE 'wsrep_flow_control_sent';")
def set_freceived(self):
"""Set flow control receive from node."""
self.received = self.query_node(
"SHOW GLOBAL STATUS LIKE 'wsrep_flow_control_recv';")
def set_fpaused(self):
"""Set flow control paused from node."""
self.paused = self.query_node(
"SHOW GLOBAL STATUS LIKE 'wsrep_flow_control_paused';")
def set_committed(self):
"""Set last commit from node."""
self.committed = self.query_node(
"SHOW GLOBAL STATUS LIKE 'wsrep_last_committed';")
def get_status(self):
"""Show the status of the server."""
threads = [
Thread(target=self.set_state),
Thread(target=self.set_squeue),
Thread(target=self.set_squeue_avg),
Thread(target=self.set_rqueue),
Thread(target=self.set_rqueue_avg),
Thread(target=self.set_fsent),
Thread(target=self.set_freceived),
Thread(target=self.set_fpaused),
Thread(target=self.set_committed)
]
logger.info('Getting status of node: '+self.get_hostname())
for thread in threads:
thread.start()
for thread in threads:
thread.join()
if self.state[1] != "Synced":
self.state = self.color_string(self.state,"\u001b[31m")
else:
self.state = self.color_string(self.state,"\u001b[32m")
if float(self.paused[1]) >= 0.02:
self.paused = self.color_string(self.paused,"\u001b[31m")
else:
self.paused = self.color_string(self.paused,"\u001b[32m")
result = [self.state[1], self.s_queue[1] + "/" + self.s_queue_avg[1],
self.r_queue[1] + "/" + self.r_queue_avg[1], self.sent[1],
self.received[1], self.paused[1], self.committed[1]]
return result
def get_version(self):
"""Get the version of galera."""
result = self.query_node(
"SHOW GLOBAL STATUS LIKE 'wsrep_provider_version';")
return result[1]
def get_hostname(self):
"""Get the hostname or IP if the server."""
return self.host
def color_string(self, param, color):
"""Color output based on result."""
param = list(param)
param[1] = color+param[1]+"\u001b[0m"
param = tuple(param)
return param
def query_node(self, query):
"""Run a query on the node and return result."""
conn = None
try:
conn = mysql.connector.connect(
host=self.host, port=self.port, user=self.username, password=self.password)
cur = conn.cursor()
cur.execute(query)
result = cur.fetchone()
cur.close()
return result
except mysql.connector.Error as error:
print(error)
finally:
if conn is not None:
conn.close()