Skip to content

Commit

Permalink
Merge pull request #50 from ophub/master
Browse files Browse the repository at this point in the history
tidy up codes
  • Loading branch information
unifreq committed Nov 19, 2021
2 parents 0c74e87 + 86715fa commit 219dd89
Show file tree
Hide file tree
Showing 7 changed files with 648 additions and 612 deletions.
124 changes: 58 additions & 66 deletions files/30-sysinfo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,28 @@ for f in $MOTD_DISABLE; do
done

# don't edit below here
function display()
{
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="<";
local great="<"
else
local great=">";
local great=">"
fi
if [[ -n "$2" && "$2" > "0" && (( "${2%.*}" -ge "$4" )) ]]; then
if [[ -n "$2" && "$2" > "0" && (("${2%.*}" -ge "$4")) ]]; then
printf "%-14s%s" "$1:"
if awk "BEGIN{exit ! ($2 $great $3)}"; then
echo -ne "\e[0;91m $2";
echo -ne "\e[0;91m $2"
else
echo -ne "\e[0;92m $2";
echo -ne "\e[0;92m $2"
fi
printf "%-1s%s\x1B[0m" "$5"
printf "%-11s%s\t" "$6"
return 1
fi
} # display


function get_ip_addresses()
{
function get_ip_addresses() {
local ips=()
for f in /sys/class/net/*; do
local intf=$(basename $f)
Expand All @@ -59,9 +56,7 @@ function get_ip_addresses()
echo "${ips[@]}"
} # get_ip_addresses


function storage_info()
{
function storage_info() {
# storage info
RootInfo=$(df -h /)
root_usage=$(awk '/\// {print $(NF-1)}' <<<${RootInfo} | sed 's/%//g')
Expand All @@ -85,45 +80,44 @@ function storage_info()
fi
} # storage_info

function get_data_storage()
{
if which lsblk >/dev/null;then
root_name=$(lsblk -l -o NAME,MOUNTPOINT | awk '$2~/^\/$/ {print $1'})
mmc_name=$(echo $root_name | awk '{print substr($1,1,length($1)-2);}')
if echo $mmc_name | grep mmcblk >/dev/null;then
DATA_STORAGE="/mnt/${mmc_name}p4"
function get_data_storage() {
if which lsblk >/dev/null; then
root_name=$(lsblk -l -o NAME,MOUNTPOINT | awk '$2~/^\/$/ {print $1'})
mmc_name=$(echo $root_name | awk '{print substr($1,1,length($1)-2);}')
if echo $mmc_name | grep mmcblk >/dev/null; then
DATA_STORAGE="/mnt/${mmc_name}p4"
fi
fi
fi
}

# query various systems and send some stuff to the background for overall faster execution.
# Works only with ambienttemp and batteryinfo since A20 is slow enough :)
ip_address=$(get_ip_addresses &)
get_data_storage
storage_info
critical_load=$(( 1 + $(grep -c processor /proc/cpuinfo) / 2 ))
critical_load=$((1 + $(grep -c processor /proc/cpuinfo) / 2))

# get uptime, logged in users and load in one take
if [ -x /usr/bin/cpustat ];then
time=$(/usr/bin/cpustat -u)
load=$(/usr/bin/cpustat -l)
if [ -x /usr/bin/cpustat ]; then
time=$(/usr/bin/cpustat -u)
load=$(/usr/bin/cpustat -l)
else
UptimeString=$(uptime | tr -d ',')
time=$(awk -F" " '{print $3" "$4}' <<<"${UptimeString}")
load="$(awk -F"average: " '{print $2}'<<<"${UptimeString}")"
case ${time} in
UptimeString=$(uptime | tr -d ',')
time=$(awk -F" " '{print $3" "$4}' <<<"${UptimeString}")
load="$(awk -F"average: " '{print $2}' <<<"${UptimeString}")"
case ${time} in
1:*) # 1-2 hours
time=$(awk -F" " '{print $3" 小时"}' <<<"${UptimeString}")
time=$(awk -F" " '{print $3" h"}' <<<"${UptimeString}")
;;
*:*) # 2-24 hours
time=$(awk -F" " '{print $3" 小时"}' <<<"${UptimeString}")
time=$(awk -F" " '{print $3" h"}' <<<"${UptimeString}")
;;
*day) # days
days=$(awk -F" " '{print $3""}' <<<"${UptimeString}")
days=$(awk -F" " '{print $3"d"}' <<<"${UptimeString}")
time=$(awk -F" " '{print $5}' <<<"${UptimeString}")
time="$days "$(awk -F":" '{print $1"小时 "$2"分钟"}' <<<"${time}")
time="$days "$(awk -F":" '{print $1"h "$2"m"}' <<<"${time}")
;;
esac
esac
fi

# memory and swap
Expand All @@ -135,61 +129,59 @@ swap_usage=$( (awk '/Swap/ { printf("%3.0f", $3/$2*100) }' <<<${swap_info} 2>/de
swap_total=$(awk '{print $(2)}' <<<${swap_info})

if grep -q "ipq40xx" "/etc/openwrt_release"; then
cpu_temp="$(sensors | grep -Eo '\+[0-9]+.+C' | sed ':a;N;$!ba;s/\n/ /g;s/+//g')"
cpu_temp="$(sensors | grep -Eo '\+[0-9]+.+C' | sed ':a;N;$!ba;s/\n/ /g;s/+//g')"
elif [ -f "/sys/class/thermal/thermal_zone0/temp" ]; then
cpu_temp="$(awk '{ printf("%.1f °C", $0 / 1000) }' /sys/class/thermal/thermal_zone0/temp)"
cpu_temp="$(awk '{ printf("%.1f °C", $0 / 1000) }' /sys/class/thermal/thermal_zone0/temp)"
elif [ -f "/sys/class/hwmon/hwmon0/temp1_input" ]; then
cpu_temp="$(awk '{ printf("%.1f °C", $0 / 1000) }' /sys/class/hwmon/hwmon0/temp1_input)"
cpu_temp="$(awk '{ printf("%.1f °C", $0 / 1000) }' /sys/class/hwmon/hwmon0/temp1_input)"
else
cpu_temp="50.0 °C"
cpu_temp="50.0 °C"
fi
cpu_tempx=`echo $cpu_temp | sed 's/°C//g'`
cpu_tempx=$(echo $cpu_temp | sed 's/°C//g')

if [ -x /usr/bin/cpustat ];then
sys_temp=$(/usr/bin/cpustat -A)
if [ -x /usr/bin/cpustat ]; then
sys_temp=$(/usr/bin/cpustat -A)
else
sys_temp=$(cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c)
sys_temp=$(cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c)
fi
sys_tempx=`echo $sys_temp | sed 's/ / /g'`
sys_tempx=$(echo $sys_temp | sed 's/ / /g')

# display info

machine_model=$(cat /proc/device-tree/model|tr -d "\000")
echo -e "设备信息: \033[93m${machine_model}\033[0m"
printf "CPU 型号: \x1B[93m%s\x1B[0m" "$sys_tempx"
machine_model=$(cat /proc/device-tree/model | tr -d "\000")
echo -e " Device Model: \033[93m${machine_model}\033[0m"
printf " Architecture: \x1B[93m%s\x1B[0m" "$sys_tempx"
echo ""
display "系统负载" "${load%% *}" "${critical_load}" "0" "" "${load#* }"
printf "运行时间: \x1B[92m%s\x1B[0m\t\t" "$time"
display " Load Average" "${load%% *}" "${critical_load}" "0" "" "${load#* }"
printf "Uptime: \x1B[92m%s\x1B[0m\t\t" "$time"
echo ""

display "环境温度" "$cpu_tempx" "60" "0" "°C" ""
if [ -x /usr/bin/cpustat ];then
cpu_freq=$(/usr/bin/cpustat -F1500)
echo -n "当前频率: $cpu_freq"
display " Ambient Temp" "$cpu_tempx" "60" "0" "" "°C"
if [ -x /usr/bin/cpustat ]; then
cpu_freq=$(/usr/bin/cpustat -F1500)
echo -n "Frequency: $cpu_freq"
else
display "当前频率" "$cpu_freq" "1500" "0" " Mhz" ""
display "Frequency" "$cpu_freq" "1500" "0" " Mhz" ""
fi
echo ""

display "内存已用" "$memory_usage" "70" "0" "%" " of ${memory_total}MB"
printf "IP 地址: \x1B[92m%s\x1B[0m" "$ip_address"
#display "交换内存" "$swap_usage" "10" "0" "%" " of $swap_total""Mb"
display " Memory Usage" "$memory_usage" "70" "0" "%" " of ${memory_total}MB"
printf "IP Address: \x1B[92m%s\x1B[0m" "$ip_address"
#display "Swap Usage" "$swap_usage" "10" "0" "%" " of $swap_total""Mb"
echo ""

#echo "" # fixed newline

display "启动存储" "$boot_usage" "90" "1" "%" " of $boot_total"
display "系统存储" "$root_usage" "90" "1" "%" " of $root_total"
display " Boot Storage" "$boot_usage" "90" "1" "%" " of $boot_total"
display "SYS Storage" "$root_usage" "90" "1" "%" " of $root_total"
echo ""

if [ "$data_usage" != "" ];then
display "数据存储" "$data_usage" "90" "1" "%" " of $data_total"
echo ""
if [ "$data_usage" != "" ]; then
display " Data Storage" "$data_usage" "90" "1" "%" " of $data_total"
echo ""
fi
if [ "$media_usage" != "" ];then
display "媒体存储" "$media_usage" "90" "1" "%" " of $media_total"
echo ""
if [ "$media_usage" != "" ]; then
display " Data Storage" "$media_usage" "90" "1" "%" " of $media_total"
echo ""
fi
echo ""


0 comments on commit 219dd89

Please sign in to comment.