server-scripts/motd.sh

239 lines
7.4 KiB
Bash
Raw Normal View History

2023-08-19 19:31:39 +02:00
#!/usr/bin/env bash
dir=$(dirname "$0")
2019-05-21 01:22:40 +02:00
### Time and date ###
echo -e "$(date)"
echo -e
### Hostname ###
2023-06-05 15:45:45 +02:00
seed=$(echo $((0x$(hostname | md5sum | cut -c 1-10))) | cut -c 2-5)
echo -e "$(/usr/bin/env figlet -w $(tput cols) "$(hostname)" | /usr/bin/env lolcat -f -F 0.05 -S $seed)"
2019-05-21 01:22:40 +02:00
echo -e
### System info ###
2023-06-05 15:45:45 +02:00
echo -e "$(cat /etc/*release | grep PRETTY_NAME | cut -d = -f 2- | sed 's/\"//g') | Kernel: $(uname -r)"
2023-08-19 19:31:39 +02:00
uptime=$(uptime | awk -F'( |,|:)+' '{d=h=m=0; if ($7=="min") m=$6; else {if ($7~/^day/) {d=$6;h=$8;m=$9} else {h=$6;m=$7}}} {print d+0,"days,",h+0,"hours,",m+0,"minutes."}')
echo -e "Uptime: ${uptime}"
2023-06-05 15:45:45 +02:00
echo -e
2019-05-21 01:22:40 +02:00
2023-06-05 15:45:45 +02:00
### Colors ###
white="\e[39m"
2019-05-21 01:22:40 +02:00
green="\e[1;32m"
red="\e[1;31m"
2023-06-05 15:45:45 +02:00
magenta="\e[1;35m"
lightgreen="\e[1;92m"
lightred="\e[1;91m"
yellow="\e[1;33m"
lightyellow="\e[1;93m"
dim="\e[2m"
bold="\e[1m"
2019-05-21 01:22:40 +02:00
undim="\e[0m"
2023-06-05 15:45:45 +02:00
graybg="\e[40m"
redbg="\e[41m"
greenbg="\e[42m"
### Services ###
2023-08-19 19:31:39 +02:00
services=($(cat "$dir/.motd.services"))
2019-05-21 01:22:40 +02:00
# sort services
2021-10-04 11:35:37 +02:00
#IFS=$'\n' services=($(sort <<<"${services[*]}"))
#unset IFS
2019-05-21 01:22:40 +02:00
service_status=()
# get status of all services
for service in "${services[@]}"; do
service_status+=($(systemctl is-active "$service"))
done
2023-06-05 15:45:45 +02:00
out=" SERVICE,STATUS\n"
out+="${bold}[SYSTEMD]${undim}\n"
2022-06-23 12:00:07 +02:00
for i in "${!services[@]}"; do
2019-05-21 01:22:40 +02:00
# color green if service is active, else red
if [[ "${service_status[$i]}" == "active" ]]; then
2023-06-05 15:45:45 +02:00
out+=" - ${services[$i]},${green}${service_status[$i]}${undim}\n"
elif [[ "${service_status[$i]}" != "inactive" ]] || [[ "$(systemctl is-enabled ${services[$i]})" == "enabled" ]]; then
2023-06-05 15:45:45 +02:00
out+=" ${redbg}/!\\\\${undim} - ${services[$i]},${red}${service_status[$i]}${undim}\n"
else
out+=" - ${services[$i]},${dim}${service_status[$i]} (disabled)${undim}\n"
2019-05-21 01:22:40 +02:00
fi
done
2022-06-23 09:26:07 +02:00
### Docker ###
2023-08-19 19:43:01 +02:00
if command -v docker &> /dev/null; then
out+="${bold}[DOCKER]${undim}\n"
containers=($(docker ps --format "{{.Image}}/{{.Names}}"))
container_statuses=($(docker ps --format "{{.State}}"))
docker_out=" [root]\n"
for i in ${!containers[@]}; do
if [[ "${container_statuses[$i]}" == "running" ]]; then
2023-08-19 19:43:01 +02:00
docker_out+=" - ${containers[$i]},${green}${container_statuses[$i]}${undim}\n"
else
2023-08-19 19:43:01 +02:00
docker_out+=" ${redbg}/!\\\\${undim} - ${containers[$i]},${red}${container_statuses[$i]}${undim}\n"
fi
done
2023-08-19 19:43:01 +02:00
docker_out+=" [rootless]\n"
rootless_users=($(cat "$dir/.motd.users"))
for user in ${rootless_users[@]}; do
if sudo test ! -e /run/user/$(id -u $user)/docker.sock; then
docker_out+=" ${redbg}/!\\\\${undim} - $user,${red}! Docker not running !${undim}\n"
continue
fi
containers=($(sudo -n DOCKER_HOST=unix:///run/user/$(id -u $user)/docker.sock docker ps --format "{{.Image}}/{{.Names}}"))
container_statuses=($(sudo -n DOCKER_HOST=unix:///run/user/$(id -u $user)/docker.sock docker ps --format "{{.State}}"))
if [ ${#containers[@]} -eq 0 ]; then
docker_out+=" ${redbg}/!\\\\${undim} - $user,${red}! No container !${undim}\n"
continue
fi
docker_out+=" - $user\n"
for i in ${!containers[@]}; do
if [[ "${container_statuses[$i]}" == "running" ]]; then
docker_out+=" - ${containers[$i]},${green}${container_statuses[$i]}${undim}\n"
else
docker_out+=" ${redbg}/!\\\\${undim} - ${containers[$i]},${red}${container_statuses[$i]}${undim}\n"
fi
done
done
out+=$docker_out
fi
2023-06-05 15:45:45 +02:00
printf "$out" | column -ts $',' -o " "
echo -e
2019-05-21 01:22:40 +02:00
### Disk space ###
# config
2021-10-04 11:35:37 +02:00
max_usage=80
2019-05-21 01:22:40 +02:00
bar_width=50
# disk usage: ignore zfs, squashfs & tmpfs
2024-03-21 10:12:30 +01:00
DISKS=$(df -H -x squashfs -x tmpfs -x devtmpfs -x zfs -x overlay --output=target,pcent,size,used | tail -n+2)" 0 0 0 -"
2020-02-15 10:13:24 +01:00
2023-03-08 11:31:20 +01:00
if test $(command -v zfs); then
2020-02-15 10:13:24 +01:00
DISKS+="\n"
2023-03-08 11:31:20 +01:00
#DISKS+=$(zpool list -o name,capacity,size | tail -n+2)
while read name available used usedsnap usedds usedchild compressratio; do
ttlsize=$((($available+$used)))
pcent=$(echo "($used/$ttlsize)*100" | bc -l | numfmt --format="%3.0f")
gfxsize=$(numfmt --to=iec --format="%9f" $ttlsize)
gfxused=$(numfmt --to=iec --format="%9f" $used)
usedsnaprecursive=$(zfs get usedbysnapshots -r "$name" -H -p -t filesystem -o value | tail -n+2 | awk '{s+=$1} END {printf "%.0f", s}')
usedsnappercent=$(echo "(($usedsnap + $usedsnaprecursive)/$ttlsize)*100" | bc -l | numfmt --format="%3.0f")
useddspercent=$(echo "($usedds/$ttlsize)*100" | bc -l | numfmt --format="%3.0f")
usedchildpercent=$(echo "(($usedchild - $usedsnaprecursive)/$ttlsize)*100" | bc -l | numfmt --format="%3.0f")
DISKS+="$name $pcent% $gfxsize $gfxused $usedsnappercent $useddspercent $usedchildpercent x$compressratio\n"
2023-03-08 11:31:20 +01:00
done <<< $(zfs list -H -p -d 0 -o name,available,used,usedsnap,usedds,usedchild,compressratio)
2020-02-15 10:13:24 +01:00
fi
2023-06-05 15:45:45 +02:00
printf "${bold}[DISK USAGE]${undim}\n"
2019-05-21 01:22:40 +02:00
2020-02-15 10:13:24 +01:00
while read line; do
if [ -z "$line" ]; then
2023-03-08 11:31:20 +01:00
continue
2020-02-15 10:13:24 +01:00
fi
2019-05-21 01:22:40 +02:00
# get disk usage
dds=$(echo "$line" | awk '{print $6}' | sed 's/%//')
2024-03-21 10:12:30 +01:00
if [ -z "$dds" ]; then
dds_width=0
child_width=0
snap_width=0
usage=$(echo "$line" | sed -E "s/.+([0-9]{2,3})%.+/\1/")
else
dds_width=$((($dds*$bar_width)/100))
child=$(echo "$line" | awk '{print $7}' | sed 's/%//')
child_width=$((($child*$bar_width)/100))
snap=$(echo "$line" | awk '{print $5}' | sed 's/%//')
snap_width=$((($snap*$bar_width)/100))
2023-03-08 11:31:20 +01:00
2024-03-21 10:12:30 +01:00
usage=$(echo "$line" | awk '{print $2}' | sed 's/%//')
fi
used_width=$((($usage*$bar_width)/100))
#echo "$line"
#echo "$dds,$dds_width|$child,$child_width|$snap,$snap_width|$usage,$used_width"
i=0
2024-03-21 10:12:30 +01:00
# [
bar="["
# DDS
if [ "${usage}" -ge "${max_usage}" ]; then
bar+=$red
else
bar+=$green
fi
for ((; i<$dds_width; i++)); do
bar+="="
done
# CHILDREN
2019-05-21 01:22:40 +02:00
if [ "${usage}" -ge "${max_usage}" ]; then
bar+=$lightred
2019-05-21 01:22:40 +02:00
else
bar+=$lightgreen
2019-05-21 01:22:40 +02:00
fi
for ((; i<(($dds_width + $child_width)); i++)); do
2019-05-21 01:22:40 +02:00
bar+="="
done
# SNAP
bar+=$lightyellow
for ((; i<(($dds_width + $child_width + $snap_width)); i++)); do
bar+="="
done
# TOTAL USAGE
if [ "${usage}" -ge "${max_usage}" ]; then
bar+=$red
else
bar+=$green
fi
for ((; i<$used_width; i++)); do
bar+="="
done
# FREE SPACE
2019-05-21 01:22:40 +02:00
bar+="${white}${dim}"
for ((; i<$bar_width; i++)); do
2019-05-21 01:22:40 +02:00
bar+="="
done
# ]
2019-05-21 01:22:40 +02:00
bar+="${undim}]"
2023-03-08 11:31:20 +01:00
2019-05-21 01:22:40 +02:00
# print usage line & bar
2023-06-05 15:45:45 +02:00
echo -e "${line}" | awk '{ printf(" %-19scompratio %+5s %+4s/%+4s [%+3s]\n", $1, $8, $4, $3, $2); }'
if [ "${usage}" -ge "${max_usage}" ]; then
echo -en " ${redbg}/!\\\\${undim} "
else
echo -en " "
fi
echo -e "${bar}"
2020-02-15 10:13:24 +01:00
done <<< $(echo -e "$DISKS")
2019-05-21 01:22:40 +02:00
echo -e
2020-02-15 10:13:24 +01:00
### ZFS available space warning ###
if test $(command -v zfs); then
2021-10-04 11:35:37 +02:00
POOLS=$(zfs list -o name,used,available -p | tail -n+2)
2020-02-15 10:13:24 +01:00
while read line; do
name=$(echo "$line" | awk '{print $1}')
2021-10-04 11:35:37 +02:00
used=$(echo "$line" | awk '{print $2}')
avail=$(echo "$line" | awk '{print $3}')
2020-02-15 10:13:24 +01:00
2021-10-04 11:35:37 +02:00
percentage=$(echo "scale=2;$avail/($used+$avail)*100" | bc)
if (( $(echo "$percentage < 20" | bc -l) )); then
2023-06-05 15:45:45 +02:00
echo -e " ${redbg}/!\\\\${undim} ${bold}$name ${undim}only has ${percentage}% capacity left"
2020-02-15 10:13:24 +01:00
fi
done <<< $(echo -e "$POOLS")
2021-10-04 11:35:37 +02:00
fi
2023-06-05 15:45:45 +02:00
echo -e
### Welcome message ###
LAST_LOGIN=$(last -w --time-format full | grep $(whoami) | head -2 | tail -1)
echo -e " Welcome to $(hostname | /usr/bin/env lolcat -f -F 0.05 -S $seed), ${green}$(whoami)${undim}!"
echo -e " Last login: ${bold}$(echo "$LAST_LOGIN" | awk '{print $3}')${undim} | [$(echo "$LAST_LOGIN" | awk '{for(i=4;i<=NF;i++) print $i}' | paste -sd ' ')]"