Hey,
I want to share an amateurish bash script to check CPU usage on containers in percentage.
#!/bin/bash
getCpuTime() {
cpuStatPath="/sys/fs/cgroup/lxc.payload.$1/cpu.stat"
if [ -f "$cpuStatPath" ]
then
cat /sys/fs/cgroup/lxc.payload.$1/cpu.stat |head -n1 |awk '{print $2}';
else
cat /sys/fs/cgroup/cpuacct/lxc.payload.$1/cpuacct.usage
fi
}
getTimeConvert() {
cpuStatPath="/sys/fs/cgroup/lxc.payload.$1/cpu.stat"
if [ -f "$cpuStatPath" ]
then
echo "1000000"
else
echo "1000000000"
fi
}
getCpuCores() {
cpuCoresLxd=$(lxc config show $1 |grep limits.cpu |awk '{print $2}' |tr -d '"' |bc)
if [[ "$cpuCoresLxd" == *"-"* ]]
then
echo "($cpuCoresLxd * -1) + 1" |bc
fi
}
getPercentage() {
container=$1
cpuCoresLxd=$(getCpuCores $container)
cpu_usage=$(getCpuTime $container)
sleep 1
cpu_usage_after=$(getCpuTime $container)
total_cpu_time=$((cpu_usage_after - cpu_usage))
timeConvert=$(getTimeConvert $container)
cpu_percentage=$(echo "scale=2; ($total_cpu_time / $timeConvert) / $cpuCoresLxd * 100" | bc )
echo "$container - $cpu_percentage%"
}
containers=($(lxc list --format=json | jq -r '.[] | select(.state.status == "Running") | .name'))
for container in "${containers[@]}"
do
getPercentage ${container} &
done
wait
Works from LXD 4.X. Yesterday i’ve tested it with INCUS and it works as well. If you use incus you just need to change “lxc config show” to “incus config show” on line 51
Hope it helps someone.
root@hostMachine1:~# ./cpu.sh
web01 - 0%
web02 - 0%
cache01 - 9.00%
db02 - 0%
web03 - 0%
db01 - 50.00%
lb01 - 4.00%
queue01 - 24.00%