]>
git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/services/ceph_service.py
1 # -*- coding: utf-8 -*-
7 from mgr_module
import CommandResult
8 from mgr_util
import get_most_recent_rate
, get_time_series_rates
13 from typing
import Any
, Dict
, Optional
, Union
15 pass # For typing only
17 logger
= logging
.getLogger('ceph_service')
20 class SendCommandError(rados
.Error
):
21 def __init__(self
, err
, prefix
, argdict
, errno
):
23 self
.argdict
= argdict
24 super(SendCommandError
, self
).__init
__(err
, errno
)
27 # pylint: disable=too-many-public-methods
28 class CephService(object):
30 OSD_FLAG_NO_SCRUB
= 'noscrub'
31 OSD_FLAG_NO_DEEP_SCRUB
= 'nodeep-scrub'
33 PG_STATUS_SCRUBBING
= 'scrubbing'
34 PG_STATUS_DEEP_SCRUBBING
= 'deep'
36 SCRUB_STATUS_DISABLED
= 'Disabled'
37 SCRUB_STATUS_ACTIVE
= 'Active'
38 SCRUB_STATUS_INACTIVE
= 'Inactive'
41 def get_service_map(cls
, service_name
):
42 service_map
= {} # type: Dict[str, dict]
43 for server
in mgr
.list_servers():
44 for service
in server
['services']:
45 if service
['type'] == service_name
:
46 if server
['hostname'] not in service_map
:
47 service_map
[server
['hostname']] = {
51 inst_id
= service
['id']
52 metadata
= mgr
.get_metadata(service_name
, inst_id
)
53 status
= mgr
.get_daemon_status(service_name
, inst_id
)
54 service_map
[server
['hostname']]['services'].append({
57 'hostname': server
['hostname'],
64 def get_service_list(cls
, service_name
):
65 service_map
= cls
.get_service_map(service_name
)
66 return [svc
for _
, svcs
in service_map
.items() for svc
in svcs
['services']]
69 def get_service_data_by_metadata_id(cls
,
71 metadata_id
: str) -> Optional
[Dict
[str, Any
]]:
72 for server
in mgr
.list_servers():
73 for service
in server
['services']:
74 if service
['type'] == service_type
:
75 metadata
= mgr
.get_metadata(service_type
, service
['id'])
76 if metadata_id
== metadata
['id']:
79 'service_map_id': str(service
['id']),
81 'hostname': server
['hostname'],
87 def get_service(cls
, service_type
: str, metadata_id
: str) -> Optional
[Dict
[str, Any
]]:
88 svc_data
= cls
.get_service_data_by_metadata_id(service_type
, metadata_id
)
90 svc_data
['status'] = mgr
.get_daemon_status(svc_data
['type'], svc_data
['service_map_id'])
94 def get_service_perf_counters(cls
, service_type
: str, service_id
: str) -> Dict
[str, Any
]:
95 schema_dict
= mgr
.get_perf_schema(service_type
, service_id
)
96 schema
= schema_dict
["{}.{}".format(service_type
, service_id
)]
98 for key
, value
in sorted(schema
.items()):
99 counter
= {'name': str(key
), 'description': value
['description']}
100 # pylint: disable=W0212
101 if mgr
._stattype
_to
_str
(value
['type']) == 'counter':
102 counter
['value'] = cls
.get_rate(
103 service_type
, service_id
, key
)
104 counter
['unit'] = mgr
._unit
_to
_str
(value
['units'])
106 counter
['value'] = mgr
.get_latest(
107 service_type
, service_id
, key
)
109 counters
.append(counter
)
113 'type': service_type
,
114 'id': str(service_id
)
120 def get_pool_list(cls
, application
=None):
121 osd_map
= mgr
.get('osd_map')
123 return osd_map
['pools']
124 return [pool
for pool
in osd_map
['pools']
125 if application
in pool
.get('application_metadata', {})]
128 def get_pool_list_with_stats(cls
, application
=None):
129 # pylint: disable=too-many-locals
130 pools
= cls
.get_pool_list(application
)
134 pg_summary
= mgr
.get("pg_summary")
135 pool_stats
= mgr
.get_updated_pool_stats()
138 pool
['pg_status'] = pg_summary
['by_pool'][pool
['pool'].__str
__()]
139 stats
= pool_stats
[pool
['pool']]
142 for stat_name
, stat_series
in stats
.items():
143 rates
= get_time_series_rates(stat_series
)
145 'latest': stat_series
[0][1],
146 'rate': get_most_recent_rate(rates
),
150 pools_w_stats
.append(pool
)
154 def get_erasure_code_profiles(cls
):
155 def _serialize_ecp(name
, ecp
):
156 def serialize_numbers(key
):
158 if value
is not None:
159 ecp
[key
] = int(value
)
162 serialize_numbers('k')
163 serialize_numbers('m')
167 for name
, ecp
in mgr
.get('osd_map').get('erasure_code_profiles', {}).items():
168 ret
.append(_serialize_ecp(name
, ecp
))
172 def get_pool_name_from_id(cls
, pool_id
):
173 # type: (int) -> Union[str, None]
174 return mgr
.rados
.pool_reverse_lookup(pool_id
)
177 def get_pool_by_attribute(cls
, attribute
, value
):
178 # type: (str, Any) -> Union[dict, None]
179 pool_list
= cls
.get_pool_list()
180 for pool
in pool_list
:
181 if attribute
in pool
and pool
[attribute
] == value
:
186 def get_pool_pg_status(cls
, pool_name
):
187 # type: (str) -> dict
188 pool
= cls
.get_pool_by_attribute('pool_name', pool_name
)
191 return mgr
.get("pg_summary")['by_pool'][pool
['pool'].__str
__()]
194 def send_command(srv_type
, prefix
, srv_spec
='', **kwargs
):
195 # type: (str, str, Optional[str], Any) -> Any
198 :param srv_type: mon |
199 :param kwargs: will be added to argdict
200 :param srv_spec: typically empty. or something like "<fs_id>:0"
202 :raises PermissionError: See rados.make_ex
203 :raises ObjectNotFound: See rados.make_ex
204 :raises IOError: See rados.make_ex
205 :raises NoSpace: See rados.make_ex
206 :raises ObjectExists: See rados.make_ex
207 :raises ObjectBusy: See rados.make_ex
208 :raises NoData: See rados.make_ex
209 :raises InterruptedOrTimeoutError: See rados.make_ex
210 :raises TimedOut: See rados.make_ex
211 :raises ValueError: return code != 0
217 argdict
.update({k
: v
for k
, v
in kwargs
.items() if v
is not None})
218 result
= CommandResult("")
219 mgr
.send_command(result
, srv_type
, srv_spec
, json
.dumps(argdict
), "")
220 r
, outb
, outs
= result
.wait()
222 logger
.error("send_command '%s' failed. (r=%s, outs=\"%s\", kwargs=%s)", prefix
, r
,
225 raise SendCommandError(outs
, prefix
, argdict
, r
)
228 return json
.loads(outb
or outs
)
229 except Exception: # pylint: disable=broad-except
233 def _get_smart_data_by_device(device
):
234 # type: (dict) -> Dict[str, dict]
235 # Check whether the device is associated with daemons.
236 if 'daemons' in device
and device
['daemons']:
237 dev_smart_data
: Dict
[str, Any
] = {}
239 # Get a list of all OSD daemons on all hosts that are 'up'
240 # because SMART data can not be retrieved from daemons that
241 # are 'down' or 'destroyed'.
242 osd_tree
= CephService
.send_command('mon', 'osd tree')
244 node
['name'] for node
in osd_tree
.get('nodes', {})
245 if node
.get('status') == 'up'
248 # All daemons on the same host can deliver SMART data,
249 # thus it is not relevant for us which daemon we are using.
250 # NOTE: the list may contain daemons that are 'down' or 'destroyed'.
251 for daemon
in device
['daemons']:
252 svc_type
, svc_id
= daemon
.split('.')
253 if 'osd' in svc_type
:
254 if daemon
not in osd_daemons_up
:
257 dev_smart_data
= CephService
.send_command(
258 svc_type
, 'smart', svc_id
, devid
=device
['devid'])
259 except SendCommandError
as error
:
260 logger
.warning(str(error
))
261 # Try to retrieve SMART data from another daemon.
263 elif 'mon' in svc_type
:
265 dev_smart_data
= CephService
.send_command(
266 svc_type
, 'device query-daemon-health-metrics', who
=daemon
)
267 except SendCommandError
as error
:
268 logger
.warning(str(error
))
269 # Try to retrieve SMART data from another daemon.
273 for dev_id
, dev_data
in dev_smart_data
.items():
274 if 'error' in dev_data
:
276 '[SMART] Error retrieving smartctl data for device ID "%s": %s',
280 return dev_smart_data
281 logger
.warning('[SMART] No daemons associated with device ID "%s"',
286 def get_devices_by_host(hostname
):
287 # type: (str) -> dict
288 return CephService
.send_command('mon',
293 def get_devices_by_daemon(daemon_type
, daemon_id
):
294 # type: (str, str) -> dict
295 return CephService
.send_command('mon',
296 'device ls-by-daemon',
298 daemon_type
, daemon_id
))
301 def get_smart_data_by_host(hostname
):
302 # type: (str) -> dict
304 Get the SMART data of all devices on the given host, regardless
305 of the daemon (osd, mon, ...).
306 :param hostname: The name of the host.
307 :return: A dictionary containing the SMART data of every device
308 on the given host. The device name is used as the key in the
311 devices
= CephService
.get_devices_by_host(hostname
)
312 smart_data
= {} # type: dict
314 for device
in devices
:
315 if device
['devid'] not in smart_data
:
317 CephService
._get
_smart
_data
_by
_device
(device
))
319 logger
.debug('[SMART] could not retrieve device list from host %s', hostname
)
323 def get_smart_data_by_daemon(daemon_type
, daemon_id
):
324 # type: (str, str) -> Dict[str, dict]
326 Get the SMART data of the devices associated with the given daemon.
327 :param daemon_type: The daemon type, e.g. 'osd' or 'mon'.
328 :param daemon_id: The daemon identifier.
329 :return: A dictionary containing the SMART data of every device
330 associated with the given daemon. The device name is used as the
331 key in the dictionary.
333 devices
= CephService
.get_devices_by_daemon(daemon_type
, daemon_id
)
334 smart_data
= {} # type: Dict[str, dict]
336 for device
in devices
:
337 if device
['devid'] not in smart_data
:
339 CephService
._get
_smart
_data
_by
_device
(device
))
341 msg
= '[SMART] could not retrieve device list from daemon with type %s and ' +\
343 logger
.debug(msg
, daemon_type
, daemon_id
)
347 def get_rates(cls
, svc_type
, svc_name
, path
):
349 :return: the derivative of mgr.get_counter()
350 :rtype: list[tuple[int, float]]"""
351 data
= mgr
.get_counter(svc_type
, svc_name
, path
)[path
]
352 return get_time_series_rates(data
)
355 def get_rate(cls
, svc_type
, svc_name
, path
):
356 """returns most recent rate"""
357 return get_most_recent_rate(cls
.get_rates(svc_type
, svc_name
, path
))
360 def get_client_perf(cls
):
361 pools_stats
= mgr
.get('osd_pool_stats')['pool_stats']
365 'read_op_per_sec': 0,
366 'write_bytes_sec': 0,
367 'write_op_per_sec': 0,
369 recovery_stats
= {'recovering_bytes_per_sec': 0}
371 for pool_stats
in pools_stats
:
372 client_io
= pool_stats
['client_io_rate']
373 for stat
in list(io_stats
.keys()):
374 if stat
in client_io
:
375 io_stats
[stat
] += client_io
[stat
]
377 client_recovery
= pool_stats
['recovery_rate']
378 for stat
in list(recovery_stats
.keys()):
379 if stat
in client_recovery
:
380 recovery_stats
[stat
] += client_recovery
[stat
]
382 client_perf
= io_stats
.copy()
383 client_perf
.update(recovery_stats
)
388 def get_scrub_status(cls
):
389 enabled_flags
= mgr
.get('osd_map')['flags_set']
390 if cls
.OSD_FLAG_NO_SCRUB
in enabled_flags
or cls
.OSD_FLAG_NO_DEEP_SCRUB
in enabled_flags
:
391 return cls
.SCRUB_STATUS_DISABLED
393 grouped_pg_statuses
= mgr
.get('pg_summary')['all']
394 for grouped_pg_status
in grouped_pg_statuses
.keys():
395 if len(grouped_pg_status
.split(cls
.PG_STATUS_SCRUBBING
)) > 1 \
396 or len(grouped_pg_status
.split(cls
.PG_STATUS_DEEP_SCRUBBING
)) > 1:
397 return cls
.SCRUB_STATUS_ACTIVE
399 return cls
.SCRUB_STATUS_INACTIVE
402 def get_pg_info(cls
):
403 pg_summary
= mgr
.get('pg_summary')
404 object_stats
= {stat
: pg_summary
['pg_stats_sum']['stat_sum'][stat
] for stat
in [
405 'num_objects', 'num_object_copies', 'num_objects_degraded',
406 'num_objects_misplaced', 'num_objects_unfound']}
409 total_osds
= len(pg_summary
['by_osd'])
412 for _
, osd_pg_statuses
in pg_summary
['by_osd'].items():
413 for _
, pg_amount
in osd_pg_statuses
.items():
414 total_pgs
+= pg_amount
416 pgs_per_osd
= total_pgs
/ total_osds
419 'object_stats': object_stats
,
420 'statuses': pg_summary
['all'],
421 'pgs_per_osd': pgs_per_osd
,