armbian-build/scripts/update-motd.d/30-sysinfo
Igor Pečovnik dc0d8d35d7 bugfix
2016-01-15 17:46:46 +01:00

168 lines
6.1 KiB
Bash

#!/bin/bash
#
# 10-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.
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"
}
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
}
# 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
fi
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)}')
users=$(users | wc -w)
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)
# read temperature from two different locations
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
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
fi
display "System load" "$load" "1" "0" "" ""
displaytime
display "Local users" "$users" "3" "2" ""
echo "" # fixed newline
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 "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=$?
(( ($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 ""