armbian-build/scripts/update-motd.d/30-sysinfo

159 lines
5.3 KiB
Bash

#!/bin/bash
#
# 30-sysinfo - generate the system information
# Copyright (c) 2015 Igor Pecovnik
# define which hard drive you want to monitor
storage=/dev/sda1
# don't edit below here
function displaytime {
# 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 "Up time: "
printf "\x1B[92m%s\x1B[0m\t\t" "$time"
}
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
# 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/charger/charging)
status_ac_connect=$(cat $axp_dir/ac/connected)
battery_percent=$(cat $axp_dir/battery/capacity)
# 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
if [[ (("$(cat $axp_dir/battery/voltage_now)" -gt "0" )) ]]; then
status_battery_text=" "$(cat $axp_dir/battery/status | awk '{print tolower($0)}')
battery_percent=$(cat $axp_dir/battery/capacity)
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 -a) | sed -n '/inet addr/s/.*addr.\([^ ]*\) .*/\1/p' | head -1)
root_usage=$(df -h / | awk '/\// {print $(NF-1)}' | sed 's/%//g')
root_total=$(df -h / | awk '/\// {print $(NF-4)}')
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)}')
[[ "$storage" == */sd* ]] && hdd_temp=$(hddtemp -u C -nq $storage)
fi
# 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
# H3 based boards, legacy kernel
if [[ -d "/sys/class/thermal/thermal_zone1" && $board_temp == "0" ]]; then
board_temp=$(cat /sys/class/thermal/thermal_zone1/temp)
fi
# read ambient temperature from USB device
if which temper >/dev/null; then
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" "" ""
displaytime
display "Local users" "$users" "3" "2" ""
echo "" # fixed newline
display "Memory usage" "$memory_usage" "70" "0" " %" " of $memory_total""Mb"
display "Swap usage" "$swap_usage" "10" "0" " %" " of $swap_total""Mb"
printf "IP: "
printf "\x1B[92m%s\x1B[0m" "$ip_address"
echo "" # fixed newline
a=0;b=0;c=0
# adjustment for marvell armada. Readings were done on heatsink
[[ "$(cat /proc/cpuinfo | grep Marvell)" != "" && $board_temp -gt "60" ]] && board_temp=$(($board_temp-30))
display "CPU temp" "$board_temp" "45" "0" "°C" "" ; a=$?
display "HDD temp" "$hdd_temp" "45" "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
display "Usage of /" "$root_usage" "90" "1" "%" " of $root_total"
display "storage/" "$storage_usage" "90" "1" "%" " of $storage_total"
display "Battery" "$battery_percent" "20" "1" "%" "$status_battery_text"
echo ""
echo ""