From 5a9efdd4827e062f7bbf1c14c84ff46d7a00f02f Mon Sep 17 00:00:00 2001 From: Carsten Date: Wed, 22 Apr 2026 22:22:31 +0200 Subject: [PATCH] feat(agent): log every shell command to Diagnostics --- agent/lib/proxmox_agent/shell.ex | 8 ++++++++ agent/test/proxmox_agent/shell_test.exs | 23 ++++++++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/agent/lib/proxmox_agent/shell.ex b/agent/lib/proxmox_agent/shell.ex index abb84be..6ab3bcb 100644 --- a/agent/lib/proxmox_agent/shell.ex +++ b/agent/lib/proxmox_agent/shell.ex @@ -8,6 +8,14 @@ defmodule ProxmoxAgent.Shell do @spec run(String.t(), [String.t()]) :: result def run(command, args) do + start = System.monotonic_time(:microsecond) + result = do_run(command, args) + duration_us = System.monotonic_time(:microsecond) - start + ProxmoxAgent.Diagnostics.log_command(command, args, result, duration_us) + result + end + + defp do_run(command, args) do try do case System.cmd(command, args, stderr_to_stdout: true) do {output, 0} -> {:ok, output} diff --git a/agent/test/proxmox_agent/shell_test.exs b/agent/test/proxmox_agent/shell_test.exs index ea9cab0..d649f40 100644 --- a/agent/test/proxmox_agent/shell_test.exs +++ b/agent/test/proxmox_agent/shell_test.exs @@ -1,5 +1,5 @@ defmodule ProxmoxAgent.ShellTest do - use ExUnit.Case, async: true + use ExUnit.Case, async: false alias ProxmoxAgent.Shell @@ -16,4 +16,25 @@ defmodule ProxmoxAgent.ShellTest do test "run/2 returns {:error, {:enoent, _}} when binary is missing" do assert {:error, {:enoent, _}} = Shell.run("/does/not/exist/nope", []) end + + test "run/2 logs to Diagnostics when dump_dir is set" do + dir = Path.join(System.tmp_dir!(), "shell-dump-#{System.unique_integer([:positive])}") + File.mkdir_p!(dir) + :ok = ProxmoxAgent.Diagnostics.configure(dir) + {:ok, writer} = ProxmoxAgent.Diagnostics.Writer.start_link(dir: dir) + + on_exit(fn -> + if Process.alive?(writer), do: GenServer.stop(writer) + Application.delete_env(:agent, :dump_dir) + File.rm_rf(dir) + end) + + assert {:ok, _} = Shell.run("/bin/echo", ["diagnostics-test"]) + :ok = GenServer.call(ProxmoxAgent.Diagnostics.Writer, :flush) + + body = File.read!(Path.join(dir, "commands.log")) + assert body =~ "$ /bin/echo diagnostics-test" + assert body =~ "exit=0" + assert body =~ "diagnostics-test" + end end