blob: c5a92882e73451b63aeb455ffea50671916f543e [file] [log] [blame]
Ved-vampira915a012015-03-18 14:38:52 +03001""" Collect data about ceph nodes"""
2import json
3
4import sh
Ved-vampir29931712015-03-18 16:31:02 +03005from node import Node
Ved-vampira915a012015-03-18 14:38:52 +03006
7
8def discover_ceph_node():
9 """ Return list of ceph's nodes ips """
10
11 ips = {}
12 osd_list = get_osds_list()
13 osd_ips = get_osds_ips(osd_list)
14 mon_ips = get_mons_or_mds_ips("mon")
15 mds_ips = get_mons_or_mds_ips("mds")
16 for ip in osd_ips:
17 url = "ssh://%s" % ip
18 if url in ips:
Yulia Portnova0e64ea22015-03-20 17:27:22 +020019 ips[url].append("ceph-osd")
Ved-vampira915a012015-03-18 14:38:52 +030020 else:
Yulia Portnova0e64ea22015-03-20 17:27:22 +020021 ips[url] = ["ceph-osd"]
Ved-vampira915a012015-03-18 14:38:52 +030022 for ip in mon_ips:
23 url = "ssh://%s" % ip
24 if url in ips:
Yulia Portnova0e64ea22015-03-20 17:27:22 +020025 ips[url].append("ceph-mon")
Ved-vampira915a012015-03-18 14:38:52 +030026 else:
Yulia Portnova0e64ea22015-03-20 17:27:22 +020027 ips[url] = ["ceph-mon"]
Ved-vampira915a012015-03-18 14:38:52 +030028 for ip in mds_ips:
29 url = "ssh://%s" % ip
30 if url in ips:
Yulia Portnova0e64ea22015-03-20 17:27:22 +020031 ips[url].append("ceph-mds")
Ved-vampira915a012015-03-18 14:38:52 +030032 else:
Yulia Portnova0e64ea22015-03-20 17:27:22 +020033 ips[url] = ["ceph-mds"]
Ved-vampira915a012015-03-18 14:38:52 +030034
35 res = []
36 for url, roles in ips:
Ved-vampir29931712015-03-18 16:31:02 +030037 res.append(Node(ip=url, roles=list(roles)))
Ved-vampira915a012015-03-18 14:38:52 +030038
39 return res
40
41
42# internal services
43
44
45class CephException(Exception):
46 """ Exceptions from ceph call"""
47 pass
48
49class ParameterException(Exception):
50 """ Bad parameter in function"""
51 pass
52
53
54def get_osds_list():
55 """ Get list of osds id"""
56 try:
57 res = sh.ceph.osd.ls()
58 osd_list = [osd_id
59 for osd_id in res.split("\n") if osd_id != '']
60 return osd_list
61 except sh.CommandNotFound:
62 raise CephException("Ceph command not found")
63 except:
64 raise CephException("Ceph command 'osd ls' execution error")
65
66
67def get_mons_or_mds_ips(who):
68 """ Return mon ip list
69 :param who - "mon" or "mds" """
70 try:
71 ips = set()
72 if who == "mon":
73 res = sh.ceph.mon.dump()
74 elif who == "mds":
75 res = sh.ceph.mds.dump()
76 else:
77 raise ParameterException("'%s' in get_mons_or_mds_ips instead of mon/mds" % who)
78
79 line_res = res.split("\n")
80 for line in line_res:
81 fields = line.split()
82 if len(fields) > 2 and who in fields[2]:
83 ips.add(fields[1].split(":")[0])
84
85 return ips
86
87 except sh.CommandNotFound:
88 raise CephException("Ceph command not found")
89 except ParameterException as e:
90 raise e
91 except:
92 mes = "Ceph command '%s dump' execution error" % who
93 raise CephException(mes)
94
95
96def get_osds_ips(osd_list):
97 """ Get osd's ips
98 :param osd_list - list of osd names from osd ls command"""
99 try:
100 ips = set()
101 for osd_id in osd_list:
102 res = sh.ceph.osd.find(osd_id)
103 ip = json.loads(str(res))["ip"]
104 ips.add(ip.split(":")[0])
105 return ips
106
107 except sh.CommandNotFound:
108 raise CephException("Ceph command not found")
109 except:
110 raise CephException("Ceph command 'osd find' execution error")
111