-
Notifications
You must be signed in to change notification settings - Fork 2
/
db-topology.py
executable file
·58 lines (49 loc) · 1.73 KB
/
db-topology.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
#!/usr/bin/env python3
import mysql.connector
from mysql.connector import errorcode
import sys
import networkx as nx
import json
from networkx.readwrite import json_graph
user = 'yourname'
password = 'yourpassword'
g = nx.DiGraph()
def find_top(host, port):
g.add_node(host + ':' + port)
try:
cnx = mysql.connector.connect(
host=host, port=port, user=user, password=password)
cursor = cnx.cursor()
query = (
"show slave status;select host from INFORMATION_SCHEMA.PROCESSLIST where command = 'Binlog Dump';show slave hosts;")
l = []
for result in cursor.execute(query, multi=True):
l.append(result.fetchall())
# print(l[0])
if l[1] != []:
for k in range(0, len(l[1])):
slave_host = str(l[1][k][0].split(':')[0])
slave_port = str(l[2][k][2])
g.add_node(slave_host + ':' + slave_port)
g.add_edge(slave_host + ':' + slave_port, host + ':' + port)
find_top(slave_host, slave_port)
if l[0] != []:
master_host = str(l[0][0][1])
master_port = str(l[0][0][3])
if master_host+':'+master_port not in g:
find_top(master_host, master_port)
except mysql.connector.Error as err:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print(
"Something is wrong with your user name or password for host", host, 'in port', port)
else:
print(err)
else:
cnx.close()
def main():
for i in sys.argv[1:]:
(host, port) = i.split(':')
find_top(host, port)
d = json_graph.node_link_data(g)
json.dump(d, open('./force.json', 'w'))
main()