Skip to content

Commit 1834881

Browse files
cephadm: add parsed_container_cpu_perc to container_engines
Add a new function that combines the call and parse operations that exist in cephadm.py (currently _parse_cpu_perc and related command calls). This will be used in a future commit to replace that code and reduce the size of cephadm.py. Signed-off-by: John Mulligan <[email protected]>
1 parent bd93a46 commit 1834881

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

src/cephadm/cephadmlib/container_engines.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,3 +248,57 @@ def parsed_container_mem_usage(
248248
ctx, container_path=container_path, verbosity=verbosity
249249
)
250250
return _parse_mem_usage(code, out)
251+
252+
253+
def _container_cpu_perc(
254+
ctx: CephadmContext,
255+
*,
256+
container_path: str = '',
257+
verbosity: CallVerbosity = CallVerbosity.QUIET,
258+
) -> Tuple[str, str, int]:
259+
container_path = container_path or ctx.container_engine.path
260+
out, err, code = call(
261+
ctx,
262+
[
263+
container_path,
264+
'stats',
265+
'--format',
266+
'{{.ID}},{{.CPUPerc}}',
267+
'--no-stream',
268+
],
269+
verbosity=CallVerbosity.QUIET,
270+
)
271+
return out, err, code
272+
273+
274+
def _parse_cpu_perc(code: int, out: str) -> Tuple[int, Dict[str, str]]:
275+
seen_cpuperc = {}
276+
seen_cpuperc_cid_len = 0
277+
if not code:
278+
for line in out.splitlines():
279+
(cid, cpuperc) = line.split(',')
280+
try:
281+
seen_cpuperc[cid] = cpuperc
282+
if not seen_cpuperc_cid_len:
283+
seen_cpuperc_cid_len = len(cid)
284+
except ValueError:
285+
logger.info(
286+
'unable to parse cpu percentage line\n>{}'.format(line)
287+
)
288+
pass
289+
return seen_cpuperc_cid_len, seen_cpuperc
290+
291+
292+
def parsed_container_cpu_perc(
293+
ctx: CephadmContext,
294+
*,
295+
container_path: str = '',
296+
verbosity: CallVerbosity = CallVerbosity.QUIET,
297+
) -> Tuple[int, Dict[str, str]]:
298+
"""Return cpu percentage used values parsed from the container engine's
299+
container status.
300+
"""
301+
out, _, code = _container_cpu_perc(
302+
ctx, container_path=container_path, verbosity=verbosity
303+
)
304+
return _parse_cpu_perc(code, out)

0 commit comments

Comments
 (0)