1 import ceph_module
# noqa
3 from typing
import cast
, Tuple
, Any
, Dict
, Generic
, Optional
, Callable
, List
, \
4 Mapping
, NamedTuple
, Sequence
, Union
, TYPE_CHECKING
7 if sys
.version_info
>= (3, 8):
8 from typing
import Literal
10 from typing_extensions
import Literal
19 from collections
import defaultdict
20 from enum
import IntEnum
26 from ceph_argparse
import CephArgtype
27 from mgr_util
import profile_method
29 if sys
.version_info
>= (3, 8):
30 from typing
import get_args
, get_origin
36 return getattr(tp
, '__args__', ())
39 return getattr(tp
, '__origin__', None)
42 ERROR_MSG_EMPTY_INPUT_FILE
= 'Empty input file'
43 ERROR_MSG_NO_INPUT_FILE
= 'Input file not specified'
44 # Full list of strings in "osd_types.cc:pg_state_string()"
82 NFS_GANESHA_SUPPORTED_FSALS
= ['CEPH', 'RGW']
83 NFS_POOL_NAME
= '.nfs'
86 class CommandResult(object):
88 Use with MgrModule.send_command
91 def __init__(self
, tag
: Optional
[str] = None):
92 self
.ev
= threading
.Event()
97 # This is just a convenience for notifications from
98 # C++ land, to avoid passing addresses around in messages.
99 self
.tag
= tag
if tag
else ""
101 def complete(self
, r
: int, outb
: str, outs
: str) -> None:
107 def wait(self
) -> Tuple
[int, str, str]:
109 return self
.r
, self
.outb
, self
.outs
112 class HandleCommandResult(NamedTuple
):
114 Tuple containing the result of `handle_command()`
116 Only write to stderr if there is an error, or in extraordinary circumstances
118 Avoid having `ceph foo bar` commands say "did foo bar" on success unless there
119 is critical information to include there.
121 Everything programmatically consumable should be put on stdout
123 retval
: int = 0 # return code. E.g. 0 or -errno.EINVAL
124 stdout
: str = "" # data of this result.
125 stderr
: str = "" # Typically used for error messages.
128 class MonCommandFailed(RuntimeError): pass
131 class OSDMap(ceph_module
.BasePyOSDMap
):
132 def get_epoch(self
) -> int:
133 return self
._get
_epoch
()
135 def get_crush_version(self
) -> int:
136 return self
._get
_crush
_version
()
138 def dump(self
) -> Dict
[str, Any
]:
141 def get_pools(self
) -> Dict
[int, Dict
[str, Any
]]:
142 # FIXME: efficient implementation
144 return dict([(p
['pool'], p
) for p
in d
['pools']])
146 def get_pools_by_name(self
) -> Dict
[str, Dict
[str, Any
]]:
147 # FIXME: efficient implementation
149 return dict([(p
['pool_name'], p
) for p
in d
['pools']])
151 def new_incremental(self
) -> 'OSDMapIncremental':
152 return self
._new
_incremental
()
154 def apply_incremental(self
, inc
: 'OSDMapIncremental') -> 'OSDMap':
155 return self
._apply
_incremental
(inc
)
157 def get_crush(self
) -> 'CRUSHMap':
158 return self
._get
_crush
()
160 def get_pools_by_take(self
, take
: int) -> List
[int]:
161 return self
._get
_pools
_by
_take
(take
).get('pools', [])
163 def calc_pg_upmaps(self
, inc
: 'OSDMapIncremental',
165 max_iterations
: int = 10,
166 pools
: Optional
[List
[str]] = None) -> int:
169 return self
._calc
_pg
_upmaps
(
171 max_deviation
, max_iterations
, pools
)
173 def map_pool_pgs_up(self
, poolid
: int) -> List
[int]:
174 return self
._map
_pool
_pgs
_up
(poolid
)
176 def pg_to_up_acting_osds(self
, pool_id
: int, ps
: int) -> Dict
[str, Any
]:
177 return self
._pg
_to
_up
_acting
_osds
(pool_id
, ps
)
179 def pool_raw_used_rate(self
, pool_id
: int) -> float:
180 return self
._pool
_raw
_used
_rate
(pool_id
)
182 def get_ec_profile(self
, name
: str) -> Optional
[List
[Dict
[str, str]]]:
183 # FIXME: efficient implementation
185 return d
['erasure_code_profiles'].get(name
, None)
187 def get_require_osd_release(self
) -> str:
189 return d
['require_osd_release']
192 class OSDMapIncremental(ceph_module
.BasePyOSDMapIncremental
):
193 def get_epoch(self
) -> int:
194 return self
._get
_epoch
()
196 def dump(self
) -> Dict
[str, Any
]:
199 def set_osd_reweights(self
, weightmap
: Dict
[int, float]) -> None:
201 weightmap is a dict, int to float. e.g. { 0: .9, 1: 1.0, 3: .997 }
203 return self
._set
_osd
_reweights
(weightmap
)
205 def set_crush_compat_weight_set_weights(self
, weightmap
: Dict
[str, float]) -> None:
207 weightmap is a dict, int to float. devices only. e.g.,
208 { 0: 3.4, 1: 3.3, 2: 3.334 }
210 return self
._set
_crush
_compat
_weight
_set
_weights
(weightmap
)
213 class CRUSHMap(ceph_module
.BasePyCRUSH
):
214 ITEM_NONE
= 0x7fffffff
215 DEFAULT_CHOOSE_ARGS
= '-1'
217 def dump(self
) -> Dict
[str, Any
]:
220 def get_item_weight(self
, item
: int) -> Optional
[int]:
221 return self
._get
_item
_weight
(item
)
223 def get_item_name(self
, item
: int) -> Optional
[str]:
224 return self
._get
_item
_name
(item
)
226 def find_takes(self
) -> List
[int]:
227 return self
._find
_takes
().get('takes', [])
229 def find_roots(self
) -> List
[int]:
230 return self
._find
_roots
().get('roots', [])
232 def get_take_weight_osd_map(self
, root
: int) -> Dict
[int, float]:
233 uglymap
= self
._get
_take
_weight
_osd
_map
(root
)
234 return {int(k
): v
for k
, v
in uglymap
.get('weights', {}).items()}
237 def have_default_choose_args(dump
: Dict
[str, Any
]) -> bool:
238 return CRUSHMap
.DEFAULT_CHOOSE_ARGS
in dump
.get('choose_args', {})
241 def get_default_choose_args(dump
: Dict
[str, Any
]) -> List
[Dict
[str, Any
]]:
242 choose_args
= dump
.get('choose_args')
243 assert isinstance(choose_args
, dict)
244 return choose_args
.get(CRUSHMap
.DEFAULT_CHOOSE_ARGS
, [])
246 def get_rule(self
, rule_name
: str) -> Optional
[Dict
[str, Any
]]:
247 # TODO efficient implementation
248 for rule
in self
.dump()['rules']:
249 if rule_name
== rule
['rule_name']:
254 def get_rule_by_id(self
, rule_id
: int) -> Optional
[Dict
[str, Any
]]:
255 for rule
in self
.dump()['rules']:
256 if rule
['rule_id'] == rule_id
:
261 def get_rule_root(self
, rule_name
: str) -> Optional
[int]:
262 rule
= self
.get_rule(rule_name
)
267 first_take
= next(s
for s
in rule
['steps'] if s
.get('op') == 'take')
268 except StopIteration:
269 logging
.warning("CRUSH rule '{0}' has no 'take' step".format(
273 return first_take
['item']
275 def get_osds_under(self
, root_id
: int) -> List
[int]:
276 # TODO don't abuse dump like this
278 buckets
= dict([(b
['id'], b
) for b
in d
['buckets']])
282 def accumulate(b
: Dict
[str, Any
]) -> None:
283 for item
in b
['items']:
285 osd_list
.append(item
['id'])
288 accumulate(buckets
[item
['id']])
292 accumulate(buckets
[root_id
])
296 def device_class_counts(self
) -> Dict
[str, int]:
297 result
= defaultdict(int) # type: Dict[str, int]
298 # TODO don't abuse dump like this
300 for device
in d
['devices']:
301 cls
= device
.get('class', None)
307 HandlerFuncType
= Callable
[..., Tuple
[int, str, str]]
310 class CLICommand(object):
311 COMMANDS
= {} # type: Dict[str, CLICommand]
320 self
.func
= None # type: Optional[Callable]
321 self
.arg_spec
= {} # type: Dict[str, Any]
322 self
.first_default
= -1
324 KNOWN_ARGS
= '_', 'self', 'mgr', 'inbuf', 'return'
327 def load_func_metadata(f
: HandlerFuncType
) -> Tuple
[str, Dict
[str, Any
], int, str]:
328 desc
= (inspect
.getdoc(f
) or '').replace('\n', ' ')
329 full_argspec
= inspect
.getfullargspec(f
)
330 arg_spec
= full_argspec
.annotations
331 first_default
= len(arg_spec
)
332 if full_argspec
.defaults
:
333 first_default
-= len(full_argspec
.defaults
)
335 for index
, arg
in enumerate(full_argspec
.args
):
336 if arg
in CLICommand
.KNOWN_ARGS
:
338 assert arg
in arg_spec
, \
339 f
"'{arg}' is not annotated for {f}: {full_argspec}"
340 has_default
= index
>= first_default
341 args
.append(CephArgtype
.to_argdesc(arg_spec
[arg
],
344 return desc
, arg_spec
, first_default
, ' '.join(args
)
346 def store_func_metadata(self
, f
: HandlerFuncType
) -> None:
347 self
.desc
, self
.arg_spec
, self
.first_default
, self
.args
= \
348 self
.load_func_metadata(f
)
350 def __call__(self
, func
: HandlerFuncType
) -> HandlerFuncType
:
351 self
.store_func_metadata(func
)
353 self
.COMMANDS
[self
.prefix
] = self
356 def _get_arg_value(self
, kwargs_switch
: bool, key
: str, val
: Any
) -> Tuple
[bool, str, Any
]:
357 def start_kwargs() -> bool:
358 if isinstance(val
, str) and '=' in val
:
359 k
, v
= val
.split('=', 1)
360 if k
in self
.arg_spec
:
364 if not kwargs_switch
:
365 kwargs_switch
= start_kwargs()
368 k
, v
= val
.split('=', 1)
371 return kwargs_switch
, k
.replace('-', '_'), v
373 def _collect_args_by_argspec(self
, cmd_dict
: Dict
[str, Any
]) -> Dict
[str, Any
]:
375 kwargs_switch
= False
376 for index
, (name
, tp
) in enumerate(self
.arg_spec
.items()):
377 if name
in CLICommand
.KNOWN_ARGS
:
379 assert self
.first_default
>= 0
380 raw_v
= cmd_dict
.get(name
)
381 if index
>= self
.first_default
:
384 kwargs_switch
, k
, v
= self
._get
_arg
_value
(kwargs_switch
,
386 kwargs
[k
] = CephArgtype
.cast_to(tp
, v
)
391 cmd_dict
: Dict
[str, Any
],
392 inbuf
: Optional
[str] = None) -> HandleCommandResult
:
393 kwargs
= self
._collect
_args
_by
_argspec
(cmd_dict
)
395 kwargs
['inbuf'] = inbuf
397 return self
.func(mgr
, **kwargs
)
399 def dump_cmd(self
) -> Dict
[str, Union
[str, bool]]:
401 'cmd': '{} {}'.format(self
.prefix
, self
.args
),
408 def dump_cmd_list(cls
) -> List
[Dict
[str, Union
[str, bool]]]:
409 return [cmd
.dump_cmd() for cmd
in cls
.COMMANDS
.values()]
412 def CLIReadCommand(prefix
: str, poll
: bool = False) -> CLICommand
:
413 return CLICommand(prefix
, "r", poll
)
416 def CLIWriteCommand(prefix
: str, poll
: bool = False) -> CLICommand
:
417 return CLICommand(prefix
, "w", poll
)
420 def CLICheckNonemptyFileInput(desc
: str) -> Callable
[[HandlerFuncType
], HandlerFuncType
]:
421 def CheckFileInput(func
: HandlerFuncType
) -> HandlerFuncType
:
422 @functools.wraps(func
)
423 def check(*args
: Any
, **kwargs
: Any
) -> Tuple
[int, str, str]:
424 if 'inbuf' not in kwargs
:
425 return -errno
.EINVAL
, '', f
'{ERROR_MSG_NO_INPUT_FILE}: Please specify the file '\
426 f
'containing {desc} with "-i" option'
427 if isinstance(kwargs
['inbuf'], str):
428 # Delete new line separator at EOF (it may have been added by a text editor).
429 kwargs
['inbuf'] = kwargs
['inbuf'].rstrip('\r\n').rstrip('\n')
430 if not kwargs
['inbuf'] or not kwargs
['inbuf'].strip():
431 return -errno
.EINVAL
, '', f
'{ERROR_MSG_EMPTY_INPUT_FILE}: Please add {desc} to '\
433 return func(*args
, **kwargs
)
434 check
.__signature
__ = inspect
.signature(func
) # type: ignore[attr-defined]
436 return CheckFileInput
439 def _get_localized_key(prefix
: str, key
: str) -> str:
440 return '{}/{}'.format(prefix
, key
)
444 MODULE_OPTIONS types and Option Class
447 OptionTypeLabel
= Literal
[
448 'uint', 'int', 'str', 'float', 'bool', 'addr', 'addrvec', 'uuid', 'size', 'secs']
451 # common/options.h: value_t
452 OptionValue
= Optional
[Union
[bool, int, float, str]]
457 Helper class to declare options for MODULE_OPTIONS list.
458 TODO: Replace with typing.TypedDict when in python_version >= 3.8
464 default
: OptionValue
= None,
465 type: 'OptionTypeLabel' = 'str',
466 desc
: Optional
[str] = None,
467 long_desc
: Optional
[str] = None,
468 min: OptionValue
= None,
469 max: OptionValue
= None,
470 enum_allowed
: Optional
[List
[str]] = None,
471 tags
: Optional
[List
[str]] = None,
472 see_also
: Optional
[List
[str]] = None,
473 runtime
: bool = False,
475 super(Option
, self
).__init
__(
476 (k
, v
) for k
, v
in vars().items()
477 if k
!= 'self' and v
is not None)
482 Helper class to declare options for COMMANDS list.
484 It also allows to specify prefix and args separately, as well as storing a
488 >>> Command(prefix="example",
489 ... args="name=arg,type=CephInt",
492 {'poll': False, 'cmd': 'example name=arg,type=CephInt', 'perm': 'w', 'desc': 'Blah'}
498 handler
: HandlerFuncType
,
502 super().__init
__(perm
=perm
,
505 self
.handler
= handler
508 def returns_command_result(instance
: Any
,
509 f
: HandlerFuncType
) -> Callable
[..., HandleCommandResult
]:
511 def wrapper(mgr
: Any
, *args
: Any
, **kwargs
: Any
) -> HandleCommandResult
:
512 retval
, stdout
, stderr
= f(instance
or mgr
, *args
, **kwargs
)
513 return HandleCommandResult(retval
, stdout
, stderr
)
514 wrapper
.__signature
__ = inspect
.signature(f
) # type: ignore[attr-defined]
517 def register(self
, instance
: bool = False) -> HandlerFuncType
:
519 Register a CLICommand handler. It allows an instance to register bound
520 methods. In that case, the mgr instance is not passed, and it's expected
521 to be available in the class instance.
522 It also uses HandleCommandResult helper to return a wrapped a tuple of 3
525 cmd
= CLICommand(prefix
=self
.prefix
, perm
=self
['perm'])
526 return cmd(self
.returns_command_result(instance
, self
.handler
))
529 class CPlusPlusHandler(logging
.Handler
):
530 def __init__(self
, module_inst
: Any
):
531 super(CPlusPlusHandler
, self
).__init
__()
532 self
._module
= module_inst
533 self
.setFormatter(logging
.Formatter("[{} %(levelname)-4s %(name)s] %(message)s"
534 .format(module_inst
.module_name
)))
536 def emit(self
, record
: logging
.LogRecord
) -> None:
537 if record
.levelno
>= self
.level
:
538 self
._module
._ceph
_log
(self
.format(record
))
541 class ClusterLogHandler(logging
.Handler
):
542 def __init__(self
, module_inst
: Any
):
544 self
._module
= module_inst
545 self
.setFormatter(logging
.Formatter("%(message)s"))
547 def emit(self
, record
: logging
.LogRecord
) -> None:
549 logging
.DEBUG
: MgrModule
.ClusterLogPrio
.DEBUG
,
550 logging
.INFO
: MgrModule
.ClusterLogPrio
.INFO
,
551 logging
.WARNING
: MgrModule
.ClusterLogPrio
.WARN
,
552 logging
.ERROR
: MgrModule
.ClusterLogPrio
.ERROR
,
553 logging
.CRITICAL
: MgrModule
.ClusterLogPrio
.ERROR
,
555 level
= levelmap
[record
.levelno
]
556 if record
.levelno
>= self
.level
:
557 self
._module
.cluster_log(self
._module
.module_name
,
562 class FileHandler(logging
.FileHandler
):
563 def __init__(self
, module_inst
: Any
):
564 path
= module_inst
.get_ceph_option("log_file")
565 idx
= path
.rfind(".log")
567 self
.path
= "{}.{}.log".format(path
[:idx
], module_inst
.module_name
)
569 self
.path
= "{}.{}".format(path
, module_inst
.module_name
)
570 super(FileHandler
, self
).__init
__(self
.path
, delay
=True)
571 self
.setFormatter(logging
.Formatter("%(asctime)s [%(threadName)s] [%(levelname)-4s] [%(name)s] %(message)s"))
574 class MgrModuleLoggingMixin(object):
575 def _configure_logging(self
,
580 log_to_cluster
: bool) -> None:
581 self
._mgr
_level
: Optional
[str] = None
582 self
._module
_level
: Optional
[str] = None
583 self
._root
_logger
= logging
.getLogger()
585 self
._unconfigure
_logging
()
587 # the ceph log handler is initialized only once
588 self
._mgr
_log
_handler
= CPlusPlusHandler(self
)
589 self
._cluster
_log
_handler
= ClusterLogHandler(self
)
590 self
._file
_log
_handler
= FileHandler(self
)
592 self
.log_to_file
= log_to_file
593 self
.log_to_cluster
= log_to_cluster
595 self
._root
_logger
.addHandler(self
._mgr
_log
_handler
)
597 self
._root
_logger
.addHandler(self
._file
_log
_handler
)
599 self
._root
_logger
.addHandler(self
._cluster
_log
_handler
)
601 self
._root
_logger
.setLevel(logging
.NOTSET
)
602 self
._set
_log
_level
(mgr_level
, module_level
, cluster_level
)
604 def _unconfigure_logging(self
) -> None:
605 # remove existing handlers:
607 h
for h
in self
._root
_logger
.handlers
608 if (isinstance(h
, CPlusPlusHandler
) or
609 isinstance(h
, FileHandler
) or
610 isinstance(h
, ClusterLogHandler
))]
611 for h
in rm_handlers
:
612 self
._root
_logger
.removeHandler(h
)
613 self
.log_to_file
= False
614 self
.log_to_cluster
= False
616 def _set_log_level(self
,
619 cluster_level
: str) -> None:
620 self
._cluster
_log
_handler
.setLevel(cluster_level
.upper())
622 module_level
= module_level
.upper() if module_level
else ''
623 if not self
._module
_level
:
624 # using debug_mgr level
625 if not module_level
and self
._mgr
_level
== mgr_level
:
626 # no change in module level neither in debug_mgr
629 if self
._module
_level
== module_level
:
630 # no change in module level
633 if not self
._module
_level
and not module_level
:
634 level
= self
._ceph
_log
_level
_to
_python
(mgr_level
)
635 self
.getLogger().debug("setting log level based on debug_mgr: %s (%s)",
637 elif self
._module
_level
and not module_level
:
638 level
= self
._ceph
_log
_level
_to
_python
(mgr_level
)
639 self
.getLogger().warning("unsetting module log level, falling back to "
640 "debug_mgr level: %s (%s)", level
, mgr_level
)
643 self
.getLogger().debug("setting log level: %s", level
)
645 self
._module
_level
= module_level
646 self
._mgr
_level
= mgr_level
648 self
._mgr
_log
_handler
.setLevel(level
)
649 self
._file
_log
_handler
.setLevel(level
)
651 def _enable_file_log(self
) -> None:
653 self
.getLogger().warning("enabling logging to file")
654 self
.log_to_file
= True
655 self
._root
_logger
.addHandler(self
._file
_log
_handler
)
657 def _disable_file_log(self
) -> None:
659 self
.getLogger().warning("disabling logging to file")
660 self
.log_to_file
= False
661 self
._root
_logger
.removeHandler(self
._file
_log
_handler
)
663 def _enable_cluster_log(self
) -> None:
665 self
.getLogger().warning("enabling logging to cluster")
666 self
.log_to_cluster
= True
667 self
._root
_logger
.addHandler(self
._cluster
_log
_handler
)
669 def _disable_cluster_log(self
) -> None:
670 # disable cluster log
671 self
.getLogger().warning("disabling logging to cluster")
672 self
.log_to_cluster
= False
673 self
._root
_logger
.removeHandler(self
._cluster
_log
_handler
)
675 def _ceph_log_level_to_python(self
, log_level
: str) -> str:
678 ceph_log_level
= int(log_level
.split("/", 1)[0])
685 if ceph_log_level
<= 0:
686 log_level
= "CRITICAL"
687 elif ceph_log_level
<= 1:
688 log_level
= "WARNING"
689 elif ceph_log_level
<= 4:
693 def getLogger(self
, name
: Optional
[str] = None) -> logging
.Logger
:
694 return logging
.getLogger(name
)
697 class MgrStandbyModule(ceph_module
.BaseMgrStandbyModule
, MgrModuleLoggingMixin
):
699 Standby modules only implement a serve and shutdown method, they
700 are not permitted to implement commands and they do not receive
703 They only have access to the mgrmap (for accessing service URI info
704 from their active peer), and to configuration settings (read only).
707 MODULE_OPTIONS
: List
[Option
] = []
708 MODULE_OPTION_DEFAULTS
= {} # type: Dict[str, Any]
710 def __init__(self
, module_name
: str, capsule
: Any
):
711 super(MgrStandbyModule
, self
).__init
__(capsule
)
712 self
.module_name
= module_name
714 # see also MgrModule.__init__()
715 for o
in self
.MODULE_OPTIONS
:
718 self
.MODULE_OPTION_DEFAULTS
[o
['name']] = o
['default']
720 self
.MODULE_OPTION_DEFAULTS
[o
['name']] = str(o
['default'])
722 # mock does not return a str
723 mgr_level
= cast(str, self
.get_ceph_option("debug_mgr"))
724 log_level
= cast(str, self
.get_module_option("log_level"))
725 cluster_level
= cast(str, self
.get_module_option('log_to_cluster_level'))
726 self
._configure
_logging
(mgr_level
, log_level
, cluster_level
,
729 # for backwards compatibility
730 self
._logger
= self
.getLogger()
732 def __del__(self
) -> None:
734 self
._unconfigure
_logging
()
736 def _cleanup(self
) -> None:
740 def _register_options(cls
, module_name
: str) -> None:
741 cls
.MODULE_OPTIONS
.append(
742 Option(name
='log_level', type='str', default
="", runtime
=True,
743 enum_allowed
=['info', 'debug', 'critical', 'error',
745 cls
.MODULE_OPTIONS
.append(
746 Option(name
='log_to_file', type='bool', default
=False, runtime
=True))
747 if not [x
for x
in cls
.MODULE_OPTIONS
if x
['name'] == 'log_to_cluster']:
748 cls
.MODULE_OPTIONS
.append(
749 Option(name
='log_to_cluster', type='bool', default
=False,
751 cls
.MODULE_OPTIONS
.append(
752 Option(name
='log_to_cluster_level', type='str', default
='info',
754 enum_allowed
=['info', 'debug', 'critical', 'error',
758 def log(self
) -> logging
.Logger
:
761 def serve(self
) -> None:
763 The serve method is mandatory for standby modules.
766 raise NotImplementedError()
768 def get_mgr_id(self
) -> str:
769 return self
._ceph
_get
_mgr
_id
()
771 def get_module_option(self
, key
: str, default
: OptionValue
= None) -> OptionValue
:
773 Retrieve the value of a persistent configuration setting
775 :param default: the default value of the config if it is not found
777 r
= self
._ceph
_get
_module
_option
(key
)
779 return self
.MODULE_OPTION_DEFAULTS
.get(key
, default
)
783 def get_ceph_option(self
, key
: str) -> OptionValue
:
784 return self
._ceph
_get
_option
(key
)
786 def get_store(self
, key
: str) -> Optional
[str]:
788 Retrieve the value of a persistent KV store entry
791 :return: Byte string or None
793 return self
._ceph
_get
_store
(key
)
795 def get_localized_store(self
, key
: str, default
: Optional
[str] = None) -> Optional
[str]:
796 r
= self
._ceph
_get
_store
(_get_localized_key(self
.get_mgr_id(), key
))
798 r
= self
._ceph
_get
_store
(key
)
803 def get_active_uri(self
) -> str:
804 return self
._ceph
_get
_active
_uri
()
806 def get(self
, data_name
: str):
807 return self
._ceph
_get
(data_name
)
809 def get_mgr_ip(self
) -> str:
810 ips
= self
.get("mgr_ips").get('ips', [])
812 return socket
.gethostname()
815 def get_localized_module_option(self
, key
: str, default
: OptionValue
= None) -> OptionValue
:
816 r
= self
._ceph
_get
_module
_option
(key
, self
.get_mgr_id())
818 return self
.MODULE_OPTION_DEFAULTS
.get(key
, default
)
823 HealthChecksT
= Mapping
[str, Mapping
[str, Union
[int, str, Sequence
[str]]]]
824 # {"type": service_type, "id": service_id}
825 ServiceInfoT
= Dict
[str, str]
826 # {"hostname": hostname,
827 # "ceph_version": version,
828 # "services": [service_info, ..]}
829 ServerInfoT
= Dict
[str, Union
[str, List
[ServiceInfoT
]]]
830 PerfCounterT
= Dict
[str, Any
]
833 class MgrModule(ceph_module
.BaseMgrModule
, MgrModuleLoggingMixin
):
834 COMMANDS
= [] # type: List[Any]
835 MODULE_OPTIONS
: List
[Option
] = []
836 MODULE_OPTION_DEFAULTS
= {} # type: Dict[str, Any]
838 # Priority definitions for perf counters
842 PRIO_UNINTERESTING
= 2
845 # counter value types
850 PERFCOUNTER_LONGRUNAVG
= 4
851 PERFCOUNTER_COUNTER
= 8
852 PERFCOUNTER_HISTOGRAM
= 0x10
853 PERFCOUNTER_TYPE_MASK
= ~
3
859 # Cluster log priorities
860 class ClusterLogPrio(IntEnum
):
867 def __init__(self
, module_name
: str, py_modules_ptr
: object, this_ptr
: object):
868 self
.module_name
= module_name
869 super(MgrModule
, self
).__init
__(py_modules_ptr
, this_ptr
)
871 for o
in self
.MODULE_OPTIONS
:
874 # we'll assume the declared type matches the
875 # supplied default value's type.
876 self
.MODULE_OPTION_DEFAULTS
[o
['name']] = o
['default']
878 # module not declaring it's type, so normalize the
879 # default value to be a string for consistent behavior
880 # with default and user-supplied option values.
881 self
.MODULE_OPTION_DEFAULTS
[o
['name']] = str(o
['default'])
883 mgr_level
= cast(str, self
.get_ceph_option("debug_mgr"))
884 log_level
= cast(str, self
.get_module_option("log_level"))
885 cluster_level
= cast(str, self
.get_module_option('log_to_cluster_level'))
886 log_to_file
= self
.get_module_option("log_to_file")
887 assert isinstance(log_to_file
, bool)
888 log_to_cluster
= self
.get_module_option("log_to_cluster")
889 assert isinstance(log_to_cluster
, bool)
890 self
._configure
_logging
(mgr_level
, log_level
, cluster_level
,
891 log_to_file
, log_to_cluster
)
893 # for backwards compatibility
894 self
._logger
= self
.getLogger()
896 self
._version
= self
._ceph
_get
_version
()
898 self
._perf
_schema
_cache
= None
900 # Keep a librados instance for those that need it.
901 self
._rados
: Optional
[rados
.Rados
] = None
903 def __del__(self
) -> None:
904 self
._unconfigure
_logging
()
907 def _register_options(cls
, module_name
: str) -> None:
908 cls
.MODULE_OPTIONS
.append(
909 Option(name
='log_level', type='str', default
="", runtime
=True,
910 enum_allowed
=['info', 'debug', 'critical', 'error',
912 cls
.MODULE_OPTIONS
.append(
913 Option(name
='log_to_file', type='bool', default
=False, runtime
=True))
914 if not [x
for x
in cls
.MODULE_OPTIONS
if x
['name'] == 'log_to_cluster']:
915 cls
.MODULE_OPTIONS
.append(
916 Option(name
='log_to_cluster', type='bool', default
=False,
918 cls
.MODULE_OPTIONS
.append(
919 Option(name
='log_to_cluster_level', type='str', default
='info',
921 enum_allowed
=['info', 'debug', 'critical', 'error',
925 def _register_commands(cls
, module_name
: str) -> None:
926 cls
.COMMANDS
.extend(CLICommand
.dump_cmd_list())
929 def log(self
) -> logging
.Logger
:
932 def cluster_log(self
, channel
: str, priority
: ClusterLogPrio
, message
: str) -> None:
934 :param channel: The log channel. This can be 'cluster', 'audit', ...
935 :param priority: The log message priority.
936 :param message: The message to log.
938 self
._ceph
_cluster
_log
(channel
, priority
.value
, message
)
941 def version(self
) -> str:
945 def release_name(self
) -> str:
947 Get the release name of the Ceph version, e.g. 'nautilus' or 'octopus'.
948 :return: Returns the release name of the Ceph version in lower case.
951 return self
._ceph
_get
_release
_name
()
953 def lookup_release_name(self
, major
: int) -> str:
954 return self
._ceph
_lookup
_release
_name
(major
)
956 def get_context(self
) -> object:
958 :return: a Python capsule containing a C++ CephContext pointer
960 return self
._ceph
_get
_context
()
962 def notify(self
, notify_type
: str, notify_id
: str) -> None:
964 Called by the ceph-mgr service to notify the Python plugin
965 that new state is available.
967 :param notify_type: string indicating what kind of notification,
968 such as osd_map, mon_map, fs_map, mon_status,
969 health, pg_summary, command, service_map
970 :param notify_id: string (may be empty) that optionally specifies
971 which entity is being notified about. With
972 "command" notifications this is set to the tag
973 ``from send_command``.
977 def _config_notify(self
) -> None:
978 # check logging options for changes
979 mgr_level
= cast(str, self
.get_ceph_option("debug_mgr"))
980 module_level
= cast(str, self
.get_module_option("log_level"))
981 cluster_level
= cast(str, self
.get_module_option("log_to_cluster_level"))
982 assert isinstance(cluster_level
, str)
983 log_to_file
= self
.get_module_option("log_to_file", False)
984 assert isinstance(log_to_file
, bool)
985 log_to_cluster
= self
.get_module_option("log_to_cluster", False)
986 assert isinstance(log_to_cluster
, bool)
987 self
._set
_log
_level
(mgr_level
, module_level
, cluster_level
)
989 if log_to_file
!= self
.log_to_file
:
991 self
._enable
_file
_log
()
993 self
._disable
_file
_log
()
994 if log_to_cluster
!= self
.log_to_cluster
:
996 self
._enable
_cluster
_log
()
998 self
._disable
_cluster
_log
()
1000 # call module subclass implementations
1001 self
.config_notify()
1003 def config_notify(self
) -> None:
1005 Called by the ceph-mgr service to notify the Python plugin
1006 that the configuration may have changed. Modules will want to
1007 refresh any configuration values stored in config variables.
1011 def serve(self
) -> None:
1013 Called by the ceph-mgr service to start any server that
1014 is provided by this Python plugin. The implementation
1015 of this function should block until ``shutdown`` is called.
1017 You *must* implement ``shutdown`` if you implement ``serve``
1021 def shutdown(self
) -> None:
1023 Called by the ceph-mgr service to request that this
1024 module drop out of its serve() function. You do not
1025 need to implement this if you do not implement serve()
1030 addrs
= self
._rados
.get_addrs()
1031 self
._rados
.shutdown()
1032 self
._ceph
_unregister
_client
(addrs
)
1034 def get(self
, data_name
: str):
1036 Called by the plugin to fetch named cluster-wide objects from ceph-mgr.
1038 :param str data_name: Valid things to fetch are osd_crush_map_text,
1039 osd_map, osd_map_tree, osd_map_crush, config, mon_map, fs_map,
1040 osd_metadata, pg_summary, io_rate, pg_dump, df, osd_stats,
1041 health, mon_status, devices, device <devid>, pg_stats,
1042 pool_stats, pg_ready, osd_ping_times.
1045 All these structures have their own JSON representations: experiment
1046 or look at the C++ ``dump()`` methods to learn about them.
1048 return self
._ceph
_get
(data_name
)
1050 def _stattype_to_str(self
, stattype
: int) -> str:
1052 typeonly
= stattype
& self
.PERFCOUNTER_TYPE_MASK
1055 if typeonly
== self
.PERFCOUNTER_LONGRUNAVG
:
1056 # this lie matches the DaemonState decoding: only val, no counts
1058 if typeonly
== self
.PERFCOUNTER_COUNTER
:
1060 if typeonly
== self
.PERFCOUNTER_HISTOGRAM
:
1065 def _perfpath_to_path_labels(self
, daemon
: str,
1066 path
: str) -> Tuple
[str, Tuple
[str, ...], Tuple
[str, ...]]:
1067 label_names
= ("ceph_daemon",) # type: Tuple[str, ...]
1068 labels
= (daemon
,) # type: Tuple[str, ...]
1070 if daemon
.startswith('rbd-mirror.'):
1072 r
'^rbd_mirror_image_([^/]+)/(?:(?:([^/]+)/)?)(.*)\.(replay(?:_bytes|_latency)?)$',
1076 path
= 'rbd_mirror_image_' + match
.group(4)
1077 pool
= match
.group(1)
1078 namespace
= match
.group(2) or ''
1079 image
= match
.group(3)
1080 label_names
+= ('pool', 'namespace', 'image')
1081 labels
+= (pool
, namespace
, image
)
1083 return path
, label_names
, labels
,
1085 def _perfvalue_to_value(self
, stattype
: int, value
: Union
[int, float]) -> Union
[float, int]:
1086 if stattype
& self
.PERFCOUNTER_TIME
:
1087 # Convert from ns to seconds
1088 return value
/ 1000000000.0
1092 def _unit_to_str(self
, unit
: int) -> str:
1093 if unit
== self
.NONE
:
1095 elif unit
== self
.BYTES
:
1098 raise ValueError(f
'bad unit "{unit}"')
1101 def to_pretty_iec(n
: int) -> str:
1102 for bits
, suffix
in [(60, 'Ei'), (50, 'Pi'), (40, 'Ti'), (30, 'Gi'),
1103 (20, 'Mi'), (10, 'Ki')]:
1105 return str(n
>> bits
) + ' ' + suffix
1109 def get_pretty_row(elems
: Sequence
[str], width
: int) -> str:
1111 Takes an array of elements and returns a string with those elements
1112 formatted as a table row. Useful for polling modules.
1114 :param elems: the elements to be printed
1115 :param width: the width of the terminal
1118 column_width
= int(width
/ n
)
1122 ret
+= '{0:>{w}} |'.format(elem
, w
=column_width
- 2)
1126 def get_pretty_header(self
, elems
: Sequence
[str], width
: int) -> str:
1128 Like ``get_pretty_row`` but adds dashes, to be used as a table title.
1130 :param elems: the elements to be printed
1131 :param width: the width of the terminal
1134 column_width
= int(width
/ n
)
1138 for i
in range(0, n
):
1139 ret
+= '-' * (column_width
- 1) + '+'
1143 ret
+= self
.get_pretty_row(elems
, width
)
1148 for i
in range(0, n
):
1149 ret
+= '-' * (column_width
- 1) + '+'
1154 def get_server(self
, hostname
) -> ServerInfoT
:
1156 Called by the plugin to fetch metadata about a particular hostname from
1159 This is information that ceph-mgr has gleaned from the daemon metadata
1160 reported by daemons running on a particular server.
1162 :param hostname: a hostname
1164 return cast(ServerInfoT
, self
._ceph
_get
_server
(hostname
))
1166 def get_perf_schema(self
,
1168 svc_name
: str) -> Dict
[str,
1169 Dict
[str, Dict
[str, Union
[str, int]]]]:
1171 Called by the plugin to fetch perf counter schema info.
1172 svc_name can be nullptr, as can svc_type, in which case
1175 :param str svc_type:
1176 :param str svc_name:
1177 :return: list of dicts describing the counters requested
1179 return self
._ceph
_get
_perf
_schema
(svc_type
, svc_name
)
1181 def get_counter(self
,
1184 path
: str) -> Dict
[str, List
[Tuple
[float, int]]]:
1186 Called by the plugin to fetch the latest performance counter data for a
1187 particular counter on a particular service.
1189 :param str svc_type:
1190 :param str svc_name:
1191 :param str path: a period-separated concatenation of the subsystem and the
1192 counter name, for example "mds.inodes".
1193 :return: A dict of counter names to their values. each value is a list of
1194 of two-tuples of (timestamp, value). This may be empty if no data is
1197 return self
._ceph
_get
_counter
(svc_type
, svc_name
, path
)
1199 def get_latest_counter(self
,
1202 path
: str) -> Dict
[str, Union
[Tuple
[float, int],
1203 Tuple
[float, int, int]]]:
1205 Called by the plugin to fetch only the newest performance counter data
1206 point for a particular counter on a particular service.
1208 :param str svc_type:
1209 :param str svc_name:
1210 :param str path: a period-separated concatenation of the subsystem and the
1211 counter name, for example "mds.inodes".
1212 :return: A list of two-tuples of (timestamp, value) or three-tuple of
1213 (timestamp, value, count) is returned. This may be empty if no
1216 return self
._ceph
_get
_latest
_counter
(svc_type
, svc_name
, path
)
1218 def list_servers(self
) -> List
[ServerInfoT
]:
1220 Like ``get_server``, but gives information about all servers (i.e. all
1221 unique hostnames that have been mentioned in daemon metadata)
1223 :return: a list of information about all servers
1226 return cast(List
[ServerInfoT
], self
._ceph
_get
_server
(None))
1228 def get_metadata(self
,
1231 default
: Optional
[Dict
[str, str]] = None) -> Optional
[Dict
[str, str]]:
1233 Fetch the daemon metadata for a particular service.
1235 ceph-mgr fetches metadata asynchronously, so are windows of time during
1236 addition/removal of services where the metadata is not available to
1237 modules. ``None`` is returned if no metadata is available.
1239 :param str svc_type: service type (e.g., 'mds', 'osd', 'mon')
1240 :param str svc_id: service id. convert OSD integer IDs to strings when
1242 :rtype: dict, or None if no metadata found
1244 metadata
= self
._ceph
_get
_metadata
(svc_type
, svc_id
)
1245 if metadata
is None:
1249 def get_daemon_status(self
, svc_type
: str, svc_id
: str) -> Dict
[str, str]:
1251 Fetch the latest status for a particular service daemon.
1253 This method may return ``None`` if no status information is
1254 available, for example because the daemon hasn't fully started yet.
1256 :param svc_type: string (e.g., 'rgw')
1257 :param svc_id: string
1258 :return: dict, or None if the service is not found
1260 return self
._ceph
_get
_daemon
_status
(svc_type
, svc_id
)
1262 def check_mon_command(self
, cmd_dict
: dict, inbuf
: Optional
[str] = None) -> HandleCommandResult
:
1264 Wrapper around :func:`~mgr_module.MgrModule.mon_command`, but raises,
1268 r
= HandleCommandResult(*self
.mon_command(cmd_dict
, inbuf
))
1270 raise MonCommandFailed(f
'{cmd_dict["prefix"]} failed: {r.stderr} retval: {r.retval}')
1273 def mon_command(self
, cmd_dict
: dict, inbuf
: Optional
[str] = None) -> Tuple
[int, str, str]:
1275 Helper for modules that do simple, synchronous mon command
1278 See send_command for general case.
1280 :return: status int, out std, err str
1284 result
= CommandResult()
1285 self
.send_command(result
, "mon", "", json
.dumps(cmd_dict
), "", inbuf
)
1289 self
.log
.debug("mon_command: '{0}' -> {1} in {2:.3f}s".format(
1290 cmd_dict
['prefix'], r
[0], t2
- t1
1297 result
: CommandResult
,
1302 inbuf
: Optional
[str] = None) -> None:
1304 Called by the plugin to send a command to the mon
1307 :param CommandResult result: an instance of the ``CommandResult``
1308 class, defined in the same module as MgrModule. This acts as a
1309 completion and stores the output of the command. Use
1310 ``CommandResult.wait()`` if you want to block on completion.
1311 :param str svc_type:
1313 :param str command: a JSON-serialized command. This uses the same
1314 format as the ceph command line, which is a dictionary of command
1315 arguments, with the extra ``prefix`` key containing the command
1316 name itself. Consult MonCommands.h for available commands and
1317 their expected arguments.
1318 :param str tag: used for nonblocking operation: when a command
1319 completes, the ``notify()`` callback on the MgrModule instance is
1320 triggered, with notify_type set to "command", and notify_id set to
1321 the tag of the command.
1322 :param str inbuf: input buffer for sending additional data.
1324 self
._ceph
_send
_command
(result
, svc_type
, svc_id
, command
, tag
, inbuf
)
1330 stdin
: Optional
[bytes
] = None
1331 ) -> Tuple
[int, str, str]:
1336 '-k', str(self
.get_ceph_option('keyring')),
1337 '-n', f
'mgr.{self.get_mgr_id()}',
1339 self
.log
.debug('exec: ' + ' '.join(cmd
))
1343 stdout
=subprocess
.PIPE
,
1344 stderr
=subprocess
.PIPE
,
1347 except subprocess
.TimeoutExpired
as ex
:
1349 return -errno
.ETIMEDOUT
, '', str(ex
)
1351 self
.log
.error(f
'Non-zero return from {cmd}: {p.stderr.decode()}')
1352 return p
.returncode
, p
.stdout
.decode(), p
.stderr
.decode()
1354 def set_health_checks(self
, checks
: HealthChecksT
) -> None:
1356 Set the module's current map of health checks. Argument is a
1357 dict of check names to info, in this form:
1363 'severity': 'warning', # or 'error'
1364 'summary': 'summary string',
1365 'count': 4, # quantify badness
1366 'detail': [ 'list', 'of', 'detail', 'strings' ],
1369 'severity': 'error',
1370 'summary': 'bars are bad',
1371 'detail': [ 'too hard' ],
1375 :param list: dict of health check dicts
1377 self
._ceph
_set
_health
_checks
(checks
)
1379 def _handle_command(self
,
1381 cmd
: Dict
[str, Any
]) -> Union
[HandleCommandResult
,
1382 Tuple
[int, str, str]]:
1383 if cmd
['prefix'] not in CLICommand
.COMMANDS
:
1384 return self
.handle_command(inbuf
, cmd
)
1386 return CLICommand
.COMMANDS
[cmd
['prefix']].call(self
, cmd
, inbuf
)
1388 def handle_command(self
,
1390 cmd
: Dict
[str, Any
]) -> Union
[HandleCommandResult
,
1391 Tuple
[int, str, str]]:
1393 Called by ceph-mgr to request the plugin to handle one
1394 of the commands that it declared in self.COMMANDS
1396 Return a status code, an output buffer, and an
1397 output string. The output buffer is for data results,
1398 the output string is for informative text.
1400 :param inbuf: content of any "-i <file>" supplied to ceph cli
1402 :param cmd: from Ceph's cmdmap_t
1405 :return: HandleCommandResult or a 3-tuple of (int, str, str)
1408 # Should never get called if they didn't declare
1410 raise NotImplementedError()
1412 def get_mgr_id(self
) -> str:
1414 Retrieve the name of the manager daemon where this plugin
1415 is currently being executed (i.e. the active manager).
1419 return self
._ceph
_get
_mgr
_id
()
1421 def get_ceph_conf_path(self
) -> str:
1422 return self
._ceph
_get
_ceph
_conf
_path
()
1424 def get_mgr_ip(self
) -> str:
1425 ips
= self
.get("mgr_ips").get('ips', [])
1427 return socket
.gethostname()
1430 def get_ceph_option(self
, key
: str) -> OptionValue
:
1431 return self
._ceph
_get
_option
(key
)
1433 def get_foreign_ceph_option(self
, entity
: str, key
: str) -> OptionValue
:
1434 return self
._ceph
_get
_foreign
_option
(entity
, key
)
1436 def _validate_module_option(self
, key
: str) -> None:
1438 Helper: don't allow get/set config callers to
1439 access config options that they didn't declare
1442 if key
not in [o
['name'] for o
in self
.MODULE_OPTIONS
]:
1443 raise RuntimeError("Config option '{0}' is not in {1}.MODULE_OPTIONS".
1444 format(key
, self
.__class
__.__name
__))
1446 def _get_module_option(self
,
1448 default
: OptionValue
,
1449 localized_prefix
: str = "") -> OptionValue
:
1450 r
= self
._ceph
_get
_module
_option
(self
.module_name
, key
,
1453 return self
.MODULE_OPTION_DEFAULTS
.get(key
, default
)
1457 def get_module_option(self
, key
: str, default
: OptionValue
= None) -> OptionValue
:
1459 Retrieve the value of a persistent configuration setting
1461 self
._validate
_module
_option
(key
)
1462 return self
._get
_module
_option
(key
, default
)
1464 def get_module_option_ex(self
, module
: str,
1466 default
: OptionValue
= None) -> OptionValue
:
1468 Retrieve the value of a persistent configuration setting
1469 for the specified module.
1471 :param module: The name of the module, e.g. 'dashboard'
1473 :param key: The configuration key, e.g. 'server_addr'.
1474 :param default: The default value to use when the
1475 returned value is ``None``. Defaults to ``None``.
1477 if module
== self
.module_name
:
1478 self
._validate
_module
_option
(key
)
1479 r
= self
._ceph
_get
_module
_option
(module
, key
)
1480 return default
if r
is None else r
1482 def get_store_prefix(self
, key_prefix
: str) -> Dict
[str, str]:
1484 Retrieve a dict of KV store keys to values, where the keys
1485 have the given prefix
1487 :param str key_prefix:
1490 return self
._ceph
_get
_store
_prefix
(key_prefix
)
1492 def _set_localized(self
,
1495 setter
: Callable
[[str, Optional
[str]], None]) -> None:
1496 return setter(_get_localized_key(self
.get_mgr_id(), key
), val
)
1498 def get_localized_module_option(self
, key
: str, default
: OptionValue
= None) -> OptionValue
:
1500 Retrieve localized configuration for this ceph-mgr instance
1502 self
._validate
_module
_option
(key
)
1503 return self
._get
_module
_option
(key
, default
, self
.get_mgr_id())
1505 def _set_module_option(self
, key
: str, val
: Any
) -> None:
1506 return self
._ceph
_set
_module
_option
(self
.module_name
, key
,
1507 None if val
is None else str(val
))
1509 def set_module_option(self
, key
: str, val
: Any
) -> None:
1511 Set the value of a persistent configuration setting
1514 :type val: str | None
1516 self
._validate
_module
_option
(key
)
1517 return self
._set
_module
_option
(key
, val
)
1519 def set_module_option_ex(self
, module
: str, key
: str, val
: OptionValue
) -> None:
1521 Set the value of a persistent configuration setting
1522 for the specified module.
1528 if module
== self
.module_name
:
1529 self
._validate
_module
_option
(key
)
1530 return self
._ceph
_set
_module
_option
(module
, key
, str(val
))
1532 def set_localized_module_option(self
, key
: str, val
: Optional
[str]) -> None:
1534 Set localized configuration for this ceph-mgr instance
1539 self
._validate
_module
_option
(key
)
1540 return self
._set
_localized
(key
, val
, self
._set
_module
_option
)
1542 def set_store(self
, key
: str, val
: Optional
[str]) -> None:
1544 Set a value in this module's persistent key value store.
1545 If val is None, remove key from store
1547 self
._ceph
_set
_store
(key
, val
)
1549 def get_store(self
, key
: str, default
: Optional
[str] = None) -> Optional
[str]:
1551 Get a value from this module's persistent key value store
1553 r
= self
._ceph
_get
_store
(key
)
1559 def get_localized_store(self
, key
: str, default
: Optional
[str] = None) -> Optional
[str]:
1560 r
= self
._ceph
_get
_store
(_get_localized_key(self
.get_mgr_id(), key
))
1562 r
= self
._ceph
_get
_store
(key
)
1567 def set_localized_store(self
, key
: str, val
: Optional
[str]) -> None:
1568 return self
._set
_localized
(key
, val
, self
.set_store
)
1570 def self_test(self
) -> None:
1572 Run a self-test on the module. Override this function and implement
1573 a best as possible self-test for (automated) testing of the module
1575 Indicate any failures by raising an exception. This does not have
1576 to be pretty, it's mainly for picking up regressions during
1577 development, rather than use in the field.
1579 :return: None, or an advisory string for developer interest, such
1580 as a json dump of some state.
1584 def get_osdmap(self
) -> OSDMap
:
1586 Get a handle to an OSDMap. If epoch==0, get a handle for the latest
1590 return cast(OSDMap
, self
._ceph
_get
_osdmap
())
1592 def get_latest(self
, daemon_type
: str, daemon_name
: str, counter
: str) -> int:
1593 data
= self
.get_latest_counter(
1594 daemon_type
, daemon_name
, counter
)[counter
]
1600 def get_latest_avg(self
, daemon_type
: str, daemon_name
: str, counter
: str) -> Tuple
[int, int]:
1601 data
= self
.get_latest_counter(
1602 daemon_type
, daemon_name
, counter
)[counter
]
1604 # https://github.com/python/mypy/issues/1178
1605 _
, value
, count
= cast(Tuple
[float, int, int], data
)
1611 def get_all_perf_counters(self
, prio_limit
: int = PRIO_USEFUL
,
1612 services
: Sequence
[str] = ("mds", "mon", "osd",
1613 "rbd-mirror", "rgw",
1614 "tcmu-runner")) -> Dict
[str, dict]:
1616 Return the perf counters currently known to this ceph-mgr
1617 instance, filtered by priority equal to or greater than `prio_limit`.
1619 The result is a map of string to dict, associating services
1620 (like "osd.123") with their counters. The counter
1621 dict for each service maps counter paths to a counter
1622 info structure, which is the information from
1623 the schema, plus an additional "value" member with the latest
1627 result
= defaultdict(dict) # type: Dict[str, dict]
1629 for server
in self
.list_servers():
1630 for service
in cast(List
[ServiceInfoT
], server
['services']):
1631 if service
['type'] not in services
:
1634 schemas
= self
.get_perf_schema(service
['type'], service
['id'])
1636 self
.log
.warning("No perf counter schema for {0}.{1}".format(
1637 service
['type'], service
['id']
1641 # Value is returned in a potentially-multi-service format,
1642 # get just the service we're asking about
1643 svc_full_name
= "{0}.{1}".format(
1644 service
['type'], service
['id'])
1645 schema
= schemas
[svc_full_name
]
1647 # Populate latest values
1648 for counter_path
, counter_schema
in schema
.items():
1649 # self.log.debug("{0}: {1}".format(
1650 # counter_path, json.dumps(counter_schema)
1652 priority
= counter_schema
['priority']
1653 assert isinstance(priority
, int)
1654 if priority
< prio_limit
:
1657 tp
= counter_schema
['type']
1658 assert isinstance(tp
, int)
1659 counter_info
= dict(counter_schema
)
1660 # Also populate count for the long running avgs
1661 if tp
& self
.PERFCOUNTER_LONGRUNAVG
:
1662 v
, c
= self
.get_latest_avg(
1667 counter_info
['value'], counter_info
['count'] = v
, c
1668 result
[svc_full_name
][counter_path
] = counter_info
1670 counter_info
['value'] = self
.get_latest(
1676 result
[svc_full_name
][counter_path
] = counter_info
1678 self
.log
.debug("returning {0} counter".format(len(result
)))
1682 def set_uri(self
, uri
: str) -> None:
1684 If the module exposes a service, then call this to publish the
1685 address once it is available.
1689 return self
._ceph
_set
_uri
(uri
)
1691 def set_device_wear_level(self
, devid
: str, wear_level
: float) -> None:
1692 return self
._ceph
_set
_device
_wear
_level
(devid
, wear_level
)
1694 def have_mon_connection(self
) -> bool:
1696 Check whether this ceph-mgr daemon has an open connection
1697 to a monitor. If it doesn't, then it's likely that the
1698 information we have about the cluster is out of date,
1699 and/or the monitor cluster is down.
1702 return self
._ceph
_have
_mon
_connection
()
1704 def update_progress_event(self
,
1708 add_to_ceph_s
: bool) -> None:
1709 return self
._ceph
_update
_progress
_event
(evid
, desc
, progress
, add_to_ceph_s
)
1711 def complete_progress_event(self
, evid
: str) -> None:
1712 return self
._ceph
_complete
_progress
_event
(evid
)
1714 def clear_all_progress_events(self
) -> None:
1715 return self
._ceph
_clear
_all
_progress
_events
()
1718 def rados(self
) -> rados
.Rados
:
1720 A librados instance to be shared by any classes within
1721 this mgr module that want one.
1726 ctx_capsule
= self
.get_context()
1727 self
._rados
= rados
.Rados(context
=ctx_capsule
)
1728 self
._rados
.connect()
1729 self
._ceph
_register
_client
(self
._rados
.get_addrs())
1733 def can_run() -> Tuple
[bool, str]:
1735 Implement this function to report whether the module's dependencies
1736 are met. For example, if the module needs to import a particular
1737 dependency to work, then use a try/except around the import at
1738 file scope, and then report here if the import failed.
1740 This will be called in a blocking way from the C++ code, so do not
1741 do any I/O that could block in this function.
1743 :return a 2-tuple consisting of a boolean and explanatory string
1748 def remote(self
, module_name
: str, method_name
: str, *args
: Any
, **kwargs
: Any
) -> Any
:
1750 Invoke a method on another module. All arguments, and the return
1751 value from the other module must be serializable.
1753 Limitation: Do not import any modules within the called method.
1754 Otherwise you will get an error in Python 2::
1756 RuntimeError('cannot unmarshal code objects in restricted execution mode',)
1760 :param module_name: Name of other module. If module isn't loaded,
1761 an ImportError exception is raised.
1762 :param method_name: Method name. If it does not exist, a NameError
1763 exception is raised.
1764 :param args: Argument tuple
1765 :param kwargs: Keyword argument dict
1766 :raises RuntimeError: **Any** error raised within the method is converted to a RuntimeError
1767 :raises ImportError: No such module
1769 return self
._ceph
_dispatch
_remote
(module_name
, method_name
,
1772 def add_osd_perf_query(self
, query
: Dict
[str, Any
]) -> Optional
[int]:
1774 Register an OSD perf query. Argument is a
1775 dict of the query parameters, in this form:
1781 {'type': subkey_type, 'regex': regex_pattern},
1784 'performance_counter_descriptors': [
1785 list, of, descriptor, types
1787 'limit': {'order_by': performance_counter_type, 'max_count': n},
1791 'client_id', 'client_address', 'pool_id', 'namespace', 'osd_id',
1792 'pg_id', 'object_name', 'snap_id'
1793 Valid performance counter types:
1794 'ops', 'write_ops', 'read_ops', 'bytes', 'write_bytes', 'read_bytes',
1795 'latency', 'write_latency', 'read_latency'
1797 :param object query: query
1798 :rtype: int (query id)
1800 return self
._ceph
_add
_osd
_perf
_query
(query
)
1802 def remove_osd_perf_query(self
, query_id
: int) -> None:
1804 Unregister an OSD perf query.
1806 :param int query_id: query ID
1808 return self
._ceph
_remove
_osd
_perf
_query
(query_id
)
1810 def get_osd_perf_counters(self
, query_id
: int) -> Optional
[Dict
[str, List
[PerfCounterT
]]]:
1812 Get stats collected for an OSD perf query.
1814 :param int query_id: query ID
1816 return self
._ceph
_get
_osd
_perf
_counters
(query_id
)
1818 def add_mds_perf_query(self
, query
: Dict
[str, Any
]) -> Optional
[int]:
1820 Register an MDS perf query. Argument is a
1821 dict of the query parameters, in this form:
1827 {'type': subkey_type, 'regex': regex_pattern},
1830 'performance_counter_descriptors': [
1831 list, of, descriptor, types
1835 NOTE: 'limit' and 'order_by' are not supported (yet).
1838 'mds_rank', 'client_id'
1839 Valid performance counter types:
1842 :param object query: query
1843 :rtype: int (query id)
1845 return self
._ceph
_add
_mds
_perf
_query
(query
)
1847 def remove_mds_perf_query(self
, query_id
: int) -> None:
1849 Unregister an MDS perf query.
1851 :param int query_id: query ID
1853 return self
._ceph
_remove
_mds
_perf
_query
(query_id
)
1855 def get_mds_perf_counters(self
, query_id
: int) -> Optional
[Dict
[str, List
[PerfCounterT
]]]:
1857 Get stats collected for an MDS perf query.
1859 :param int query_id: query ID
1861 return self
._ceph
_get
_mds
_perf
_counters
(query_id
)
1863 def is_authorized(self
, arguments
: Dict
[str, str]) -> bool:
1865 Verifies that the current session caps permit executing the py service
1866 or current module with the provided arguments. This provides a generic
1867 way to allow modules to restrict by more fine-grained controls (e.g.
1870 :param arguments: dict of key/value arguments to test
1872 return self
._ceph
_is
_authorized
(arguments
)
1874 def send_rgwadmin_command(self
, args
: List
[str],
1875 stdout_as_json
: bool = True) -> Tuple
[int, Union
[str, dict], str]:
1879 '-c', str(self
.get_ceph_conf_path()),
1880 '-k', str(self
.get_ceph_option('keyring')),
1881 '-n', f
'mgr.{self.get_mgr_id()}',
1883 self
.log
.debug('Executing %s', str(cmd
))
1884 result
= subprocess
.run( # pylint: disable=subprocess-run-check
1886 stdout
=subprocess
.PIPE
,
1887 stderr
=subprocess
.PIPE
,
1890 stdout
= result
.stdout
.decode('utf-8')
1891 stderr
= result
.stderr
.decode('utf-8')
1892 if stdout
and stdout_as_json
:
1893 stdout
= json
.loads(stdout
)
1894 if result
.returncode
:
1895 self
.log
.debug('Error %s executing %s: %s', result
.returncode
, str(cmd
), stderr
)
1896 return result
.returncode
, stdout
, stderr
1897 except subprocess
.CalledProcessError
as ex
:
1898 self
.log
.exception('Error executing radosgw-admin %s: %s', str(ex
.cmd
), str(ex
.output
))
1900 except subprocess
.TimeoutExpired
as ex
:
1901 self
.log
.error('Timeout (10s) executing radosgw-admin %s', str(ex
.cmd
))