2019-05-21 01:22:40 +02:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
### Time and date ###
|
|
|
|
echo -e "$(date)"
|
|
|
|
echo -e
|
|
|
|
|
|
|
|
|
|
|
|
### Hostname ###
|
2019-12-17 14:42:43 +01:00
|
|
|
echo -e "$(/usr/bin/env figlet -w $(tput cols) "$(hostname)" | /usr/bin/env lolcat -f -F 0.05 -S $(echo $((0x$(hostname | md5sum | cut -c 1-10))) | cut -c 2-5))"
|
2019-05-21 01:22:40 +02:00
|
|
|
|
|
|
|
echo -e
|
|
|
|
|
|
|
|
|
|
|
|
### System info ###
|
2020-02-05 01:34:10 +01:00
|
|
|
echo -e "$(cat /etc/*release | grep PRETTY_NAME | cut -d = -f 2- | sed 's/\"//g') | Kernel: $(uname -r)
|
|
|
|
Uptime: $(uptime -p | sed 's/up //')"
|
2019-05-21 01:22:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
### Services ###
|
|
|
|
# set column width
|
|
|
|
COLUMNS=1
|
|
|
|
# colors
|
|
|
|
green="\e[1;32m"
|
|
|
|
red="\e[1;31m"
|
|
|
|
undim="\e[0m"
|
|
|
|
|
2020-02-05 01:34:10 +01:00
|
|
|
services=($(cat /.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
|
|
|
|
|
|
|
|
out=""
|
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
|
|
|
|
out+="${services[$i]}:,${green}${service_status[$i]}${undim},"
|
|
|
|
else
|
|
|
|
out+="${services[$i]}:,${red}${service_status[$i]}${undim},"
|
|
|
|
fi
|
|
|
|
# insert \n every $COLUMNS column
|
|
|
|
if [ $((($i+1) % $COLUMNS)) -eq 0 ]; then
|
|
|
|
out+="\n"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
out+="\n"
|
|
|
|
|
|
|
|
printf "\nServices:\n"
|
|
|
|
printf "$out" | column -ts $',' | sed -e 's/^/ /'
|
2022-06-23 09:26:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
### Docker ###
|
2022-06-23 12:00:07 +02:00
|
|
|
containers=($(docker ps --format "{{.Image}}/{{.Names}}"))
|
|
|
|
container_statuses=($(docker ps --format "{{.State}}"))
|
2022-06-23 09:26:07 +02:00
|
|
|
out=""
|
|
|
|
for i in ${!containers[@]}; do
|
|
|
|
if [[ "${container_statuses[$i]}" == "running" ]]; then
|
|
|
|
out+="${containers[$i]}:,${green}${container_statuses[$i]}${undim},"
|
|
|
|
else
|
|
|
|
out+="${containers[$i]}:,${red}${container_statuses[$i]}${undim},"
|
|
|
|
fi
|
2022-06-23 12:00:07 +02:00
|
|
|
out+="\n"
|
2022-06-23 09:26:07 +02:00
|
|
|
done
|
|
|
|
printf "\nDocker:\n"
|
|
|
|
printf "$out" | column -ts $',' | sed -e 's/^/ /'
|
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
|
|
|
|
# colors
|
|
|
|
white="\e[39m"
|
|
|
|
green="\e[1;32m"
|
|
|
|
red="\e[1;31m"
|
2023-03-08 12:16:20 +01:00
|
|
|
magenta="\e[1;35m"
|
|
|
|
lightgreen="\e[1;92m"
|
|
|
|
lightred="\e[1;91m"
|
|
|
|
yellow="\e[1;33m"
|
|
|
|
lightyellow="\e[1;93m"
|
2019-05-21 01:22:40 +02:00
|
|
|
dim="\e[2m"
|
|
|
|
undim="\e[0m"
|
|
|
|
|
|
|
|
# disk usage: ignore zfs, squashfs & tmpfs
|
2023-03-08 12:16:20 +01:00
|
|
|
DISKS=$(df -H -x squashfs -x tmpfs -x devtmpfs -x zfs --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)
|
2023-03-08 12:24:45 +01:00
|
|
|
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")
|
2023-03-08 12:16:20 +01:00
|
|
|
useddspercent=$(echo "($usedds/$ttlsize)*100" | bc -l | numfmt --format="%3.0f")
|
2023-03-08 12:24:45 +01:00
|
|
|
usedchildpercent=$(echo "(($usedchild - $usedsnaprecursive)/$ttlsize)*100" | bc -l | numfmt --format="%3.0f")
|
2023-03-08 12:16:20 +01:00
|
|
|
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
|
2019-05-21 01:22:40 +02:00
|
|
|
printf "\nDisk usage:\n"
|
|
|
|
|
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
|
2023-03-08 12:16:20 +01:00
|
|
|
dds=$(echo "$line" | awk '{print $6}' | sed 's/%//')
|
|
|
|
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))
|
2019-05-21 01:22:40 +02:00
|
|
|
usage=$(echo "$line" | awk '{print $2}' | sed 's/%//')
|
|
|
|
used_width=$((($usage*$bar_width)/100))
|
2023-03-08 11:31:20 +01:00
|
|
|
|
2023-03-08 12:16:20 +01:00
|
|
|
#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
|
2019-05-21 01:22:40 +02:00
|
|
|
if [ "${usage}" -ge "${max_usage}" ]; then
|
2023-03-08 12:16:20 +01:00
|
|
|
bar+=$lightred
|
2019-05-21 01:22:40 +02:00
|
|
|
else
|
2023-03-08 12:16:20 +01:00
|
|
|
bar+=$lightgreen
|
2019-05-21 01:22:40 +02:00
|
|
|
fi
|
2023-03-08 12:16:20 +01:00
|
|
|
for ((; i<(($dds_width + $child_width)); i++)); do
|
2019-05-21 01:22:40 +02:00
|
|
|
bar+="="
|
|
|
|
done
|
2023-03-08 12:16:20 +01:00
|
|
|
|
|
|
|
# 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}"
|
2023-03-08 12:16:20 +01:00
|
|
|
for ((; i<$bar_width; i++)); do
|
2019-05-21 01:22:40 +02:00
|
|
|
bar+="="
|
|
|
|
done
|
2023-03-08 12:16:20 +01:00
|
|
|
|
|
|
|
# ]
|
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-03-08 12:16:20 +01:00
|
|
|
echo "${line}" | awk '{ printf("%-19scompratio %+5s %+4s/%+4s [%+3s]\n", $1, $8, $4, $3, $2); }' | sed -e 's/^/ /'
|
2019-05-21 01:22:40 +02:00
|
|
|
echo -e "${bar}" | sed -e 's/^/ /'
|
2020-02-15 10:13:24 +01:00
|
|
|
done <<< $(echo -e "$DISKS")
|
2019-05-21 01:22:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
### Welcome message ###
|
2021-08-29 16:25:08 +02:00
|
|
|
LAST_LOGIN=$(last -w --time-format iso | grep $(whoami) | head -2 | tail -1)
|
2019-05-21 01:22:40 +02:00
|
|
|
echo -e
|
|
|
|
echo -e "Welcome to $(hostname), $(whoami)!
|
|
|
|
Last login: $(echo "$LAST_LOGIN" | awk '{for(i=4;i<=NF;i++) print $i}' | paste -sd ' ') from $(echo "$LAST_LOGIN" | awk '{print $3}')"
|
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
|
|
|
|
echo -e "${red}Warning! ${white}$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
|