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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
|
"""
High level status display commands
"""
from collections import defaultdict
from prettytable import PrettyTable
from typing import Any, Dict, List, Optional, Tuple, Union
import errno
import fnmatch
import mgr_util
import json
from mgr_module import CLIReadCommand, MgrModule, HandleCommandResult
class Module(MgrModule):
def get_latest(self, daemon_type: str, daemon_name: str, stat: str) -> int:
data = self.get_counter(daemon_type, daemon_name, stat)[stat]
if data:
return data[-1][1]
else:
return 0
def get_rate(self, daemon_type: str, daemon_name: str, stat: str) -> int:
data = self.get_counter(daemon_type, daemon_name, stat)[stat]
if data and len(data) > 1 and (int(data[-1][0] - data[-2][0]) != 0):
return (data[-1][1] - data[-2][1]) // int(data[-1][0] - data[-2][0])
else:
return 0
@CLIReadCommand("fs status")
def handle_fs_status(self,
fs: Optional[str] = None,
format: str = 'plain') -> Tuple[int, str, str]:
"""
Show the status of a CephFS filesystem
"""
output = ""
json_output: Dict[str, List[Dict[str, Union[int, str, List[str]]]]] = \
dict(mdsmap=[],
pools=[],
clients=[],
mds_version=[])
output_format = format
fs_filter = fs
mds_versions = defaultdict(list)
fsmap = self.get("fs_map")
for filesystem in fsmap['filesystems']:
if fs_filter and filesystem['mdsmap']['fs_name'] != fs_filter:
continue
rank_table = PrettyTable(
("RANK", "STATE", "MDS", "ACTIVITY", "DNS", "INOS", "DIRS", "CAPS"),
border=False,
)
rank_table.left_padding_width = 0
rank_table.right_padding_width = 2
mdsmap = filesystem['mdsmap']
client_count = 0
for rank in mdsmap["in"]:
up = "mds_{0}".format(rank) in mdsmap["up"]
if up:
gid = mdsmap['up']["mds_{0}".format(rank)]
info = mdsmap['info']['gid_{0}'.format(gid)]
dns = self.get_latest("mds", info['name'], "mds_mem.dn")
inos = self.get_latest("mds", info['name'], "mds_mem.ino")
dirs = self.get_latest("mds", info['name'], "mds_mem.dir")
caps = self.get_latest("mds", info['name'], "mds_mem.cap")
if rank == 0:
client_count = self.get_latest("mds", info['name'],
"mds_sessions.session_count")
elif client_count == 0:
# In case rank 0 was down, look at another rank's
# sessionmap to get an indication of clients.
client_count = self.get_latest("mds", info['name'],
"mds_sessions.session_count")
laggy = "laggy_since" in info
state = info['state'].split(":")[1]
if laggy:
state += "(laggy)"
if state == "active" and not laggy:
c_state = mgr_util.colorize(state, mgr_util.GREEN)
else:
c_state = mgr_util.colorize(state, mgr_util.YELLOW)
# Populate based on context of state, e.g. client
# ops for an active daemon, replay progress, reconnect
# progress
activity = ""
if state == "active":
rate = self.get_rate("mds", info['name'],
"mds_server.handle_client_request")
if output_format not in ('json', 'json-pretty'):
activity = "Reqs: " + mgr_util.format_dimless(rate, 5) + "/s"
metadata = self.get_metadata('mds', info['name'],
default=defaultdict(lambda: 'unknown'))
assert metadata
mds_versions[metadata['ceph_version']].append(info['name'])
if output_format in ('json', 'json-pretty'):
json_output['mdsmap'].append({
'rank': rank,
'name': info['name'],
'state': state,
'rate': rate if state == "active" else "0",
'dns': dns,
'inos': inos,
'dirs': dirs,
'caps': caps
})
else:
rank_table.add_row([
mgr_util.bold(rank.__str__()), c_state, info['name'],
activity,
mgr_util.format_dimless(dns, 5),
mgr_util.format_dimless(inos, 5),
mgr_util.format_dimless(dirs, 5),
mgr_util.format_dimless(caps, 5)
])
else:
if output_format in ('json', 'json-pretty'):
json_output['mdsmap'].append({
'rank': rank,
'state': "failed"
})
else:
rank_table.add_row([
rank, "failed", "", "", "", "", "", ""
])
# Find the standby replays
for gid_str, daemon_info in mdsmap['info'].items():
if daemon_info['state'] != "up:standby-replay":
continue
inos = self.get_latest("mds", daemon_info['name'], "mds_mem.ino")
dns = self.get_latest("mds", daemon_info['name'], "mds_mem.dn")
dirs = self.get_latest("mds", daemon_info['name'], "mds_mem.dir")
caps = self.get_latest("mds", daemon_info['name'], "mds_mem.cap")
events = self.get_rate("mds", daemon_info['name'], "mds_log.replayed")
if output_format not in ('json', 'json-pretty'):
activity = "Evts: " + mgr_util.format_dimless(events, 5) + "/s"
metadata = self.get_metadata('mds', daemon_info['name'],
default=defaultdict(lambda: 'unknown'))
assert metadata
mds_versions[metadata['ceph_version']].append(daemon_info['name'])
if output_format in ('json', 'json-pretty'):
json_output['mdsmap'].append({
'rank': rank,
'name': daemon_info['name'],
'state': 'standby-replay',
'events': events,
'dns': 5,
'inos': 5,
'dirs': 5,
'caps': 5
})
else:
rank_table.add_row([
"{0}-s".format(daemon_info['rank']), "standby-replay",
daemon_info['name'], activity,
mgr_util.format_dimless(dns, 5),
mgr_util.format_dimless(inos, 5),
mgr_util.format_dimless(dirs, 5),
mgr_util.format_dimless(caps, 5)
])
df = self.get("df")
pool_stats = dict([(p['id'], p['stats']) for p in df['pools']])
osdmap = self.get("osd_map")
pools = dict([(p['pool'], p) for p in osdmap['pools']])
metadata_pool_id = mdsmap['metadata_pool']
data_pool_ids = mdsmap['data_pools']
pools_table = PrettyTable(["POOL", "TYPE", "USED", "AVAIL"],
border=False)
pools_table.left_padding_width = 0
pools_table.right_padding_width = 2
for pool_id in [metadata_pool_id] + data_pool_ids:
pool_type = "metadata" if pool_id == metadata_pool_id else "data"
stats = pool_stats[pool_id]
if output_format in ('json', 'json-pretty'):
json_output['pools'].append({
'id': pool_id,
'name': pools[pool_id]['pool_name'],
'type': pool_type,
'used': stats['bytes_used'],
'avail': stats['max_avail']
})
else:
pools_table.add_row([
pools[pool_id]['pool_name'], pool_type,
mgr_util.format_bytes(stats['bytes_used'], 5),
mgr_util.format_bytes(stats['max_avail'], 5)
])
if output_format in ('json', 'json-pretty'):
json_output['clients'].append({
'fs': mdsmap['fs_name'],
'clients': client_count,
})
else:
output += "{0} - {1} clients\n".format(
mdsmap['fs_name'], client_count)
output += "=" * len(mdsmap['fs_name']) + "\n"
output += rank_table.get_string()
output += "\n" + pools_table.get_string() + "\n"
if not output and not json_output and fs_filter is not None:
return errno.EINVAL, "", "Invalid filesystem: " + fs_filter
standby_table = PrettyTable(["STANDBY MDS"], border=False)
standby_table.left_padding_width = 0
standby_table.right_padding_width = 2
for standby in fsmap['standbys']:
metadata = self.get_metadata('mds', standby['name'],
default=defaultdict(lambda: 'unknown'))
assert metadata
mds_versions[metadata['ceph_version']].append(standby['name'])
if output_format in ('json', 'json-pretty'):
json_output['mdsmap'].append({
'name': standby['name'],
'state': "standby"
})
else:
standby_table.add_row([standby['name']])
if output_format not in ('json', 'json-pretty'):
output += "\n" + standby_table.get_string() + "\n"
if len(mds_versions) == 1:
if output_format in ('json', 'json-pretty'):
json_output['mds_version'] = [dict(version=k, daemon=v)
for k, v in mds_versions.items()]
else:
output += "MDS version: {0}".format([*mds_versions][0])
else:
version_table = PrettyTable(["VERSION", "DAEMONS"],
border=False)
version_table.left_padding_width = 0
version_table.right_padding_width = 2
for version, daemons in mds_versions.items():
if output_format in ('json', 'json-pretty'):
json_output['mds_version'].append({
'version': version,
'daemons': daemons
})
else:
version_table.add_row([
version,
", ".join(daemons)
])
if output_format not in ('json', 'json-pretty'):
output += version_table.get_string() + "\n"
if output_format == "json":
return HandleCommandResult(stdout=json.dumps(json_output, sort_keys=True))
elif output_format == "json-pretty":
return HandleCommandResult(stdout=json.dumps(json_output, sort_keys=True, indent=4,
separators=(',', ': ')))
else:
return HandleCommandResult(stdout=output)
@CLIReadCommand("osd status")
def handle_osd_status(self, bucket: Optional[str] = None, format: str = 'plain') -> Tuple[int, str, str]:
"""
Show the status of OSDs within a bucket, or all
"""
json_output: Dict[str, List[Any]] = \
dict(OSDs=[])
output_format = format
osd_table = PrettyTable(['ID', 'HOST', 'USED', 'AVAIL', 'WR OPS',
'WR DATA', 'RD OPS', 'RD DATA', 'STATE'],
border=False)
osd_table.align['ID'] = 'r'
osd_table.align['HOST'] = 'l'
osd_table.align['USED'] = 'r'
osd_table.align['AVAIL'] = 'r'
osd_table.align['WR OPS'] = 'r'
osd_table.align['WR DATA'] = 'r'
osd_table.align['RD OPS'] = 'r'
osd_table.align['RD DATA'] = 'r'
osd_table.align['STATE'] = 'l'
osd_table.left_padding_width = 0
osd_table.right_padding_width = 2
osdmap = self.get("osd_map")
filter_osds = set()
bucket_filter = None
if bucket is not None:
self.log.debug(f"Filtering to bucket '{bucket}'")
bucket_filter = bucket
crush = self.get("osd_map_crush")
found = False
for bucket_ in crush['buckets']:
if fnmatch.fnmatch(bucket_['name'], bucket_filter):
found = True
filter_osds.update([i['id'] for i in bucket_['items']])
if not found:
msg = "Bucket '{0}' not found".format(bucket_filter)
return errno.ENOENT, msg, ""
# Build dict of OSD ID to stats
osd_stats = dict([(o['osd'], o) for o in self.get("osd_stats")['osd_stats']])
for osd in osdmap['osds']:
osd_id = osd['osd']
if bucket_filter and osd_id not in filter_osds:
continue
hostname = ""
kb_used = 0
kb_avail = 0
if osd_id in osd_stats:
metadata = self.get_metadata('osd', str(osd_id), default=defaultdict(str))
stats = osd_stats[osd_id]
assert metadata
hostname = metadata['hostname']
kb_used = stats['kb_used'] * 1024
kb_avail = stats['kb_avail'] * 1024
wr_ops_rate = (self.get_rate("osd", osd_id.__str__(), "osd.op_w") +
self.get_rate("osd", osd_id.__str__(), "osd.op_rw"))
wr_byte_rate = self.get_rate("osd", osd_id.__str__(), "osd.op_in_bytes")
rd_ops_rate = self.get_rate("osd", osd_id.__str__(), "osd.op_r")
rd_byte_rate = self.get_rate("osd", osd_id.__str__(), "osd.op_out_bytes")
osd_table.add_row([osd_id, hostname,
mgr_util.format_bytes(kb_used, 5),
mgr_util.format_bytes(kb_avail, 5),
mgr_util.format_dimless(wr_ops_rate, 5),
mgr_util.format_bytes(wr_byte_rate, 5),
mgr_util.format_dimless(rd_ops_rate, 5),
mgr_util.format_bytes(rd_byte_rate, 5),
','.join(osd['state']),
])
if output_format in ('json', 'json-pretty'):
json_output['OSDs'].append({
'id': osd_id,
'host name': hostname,
'kb used' : kb_used,
'kb available':kb_avail,
'write ops rate': wr_ops_rate,
'write byte rate': wr_byte_rate,
'read ops rate': rd_ops_rate,
'read byte rate': rd_byte_rate,
'state': osd['state']
})
if output_format == "json":
return 0, json.dumps(json_output, sort_keys=True) , ""
elif output_format == "json-pretty":
return 0, json.dumps(json_output, sort_keys=True,indent=4,separators=(',', ': ')) , ""
else:
return 0, osd_table.get_string(), ""
|