Skip to content

Instantly share code, notes, and snippets.

@42wim
Created October 20, 2022 20:27
Show Gist options
  • Save 42wim/45be61e4950859738e8f8d2ab4118fc9 to your computer and use it in GitHub Desktop.
Save 42wim/45be61e4950859738e8f8d2ab4118fc9 to your computer and use it in GitHub Desktop.
cpu stats docker bash
#!/bin/bash
id=$(docker inspect $1|jq ".[]|.Id" -r)
mhz=$(cat /proc/cpuinfo |grep MHz|awk '{print $4}'|paste -d "+" -s|bc -l)
while true;do
allpre=$(cat /proc/stat |head -n 1|cut -d " " -f 3-9|sed s#" "#+#g|awk '{print "("$1")*1000000000/100"}'|bc)
acctpre=$(cat /sys/fs/cgroup/cpu,cpuacct/system.slice/docker-$id.scope/cpuacct.usage)
sleep 1
all=$(cat /proc/stat |head -n 1|cut -d " " -f 3-9|sed s#" "#+#g|awk '{print "("$1")*1000000000/100"}'|bc)
acct=$(cat /sys/fs/cgroup/cpu,cpuacct/system.slice/docker-$id.scope/cpuacct.usage)
echo "($acct-$acctpre)/($all-$allpre)*$mhz"|bc -l
done
@jovandeginste
Copy link

allpre=$(awk '/^cpu / {print ($2 + $3 + $4 + $5 + $6 + $7 + $8) * (10**7)}' </proc/stat)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment