Created
February 18, 2015 17:41
-
-
Save dlanderson/554a00b778064de85d8a to your computer and use it in GitHub Desktop.
Bash calculate CPU steal%
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Adopted from http://stackoverflow.com/questions/26791240/how-to-get-percentage-of-processor-use-with-bash | |
# Read /proc/stat file (for first datapoint) | |
read cpu user nice system idle iowait irq softirq steal guest< /proc/stat | |
# compute active and total utilizations | |
cpu_steal_prev=$((steal)) | |
cpu_total_prev=$((user+system+nice+softirq+steal+idle+iowait)) | |
sleep 0.05 | |
# Read /proc/stat file (for second datapoint) | |
read cpu user nice system idle iowait irq softirq steal guest< /proc/stat | |
# compute active and total utilizations | |
cpu_steal_cur=$((steal)) | |
cpu_total_cur=$((user+system+nice+softirq+steal+idle+iowait)) | |
# compute CPU steal (%) | |
if [ $(( cpu_steal_cur-cpu_steal_prev )) -eq 0 ]; then | |
steal_pct=0 | |
else | |
steal_pct=$((100*( cpu_steal_cur-cpu_steal_prev ) / (cpu_total_cur-cpu_total_prev) )) | |
fi | |
printf " Current CPU Steal : %s\n" "$steal_pct" | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment