sysinfo.sh 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #!/bin/bash
  2. export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
  3. export LANG=zh_CN.UTF-8
  4. THIS_SCRIPT="sysinfo"
  5. MOTD_DISABLE=""
  6. SHOW_IP_PATTERN="^[ewr].*|^br.*|^lt.*|^umts.*"
  7. DATA_STORAGE=/userdisk/data
  8. MEDIA_STORAGE=/userdisk/snail
  9. [[ -f /etc/default/motd ]] && . /etc/default/motd
  10. for f in $MOTD_DISABLE; do
  11. [[ $f == $THIS_SCRIPT ]] && exit 0
  12. done
  13. # don't edit below here
  14. function display()
  15. {
  16. # $1=name $2=value $3=red_limit $4=minimal_show_limit $5=unit $6=after $7=acs/desc{
  17. # battery red color is opposite, lower number
  18. if [[ "$1" == "Battery" ]]; then
  19. local great="<";
  20. else
  21. local great=">";
  22. fi
  23. if [[ -n "$2" && "$2" > "0" && (( "${2%.*}" -ge "$4" )) ]]; then
  24. printf "%-14s%s" "$1:"
  25. if awk "BEGIN{exit ! ($2 $great $3)}"; then
  26. echo -ne "\e[0;91m $2";
  27. else
  28. echo -ne "\e[0;92m $2";
  29. fi
  30. printf "%-1s%s\x1B[0m" "$5"
  31. printf "%-11s%s\t" "$6"
  32. return 1
  33. fi
  34. } # display
  35. function get_ip_addresses()
  36. {
  37. local ips=()
  38. for f in /sys/class/net/*; do
  39. local intf=$(basename $f)
  40. # match only interface names starting with e (Ethernet), br (bridge), w (wireless), r (some Ralink drivers use ra<number> format)
  41. if [[ $intf =~ $SHOW_IP_PATTERN ]]; then
  42. local tmp=$(ip -4 addr show dev $intf | awk '/inet/ {print $2}' | cut -d'/' -f1)
  43. # add both name and IP - can be informative but becomes ugly with long persistent/predictable device names
  44. #[[ -n $tmp ]] && ips+=("$intf: $tmp")
  45. # add IP only
  46. [[ -n $tmp ]] && ips+=("$tmp")
  47. fi
  48. done
  49. echo "${ips[@]}"
  50. } # get_ip_addresses
  51. function storage_info()
  52. {
  53. # storage info
  54. RootInfo=$(df -h /)
  55. root_usage=$(awk '/\// {print $(NF-1)}' <<<${RootInfo} | sed 's/%//g')
  56. root_total=$(awk '/\// {print $(NF-4)}' <<<${RootInfo})
  57. # storage info
  58. BootInfo=$(df -h /boot)
  59. boot_usage=$(awk '/\// {print $(NF-1)}' <<<${BootInfo} | sed 's/%//g')
  60. boot_total=$(awk '/\// {print $(NF-4)}' <<<${BootInfo})
  61. StorageInfo=$(df -h $MEDIA_STORAGE 2>/dev/null | grep $MEDIA_STORAGE)
  62. if [[ -n "${StorageInfo}" && ${RootInfo} != *$MEDIA_STORAGE* ]]; then
  63. media_usage=$(awk '/\// {print $(NF-1)}' <<<${StorageInfo} | sed 's/%//g')
  64. media_total=$(awk '/\// {print $(NF-4)}' <<<${StorageInfo})
  65. fi
  66. StorageInfo=$(df -h $DATA_STORAGE 2>/dev/null | grep $DATA_STORAGE)
  67. if [[ -n "${StorageInfo}" && ${RootInfo} != *$DATA_STORAGE* ]]; then
  68. data_usage=$(awk '/\// {print $(NF-1)}' <<<${StorageInfo} | sed 's/%//g')
  69. data_total=$(awk '/\// {print $(NF-4)}' <<<${StorageInfo})
  70. fi
  71. } # storage_info
  72. # query various systems and send some stuff to the background for overall faster execution.
  73. # Works only with ambienttemp and batteryinfo since A20 is slow enough :)
  74. ip_address=$(get_ip_addresses &)
  75. storage_info
  76. critical_load=$(( 1 + $(grep -c processor /proc/cpuinfo) / 2 ))
  77. # get uptime, logged in users and load in one take
  78. UptimeString=$(uptime | tr -d ',')
  79. time=$(awk -F" " '{print $3" "$4}' <<<"${UptimeString}")
  80. load="$(awk -F"average: " '{print $2}'<<<"${UptimeString}")"
  81. case ${time} in
  82. 1:*) # 1-2 hours
  83. time=$(awk -F" " '{print $3" 小时"}' <<<"${UptimeString}")
  84. ;;
  85. *:*) # 2-24 hours
  86. time=$(awk -F" " '{print $3" 小时"}' <<<"${UptimeString}")
  87. ;;
  88. *day) # days
  89. days=$(awk -F" " '{print $3"天"}' <<<"${UptimeString}")
  90. time=$(awk -F" " '{print $5}' <<<"${UptimeString}")
  91. time="$days "$(awk -F":" '{print $1"小时 "$2"分钟"}' <<<"${time}")
  92. ;;
  93. esac
  94. # memory and swap
  95. mem_info=$(LC_ALL=C free -w 2>/dev/null | grep "^Mem" || LC_ALL=C free | grep "^Mem")
  96. memory_usage=$(awk '{printf("%.0f",(($2-($4+$6))/$2) * 100)}' <<<${mem_info})
  97. memory_total=$(awk '{printf("%d",$2/1024)}' <<<${mem_info})
  98. swap_info=$(LC_ALL=C free -m | grep "^Swap")
  99. swap_usage=$( (awk '/Swap/ { printf("%3.0f", $3/$2*100) }' <<<${swap_info} 2>/dev/null || echo 0) | tr -c -d '[:digit:]')
  100. swap_total=$(awk '{print $(2)}' <<<${swap_info})
  101. # display info
  102. display "系统负载" "${load%% *}" "${critical_load}" "0" "" "${load#* }"
  103. printf "运行时间: \x1B[92m%s\x1B[0m\t\t" "$time"
  104. echo "" # fixed newline
  105. display "内存已用" "$memory_usage" "70" "0" " %" " of ${memory_total}MB"
  106. display "交换内存" "$swap_usage" "10" "0" " %" " of $swap_total""Mb"
  107. printf "IP 地址: \x1B[92m%s\x1B[0m" "$ip_address"
  108. echo "" # fixed newline
  109. a=0;b=0;c=0
  110. display "CPU 温度" "$board_temp" "45" "0" "°C" "" ; a=$?
  111. display "环境温度" "$amb_temp" "40" "0" "°C" "" ; b=$?
  112. (( ($a+$b) >0 )) && echo "" # new line only if some value is displayed
  113. display "启动存储" "$boot_usage" "90" "1" "%" " of $boot_total"
  114. display "系统存储" "$root_usage" "90" "1" "%" " of $root_total"
  115. echo ""
  116. display "数据存储" "$data_usage" "90" "1" "%" " of $data_total"
  117. display "媒体存储" "$media_usage" "90" "1" "%" " of $media_total"
  118. echo ""
  119. echo ""