Skip to content

Commit 1ecc02e

Browse files
committed
CPU and RAM percentage usage as 2-digit integer padded outputs
[samoshkin#17}(samoshkin#17)
1 parent 445cb28 commit 1ecc02e

File tree

2 files changed

+20
-20
lines changed

2 files changed

+20
-20
lines changed

scripts/cpu.sh

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ get_cpu_color(){
3434
print_cpu_usage() {
3535
local cpu_pused=$(get_cpu_usage_or_collect)
3636
local cpu_color=$(get_cpu_color "$cpu_pused")
37-
37+
3838
local cpu_view="$cpu_view_tmpl"
39-
cpu_view="${cpu_view//'#{cpu.pused}'/$(printf "%.1f%%" "$cpu_pused")}"
39+
cpu_view="${cpu_view//'#{cpu.pused}'/$(printf "%2.0f%%" "$cpu_pused")}"
4040
cpu_view="${cpu_view//'#{cpu.color}'/$(echo "$cpu_color" | awk '{ print $1 }')}"
4141
cpu_view="${cpu_view//'#{cpu.color2}'/$(echo "$cpu_color" | awk '{ print $2 }')}"
4242
cpu_view="${cpu_view//'#{cpu.color3}'/$(echo "$cpu_color" | awk '{ print $3 }')}"
@@ -60,7 +60,7 @@ start_cpu_collect_if_required() {
6060
if [ -f "$collect_cpu_pidfile" ] && ps -p "$(cat "$collect_cpu_pidfile")" > /dev/null 2>&1; then
6161
return;
6262
fi
63-
63+
6464
jobs >/dev/null 2>&1
6565
"$CURRENT_DIR/cpu_collect.sh" &>/dev/null &
6666
if [ -n "$(jobs -n)" ]; then

scripts/mem.sh

+17-17
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ print_mem() {
3535
local mem_usage
3636
local scale
3737
local size_format
38-
38+
3939
if is_osx; then
4040
mem_usage=$(get_mem_usage_osx)
41-
elif is_linux; then
41+
elif is_linux; then
4242
mem_usage=$(get_mem_usage_linux)
4343
elif is_freebsd; then
4444
mem_usage=$(get_mem_usage_freebsd)
@@ -53,16 +53,16 @@ print_mem() {
5353
local mem_total=$(echo "$mem_free + $mem_used" | calc)
5454
local mem_pused=$(echo "($mem_used / $mem_total) * 100" | calc)
5555
local mem_pfree=$(echo "($mem_free / $mem_total) * 100" | calc)
56-
56+
5757
# Calculate colors for mem and swap
5858
local mem_color=$(get_mem_color "$mem_pused")
59-
59+
6060
local mem_view="$mem_view_tmpl"
6161
mem_view="${mem_view//'#{mem.used}'/$(printf "$size_format" "$mem_used" "$size_unit")}"
62-
mem_view="${mem_view//'#{mem.pused}'/$(printf "%.0f%%" "$mem_pused")}"
62+
mem_view="${mem_view//'#{mem.pused}'/$(printf "%2.0f%%" "$mem_pused")}"
6363
mem_view="${mem_view//'#{mem.free}'/$(printf "$size_format" "$mem_free" "$size_unit")}"
64-
mem_view="${mem_view//'#{mem.pfree}'/$(printf "%.0f%%" "$mem_pfree")}"
65-
mem_view="${mem_view//'#{mem.total}'/$(printf "$size_format" "$mem_total" "$size_unit")}"
64+
mem_view="${mem_view//'#{mem.pfree}'/$(printf "%2.0f%%" "$mem_pfree")}"
65+
mem_view="${mem_view//'#{mem.total}'/$(printf "$size_format" "$mem_total" "$size_unit")}"
6666
mem_view="${mem_view//'#{mem.color}'/$(echo "$mem_color" | awk '{ print $1 }')}"
6767
mem_view="${mem_view//'#{mem.color2}'/$(echo "$mem_color" | awk '{ print $2 }')}"
6868
mem_view="${mem_view//'#{mem.color3}'/$(echo "$mem_color" | awk '{ print $3 }')}"
@@ -76,19 +76,19 @@ print_mem() {
7676
# free = free + inactive + speculative + occupied by compressor
7777
# see `vm_stat` command
7878
get_mem_usage_osx(){
79-
79+
8080
local page_size=$(sysctl -nq "vm.pagesize")
8181
vm_stat | awk -v page_size=$page_size -F ':' '
8282
BEGIN { free=0; used=0 }
83-
84-
/Pages active/ ||
85-
/Pages wired/ {
83+
84+
/Pages active/ ||
85+
/Pages wired/ {
8686
gsub(/^[ \t]+|[ \t]+$/, "", $2); used+=$2;
8787
}
88-
/Pages free/ ||
89-
/Pages inactive/ ||
90-
/Pages speculative/ ||
91-
/Pages occupied by compressor/ {
88+
/Pages free/ ||
89+
/Pages inactive/ ||
90+
/Pages speculative/ ||
91+
/Pages occupied by compressor/ {
9292
gsub(/^[ \t]+|[ \t]+$/, "", $2); free+=$2;
9393
}
9494
@@ -103,7 +103,7 @@ get_mem_usage_freebsd(){
103103

104104
# Method #1. Sum up free+buffers+cached, treat it as "available" memory, assuming buff+cache can be reclaimed. Note, that this assumption is not 100% correct, buff+cache most likely cannot be 100% reclaimed, but this is how memory calculation is used to be done on Linux
105105

106-
# Method #2. If "MemAvailable" is provided by system, use it. This is more correct method, because we're not relying on fragile "free+buffer+cache" equation.
106+
# Method #2. If "MemAvailable" is provided by system, use it. This is more correct method, because we're not relying on fragile "free+buffer+cache" equation.
107107

108108
# See: Interpreting /proc/meminfo and free output for Red Hat Enterprise Linux 5, 6 and 7 - Red Hat Customer Portal - https://access.redhat.com/solutions/406773
109109

@@ -112,7 +112,7 @@ get_mem_usage_linux(){
112112
</proc/meminfo awk '
113113
BEGIN { total=0; free=0; }
114114
/MemTotal:/ { total=$2; }
115-
115+
116116
/MemFree:/ { free+=$2; }
117117
/Buffers:/ { free+=$2; }
118118
/Cached:/ { free+=$2; }

0 commit comments

Comments
 (0)