[systemd-devel] systemd-cgtop vs docker-stats

Tiago Alves tiagoalves83 at gmail.com
Wed Nov 25 20:28:51 PST 2015


Hi,

I am implementing a zabbix monitoring service for docker (some
implementations that I found are not working the way I need).

I am using a bash script for collecting container CPU % direct from cgroup
cpuacct. The bash script (at the end of this message) is very simple (I
copied the idea from docker-stats -
https://github.com/docker/docker/blob/master/api/client/stats.go#L81)

However, it seems that docker-stats is not so accurate as systemd-cgtop so
my question are:

a) How systemd-cgtop calculate a system.slice (in my case, an docker
container) CPU % ? Can someone point me the cgroups/systemd/linux files
used?
I am looking at
https://github.com/systemd/systemd/blob/master/src/cgtop/cgtop.c#L126 but I
am not a C expert.

b) Is it possible to get CPU% using a bash script reading systemd-cgtop ?
I have tried something like: systemd-cgtop -b -n2 , but it is not
collecting CPU% info.

Thank you,
Tiago

MY BASH SCRIPT:

*get_container_cpu_percentage()* {
CID=$1

NUM_PROC=$(cat /proc/cpuinfo | grep processor | wc -l)

SYSTEM_PRE=$(get_system_cpu_usage)
CONTAINER_PRE=$(get_container_cpu_usage $CID)

sleep 2

SYSTEM=$(get_system_cpu_usage)
CONTAINER=$(get_container_cpu_usage $CID)

CONTAINER_DELTA=$((CONTAINER-CONTAINER_PRE))
SYSTEM_DELTA=$((SYSTEM-SYSTEM_PRE))

if [ "$CONTAINER_DELTA" -gt "0" ] && [ "$SYSTEM_DELTA" -gt "0" ];
then
#awk "BEGIN {printf
\"%.2f\",${CONTAINER_DELTA}/${SYSTEM_DELTA}*100*${NUM_PROC}}"
awk "BEGIN {printf \"%.2f\",${CONTAINER_DELTA}/${SYSTEM_DELTA}*100}"
else
echo "0"
fi
}

*get_system_cpu_usage()* {
cat /proc/stat | grep "cpu " | awk -v CLK_TCK=$(getconf CLK_TCK) '{sum=0;
mult=1000000000; for (i=2;i<=9;i++) {sum+=$i}; sum*=(mult/CLK_TCK); print
sum}'
}

*get_container_cpu_usage()* {
CID=$1
FILE="/sys/fs/cgroup/cpu,cpuacct/system.slice/docker-${CID}*.scope/cpuacct.usage"
        get_container_info $FILE
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freedesktop.org/archives/systemd-devel/attachments/20151126/13e3d9a2/attachment.html>


More information about the systemd-devel mailing list