forked from pinterest/mysql_utils
-
Notifications
You must be signed in to change notification settings - Fork 2
/
find_unused_db_servers.py
executable file
·90 lines (73 loc) · 2.6 KB
/
find_unused_db_servers.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
#!/usr/bin/env python
import argparse
import re
import datetime
import boto.utils
from lib import environment_specific
from lib import host_utils
import retirement_queue
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-a',
'--add_retirement_queue',
help="Add the servers to the retirement queue",
action='store_true')
args = parser.parse_args()
hosts_not_in_zk = find_unused_db_servers()
for host in sorted(hosts_not_in_zk):
if args.add_retirement_queue:
retirement_queue.add_to_queue(hostname=host, dry_run=False)
else:
print host
def get_db_host_prefix(hostname):
""" This function finds the host prefix for a db host
Argument:
hostname - a hostname
Returns:
a prefix of the hostname
"""
prefix_match = re.match('(.+db)', hostname)
if prefix_match is None:
prefix_match = re.match('([a-z]+)', hostname)
if prefix_match is None:
prefix = None
else:
prefix = prefix_match.group(0)
return prefix
def find_unused_db_servers():
""" Compare zk and AWS to determine which servers are likely not in use
Returns:
A set of hosts that appear to not be in use
"""
# First find out what servers we know about from zk, and make a
# of hostname prefixes that we think we own.
zk = host_utils.MysqlZookeeper()
config = zk.get_all_mysql_config()
zk_servers = set()
zk_prefixes = set()
mysql_aws_hosts = set()
for db in config:
for rtype in host_utils.REPLICA_TYPES:
if rtype in config[db]:
host = config[db][rtype]['host']
zk_servers.add(host)
prefix = get_db_host_prefix(host)
zk_prefixes.add(prefix)
cmdb_servers = environment_specific.get_all_server_metadata()
for host in cmdb_servers:
match = False
for prefix in zk_prefixes:
if host.startswith(prefix):
match = True
if not match:
continue
# We need to give servers a chance to build and then add themselves
# to zk, so we will ignore server for a week.
creation = boto.utils.parse_ts(cmdb_servers[host]['launch_time'])
if creation < datetime.datetime.now()-datetime.timedelta(weeks=1):
mysql_aws_hosts.add(host)
hosts_not_in_zk = mysql_aws_hosts.difference(zk_servers)
hosts_not_protected = hosts_not_in_zk.difference(retirement_queue.get_protected_hosts('set'))
return hosts_not_protected
if __name__ == "__main__":
main()