Created
October 18, 2023 05:10
-
-
Save ppsilv/5dbb62582e6fe60d09a1d4c3f3e8ac4d to your computer and use it in GitHub Desktop.
This file must be put into /etc/update-motd/ directory and chmoded to exectable.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # | |
| # 30-sysinfo - generate the system information | |
| # Copyright (c) 2015 Igor Pecovnik | |
| # define which hard drive you want to monitor | |
| #storage=/dev/sda1 | |
| storage=/dev/mmcblk0p2 | |
| # 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 "5" )) ]]; 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}') | |
| # workaround that it works on old and new | |
| free -w &> /dev/null | |
| if [[ $? -ne 0 ]]; then | |
| memory_usage=$(free | awk '/Mem/ {printf("%.0f",(($2-($4+$6+$7))/$2) * 100)}') | |
| else | |
| memory_usage=$(free -w | awk '/Mem/ {printf("%.0f",(($2-($4+$6+$7))/$2) * 100)}') | |
| fi | |
| memory_total=$(free -m | awk '/Mem/ {print $(2)}') | |
| users=$(users | wc -w) | |
| swap_usage=$(free -m | ( awk '/Swap/ { printf("%3.0f", $3/$2*100) }' 2>/dev/null || echo 0 ) | 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=$(hostname -I | tr " " "\n" | grep -E "^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$" | tail -n2 | tr "\n" " ") | |
| #ip_address=$(echo $ip_address | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' | sed 's/ /,/g') | |
| ip_address=$(hostname -I | awk -F ' ' '{print $1}') | |
| root_usage=$(df -h / | awk '/\// {print $(NF-1)}' | sed 's/%//g') | |
| root_total=$(df -h / | awk '/\// {print $(NF-4)}') | |
| hostname=$(hostname) | |
| 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 | |
| mkdir -p -m775 /etc/armbianmonitor/datasources/ | |
| # from axp via i2c for some old sunxi | |
| if [ -d "/sys/devices/platform/sunxi-i2c.0/i2c-0/0-0034/" ]; then | |
| board_temp=$(awk '{printf("%d",$1/1000)}' </sys/devices/platform/sunxi-i2c.0/i2c-0/0-0034/temp1_input) | |
| ln -fs /sys/devices/platform/sunxi-i2c.0/i2c-0/0-0034/temp1_input /etc/armbianmonitor/datasources/soctemp | |
| fi | |
| # if we are reading from A20 | |
| if [ -d "/sys/devices/platform/a20-tp-hwmon/" ]; then | |
| board_temp=$(awk '{printf("%d",$1/1000)}' </sys/devices/platform/a20-tp-hwmon/temp1_input) | |
| ln -fs /sys/devices/platform/a20-tp-hwmon/temp1_input /etc/armbianmonitor/datasources/soctemp | |
| fi | |
| # where it should be | |
| if [ -d "/sys/devices/virtual/thermal/thermal_zone0/" ]; then | |
| board_temp=$(awk '{printf("%d",$1/1000)}' </sys/devices/virtual/thermal/thermal_zone0/temp) | |
| ln -fs /sys/devices/virtual/thermal/thermal_zone0/temp /etc/armbianmonitor/datasources/soctemp | |
| fi | |
| # lemaker guitar / roseapple pi | |
| #source /etc/armbian-release | |
| #if [[ -d "/sys/devices/virtual/thermal/thermal_zone1/" && ${LINUXFAMILY} == "s500" ]]; then | |
| #board_temp=$(awk '{printf("%d",$1/1000)}' </sys/devices/virtual/thermal/thermal_zone1/temp) | |
| #ln -fs /sys/devices/virtual/thermal/thermal_zone1/temp /etc/armbianmonitor/datasources/soctemp | |
| #fi | |
| # H3 based boards, legacy kernel | |
| if [[ -d "/sys/class/thermal/thermal_zone1" && ${LINUXFAMILY} == "sun8i" ]]; then | |
| read board_temp </sys/class/thermal/thermal_zone1/temp | |
| ln -fs /sys/devices/virtual/thermal/thermal_zone1/temp /etc/armbianmonitor/datasources/soctemp | |
| fi | |
| # read ambient temperature from USB device | |
| if [[ -n $(which temper) && $(dpkg --print-architecture) == armhf ]]; then | |
| amb_temp=$(temper -c) | |
| if echo $amb_temp | egrep -qv "Couldn't find the USB device"; then | |
| amb_temp=$(echo $amb_temp | awk '{print $NF}' | sed 's/C//g') | |
| amb_temp=$(echo "scale=1;${amb_temp}/1" | bc) | |
| else | |
| amb_temp="" | |
| fi | |
| fi | |
| displaytime | |
| echo "" | |
| display "Local users" "$users" "1" "0" "" | |
| display "Machine name" "$hostname" "1" "0" "" "" | |
| display "System load" "$load" "1" "0" "" "" | |
| 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 "" |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This file when installed shows system information in terminal when you access the server via ssh.