]>
git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/controllers/cephfs.py
1 # -*- coding: utf-8 -*-
4 from collections
import defaultdict
10 from ..exceptions
import DashboardException
11 from ..security
import Scope
12 from ..services
.ceph_service
import CephService
13 from ..services
.cephfs
import CephFS
as CephFS_
14 from ..services
.exception
import handle_cephfs_error
15 from ..tools
import ViewCache
16 from . import APIDoc
, APIRouter
, EndpointDoc
, RESTController
, UIRouter
, allow_empty_body
19 'max_bytes': (int, ''),
20 'max_files': (int, '')
23 logger
= logging
.getLogger("controllers.rgw")
26 @APIRouter('/cephfs', Scope
.CEPHFS
)
27 @APIDoc("Cephfs Management API", "Cephfs")
28 class CephFS(RESTController
):
29 def __init__(self
): # pragma: no cover
32 # Stateful instances of CephFSClients, hold cached results. Key to
34 self
.cephfs_clients
= {}
37 fsmap
= mgr
.get("fs_map")
38 return fsmap
['filesystems']
41 fs_id
= self
.fs_id_to_int(fs_id
)
42 return self
.fs_status(fs_id
)
44 @RESTController.Resource('GET')
45 def clients(self
, fs_id
):
46 fs_id
= self
.fs_id_to_int(fs_id
)
48 return self
._clients
(fs_id
)
50 @RESTController.Resource('DELETE', path
='/client/{client_id}')
51 def evict(self
, fs_id
, client_id
):
52 fs_id
= self
.fs_id_to_int(fs_id
)
53 client_id
= self
.client_id_to_int(client_id
)
55 return self
._evict
(fs_id
, client_id
)
57 @RESTController.Resource('GET')
58 def mds_counters(self
, fs_id
, counters
=None):
59 fs_id
= self
.fs_id_to_int(fs_id
)
60 return self
._mds
_counters
(fs_id
, counters
)
62 def _mds_counters(self
, fs_id
, counters
=None):
64 Result format: map of daemon name to map of counter to list of datapoints
65 rtype: dict[str, dict[str, list]]
69 # Opinionated list of interesting performance counters for the GUI
71 "mds_server.handle_client_request",
73 "mds_cache.num_strays",
75 "mds.exported_inodes",
77 "mds.imported_inodes",
85 mds_names
= self
._get
_mds
_names
(fs_id
)
87 for mds_name
in mds_names
:
89 for counter
in counters
:
90 data
= mgr
.get_counter("mds", mds_name
, counter
)
92 result
[mds_name
][counter
] = data
[counter
]
94 result
[mds_name
][counter
] = []
99 def fs_id_to_int(fs_id
):
103 raise DashboardException(code
='invalid_cephfs_id',
104 msg
="Invalid cephfs ID {}".format(fs_id
),
108 def client_id_to_int(client_id
):
110 return int(client_id
)
112 raise DashboardException(code
='invalid_cephfs_client_id',
113 msg
="Invalid cephfs client ID {}".format(client_id
),
116 def _get_mds_names(self
, filesystem_id
=None):
119 fsmap
= mgr
.get("fs_map")
120 for fs
in fsmap
['filesystems']:
121 if filesystem_id
is not None and fs
['id'] != filesystem_id
:
123 names
.extend([info
['name']
124 for _
, info
in fs
['mdsmap']['info'].items()])
126 if filesystem_id
is None:
127 names
.extend(info
['name'] for info
in fsmap
['standbys'])
131 def _append_mds_metadata(self
, mds_versions
, metadata_key
):
132 metadata
= mgr
.get_metadata('mds', metadata_key
)
135 mds_versions
[metadata
.get('ceph_version', 'unknown')].append(metadata_key
)
137 # pylint: disable=too-many-statements,too-many-branches
138 def fs_status(self
, fs_id
):
139 mds_versions
: dict = defaultdict(list)
141 fsmap
= mgr
.get("fs_map")
143 for fs
in fsmap
['filesystems']:
144 if fs
['id'] == fs_id
:
148 if filesystem
is None:
149 raise cherrypy
.HTTPError(404,
150 "CephFS id {0} not found".format(fs_id
))
154 mdsmap
= filesystem
['mdsmap']
158 for rank
in mdsmap
["in"]:
159 up
= "mds_{0}".format(rank
) in mdsmap
["up"]
161 gid
= mdsmap
['up']["mds_{0}".format(rank
)]
162 info
= mdsmap
['info']['gid_{0}'.format(gid
)]
163 dns
= mgr
.get_latest("mds", info
['name'], "mds_mem.dn")
164 inos
= mgr
.get_latest("mds", info
['name'], "mds_mem.ino")
165 dirs
= mgr
.get_latest("mds", info
['name'], "mds_mem.dir")
166 caps
= mgr
.get_latest("mds", info
['name'], "mds_mem.cap")
169 client_count
= mgr
.get_latest("mds", info
['name'],
170 "mds_sessions.session_count")
171 elif client_count
== 0:
172 # In case rank 0 was down, look at another rank's
173 # sessionmap to get an indication of clients.
174 client_count
= mgr
.get_latest("mds", info
['name'],
175 "mds_sessions.session_count")
177 laggy
= "laggy_since" in info
179 state
= info
['state'].split(":")[1]
183 # Populate based on context of state, e.g. client
184 # ops for an active daemon, replay progress, reconnect
186 if state
== "active":
187 activity
= CephService
.get_rate("mds",
189 "mds_server.handle_client_request")
191 activity
= 0.0 # pragma: no cover
193 self
._append
_mds
_metadata
(mds_versions
, info
['name'])
199 "activity": activity
,
221 # Find the standby replays
222 # pylint: disable=unused-variable
223 for gid_str
, daemon_info
in mdsmap
['info'].items():
224 if daemon_info
['state'] != "up:standby-replay":
227 inos
= mgr
.get_latest("mds", daemon_info
['name'], "mds_mem.ino")
228 dns
= mgr
.get_latest("mds", daemon_info
['name'], "mds_mem.dn")
229 dirs
= mgr
.get_latest("mds", daemon_info
['name'], "mds_mem.dir")
230 caps
= mgr
.get_latest("mds", daemon_info
['name'], "mds_mem.cap")
232 activity
= CephService
.get_rate(
233 "mds", daemon_info
['name'], "mds_log.replay")
237 "rank": "{0}-s".format(daemon_info
['rank']),
238 "state": "standby-replay",
239 "mds": daemon_info
['name'],
240 "activity": activity
,
249 pool_stats
= {p
['id']: p
['stats'] for p
in df
['pools']}
250 osdmap
= mgr
.get("osd_map")
251 pools
= {p
['pool']: p
for p
in osdmap
['pools']}
252 metadata_pool_id
= mdsmap
['metadata_pool']
253 data_pool_ids
= mdsmap
['data_pools']
256 for pool_id
in [metadata_pool_id
] + data_pool_ids
:
257 pool_type
= "metadata" if pool_id
== metadata_pool_id
else "data"
258 stats
= pool_stats
[pool_id
]
260 "pool": pools
[pool_id
]['pool_name'],
262 "used": stats
['stored'],
263 "avail": stats
['max_avail']
267 for standby
in fsmap
['standbys']:
268 self
._append
_mds
_metadata
(mds_versions
, standby
['name'])
269 standby_table
.append({
270 'name': standby
['name']
276 "name": mdsmap
['fs_name'],
277 "client_count": client_count
,
281 "standbys": standby_table
,
282 "versions": mds_versions
285 def _clients(self
, fs_id
):
286 cephfs_clients
= self
.cephfs_clients
.get(fs_id
, None)
287 if cephfs_clients
is None:
288 cephfs_clients
= CephFSClients(mgr
, fs_id
)
289 self
.cephfs_clients
[fs_id
] = cephfs_clients
292 status
, clients
= cephfs_clients
.get()
293 except AttributeError:
294 raise cherrypy
.HTTPError(404,
295 "No cephfs with id {0}".format(fs_id
))
298 raise cherrypy
.HTTPError(404,
299 "No cephfs with id {0}".format(fs_id
))
301 # Decorate the metadata with some fields that will be
302 # indepdendent of whether it's a kernel or userspace
303 # client, so that the javascript doesn't have to grok that.
304 for client
in clients
:
305 if "ceph_version" in client
['client_metadata']: # pragma: no cover - no complexity
306 client
['type'] = "userspace"
307 client
['version'] = client
['client_metadata']['ceph_version']
308 client
['hostname'] = client
['client_metadata']['hostname']
309 client
['root'] = client
['client_metadata']['root']
310 elif "kernel_version" in client
['client_metadata']: # pragma: no cover - no complexity
311 client
['type'] = "kernel"
312 client
['version'] = client
['client_metadata']['kernel_version']
313 client
['hostname'] = client
['client_metadata']['hostname']
314 client
['root'] = client
['client_metadata']['root']
315 else: # pragma: no cover - no complexity there
316 client
['type'] = "unknown"
317 client
['version'] = ""
318 client
['hostname'] = ""
325 def _evict(self
, fs_id
, client_id
):
326 clients
= self
._clients
(fs_id
)
327 if not [c
for c
in clients
['data'] if c
['id'] == client_id
]:
328 raise cherrypy
.HTTPError(404,
329 "Client {0} does not exist in cephfs {1}".format(client_id
,
331 CephService
.send_command('mds', 'client evict',
332 srv_spec
='{0}:0'.format(fs_id
), id=client_id
)
335 def _cephfs_instance(fs_id
):
337 :param fs_id: The filesystem identifier.
338 :type fs_id: int | str
339 :return: A instance of the CephFS class.
341 fs_name
= CephFS_
.fs_name_from_id(fs_id
)
343 raise cherrypy
.HTTPError(404, "CephFS id {} not found".format(fs_id
))
344 return CephFS_(fs_name
)
346 @RESTController.Resource('GET')
347 def get_root_directory(self
, fs_id
):
349 The root directory that can't be fetched using ls_dir (api).
350 :param fs_id: The filesystem identifier.
351 :return: The root directory
355 return self
._get
_root
_directory
(self
._cephfs
_instance
(fs_id
))
356 except (cephfs
.PermissionError
, cephfs
.ObjectNotFound
): # pragma: no cover
359 def _get_root_directory(self
, cfs
):
361 The root directory that can't be fetched using ls_dir (api).
362 It's used in ls_dir (ui-api) and in get_root_directory (api).
363 :param cfs: CephFS service instance
365 :return: The root directory
368 return cfs
.get_directory(os
.sep
.encode())
370 @handle_cephfs_error()
371 @RESTController.Resource('GET')
372 def ls_dir(self
, fs_id
, path
=None, depth
=1):
374 List directories of specified path.
375 :param fs_id: The filesystem identifier.
376 :param path: The path where to start listing the directory content.
377 Defaults to '/' if not set.
378 :type path: str | bytes
379 :param depth: The number of steps to go down the directory tree.
380 :type depth: int | str
381 :return: The names of the directories below the specified path.
384 path
= self
._set
_ls
_dir
_path
(path
)
386 cfs
= self
._cephfs
_instance
(fs_id
)
387 paths
= cfs
.ls_dir(path
, depth
)
388 except (cephfs
.PermissionError
, cephfs
.ObjectNotFound
): # pragma: no cover
392 def _set_ls_dir_path(self
, path
):
394 Transforms input path parameter of ls_dir methods (api and ui-api).
395 :param path: The path where to start listing the directory content.
396 Defaults to '/' if not set.
397 :type path: str | bytes
398 :return: Normalized path or root path
404 path
= os
.path
.normpath(path
)
407 @RESTController.Resource('POST', path
='/tree')
409 def mk_tree(self
, fs_id
, path
):
412 :param fs_id: The filesystem identifier.
413 :param path: The path of the directory.
415 cfs
= self
._cephfs
_instance
(fs_id
)
418 @RESTController.Resource('DELETE', path
='/tree')
419 def rm_tree(self
, fs_id
, path
):
422 :param fs_id: The filesystem identifier.
423 :param path: The path of the directory.
425 cfs
= self
._cephfs
_instance
(fs_id
)
428 @RESTController.Resource('PUT', path
='/quota')
430 def quota(self
, fs_id
, path
, max_bytes
=None, max_files
=None):
432 Set the quotas of the specified path.
433 :param fs_id: The filesystem identifier.
434 :param path: The path of the directory/file.
435 :param max_bytes: The byte limit.
436 :param max_files: The file limit.
438 cfs
= self
._cephfs
_instance
(fs_id
)
439 return cfs
.set_quotas(path
, max_bytes
, max_files
)
441 @RESTController.Resource('GET', path
='/quota')
442 @EndpointDoc("Get Cephfs Quotas of the specified path",
444 'fs_id': (str, 'File System Identifier'),
445 'path': (str, 'File System Path'),
447 responses
={200: GET_QUOTAS_SCHEMA
})
448 def get_quota(self
, fs_id
, path
):
450 Get the quotas of the specified path.
451 :param fs_id: The filesystem identifier.
452 :param path: The path of the directory/file.
453 :return: Returns a dictionary containing 'max_bytes'
457 cfs
= self
._cephfs
_instance
(fs_id
)
458 return cfs
.get_quotas(path
)
460 @RESTController.Resource('POST', path
='/snapshot')
462 def snapshot(self
, fs_id
, path
, name
=None):
465 :param fs_id: The filesystem identifier.
466 :param path: The path of the directory.
467 :param name: The name of the snapshot. If not specified, a name using the
468 current time in RFC3339 UTC format will be generated.
469 :return: The name of the snapshot.
472 cfs
= self
._cephfs
_instance
(fs_id
)
473 list_snaps
= cfs
.ls_snapshots(path
)
474 for snap
in list_snaps
:
475 if name
== snap
['name']:
476 raise DashboardException(code
='Snapshot name already in use',
477 msg
='Snapshot name {} is already in use.'
478 'Please use another name'.format(name
),
481 return cfs
.mk_snapshot(path
, name
)
483 @RESTController.Resource('DELETE', path
='/snapshot')
484 def rm_snapshot(self
, fs_id
, path
, name
):
487 :param fs_id: The filesystem identifier.
488 :param path: The path of the directory.
489 :param name: The name of the snapshot.
491 cfs
= self
._cephfs
_instance
(fs_id
)
492 cfs
.rm_snapshot(path
, name
)
495 class CephFSClients(object):
496 def __init__(self
, module_inst
, fscid
):
497 self
._module
= module_inst
502 return CephService
.send_command('mds', 'session ls', srv_spec
='{0}:0'.format(self
.fscid
))
505 @UIRouter('/cephfs', Scope
.CEPHFS
)
506 @APIDoc("Dashboard UI helper function; not part of the public API", "CephFSUi")
507 class CephFsUi(CephFS
):
508 RESOURCE_ID
= 'fs_id'
510 @RESTController.Resource('GET')
511 def tabs(self
, fs_id
):
513 fs_id
= self
.fs_id_to_int(fs_id
)
515 # Needed for detail tab
516 fs_status
= self
.fs_status(fs_id
)
517 for pool
in fs_status
['cephfs']['pools']:
518 pool
['size'] = pool
['used'] + pool
['avail']
519 data
['pools'] = fs_status
['cephfs']['pools']
520 data
['ranks'] = fs_status
['cephfs']['ranks']
521 data
['name'] = fs_status
['cephfs']['name']
522 data
['standbys'] = ', '.join([x
['name'] for x
in fs_status
['standbys']])
523 counters
= self
._mds
_counters
(fs_id
)
524 for k
, v
in counters
.items():
526 data
['mds_counters'] = counters
528 # Needed for client tab
529 data
['clients'] = self
._clients
(fs_id
)
533 @handle_cephfs_error()
534 @RESTController.Resource('GET')
535 def ls_dir(self
, fs_id
, path
=None, depth
=1):
537 The difference to the API version is that the root directory will be send when listing
539 To only do one request this endpoint was created.
540 :param fs_id: The filesystem identifier.
541 :type fs_id: int | str
542 :param path: The path where to start listing the directory content.
543 Defaults to '/' if not set.
544 :type path: str | bytes
545 :param depth: The number of steps to go down the directory tree.
546 :type depth: int | str
547 :return: The names of the directories below the specified path.
550 path
= self
._set
_ls
_dir
_path
(path
)
552 cfs
= self
._cephfs
_instance
(fs_id
)
553 paths
= cfs
.ls_dir(path
, depth
)
555 paths
= [self
._get
_root
_directory
(cfs
)] + paths
556 except (cephfs
.PermissionError
, cephfs
.ObjectNotFound
): # pragma: no cover