server-scripts/motd.sh

239 lines
7.4 KiB
Bash
Executable File

#!/usr/bin/env bash
dir=$(dirname "$0")
### Time and date ###
echo -e "$(date)"
echo -e
### Hostname ###
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)"
echo -e
### System info ###
echo -e "$(cat /etc/*release | grep PRETTY_NAME | cut -d = -f 2- | sed 's/\"//g') | Kernel: $(uname -r)"
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}"
echo -e
### Colors ###
white="\e[39m"
green="\e[1;32m"
red="\e[1;31m"
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"
undim="\e[0m"
graybg="\e[40m"
redbg="\e[41m"
greenbg="\e[42m"
### Services ###
services=($(cat "$dir/.motd.services"))
# sort services
#IFS=$'\n' services=($(sort <<<"${services[*]}"))
#unset IFS
service_status=()
# get status of all services
for service in "${services[@]}"; do
service_status+=($(systemctl is-active "$service"))
done
out=" SERVICE,STATUS\n"
out+="${bold}[SYSTEMD]${undim}\n"
for i in "${!services[@]}"; do
# color green if service is active, else red
if [[ "${service_status[$i]}" == "active" ]]; then
out+=" - ${services[$i]},${green}${service_status[$i]}${undim}\n"
elif [[ "${service_status[$i]}" != "inactive" ]] || [[ "$(systemctl is-enabled ${services[$i]})" == "enabled" ]]; then
out+=" ${redbg}/!\\\\${undim} - ${services[$i]},${red}${service_status[$i]}${undim}\n"
else
out+=" - ${services[$i]},${dim}${service_status[$i]} (disabled)${undim}\n"
fi
done
### Docker ###
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
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
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
printf "$out" | column -ts $',' -o " "
echo -e
### Disk space ###
# config
max_usage=80
bar_width=50
# disk usage: ignore zfs, squashfs & tmpfs
DISKS=$(df -H -x squashfs -x tmpfs -x devtmpfs -x zfs -x overlay --output=target,pcent,size,used | tail -n+2)" 0 0 0 -"
if test $(command -v zfs); then
DISKS+="\n"
#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"
done <<< $(zfs list -H -p -d 0 -o name,available,used,usedsnap,usedds,usedchild,compressratio)
fi
printf "${bold}[DISK USAGE]${undim}\n"
while read line; do
if [ -z "$line" ]; then
continue
fi
# get disk usage
dds=$(echo "$line" | awk '{print $6}' | sed 's/%//')
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))
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
# [
bar="["
# DDS
if [ "${usage}" -ge "${max_usage}" ]; then
bar+=$red
else
bar+=$green
fi
for ((; i<$dds_width; i++)); do
bar+="="
done
# CHILDREN
if [ "${usage}" -ge "${max_usage}" ]; then
bar+=$lightred
else
bar+=$lightgreen
fi
for ((; i<(($dds_width + $child_width)); i++)); do
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
bar+="${white}${dim}"
for ((; i<$bar_width; i++)); do
bar+="="
done
# ]
bar+="${undim}]"
# print usage line & bar
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}"
done <<< $(echo -e "$DISKS")
echo -e
### ZFS available space warning ###
if test $(command -v zfs); then
POOLS=$(zfs list -o name,used,available -p | tail -n+2)
while read line; do
name=$(echo "$line" | awk '{print $1}')
used=$(echo "$line" | awk '{print $2}')
avail=$(echo "$line" | awk '{print $3}')
percentage=$(echo "scale=2;$avail/($used+$avail)*100" | bc)
if (( $(echo "$percentage < 20" | bc -l) )); then
echo -e " ${redbg}/!\\\\${undim} ${bold}$name ${undim}only has ${percentage}% capacity left"
fi
done <<< $(echo -e "$POOLS")
fi
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 ' ')]"