diff options
author | Archie Hilton (thytom) <archie.hilton1@gmail.com> | 2019-10-23 13:46:26 +0100 |
---|---|---|
committer | Archie Hilton (thytom) <archie.hilton1@gmail.com> | 2019-10-23 13:46:26 +0100 |
commit | aa8ca39b7c13ecdcac9ab1aa3281fe967f5cbdde (patch) | |
tree | b9671d22d84259da49ec4ab090a65864f81f9201 /modules/cpuload | |
parent | 879e1ebc005ba703628adfea28e2f380a8543fe3 (diff) |
Improved cpuload module
Diffstat (limited to 'modules/cpuload')
-rwxr-xr-x | modules/cpuload | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/modules/cpuload b/modules/cpuload index a6cbc26..44a8ff1 100755 --- a/modules/cpuload +++ b/modules/cpuload @@ -6,7 +6,27 @@ PREFIX=' ' get_load() { - echo "$PREFIX$( printf "%.0f" $(awk '{u=$2+$4; t=$2+$4+$5; if (NR==1){u1=u; t1=t;} else print ($2+$4-u1) * 100 / (t-t1); }' <(grep 'cpu ' /proc/stat) <(sleep 1;grep 'cpu ' /proc/stat)) )%" + # Get the first line with aggregate of all CPUs + cpu_last=($(head -n1 /proc/stat)) + cpu_last_sum="${cpu_last[@]:1}" + cpu_last_sum=$((${cpu_last_sum// /+})) + + sleep 0.05 + + cpu_now=($(head -n1 /proc/stat)) + cpu_sum="${cpu_now[@]:1}" + cpu_sum=$((${cpu_sum// /+})) + + cpu_delta=$((cpu_sum - cpu_last_sum)) + cpu_idle=$((cpu_now[4]- cpu_last[4])) + cpu_used=$((cpu_delta - cpu_idle)) + cpu_usage=$((100 * cpu_used / cpu_delta)) + + # Keep this as last for our next read + cpu_last=("${cpu_now[@]}") + cpu_last_sum=$cpu_sum + + echo "$PREFIX$cpu_usage%" } get_load |