feat(agent): add [debug] dump_dir config field

This commit is contained in:
Carsten 2026-04-22 22:15:01 +02:00
parent b798b462ca
commit 1c289a5a0d
3 changed files with 49 additions and 0 deletions

View file

@ -5,6 +5,7 @@ defmodule ProxmoxAgent.Config do
:server_url,
:token,
:host_id,
:dump_dir,
fast_seconds: 30,
medium_seconds: 300,
slow_seconds: 1800
@ -14,6 +15,7 @@ defmodule ProxmoxAgent.Config do
server_url: String.t(),
token: String.t(),
host_id: String.t(),
dump_dir: String.t() | nil,
fast_seconds: pos_integer(),
medium_seconds: pos_integer(),
slow_seconds: pos_integer()
@ -57,17 +59,24 @@ defmodule ProxmoxAgent.Config do
defp build(map) do
intervals = Map.get(map, "intervals", %{})
debug = Map.get(map, "debug", %{})
%__MODULE__{
server_url: map["server_url"],
token: map["token"],
host_id: map["host_id"] || hostname(),
dump_dir: normalize_dump_dir(Map.get(debug, "dump_dir")),
fast_seconds: Map.get(intervals, "fast_seconds", 30),
medium_seconds: Map.get(intervals, "medium_seconds", 300),
slow_seconds: Map.get(intervals, "slow_seconds", 1800)
}
end
defp normalize_dump_dir(nil), do: nil
defp normalize_dump_dir(""), do: nil
defp normalize_dump_dir(s) when is_binary(s), do: s
defp normalize_dump_dir(_), do: nil
defp hostname do
case :inet.gethostname() do
{:ok, name} -> List.to_string(name)

View file

@ -6,3 +6,6 @@ host_id = "pve-test-01"
fast_seconds = 15
medium_seconds = 120
slow_seconds = 600
[debug]
dump_dir = "/tmp/proxmox-monitor-test"

View file

@ -14,6 +14,43 @@ defmodule ProxmoxAgent.ConfigTest do
assert cfg.fast_seconds == 15
assert cfg.medium_seconds == 120
assert cfg.slow_seconds == 600
assert cfg.dump_dir == "/tmp/proxmox-monitor-test"
end
test "parses [debug] dump_dir when present" do
assert {:ok, cfg} = Config.load(@fixture)
assert cfg.dump_dir == "/tmp/proxmox-monitor-test"
end
test "dump_dir is nil when [debug] is absent" do
tmp = Path.join(System.tmp_dir!(), "agent_nodebug.toml")
File.write!(tmp, """
server_url = "wss://x/socket/websocket"
token = "t"
""")
on_exit(fn -> File.rm(tmp) end)
assert {:ok, cfg} = Config.load(tmp)
assert cfg.dump_dir == nil
end
test "dump_dir is nil when set to empty string" do
tmp = Path.join(System.tmp_dir!(), "agent_emptydebug.toml")
File.write!(tmp, """
server_url = "wss://x/socket/websocket"
token = "t"
[debug]
dump_dir = ""
""")
on_exit(fn -> File.rm(tmp) end)
assert {:ok, cfg} = Config.load(tmp)
assert cfg.dump_dir == nil
end
test "returns error for missing file" do