Skip to content

Instantly share code, notes, and snippets.

@ThinGuy
Last active April 13, 2022 21:09
Show Gist options
  • Save ThinGuy/d2491bae65b4cea7c6b5f05a35e9f700 to your computer and use it in GitHub Desktop.
Save ThinGuy/d2491bae65b4cea7c6b5f05a35e9f700 to your computer and use it in GitHub Desktop.
One liners for system CPU Usage
#faster
grep 'cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} END {print usage "%"}'
#For grep cpu for use with juju (counts machines, then runs command remotely)
for i in $(juju machines|awk '!/lxd/&&!/kvm/&&!/^M/&&!/^$/{print $1}');do printf "Juju Machine $i CPU %%: ";juju ssh $i "grep 'cpu ' /proc/stat | awk '{usage=(\$2+\$4)*100/(\$2+\$4+\$5)} END {print usage \"%\"}'";done 2>/dev/null
#Slower, but better
top -b -n2 -p 1 | fgrep "Cpu(s)" | tail -1 | awk -F'id,' '{ split($1, vs, ","); v=vs[length(vs)]; sub("ex%", "", v); printf "%.1f%%\n", 100 - v }'
# For above command with juju (counts machines, then runs command remotely)
for i in $(juju machines|awk '!/lxd/&&!/kvm/&&!/^M/&&!/^$/{print $1}');do printf "Juju Machine $i CPU %%: ";juju ssh 2>/dev/null 0 top -b -n2 -p 1 | fgrep "Cpu(s)" | tail -1 | awk -F'id,' '{ split($1, vs, ","); v=vs[length(vs)]; sub("ex%", "", v); printf "%.1f%%\n", 100 - v }';done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment