local cron = require("assay.cron")
local M = {
linux = type(linux) == "table" and linux or nil,
cgroup = type(cgroup) == "table" and cgroup or nil,
systemd = type(systemd) == "table" and systemd or nil,
cron = cron,
}
local function require_linux()
if not M.linux then error("assay.system: linux builtin not available (Linux only)") end
end
local function require_systemd()
if not M.systemd then error("assay.system: systemd builtin not available (Linux only)") end
end
local function require_cgroup()
if not M.cgroup then error("assay.system: cgroup builtin not available (Linux only)") end
end
local function pcall_or_nil(fn, ...)
if not fn then return nil end
local ok, val = pcall(fn, ...)
if ok then return val end
return nil
end
function M.host_snapshot()
require_linux()
return {
cpu = pcall_or_nil(M.linux.cpu_stat),
mem = pcall_or_nil(M.linux.meminfo),
load = pcall_or_nil(M.linux.loadavg),
uptime = pcall_or_nil(M.linux.uptime),
netdev = pcall_or_nil(M.linux.netdev),
kernel = pcall_or_nil(M.linux.kernel),
}
end
function M.machine_snapshot(name)
require_systemd()
require_cgroup()
local cgroup_path = "/sys/fs/cgroup/machine.slice/systemd-nspawn@" .. name .. ".service"
return {
info = pcall_or_nil(M.systemd.machine_status, name),
cgroup = {
cpu = pcall_or_nil(M.cgroup.cpu_stat, cgroup_path),
memory = pcall_or_nil(M.cgroup.memory, cgroup_path),
io = pcall_or_nil(M.cgroup.io, cgroup_path),
pids = pcall_or_nil(M.cgroup.pids, cgroup_path),
},
journal_tail = pcall_or_nil(M.systemd.journal, { machine = name, lines = 20 }),
}
end
function M.machines()
require_systemd()
local items = pcall_or_nil(M.systemd.list_machines) or {}
if not M.cgroup then return items end
for _, m in ipairs(items) do
local p = "/sys/fs/cgroup/machine.slice/systemd-nspawn@" .. (m.name or "") .. ".service"
m.cgroup = {
cpu = pcall_or_nil(M.cgroup.cpu_stat, p),
memory = pcall_or_nil(M.cgroup.memory, p),
pids = pcall_or_nil(M.cgroup.pids, p),
}
end
return items
end
return M