memory.sh
· 1.5 KiB · Bash
Brut
#!/bin/bash
# adapted from https://github.com/pfoo/armbian-lib/blob/master/scripts/update-motd.d/30-sysinfo
function display() {
# $1=name $2=value $3=red_limit $4=minimal_show_limit $5=unit $6=after $7=acs/desc{
# battery red color is opposite, lower number
if [[ "$1" == "Battery" ]]; then local great="<"; else local great=">"; fi
#if [[ -n "$2" && "$2" -gt "0" && (( "${2%.*}" -ge "$4" )) ]]; then
printf "%-14s" "$1:"
if awk "BEGIN{exit ! ($2 $great $3)}"; then echo -ne "\e[0;91m $2"; else echo -ne "\e[0;92m $2"; fi
printf "%-1s\x1B[0m" "$5"
printf "%-11s\t" "$6"
return 1
#fi
} # display
# memory and swap
mem_info=$(LC_ALL=C free -w 2>/dev/null | grep "^Mem" || LC_ALL=C free | grep "^Mem")
memory_usage=$(awk '{printf("%.0f",(($2-($4+$6+$7))/$2) * 100)}' <<<${mem_info})
mem_info=$(echo $mem_info | awk '{print $2}')
memory_total=$(( mem_info / 1024 ))
swap_info=$(LC_ALL=C free -m | grep "^Swap")
swap_usage=$( (awk '/Swap/ { printf("%3.0f", $3/$2*100) }' <<<${swap_info} 2>/dev/null || echo 0) | tr -c -d '[:digit:]')
swap_total=$(awk '{print $(2)}' <<<${swap_info})
if [[ ${memory_total} -gt 1000 ]]; then
memory_total=$(awk '{printf("%.2f",$1/1024)}' <<<${memory_total})"G"
else
memory_total+="M"
fi
if [[ ${swap_total} -gt 500 ]]; then
swap_total=$(awk '{printf("%.2f",$1/1024)}' <<<${swap_total})"G"
else
swap_total+="M"
fi
display "Memory usage" "$memory_usage" "70" "0" "%" " of ${memory_total}"
echo "" # fixed newline
display "Zram usage" "$swap_usage" "75" "0" "%" " of ${swap_total}"
echo "" # fixed newline
| 1 | #!/bin/bash |
| 2 | # adapted from https://github.com/pfoo/armbian-lib/blob/master/scripts/update-motd.d/30-sysinfo |
| 3 | |
| 4 | function display() { |
| 5 | # $1=name $2=value $3=red_limit $4=minimal_show_limit $5=unit $6=after $7=acs/desc{ |
| 6 | # battery red color is opposite, lower number |
| 7 | if [[ "$1" == "Battery" ]]; then local great="<"; else local great=">"; fi |
| 8 | #if [[ -n "$2" && "$2" -gt "0" && (( "${2%.*}" -ge "$4" )) ]]; then |
| 9 | printf "%-14s" "$1:" |
| 10 | if awk "BEGIN{exit ! ($2 $great $3)}"; then echo -ne "\e[0;91m $2"; else echo -ne "\e[0;92m $2"; fi |
| 11 | printf "%-1s\x1B[0m" "$5" |
| 12 | printf "%-11s\t" "$6" |
| 13 | return 1 |
| 14 | #fi |
| 15 | } # display |
| 16 | |
| 17 | # memory and swap |
| 18 | mem_info=$(LC_ALL=C free -w 2>/dev/null | grep "^Mem" || LC_ALL=C free | grep "^Mem") |
| 19 | memory_usage=$(awk '{printf("%.0f",(($2-($4+$6+$7))/$2) * 100)}' <<<${mem_info}) |
| 20 | mem_info=$(echo $mem_info | awk '{print $2}') |
| 21 | memory_total=$(( mem_info / 1024 )) |
| 22 | swap_info=$(LC_ALL=C free -m | grep "^Swap") |
| 23 | swap_usage=$( (awk '/Swap/ { printf("%3.0f", $3/$2*100) }' <<<${swap_info} 2>/dev/null || echo 0) | tr -c -d '[:digit:]') |
| 24 | swap_total=$(awk '{print $(2)}' <<<${swap_info}) |
| 25 | |
| 26 | if [[ ${memory_total} -gt 1000 ]]; then |
| 27 | memory_total=$(awk '{printf("%.2f",$1/1024)}' <<<${memory_total})"G" |
| 28 | else |
| 29 | memory_total+="M" |
| 30 | fi |
| 31 | |
| 32 | if [[ ${swap_total} -gt 500 ]]; then |
| 33 | swap_total=$(awk '{printf("%.2f",$1/1024)}' <<<${swap_total})"G" |
| 34 | else |
| 35 | swap_total+="M" |
| 36 | fi |
| 37 | |
| 38 | display "Memory usage" "$memory_usage" "70" "0" "%" " of ${memory_total}" |
| 39 | echo "" # fixed newline |
| 40 | display "Zram usage" "$swap_usage" "75" "0" "%" " of ${swap_total}" |
| 41 | echo "" # fixed newline |