-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgsql.py
More file actions
96 lines (81 loc) · 3.92 KB
/
gsql.py
File metadata and controls
96 lines (81 loc) · 3.92 KB
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
#!/usr/bin/python
import subprocess
import sys
import argparse
import logging
import base64
import paramiko
from threading import Thread
from tabulate import tabulate
from galeranode import GaleraNode
logging.basicConfig(level=logging.INFO,
format='%(asctime)s (%(threadName)-2s) %(message)s',)
logger = logging.getLogger(__name__)
def run():
parser = argparse.ArgumentParser()
parser.add_argument("-H", dest='hname', help="The hostname of DB server.")
parser.add_argument("-P", dest='port', help="The port of DB server.")
parser.add_argument("-u", dest='user', help="DB username.")
parser.add_argument("-p", dest='passwd', help="DB password.")
parser.add_argument("-ussh", dest='sshu', help="User to connect to node via ssh.")
parser.add_argument("-pssh", dest='sshp', help="User to connect to node via ssh.")
parser.add_argument("-q", dest='query', help="Query to run on all nodes.", default="false")
args = parser.parse_args()
logger.info('Getting cluster nodes.')
init_node = GaleraNode(args.hname, args.port, args.user, args.passwd)
nodes = init_node.query_node("show status like 'wsrep_incoming_addresses';")
nodes = nodes[1].split(",")
nodes = [i.split(':')[0] for i in nodes]
gnodes = []
statuses = []
for i in range(len(nodes)):
gnodes.append(GaleraNode(nodes[i], args.port, args.user, args.passwd))
commands = args.query.split(" ")
if args.query.lower() == "false":
print("No query was entered.")
else:
if commands[0].lower() == "set":
logger.info('Checking if query is compatible.')
for node in gnodes:
logger.info('Running query on node %s', node.host)
node.query_node(args.query)
elif commands[0].lower() == "show" and commands[1].lower() == "galera" and commands[2].lower() == "status":
for i in range(len(gnodes)):
statuses.append([])
node_status = gnodes[i].get_status()
statuses[i].append(gnodes[i].get_hostname())
for ii in range(len(node_status)):
statuses[i].append(node_status[ii])
statuses.sort()
print_out(statuses, ["Host", "Status", "Send Q [Now/Avg]",
"Receive Q [Now/Avg]", "Flow Ctrl Sent",
"Flow Ctrl Recieved", "Flow Ctrl Paused", "Last Committed"])
elif commands[0].lower() == "show" and commands[1].lower() == "galera" and commands[2].lower() == "version":
for i in range(len(gnodes)):
statuses.append([])
statuses[i].append(gnodes[i].get_hostname())
statuses[i].append(gnodes[i].get_version())
statuses.sort()
print_out(statuses, ["Host", "Galera Version"])
elif commands[0].lower() == "restart" and commands[1].lower() == "galera" and commands[2].lower() == "nodes":
for i in range(len(gnodes)):
ssh(gnodes[i].get_hostname(), args.sshu, args.sshp, "service mysql restart")
elif commands[0].lower() == "update" and commands[1].lower() == "galera" and commands[2].lower() == "nodes":
for i in range(len(gnodes)):
logger.info("Updating: " + gnodes[i].get_hostname())
ssh(gnodes[i].get_hostname(), args.sshu, args.sshp, "yum update -y")
else:
logger.error(
'Invalid commmand.')
def print_out(out, headers):
print(tabulate(out, headers, tablefmt="grid"))
def ssh(node, user, password, command):
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(node, username=user, password=password)
stdin, stdout, stderr = client.exec_command(command)
for line in stdout:
logger.info(line.strip('\n'))
client.close()
if __name__ == '__main__':
run()