forked from cholcombe973/mysql_utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql_replica_mappings.py
executable file
·99 lines (83 loc) · 3.45 KB
/
mysql_replica_mappings.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
#!/usr/bin/env python
import argparse
from lib import environment_specific
from lib import host_utils
OUTPUT_FORMAT = ('{replica_set:<RS}'
'{replica_type:<12}'
'{hostport}')
OUTPUT_FORMAT_EXTENDED = ('{replica_set:<RS}'
'{replica_type:<12}'
'{hostport:<HP}'
'{hw}\t'
'{az}\t'
'{sg:<SGL}'
'{id}')
def main():
parser = argparse.ArgumentParser(description='MySQL production viewer')
parser.add_argument('-e',
'--extended',
help='Include information about hardware, az, etc',
default=False,
action='store_true')
args = parser.parse_args()
zk = host_utils.MysqlZookeeper()
config = zk.get_all_mysql_config()
if args.extended:
servers = environment_specific.get_all_server_metadata()
output = list()
max_rs_length = 10
max_sg_length = 0
# iterate through and find the longest replica set name and
# security group, then use that to set the spacing
for replica_set in config:
for rtype in host_utils.REPLICA_TYPES:
if rtype in config[replica_set]:
inst = config[replica_set][rtype]
if len(replica_set) > max_rs_length:
max_rs_length = len(replica_set)
if args.extended and inst['host'] in servers:
sg = ','.join(servers[inst['host']].get('security_groups',
'N/A'))
if len(sg) > max_sg_length:
max_sg_length = len(sg)
max_rs_length += 4
max_sg_length += 4
hostport_length = max_rs_length + 6
# dynamically generate padding
format_str = OUTPUT_FORMAT.replace(
'RS', str(max_rs_length)).replace(
'HP', str(hostport_length)).replace(
'SGL', str(max_sg_length))
format_str_extended = OUTPUT_FORMAT_EXTENDED.replace(
'RS', str(max_rs_length)).replace(
'HP', str(hostport_length)).replace(
'SGL', str(max_sg_length))
for replica_set in config:
for rtype in host_utils.REPLICA_TYPES:
if rtype in config[replica_set]:
inst = config[replica_set][rtype]
if args.extended and inst['host'] in servers:
az = servers[inst['host']]['zone']
id = servers[inst['host']]['instance_id']
hw = servers[inst['host']]['instance_type']
try:
sg = ','.join(servers[inst['host']]['security_groups'])
except KeyError:
sg = '??VPC??'
output.append(format_str_extended.format(
replica_set=replica_set,
replica_type=rtype,
hostport=':'.join([inst['host'], str(inst['port'])]),
az=az,
hw=hw,
sg=sg,
id=id))
else:
output.append(format_str.format(
replica_set=replica_set,
replica_type=rtype,
hostport=':'.join([inst['host'], str(inst['port'])])))
output.sort()
print '\n'.join(output)
if __name__ == "__main__":
main()