Ostatnio aktywny 1754992079

memory.sh Surowy
1#!/bin/bash
2# adapted from https://github.com/pfoo/armbian-lib/blob/master/scripts/update-motd.d/30-sysinfo
3
4function 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
18mem_info=$(LC_ALL=C free -w 2>/dev/null | grep "^Mem" || LC_ALL=C free | grep "^Mem")
19memory_usage=$(awk '{printf("%.0f",(($2-($4+$6+$7))/$2) * 100)}' <<<${mem_info})
20mem_info=$(echo $mem_info | awk '{print $2}')
21memory_total=$(( mem_info / 1024 ))
22swap_info=$(LC_ALL=C free -m | grep "^Swap")
23swap_usage=$( (awk '/Swap/ { printf("%3.0f", $3/$2*100) }' <<<${swap_info} 2>/dev/null || echo 0) | tr -c -d '[:digit:]')
24swap_total=$(awk '{print $(2)}' <<<${swap_info})
25
26if [[ ${memory_total} -gt 1000 ]]; then
27 memory_total=$(awk '{printf("%.2f",$1/1024)}' <<<${memory_total})"G"
28else
29 memory_total+="M"
30fi
31
32if [[ ${swap_total} -gt 500 ]]; then
33 swap_total=$(awk '{printf("%.2f",$1/1024)}' <<<${swap_total})"G"
34else
35 swap_total+="M"
36fi
37
38display "Memory usage" "$memory_usage" "70" "0" "%" " of ${memory_total}"
39echo "" # fixed newline
40display "Zram usage" "$swap_usage" "75" "0" "%" " of ${swap_total}"
41echo "" # fixed newline