From: Oto Šťáva Date: Mon, 18 Sep 2023 13:53:40 +0000 (+0200) Subject: manager: add more verbose PIDs listing X-Git-Tag: v6.0.10~9^2~6 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1d772a780a61cc3ecde5d2e0d165f74e6cae2d71;p=thirdparty%2Fknot-resolver.git manager: add more verbose PIDs listing --- diff --git a/python/knot_resolver/client/commands/debug.py b/python/knot_resolver/client/commands/debug.py index 0d1129277..44ab33ebd 100644 --- a/python/knot_resolver/client/commands/debug.py +++ b/python/knot_resolver/client/commands/debug.py @@ -8,7 +8,7 @@ from knot_resolver.client.command import Command, CommandArgs, CompWords, regist from knot_resolver.utils import which from knot_resolver.utils.requests import request -PIDS_TYPE = List +PROCS_TYPE = List @register_command @@ -57,19 +57,19 @@ class DebugCommand(Command): gdb_cmd = str(which.which(self.gdb)) sudo_cmd = str(which.which("sudo")) - response = request(args.socket, "GET", f"pids/{self.proc_type}") + response = request(args.socket, "GET", f"processes/{self.proc_type}") if response.status != 200: print(response, file=sys.stderr) sys.exit(1) - pids = json.loads(response.body) - if not isinstance(pids, PIDS_TYPE): + procs = json.loads(response.body) + if not isinstance(procs, PROCS_TYPE): print( - f"Unexpected response type '{type(pids).__name__}' from manager. Expected '{PIDS_TYPE.__name__}'", + f"Unexpected response type '{type(procs).__name__}' from manager. Expected '{PROCS_TYPE.__name__}'", file=sys.stderr, ) sys.exit(1) - if len(pids) == 0: + if len(procs) == 0: print( f"There are no processes of type '{self.proc_type}' available to debug", file=sys.stderr, @@ -81,12 +81,12 @@ class DebugCommand(Command): exec_args.extend([sudo_cmd, "--"]) # attach to PIDs - exec_args.extend([gdb_cmd, "--pid", str(pids[0])]) + exec_args.extend([gdb_cmd, "--pid", str(procs[0]["pid"])]) inferior = 2 - for pid in pids[1:]: + for proc in procs[1:]: exec_args.extend(["-init-eval-command", "add-inferior"]) exec_args.extend(["-init-eval-command", f"inferior {inferior}"]) - exec_args.extend(["-init-eval-command", f"attach {pid}"]) + exec_args.extend(["-init-eval-command", f'attach {proc["pid"]}']) inferior += 1 exec_args.extend(["-init-eval-command", "inferior 1"]) diff --git a/python/knot_resolver/client/commands/pids.py b/python/knot_resolver/client/commands/pids.py index c14c734e8..2a7a5e85f 100644 --- a/python/knot_resolver/client/commands/pids.py +++ b/python/knot_resolver/client/commands/pids.py @@ -6,13 +6,14 @@ from typing import Iterable, List, Optional, Tuple, Type from knot_resolver.client.command import Command, CommandArgs, CompWords, register_command from knot_resolver.utils.requests import request -PIDS_TYPE = Iterable +PROCESSES_TYPE = Iterable @register_command class PidsCommand(Command): def __init__(self, namespace: argparse.Namespace) -> None: self.proc_type: Optional[str] = namespace.proc_type + self.verbose: int = namespace.verbose super().__init__(namespace) @@ -27,6 +28,13 @@ class PidsCommand(Command): nargs="?", default="all", ) + pids.add_argument( + "-v", + "--verbose", + help="Optional, makes the output more verbose, in a machine-readable format.", + action="count", + default=0, + ) return pids, PidsCommand @staticmethod @@ -34,16 +42,21 @@ class PidsCommand(Command): return {} def run(self, args: CommandArgs) -> None: - response = request(args.socket, "GET", f"pids/{self.proc_type}") + response = request(args.socket, "GET", f"processes/{self.proc_type}") if response.status == 200: - pids = json.loads(response.body) - if isinstance(pids, PIDS_TYPE): - for pid in pids: - print(pid) + processes = json.loads(response.body) + if isinstance(processes, PROCESSES_TYPE): + if self.verbose < 1: + for p in processes: + print(p["pid"]) + else: + for p in processes: + print(p) + else: print( - f"Unexpected response type '{type(pids).__name__}' from manager. Expected '{PIDS_TYPE.__name__}'", + f"Unexpected response type '{type(processes).__name__}' from manager. Expected '{PROCESSES_TYPE.__name__}'", file=sys.stderr, ) sys.exit(1) diff --git a/python/knot_resolver/manager/manager.py b/python/knot_resolver/manager/manager.py index aef7ae855..a26bb8061 100644 --- a/python/knot_resolver/manager/manager.py +++ b/python/knot_resolver/manager/manager.py @@ -55,6 +55,13 @@ async def _deny_max_worker_changes(config_old: KresConfig, config_new: KresConfi return Result.ok(None) +async def _subprocess_desc(subprocess: Subprocess) -> object: + return { + "type": subprocess.type.name, + "pid": await subprocess.get_pid(), + } + + class KresManager: # pylint: disable=too-many-instance-attributes """ Core of the whole operation. Orchestrates individual instances under some @@ -235,9 +242,9 @@ class KresManager: # pylint: disable=too-many-instance-attributes logger.debug("Canary process test passed.") return Result.ok(None) - async def get_pids(self, proc_type: Optional[SubprocessType]) -> List[int]: + async def get_processes(self, proc_type: Optional[SubprocessType]) -> List[object]: processes = await self._controller.get_all_running_instances() - return [await pr.get_pid() for pr in processes if proc_type is None or pr.type == proc_type] + return [await _subprocess_desc(pr) for pr in processes if proc_type is None or pr.type == proc_type] async def _reload_system_state(self) -> None: async with self._manager_lock: diff --git a/python/knot_resolver/manager/server.py b/python/knot_resolver/manager/server.py index 5e0e484ed..06fab0cfc 100644 --- a/python/knot_resolver/manager/server.py +++ b/python/knot_resolver/manager/server.py @@ -325,7 +325,7 @@ class Server: await self._reload_config() return web.Response(text="Reloading...") - async def _handler_pids(self, request: web.Request) -> web.Response: + async def _handler_processes(self, request: web.Request) -> web.Response: """ Route handler for listing PIDs of subprocesses """ @@ -344,7 +344,7 @@ class Server: return web.Response(text=f"Invalid process type '{ptstr}'", status=400) return web.json_response( - await self._manager.get_pids(proc_type), + await self._manager.get_processes(proc_type), headers={"Access-Control-Allow-Origin": "*"}, dumps=partial(json.dumps, indent=4), ) @@ -365,7 +365,7 @@ class Server: web.get("/metrics/json", self._handler_metrics_json), web.get("/metrics/prometheus", self._handler_metrics_prometheus), web.post("/cache/clear", self._handler_cache_clear), - web.get("/pids{path:.*}", self._handler_pids), + web.get("/processes{path:.*}", self._handler_processes), ] )