blob: 70a6edfd3e3c3187765e721fdd9de96f5239fcd8 [file] [log] [blame]
Ved-vampira915a012015-03-18 14:38:52 +03001""" Collect data about ceph nodes"""
2import json
koder aka kdanilov3a6633e2015-03-26 18:20:00 +02003import logging
koder aka kdanilov2c473092015-03-29 17:12:13 +03004import subprocess
koder aka kdanilov3a6633e2015-03-26 18:20:00 +02005
Ved-vampira915a012015-03-18 14:38:52 +03006
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +03007from .node import Node
8from wally.ssh_utils import connect
Ved-vampira915a012015-03-18 14:38:52 +03009
10
koder aka kdanilov3a6633e2015-03-26 18:20:00 +020011logger = logging.getLogger("io-perf-tool")
12
13
koder aka kdanilov2c473092015-03-29 17:12:13 +030014def local_execute(cmd):
15 return subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=True)
16
17
18def ssh_execute(ssh):
19 def closure(cmd):
20 _, chan, _ = ssh.exec_command(cmd)
21 return chan.read()
22 return closure
23
24
25def discover_ceph_nodes(ip):
Ved-vampira915a012015-03-18 14:38:52 +030026 """ Return list of ceph's nodes ips """
Ved-vampira915a012015-03-18 14:38:52 +030027 ips = {}
koder aka kdanilove06762a2015-03-22 23:32:09 +020028
koder aka kdanilov2c473092015-03-29 17:12:13 +030029 if ip != 'local':
30 executor = ssh_execute(connect(ip))
31 else:
32 executor = local_execute
33
34 osd_ips = get_osds_ips(executor, get_osds_list(executor))
35 mon_ips = get_mons_or_mds_ips(executor, "mon")
36 mds_ips = get_mons_or_mds_ips(executor, "mds")
koder aka kdanilove06762a2015-03-22 23:32:09 +020037
Ved-vampira915a012015-03-18 14:38:52 +030038 for ip in osd_ips:
39 url = "ssh://%s" % ip
koder aka kdanilove06762a2015-03-22 23:32:09 +020040 ips.setdefault(url, []).append("ceph-osd")
41
Ved-vampira915a012015-03-18 14:38:52 +030042 for ip in mon_ips:
43 url = "ssh://%s" % ip
koder aka kdanilove06762a2015-03-22 23:32:09 +020044 ips.setdefault(url, []).append("ceph-mon")
45
Ved-vampira915a012015-03-18 14:38:52 +030046 for ip in mds_ips:
47 url = "ssh://%s" % ip
koder aka kdanilove06762a2015-03-22 23:32:09 +020048 ips.setdefault(url, []).append("ceph-mds")
Ved-vampira915a012015-03-18 14:38:52 +030049
koder aka kdanilov2c473092015-03-29 17:12:13 +030050 return [Node(url, list(roles)) for url, roles in ips.items()]
Ved-vampira915a012015-03-18 14:38:52 +030051
52
koder aka kdanilov2c473092015-03-29 17:12:13 +030053def get_osds_list(executor):
Ved-vampira915a012015-03-18 14:38:52 +030054 """ Get list of osds id"""
koder aka kdanilov2c473092015-03-29 17:12:13 +030055 return filter(None, executor("ceph osd ls").split("\n"))
Ved-vampira915a012015-03-18 14:38:52 +030056
57
koder aka kdanilov2c473092015-03-29 17:12:13 +030058def get_mons_or_mds_ips(executor, who):
Ved-vampira915a012015-03-18 14:38:52 +030059 """ Return mon ip list
60 :param who - "mon" or "mds" """
koder aka kdanilov2c473092015-03-29 17:12:13 +030061 if who not in ("mon", "mds"):
koder aka kdanilove06762a2015-03-22 23:32:09 +020062 raise ValueError(("'%s' in get_mons_or_mds_ips instead" +
63 "of mon/mds") % who)
Ved-vampira915a012015-03-18 14:38:52 +030064
koder aka kdanilov2c473092015-03-29 17:12:13 +030065 line_res = executor("ceph {0} dump".format(who)).split("\n")
Ved-vampira915a012015-03-18 14:38:52 +030066
koder aka kdanilov2c473092015-03-29 17:12:13 +030067 ips = set()
koder aka kdanilove06762a2015-03-22 23:32:09 +020068 for line in line_res:
69 fields = line.split()
Ved-vampira915a012015-03-18 14:38:52 +030070
koder aka kdanilove06762a2015-03-22 23:32:09 +020071 # what does fields[1], fields[2] means?
72 # make this code looks like:
73 # SOME_MENINGFULL_VAR1, SOME_MENINGFULL_VAR2 = line.split()[1:3]
74
75 if len(fields) > 2 and who in fields[2]:
76 ips.add(fields[1].split(":")[0])
77
78 return ips
Ved-vampira915a012015-03-18 14:38:52 +030079
80
koder aka kdanilov2c473092015-03-29 17:12:13 +030081def get_osds_ips(executor, osd_list):
koder aka kdanilove06762a2015-03-22 23:32:09 +020082 """ Get osd's ips
Ved-vampira915a012015-03-18 14:38:52 +030083 :param osd_list - list of osd names from osd ls command"""
koder aka kdanilove06762a2015-03-22 23:32:09 +020084 ips = set()
85 for osd_id in osd_list:
koder aka kdanilov2c473092015-03-29 17:12:13 +030086 out = executor("ceph osd find {0}".format(osd_id))
87 ip = json.loads(out)["ip"]
88 ips.add(str(ip.split(":")[0]))
koder aka kdanilove06762a2015-03-22 23:32:09 +020089 return ips