MOTD now reads from sysfs on both sunxi kernels, implemented all alternative temp readings, fixed bugs
This commit is contained in:
parent
6ca0164177
commit
b93f90da02
@ -1,4 +1,5 @@
|
||||
#!/bin/bash
|
||||
export TERM=linux
|
||||
clear
|
||||
DISTROID=$(lsb_release -s -i)
|
||||
DISTROCODE=$(lsb_release -s -c)
|
||||
|
||||
@ -1,116 +1,84 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# 10-sysinfo - generate the system information
|
||||
# 30-sysinfo - generate the system information
|
||||
# Copyright (c) 2015 Igor Pecovnik
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation; either version 2 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License along
|
||||
# with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
|
||||
# define which hard drive you want to monitor
|
||||
storage=/dev/sda1
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
function displaytime {
|
||||
local T=$(cat /proc/uptime | awk '{print $1}' | sed 's/[.].*//')
|
||||
local D=$((T/60/60/24))
|
||||
local H=$((T/60/60%24))
|
||||
local M=$((T/60%60))
|
||||
local S=$((T%60))
|
||||
local time=$S
|
||||
time=$S" sec"
|
||||
(( $M > 0 )) && time=$M" min"
|
||||
(( $H > 0 )) && time=$H" hour"
|
||||
(( $H > 1 )) && time=$H" hours"
|
||||
(( $D > 0 )) && time=$D" day"
|
||||
(( $D > 1 )) && time=$D" days"
|
||||
#printf "Up time: \x1B[92m\t\t%s\t\x1B[0m" "$time"
|
||||
printf "%-13s%s" "Up time:"
|
||||
printf "\x1B[92m%-10s\x1B[0m\t\t" " $time"
|
||||
# we need dedicated function
|
||||
local T=$(cat /proc/uptime | awk '{print $1}' | sed 's/[.].*//')
|
||||
local D=$((T/60/60/24))
|
||||
local H=$((T/60/60%24))
|
||||
local M=$((T/60%60))
|
||||
local S=$((T%60))
|
||||
local time=$S
|
||||
time=$S" sec"
|
||||
(( $M > 0 )) && time=$M" min"
|
||||
(( $H > 0 )) && time=$H" hour"
|
||||
(( $H > 1 )) && time=$H" hours"
|
||||
(( $D > 0 )) && time=$D" day"
|
||||
(( $D > 1 )) && time=$D" days"
|
||||
printf "%-13s%s" "Up time:"
|
||||
printf "\x1B[92m%-10s\x1B[0m\t\t" " $time"
|
||||
}
|
||||
|
||||
function display()
|
||||
# $1=name $2=value $3=red_limit $4=minimal_show_limit $5=unit $6=after
|
||||
{
|
||||
if [[ -n "$2" && "$2" > "0" && (( "${2%.*}" -ge "$4" )) ]]; then
|
||||
printf "%-14s%s" "$1:"
|
||||
if (( $(echo "$2 > $3" | bc -l) )); then echo -ne "\e[0;91m $2"; else echo -ne "\e[0;92m $2"; fi
|
||||
printf "%-1s%s\x1B[0m" "$5"
|
||||
printf "%-11s%s\t" "$6"
|
||||
return 1
|
||||
fi
|
||||
|
||||
|
||||
|
||||
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" > "0" && (( "${2%.*}" -ge "$4" )) ]]; then
|
||||
printf "%-14s%s" "$1:"
|
||||
if (( $(echo "$2 $great $3" | bc -l) )); then echo -ne "\e[0;91m $2"; else echo -ne "\e[0;92m $2"; fi
|
||||
printf "%-1s%s\x1B[0m" "$5"
|
||||
printf "%-11s%s\t" "$6"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
# Battery info for Allwinner
|
||||
HARDWARE=$(cat /proc/cpuinfo | grep Hardware | awk '{print $3}')
|
||||
# for root users which have acces to hw and allwinner and old kernel
|
||||
if [[ "$UID" == 0 && ($HARDWARE = "sun7i" || $HARDWARE = "Allwinner") ]]; then
|
||||
if [ "$(printf '0x%X' $(($(i2cget -y -f 0 0x34 0x82) & 0xC3)))" != "0xC3" ]; then
|
||||
i2cset -y -f 0 0x34 0x82 0xC3
|
||||
fi
|
||||
POWER_OP_MODE=$(i2cget -y -f 0 0x34 0x01) # read power OPERATING MODE register @01h
|
||||
BAT_EXIST=$(($(($POWER_OP_MODE&0x20))/32)) # divide by 32 is like shifting rigth 5 times
|
||||
CHARG_IND=$(($(($POWER_OP_MODE&0x40))/64)) # divide by 64 is like shifting rigth 6 times
|
||||
|
||||
if [ "$BAT_EXIST" == "1" ]; then
|
||||
#read battery voltage 79h, 78h 0 mV -> 000h, 1.1 mV/bit FFFh -> 4.5045 V
|
||||
BAT_VOLT_LSB=$(i2cget -y -f 0 0x34 0x79)
|
||||
BAT_VOLT_MSB=$(i2cget -y -f 0 0x34 0x78)
|
||||
|
||||
BAT_BIN=$(( $(($BAT_VOLT_MSB << 4)) | $(($(($BAT_VOLT_LSB & 0xF0)) >> 4)) ))
|
||||
BAT_VOLT=$(echo "($BAT_BIN*1.1)"|bc)
|
||||
|
||||
# store maximum battery voltage to compare to
|
||||
if [ -f "/etc/default/battery" ]; then
|
||||
source "/etc/default/battery"
|
||||
else
|
||||
echo "MAX=$BAT_VOLT" > /etc/default/battery
|
||||
echo "MIN=3484" >> /etc/default/battery
|
||||
source "/etc/default/battery"
|
||||
fi
|
||||
# integer is enough, cut down the decimals
|
||||
|
||||
BAT_VOLT=${BAT_VOLT%.*}
|
||||
MAX=${MAX%.*}
|
||||
# if we have new max value, alter defaults
|
||||
if [[ "$CHARG_IND" == "0" && "$BAT_VOLT" -gt "$MAX" ]]; then
|
||||
MAX=$BAT_VOLT
|
||||
sed -e 's/MAX=.*/MAX='$BAT_VOLT'/g' -i /etc/default/battery
|
||||
fi
|
||||
|
||||
# if we have new min value, alter defaults
|
||||
if [ "$BAT_VOLT" -lt "$MIN" ]; then
|
||||
MIN=$BAT_VOLT
|
||||
#sed -e 's/MIN=.*/MIN='$BAT_VOLT'/g' -i /etc/default/battery
|
||||
fi
|
||||
|
||||
# calculate percentage
|
||||
batt_percent=$(echo "($BAT_VOLT-$MIN)*100/($MAX-$MIN)"|bc)
|
||||
|
||||
|
||||
# dispay charging / percentage
|
||||
if [[ "$CHARG_IND" == "1" && "$batt_percent" == "100" ]]; then
|
||||
batt_status=" charged"
|
||||
elif [ "$CHARG_IND" == "1" ]; then
|
||||
batt_status=" charging"
|
||||
else
|
||||
batt_status=" discharging"
|
||||
fi
|
||||
if [[ "$CHARG_IND" == "1" && "$batt_percent" -gt "100" ]]; then
|
||||
batt_status=" uncalibrated"
|
||||
batt_percent=100
|
||||
fi
|
||||
# kernel 4.4+
|
||||
axp_dir="/sys/power/axp_pmu"
|
||||
if [[ -e "$axp_dir" ]]; then
|
||||
status_battery_connected=$(cat $axp_dir/battery/connected)
|
||||
if [[ "$status_battery_connected" == "1" ]]; then
|
||||
status_battery_charging=$(cat $axp_dir/battery/charging)
|
||||
status_ac_connect=$(cat $axp_dir/ac/connected)
|
||||
battery_percent=$(cat $axp_dir/battery/percentage)
|
||||
# dispay charging / percentage
|
||||
if [[ "$status_ac_connect" == "1" && "$battery_percent" -lt "100" ]]; then
|
||||
status_battery_text=" charging"
|
||||
elif [[ "$status_ac_connect" == "1" && "$battery_percent" -eq "100" ]]; then
|
||||
status_battery_text=" charged"
|
||||
else
|
||||
status_battery_text=" discharging"
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
|
||||
|
||||
# legacy kernel
|
||||
axp_dir="/sys/class/power_supply"
|
||||
if [[ -e "$axp_dir" && -e "$axp_dir/battery" ]]; then
|
||||
status_battery_text=" "$(cat $axp_dir/battery/status | awk '{print tolower($0)}')
|
||||
battery_percent=$(cat $axp_dir/battery/capacity)
|
||||
fi
|
||||
|
||||
|
||||
|
||||
|
||||
load=$(cat /proc/loadavg | awk '{print $1}')
|
||||
memory_usage=$(free | awk '/Mem/ {printf("%.0f",(($2-($4+$6+$7))/$2) * 100)}')
|
||||
memory_total=$(free -m | awk '/Mem/ {print $(2)}')
|
||||
@ -119,30 +87,44 @@ swap_total=$(free -m | awk '/Swap/ { printf("%3.0f", $3/$2*100) }' | sed 's/ //g
|
||||
swap_usage=${swap_usage//[!0-9]/} # to remove alfanumeric if swap not used
|
||||
swap_total=$(free -m | awk '/Swap/ {print $(2)}')
|
||||
ip_address=$(ifconfig eth0 | sed -n '/inet addr/s/.*addr.\([^ ]*\) .*/\1/p')
|
||||
|
||||
root_usage=$(df -h / | awk '/\// {print $(NF-1)}' | sed 's/%//g')
|
||||
root_total=$(df -h / | awk '/\// {print $(NF-4)}')
|
||||
storage_usage=$(df -h $storage | grep $storage | awk '/\// {print $(NF-1)}' | sed 's/%//g')
|
||||
storage_total=$(df -h $storage | grep $storage | awk '/\// {print $(NF-4)}')
|
||||
hdd_temp=$(hddtemp -u C -nq /dev/sda)
|
||||
if [ -e "$storage" ]; then
|
||||
storage_usage=$(df -h $storage | grep $storage | awk '/\// {print $(NF-1)}' | sed 's/%//g')
|
||||
storage_total=$(df -h $storage | grep $storage | awk '/\// {print $(NF-4)}')
|
||||
hdd_temp=$(hddtemp -u C -nq $storage)
|
||||
fi
|
||||
|
||||
# read temperature from two different locations
|
||||
# read temperature from different locations
|
||||
|
||||
# from axp via i2c for some old sunxi
|
||||
if [ -d "/sys/devices/platform/sunxi-i2c.0/i2c-0/0-0034/" ]; then
|
||||
board_temp=$(cat /sys/devices/platform/sunxi-i2c.0/i2c-0/0-0034/temp1_input | awk '{printf("%d",$1/1000)}')
|
||||
fi
|
||||
|
||||
# if we are reading from A20
|
||||
if [ -d "/sys/devices/platform/a20-tp-hwmon/" ]; then
|
||||
board_temp=$(cat /sys/devices/platform/a20-tp-hwmon/temp1_input | awk '{printf("%d",$1/1000)}')
|
||||
fi
|
||||
|
||||
# where it should be
|
||||
if [ -d "/sys/devices/virtual/thermal/thermal_zone0/" ]; then
|
||||
board_temp=$(cat /sys/devices/virtual/thermal/thermal_zone0/temp | awk '{printf("%d",$1/1000)}')
|
||||
fi
|
||||
|
||||
# lemaker guitar
|
||||
if [[ -d "/sys/devices/virtual/thermal/thermal_zone1/" && $board_temp == "0" ]]; then
|
||||
board_temp=$(cat /sys/devices/virtual/thermal/thermal_zone1/temp | awk '{printf("%d",$1/1000)}')
|
||||
fi
|
||||
|
||||
# read ambient temperature from USB device
|
||||
if which temper >/dev/null; then
|
||||
TEMPER=$(temper -c)
|
||||
if echo $TEMPER | egrep -qv "Couldn't find the USB device"; then
|
||||
TEMPER=$(echo "scale=1;${TEMPER}/1" | bc)
|
||||
TEMPER="- Ambient: ${TEMPER}"
|
||||
else
|
||||
TEMPER=""
|
||||
fi
|
||||
amb_temp=$(temper -c)
|
||||
if echo $amb_temp | egrep -qv "Couldn't find the USB device"; then
|
||||
amb_temp=$(echo "scale=1;${amb_temp}/1" | bc)
|
||||
else
|
||||
amb_temp=""
|
||||
fi
|
||||
fi
|
||||
|
||||
display "System load" "$load" "1" "0" "" ""
|
||||
@ -153,15 +135,15 @@ display "Memory usage" "$memory_usage" "10" "0" " %" " of $memory_total""Mb"
|
||||
display "Swap usage" "$swap_usage" "10" "0" " %" " of $swap_total""Mb"
|
||||
echo "" # fixed newline
|
||||
a=0;b=0;c=0
|
||||
display "Board temp" "$board_temp" "10" "0" "°C" "" ; a=$?
|
||||
display "HDD temp" "$hdd_temp" "40" "0" "°C" "" ; b=$?
|
||||
display "CPU temp" "$board_temp" "50" "0" "°C" "" ; a=$?
|
||||
display "HDD temp" "$hdd_temp" "50" "0" "°C" "" ; b=$?
|
||||
display "Ambient temp" "$amb_temp" "40" "0" "°C" "" ; c=$?
|
||||
(( ($a+$b+$c) >0 )) && echo "" # new line only if some value is displayed
|
||||
a=0;b=0;c=0
|
||||
display "Battery" "$batt_percent" "100" "1" "%" "$batt_status" ; a=$?
|
||||
display "Battery" "$battery_percent" "20" "1" "%" "$status_battery_text" ; a=$?
|
||||
(( ($a+$b+$c) >0 )) && echo "" # new line only if some value is displayed
|
||||
|
||||
display "Usage of /" "$root_usage" "90" "1" "%" " of $root_total"
|
||||
display "storage/" "$storage_usage" "90" "1" "%" " of $storage_total"
|
||||
echo ""
|
||||
echo ""
|
||||
echo ""
|
||||
Loading…
Reference in New Issue
Block a user