iax

30-sysinfo

iax
Oct 20th, 2023 (edited)
1,141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 9.87 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # Copyright (c) Authors: https://www.armbian.com/authors
  4. #
  5. # This file is licensed under the terms of the GNU General Public
  6. # License version 2. This program is licensed "as is" without any
  7. # warranty of any kind, whether express or implied.
  8. #
  9. export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
  10. THIS_SCRIPT="sysinfo"
  11.  
  12. # don't change default values - they are lost on upgrades. Use /etc/default/orangepi-motd for your values
  13.  
  14. MOTD_DISABLE=""
  15. PRIMARY_INTERFACE="eth0"
  16. PRIMARY_DIRECTION="rx"
  17. STORAGE=/dev/sda1
  18. HIDE_IP_PATTERN="^dummy0|^lo"
  19. CPU_TEMP_LIMIT=60
  20. # Temperature offset in Celcius degrees
  21. CPU_TEMP_OFFSET=0
  22. HDD_TEMP_LIMIT=60
  23. AMB_TEMP_LIMIT=40
  24.  
  25. for f in $MOTD_DISABLE; do
  26.     [[ $f == $THIS_SCRIPT ]] && exit 0
  27. done
  28.  
  29. # don't edit below here
  30.  
  31. function display() {
  32.     # $1=name $2=value $3=red_limit $4=minimal_show_limit $5=unit $6=after $7=acs/desc{
  33.     # battery red color is opposite, lower number
  34.     if [[ "$1" == "Battery" ]]; then local great="<"; else local great=">"; fi
  35.     if [[ -n "$2" && "$2" > "0" && (( "${2%.*}" -ge "$4" )) ]]; then
  36.         printf "%-14s" "$1:"
  37.         if awk "BEGIN{exit ! ($2 $great $3)}"; then echo -ne "\e[0;91m $2"; else echo -ne "\e[0;92m $2"; fi
  38.         printf "%-1s\x1B[0m" "$5"
  39.         printf "%-11s\t" "$6"
  40.         return 1
  41.     fi
  42. } # display
  43.  
  44. function getboardtemp() {
  45.     if [ -f /etc/orangepimonitor/datasources/soctemp ]; then
  46.         read raw_temp </etc/orangepimonitor/datasources/soctemp 2>/dev/null
  47.         if [ ! -z $(echo "$raw_temp" | grep -o "^[1-9][0-9]*\.\?[0-9]*$") ] && (( $(echo "${raw_temp} < 200" |bc -l) )); then
  48.             # Allwinner legacy kernels output degree C
  49.             board_temp=${raw_temp}
  50.         else
  51.             board_temp=$(awk '{printf("%d",$1/1000)}' <<<${raw_temp})
  52.         fi
  53.     elif [ -f /etc/orangepimonitor/datasources/pmictemp ]; then
  54.         # fallback to PMIC temperature
  55.         board_temp=$(awk '{printf("%d",$1/1000)}' </etc/orangepimonitor/datasources/pmictemp)
  56.     fi
  57.     # Some boards, such as the Orange Pi Zero LTS, report shifted CPU temperatures
  58.     board_temp=$((board_temp + CPU_TEMP_OFFSET))
  59. } # getboardtemp
  60.  
  61. function batteryinfo() {
  62.     # Battery info for Allwinner
  63.     mainline_dir="/sys/power/axp_pmu"
  64.     legacy_dir="/sys/class/power_supply"
  65.     if [[ -e "$mainline_dir" ]]; then
  66.         read status_battery_connected < $mainline_dir/battery/connected 2>/dev/null
  67.         if [[ "$status_battery_connected" == "1" ]]; then
  68.             read status_battery_charging < $mainline_dir/charger/charging
  69.             read status_ac_connect < $mainline_dir/ac/connected
  70.             read battery_percent< $mainline_dir/battery/capacity
  71.             # dispay charging / percentage
  72.             if [[ "$status_ac_connect" == "1" && "$battery_percent" -lt "100" ]]; then
  73.                 status_battery_text=" charging"
  74.             elif [[ "$status_ac_connect" == "1" && "$battery_percent" -eq "100" ]]; then
  75.                 status_battery_text=" charged"
  76.             else
  77.                 status_battery_text=" discharging"
  78.             fi
  79.         fi
  80.     elif [[ -e "$legacy_dir/axp813-ac" ]]; then
  81.         read status_battery_connected < $legacy_dir/axp20x-battery/present
  82.         if [[ "$status_battery_connected" == "1" ]]; then
  83.             status_battery_text=" "$(awk '{print tolower($0)}' < $legacy_dir/axp20x-battery/status)
  84.             read status_ac_connect < $legacy_dir/axp813-ac/present
  85.             read battery_percent< $legacy_dir/axp20x-battery/capacity
  86.         fi
  87.     elif [[ -e "$legacy_dir/battery" ]]; then
  88.         if [[ (("$(cat $legacy_dir/battery/voltage_now)" -gt "5" )) ]]; then
  89.             status_battery_text=" "$(awk '{print tolower($0)}' < $legacy_dir/battery/status)
  90.             read battery_percent <$legacy_dir/battery/capacity
  91.         fi
  92.     fi
  93. } # batteryinfo
  94.  
  95. function ambienttemp() {
  96.     # define where w1 usually shows up
  97.         W1_DIR="/sys/devices/w1_bus_master1/"
  98.     if [ -f /etc/orangepimonitor/datasources/ambienttemp ]; then
  99.         read raw_temp </etc/orangepimonitor/datasources/ambienttemp 2>/dev/null
  100.         amb_temp=$(awk '{printf("%d",$1/1000)}' <<<${raw_temp})
  101.         echo $amb_temp
  102.     elif [[ -d $W1_DIR && $ONE_WIRE == yes ]]; then
  103.         device=$(ls -1 $W1_DIR | grep -E '^[0-9]{1,4}' | head -1)
  104.         if [[ -n $device ]]; then
  105.             read raw_temp < ${W1_DIR}${device}/hwmon/$(ls -1 ${W1_DIR}${device}/hwmon)/temp1_input 2>/dev/null
  106.             amb_temp=$(awk '{printf("%d",$1/1000)}' <<<${raw_temp})
  107.             echo $amb_temp
  108.         fi
  109.     else
  110.         # read ambient temperature from USB device if available
  111.         if [[ ! -f /usr/bin/temper ]]; then
  112.             echo ""
  113.             return
  114.         fi
  115.         amb_temp=$(temper -c 2>/dev/null)
  116.         case ${amb_temp} in
  117.             *"find the USB device"*)
  118.                 echo ""
  119.                 ;;
  120.             *)
  121.                 amb_temp=$(awk '{print $NF}' <<<$amb_temp |  sed 's/C//g')
  122.                 echo -n "scale=1;${amb_temp}/1" | grep -oE "\-?[[:digit:]]+\.[[:digit:]]"
  123.         esac
  124.     fi
  125. } # ambienttemp
  126.  
  127. function get_ip_addresses() {
  128.     local ips=()
  129.     for f in /sys/class/net/*; do
  130.         local intf=$(basename $f)
  131.         # match only interface names "dummy0" and "lo"
  132.         if [[ $intf =~ $HIDE_IP_PATTERN ]]; then
  133.             continue
  134.         else
  135.             local tmp=$(ip -4 addr show dev $intf | grep -v "$intf:avahi" | awk '/inet/ {print $2}' | cut -d'/' -f1)
  136.             # add both name and IP - can be informative but becomes ugly with long persistent/predictable device names
  137.             #[[ -n $tmp ]] && ips+=("$intf: $tmp")
  138.             # add IP only
  139.             [[ -n $tmp ]] && ips+=("$tmp")
  140.         fi
  141.     done
  142.     echo "${ips[@]}"
  143. } # get_ip_addresses
  144.  
  145. function storage_info() {
  146.     # storage info
  147.     RootInfo=$(df -h /)
  148.     root_usage=$(awk '/\// {print $(NF-1)}' <<<${RootInfo} | sed 's/%//g')
  149.     root_total=$(awk '/\// {print $(NF-4)}' <<<${RootInfo})
  150.     StorageInfo=$(df -h $STORAGE 2>/dev/null | grep $STORAGE)
  151.     if [[ -n "${StorageInfo}" && ${RootInfo} != *$STORAGE* ]]; then
  152.         storage_usage=$(awk '/\// {print $(NF-1)}' <<<${StorageInfo} | sed 's/%//g')
  153.         storage_total=$(awk '/\// {print $(NF-4)}' <<<${StorageInfo})
  154.         if [[ -n "$(command -v smartctl)" ]]; then
  155.             DISK="${STORAGE::-1}"
  156.             storage_temp+=$(smartctl -l scttempsts $DISK 2> /dev/null | grep -i 'Current Temperature:' | awk '{print $(NF-1)}')
  157.         fi
  158.     fi
  159. } # storage_info
  160.  
  161.  
  162.  
  163. # query various systems and send some stuff to the background for overall faster execution.
  164. # Works only with ambienttemp and batteryinfo since A20 is slow enough :)
  165. amb_temp=$(ambienttemp &)
  166. ip_address=$(get_ip_addresses &)
  167. batteryinfo
  168. storage_info
  169. getboardtemp
  170. critical_load=80
  171.  
  172. # get uptime, logged in users and load in one take
  173. UPTIME=$(LC_ALL=C uptime)
  174. UPT1=${UPTIME#*'up '}
  175. UPT2=${UPT1%'user'*}
  176. users=${UPT2//*','}
  177. users=${users//' '}
  178. time=${UPT2%','*}
  179. time=${time//','}
  180. time=$(echo $time | xargs)
  181. load=${UPTIME#*'load average: '}
  182. load=${load//','}
  183. load=$(echo $load | cut -d" " -f1)
  184. [[ $load == 0.0* ]] && load=0.10
  185. cpucount=$(grep -c processor /proc/cpuinfo)
  186.  
  187. load=$(awk '{printf("%.0f",($1/$2) * 100)}' <<< "$load $cpucount")
  188.  
  189. # memory and swap
  190. mem_info=$(LC_ALL=C free -w 2>/dev/null | grep "^Mem" || LC_ALL=C free | grep "^Mem")
  191. memory_usage=$(awk '{printf("%.0f",(($2-($4+$6+$7))/$2) * 100)}' <<<${mem_info})
  192. mem_info=$(echo $mem_info | awk '{print $2}')
  193. memory_total=$(( mem_info / 1024 ))
  194. swap_info=$(LC_ALL=C free -m | grep "^Swap")
  195. swap_usage=$( (awk '/Swap/ { printf("%3.0f", $3/$2*100) }' <<<${swap_info} 2>/dev/null || echo 0) | tr -c -d '[:digit:]')
  196. swap_total=$(awk '{print $(2)}' <<<${swap_info})
  197.  
  198. # display info
  199. display "System load" "${load%% *}" "${critical_load}" "0" "%" ""
  200.  
  201. printf "Up time:       \x1B[92m%s\x1B[0m\t" "$time"
  202. display "Local users" "${users##* }" "3" "2" ""
  203. echo "" # fixed newline
  204. if [[ ${memory_total} -gt 1000 ]]; then
  205.     memory_total=$(awk '{printf("%.2f",$1/1024)}' <<<${memory_total})"G"
  206. else
  207.     memory_total+="M"
  208. fi
  209.  
  210. if [[ ${swap_total} -gt 500 ]]; then
  211.     swap_total=$(awk '{printf("%.2f",$1/1024)}' <<<${swap_total})"G"
  212. else
  213.     swap_total+="M"
  214. fi
  215.  
  216. display "Memory usage" "$memory_usage" "70" "0" "%" " of ${memory_total}"
  217. display "Zram usage" "$swap_usage" "75" "0" "%" " of ${swap_total}"
  218. printf "IP:        "
  219. printf "\x1B[92m%s\x1B[0m" "$ip_address"
  220. echo "" # fixed newline
  221. display "CPU temp" "$board_temp" $CPU_TEMP_LIMIT "0" "°C" ""
  222. display "Ambient temp" "$amb_temp" $AMB_TEMP_LIMIT "0" "°C" ""
  223. display "Usage of /" "$root_usage" "90" "1" "%" " of $root_total"
  224. echo "" # fixed newline
  225. a=0
  226. display "storage/" "$storage_usage" "90" "1" "%" " of $storage_total" ; a=$((a+$?))
  227. display "storage temp" "$storage_temp" $HDD_TEMP_LIMIT "0" "°C" "" ; a=$((a+$?))
  228. display "Battery" "$battery_percent" "20" "1" "%" "$status_battery_text" ; a=$((a+$?))
  229. (( $a > 0 )) && echo "" # new line only if some value is displayed
  230.  
  231. # Check whether PRIMARY_INTERFACE exist in /var/lib/vnstat/
  232. PRIMARY_INTERFACE=$(comm -12 <(ls -1 /var/lib/vnstat/ 2> /dev/null) <(echo "$PRIMARY_INTERFACE" | sed 's/+/\n/g') | sed -n -e 'H;${x;s/\n/+/g;s/^+//;p;}')
  233.  
  234. line=0
  235. if [[ $(command -v vnstat) && -n $PRIMARY_INTERFACE ]]; then
  236.     traffic=$(vnstat -i $PRIMARY_INTERFACE --oneline | cut -d";" -f4,5)
  237.     traffic_rx=$(echo $traffic | cut -d";" -f1,1 | sed -r 's/([0-9]+\.[0-9]{1})[0-9]*/\1/')
  238.     traffic_tx=$(echo $traffic | cut -d";" -f2,2 | sed -r 's/([0-9]+\.[0-9]{1})[0-9]*/\1/')
  239.     [[ "$traffic" == *"Not enough"* ]] && { traffic_tx="n/a "; traffic_rx="n/a  "; }
  240.     case $PRIMARY_DIRECTION in
  241.         tx)
  242.             printf "TX today:      \x1B[92m%s\x1B[0m  \t"  "$traffic_tx"
  243.             line=$((line+1))
  244.         ;;
  245.         rx)
  246.             printf "RX today:      \x1B[92m%s\x1B[0m  \t"  "$traffic_rx"
  247.             line=$((line+1))
  248.         ;;
  249.         both)
  250.             printf "TX today:      \x1B[92m%s\x1B[0m  \t"  "$traffic_tx"
  251.             printf "RX today:      \x1B[92m%s\x1B[0m  \t"  "$traffic_rx"
  252.             line=$((line+1))
  253.         ;;
  254.         *) #off or whatever
  255.         ;;
  256.     esac
  257. fi
  258.  
  259. if [[ $(command -v zpool) ]]; then
  260.     zpoolstatus="$(zpool status </dev/stdin 2>&1 )"
  261.     if [[ -n $(echo $zpoolstatus | grep 'not loaded') ]]; then
  262.     :
  263.     elif [[ -n $(echo $zpoolstatus | grep 'degraded\|OFFLINE') ]]; then
  264.         printf "ZFS pool:      "
  265.         echo -ne "\e[0;91mDegraded\x1B[0m"
  266.         line=$((line+1))
  267.     elif [[ -n $(echo $zpoolstatus | grep 'no pools available') ]]; then
  268.         printf "ZFS pool:      "
  269.         echo -ne "n/a"
  270.         line=$((line+1))
  271.     else
  272.         printf "ZFS pool:      "
  273.         echo -ne "\e[0;92mOnline\x1B[0m"
  274.         line=$((line+1))
  275.     fi
  276. fi
  277. [[ $line -ne 0 ]] && echo ""
  278. echo ""
  279.  
Advertisement
Add Comment
Please, Sign In to add comment