<div dir="ltr">Hi,<div><br></div><div>I am implementing a zabbix monitoring service for docker (some implementations that I found are not working the way I need).</div><div> </div><div>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 - <a href="https://github.com/docker/docker/blob/master/api/client/stats.go#L81" style="color:rgb(64,120,192);outline:0px;font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:14px;line-height:22.4px">https://github.com/docker/docker/blob/master/api/client/stats.go#L81</a>)</div><div><br></div><div>However, it seems that docker-stats is not so accurate as systemd-cgtop so my question are:</div><div><br></div><div>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? </div><div>I am looking at <a href="https://github.com/systemd/systemd/blob/master/src/cgtop/cgtop.c#L126" style="color:rgb(64,120,192);text-decoration:none;font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:14px;line-height:22.4px">https://github.com/systemd/systemd/blob/master/src/cgtop/cgtop.c#L126</a> but I am not a C expert.</div><div><br></div><div>b) Is it possible to get CPU% using a bash script reading systemd-cgtop ? </div><div>I have tried something like: systemd-cgtop -b -n2 , but it is not collecting CPU% info.</div><div><br></div><div>Thank you,</div><div>Tiago</div><div><br></div><div>MY BASH SCRIPT:</div><div><br></div><div><div><b>get_container_cpu_percentage()</b> {</div><div><span class="" style="white-space:pre"> </span>CID=$1</div><div><br></div><div><span class="" style="white-space:pre">    </span>NUM_PROC=$(cat /proc/cpuinfo | grep processor | wc -l)</div><div><br></div><div><span class="" style="white-space:pre">    </span>SYSTEM_PRE=$(get_system_cpu_usage)</div><div><span class="" style="white-space:pre"> </span>CONTAINER_PRE=$(get_container_cpu_usage $CID)</div><div><br></div><div><span class="" style="white-space:pre">     </span>sleep 2</div><div><br></div><div><span class="" style="white-space:pre">   </span>SYSTEM=$(get_system_cpu_usage)</div><div><span class="" style="white-space:pre">     </span>CONTAINER=$(get_container_cpu_usage $CID)</div><div><br></div><div><span class="" style="white-space:pre"> </span>CONTAINER_DELTA=$((CONTAINER-CONTAINER_PRE))</div><div><span class="" style="white-space:pre">       </span>SYSTEM_DELTA=$((SYSTEM-SYSTEM_PRE))</div><div><br></div><div><span class="" style="white-space:pre">       </span>if [ "$CONTAINER_DELTA" -gt "0" ] && [ "$SYSTEM_DELTA" -gt "0" ];</div><div><span class="" style="white-space:pre">  </span>then</div><div><span class="" style="white-space:pre">               </span>#awk "BEGIN {printf \"%.2f\",${CONTAINER_DELTA}/${SYSTEM_DELTA}*100*${NUM_PROC}}"</div><div><span class="" style="white-space:pre">              </span>awk "BEGIN {printf \"%.2f\",${CONTAINER_DELTA}/${SYSTEM_DELTA}*100}"</div><div><span class="" style="white-space:pre">   </span>else</div><div><span class="" style="white-space:pre">               </span>echo "0"</div><div><span class="" style="white-space:pre"> </span>fi</div><div>}</div></div><div><br></div><div><div><b>get_system_cpu_usage()</b> {</div><div><span class="" style="white-space:pre">       </span>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}'</div><div>}</div><div><br></div><div><b>get_container_cpu_usage()</b> {</div><div><span class="" style="white-space:pre">       </span>CID=$1</div><div><span class="" style="white-space:pre">     </span>FILE="/sys/fs/cgroup/cpu,cpuacct/system.slice/docker-${CID}*.scope/cpuacct.usage"</div><div>        get_container_info $FILE<span class="" style="white-space:pre">    </span></div><div>}</div></div><div><br></div></div>