lxc-info 與 lxc-monitor 能看容器狀態,但若要追蹤容器內特定應用,可搭配 lxc-ps、ps 與 lxc-cgroup。
get_pid() {
local pid=`lxc-ps -n $1 | grep $2 | awk '{print $2}' | grep -v PID`
echo $pid
}
可用 ps -p PID -o psr 取得 CPU 核心,用 pcpu 取得 CPU 百分比,用 rss 取得實體記憶體。
memory.usage_in_bytes 可看容器整體記憶體,memory.memsw.usage_in_bytes 可看記憶體加 swap。再搭配 lxc-info 取得 state,就能定期收集容器與應用狀態。
把這些函式整合成腳本,再用 cron 或 systemd timer 排程,即可持續監控,甚至依消耗狀況動態調整資源。