New self-contained motd script

This commit is contained in:
2024-07-17 22:04:31 -04:00
parent 61c36f2700
commit 27696ee36c

View File

@@ -1,7 +1,11 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# Install motd scripts # Install and generate motd
# Bryan C. Roessler # Bryan C. Roessler
main() {
if [[ " $1 " == " --motd " ]]; then
print_motd
else
parent="${BASH_SOURCE[0]}" parent="${BASH_SOURCE[0]}"
parent=${parent%/*} parent=${parent%/*}
@@ -9,71 +13,59 @@ parent=${parent%/*}
is_root is_root
DEBUG=0 cp "$0" /usr/local/bin/
[[ "$#" -gt 0 ]] && echo "Debug on" && DEBUG=1 script="/usr/local/bin/${0##*/}"
service="/usr/lib/systemd/system/motd.service"
timer="/usr/lib/systemd/system/motd.timer"
installdir="/usr/local/bin" install_services
[[ -d "$installdir" ]] || mkdir -p "$installdir"
if (( DEBUG )); then
script="generate-motd.sh"
else
script="$installdir/generate-motd.sh"
fi fi
}
cat <<- 'EOF' > "$script" print_motd() {
#!/usr/bin/env bash # colors
default='\e[0m'
green='\e[32m'
red='\e[31m'
dim='\e[2m'
undim='\e[0m'
echo -n ' # shellcheck disable=SC2016
echo -e '
_ _ _ _ _ _ _ _ _ _
| | | | | | | | | | | | | | | | | | | |
| |__| | __ _ _ __| |_ _ __ ___ __ _ _ __ | | __ _| |__ | |__| | __ _ _ __| |_ _ __ ___ __ _ _ __ | | __ _| |__
| __ |/ _` | `__| __| `_ ` _ \ / _` | `_ \ | | / _` | `_ \ | __ |/ _` | `__| __| `_ ` _ \ / _` | `_ \ | | / _` | `_ \
| | | | (_| | | | |_| | | | | | (_| | | | | | |___| (_| | |_) | | | | | (_| | | | |_| | | | | | (_| | | | | | |___| (_| | |_) |
|_| |_|\__,_|_| \__|_| |_| |_|\__,_|_| |_| |______\__,_|_.__/ |_| |_|\__,_|_| \__|_| |_| |_|\__,_|_| |_| |______\__,_|_.__/ '
'
EOF
# System info # System info
cat <<- 'EOF' >> "$script"
# get load averages # get load averages
IFS=" " read LOAD1 LOAD5 LOAD15 <<<$(cat /proc/loadavg | awk '{ print $1,$2,$3 }') IFS=" " read -r LOAD1 LOAD5 LOAD15 <<<"$(awk '{ print $1,$2,$3 }' /proc/loadavg)"
# get free memory # get free memory
IFS=" " read USED AVAIL TOTAL <<<$(free -htm | grep "Mem" | awk {'print $3,$7,$2'}) IFS=" " read -r USED AVAIL TOTAL <<<"$(free -htm | grep "Mem" | awk '{print $3,$7,$2}')"
# get processes # get processes
PROCESS=`ps -eo user=|sort|uniq -c | awk '{ print $2 " " $1 }'` PROCESS=$(ps -eo user=|sort|uniq -c | awk '{ print $2 " " $1 }')
PROCESS_ALL=`echo "$PROCESS"| awk {'print $2'} | awk '{ SUM += $1} END { print SUM }'` PROCESS_ALL=$(echo "$PROCESS"| awk '{print $2}' | awk '{ SUM += $1} END { print SUM }')
PROCESS_ROOT=`echo "$PROCESS"| grep root | awk {'print $2'}` PROCESS_ROOT=$(echo "$PROCESS"| grep root | awk '{print $2}')
PROCESS_USER=`echo "$PROCESS"| grep -v root | awk {'print $2'} | awk '{ SUM += $1} END { print SUM }'` PROCESS_USER=$(echo "$PROCESS"| grep -v root | awk '{print $2}' | awk '{ SUM += $1} END { print SUM }')
# get processors # get processors
PROCESSOR_NAME=`grep "model name" /proc/cpuinfo | cut -d ' ' -f3- | awk {'print $0'} | head -1` PROCESSOR_NAME=$(grep "model name" /proc/cpuinfo | cut -d ' ' -f3- | awk '{print $0}' | head -1)
PROCESSOR_COUNT=`grep -ioP 'processor\t:' /proc/cpuinfo | wc -l` PROCESSOR_COUNT=$(grep -ioP 'processor\t:' /proc/cpuinfo | wc -l)
W="\e[0;39m"
G="\e[1;32m"
echo -e " echo -e "
$W Distro......: $W`cat /etc/*release | grep "PRETTY_NAME" | cut -d "=" -f 2- | sed 's/"//g'` ${default}Distro......: $default$(cat /etc/*release | grep "PRETTY_NAME" | cut -d "=" -f 2- | sed 's/"//g')
$W Kernel......: $W`uname -sr` ${default}Kernel......: $default$(uname -sr)
$W Uptime......: $W`uptime -p` ${default}Uptime......: $default$(uptime -p)
$W Load........: $G$LOAD1$W (1m), $G$LOAD5$W (5m), $G$LOAD15$W (15m) ${default}Load........: $green$LOAD1$default (1m), $green$LOAD5$default (5m), $green$LOAD15$default (15m)
$W Processes...: $W$G$PROCESS_ROOT$W (root), $G$PROCESS_USER$W (user), $G$PROCESS_ALL$W (total) ${default}Processes...: $default$green$PROCESS_ROOT$default (root), $green$PROCESS_USER$default (user), $green$PROCESS_ALL$default (total)
$W CPU.........: $W$PROCESSOR_NAME ($G$PROCESSOR_COUNT$W vCPU) ${default}CPU.........: $default$PROCESSOR_NAME ($green$PROCESSOR_COUNT$default vCPU)
$W Memory......: $G$USED$W used, $G$AVAIL$W avail, $G$TOTAL$W total$W" ${default}Memory......: $green$USED$default used, $green$AVAIL$default avail, $green$TOTAL$default total$default"
EOF
# Disk usage # Disk usage
cat <<- 'EOF' >> "$script"
# config # config
max_usage=90 max_usage=90
bar_width=50 bar_width=50
# colors
white="\e[39m"
green="\e[1;32m"
red="\e[1;31m"
dim="\e[2m"
undim="\e[0m"
# disk usage: ignore zfs, squashfs & tmpfs # disk usage: ignore zfs, squashfs & tmpfs
while IFS= read -r line; do dfs+=("$line"); done < <(df -H -x zfs -x squashfs -x tmpfs -x devtmpfs -x overlay --output=target,pcent,size | tail -n+2) while IFS= read -r line; do dfs+=("$line"); done < <(df -H -x zfs -x squashfs -x tmpfs -x devtmpfs -x overlay --output=target,pcent,size | tail -n+2)
@@ -82,7 +74,7 @@ printf "\nDisk usage\n"
for line in "${dfs[@]}"; do for line in "${dfs[@]}"; do
# get disk usage # get disk usage
usage=$(echo "$line" | awk '{print $2}' | sed 's/%//') usage=$(echo "$line" | awk '{print $2}' | sed 's/%//')
used_width=$((($usage*$bar_width)/100)) used_width=$(((usage*bar_width)/100))
# color is green if usage < max_usage, else red # color is green if usage < max_usage, else red
if [ "${usage}" -ge "${max_usage}" ]; then if [ "${usage}" -ge "${max_usage}" ]; then
color=$red color=$red
@@ -91,12 +83,12 @@ for line in "${dfs[@]}"; do
fi fi
# print green/red bar until used_width # print green/red bar until used_width
bar="[${color}" bar="[${color}"
for ((i=0; i<$used_width; i++)); do for ((i=0; i<used_width; i++)); do
bar+="=" bar+="="
done done
# print dimmmed bar until end # print dimmmed bar until end
bar+="${white}${dim}" bar+="${default}${dim}"
for ((i=$used_width; i<$bar_width; i++)); do for ((i=used_width; i<bar_width; i++)); do
bar+="=" bar+="="
done done
bar+="${undim}]" bar+="${undim}]"
@@ -104,7 +96,6 @@ for line in "${dfs[@]}"; do
echo "${line}" | awk '{ printf("%-31s%+3s used out of %+4s\n", $1, $2, $3); }' | sed -e 's/^/ /' echo "${line}" | awk '{ printf("%-31s%+3s used out of %+4s\n", $1, $2, $3); }' | sed -e 's/^/ /'
echo -e "${bar}" | sed -e 's/^/ /' echo -e "${bar}" | sed -e 's/^/ /'
done done
EOF
# # Disk health # # Disk health
# cat <<- 'EOF' >> "$script" # cat <<- 'EOF' >> "$script"
@@ -113,11 +104,6 @@ EOF
# # set column width # # set column width
# COLUMNS=2 # COLUMNS=2
# # colors # # colors
# white="\e[39m"
# green="\e[1;32m"
# red="\e[1;31m"
# dim="\e[2m"
# undim="\e[0m"
# # disks to check # # disks to check
# disks=(sda sdb sdc sdd sde sdf sdg sdi) # disks=(sda sdb sdc sdd sde sdf sdg sdi)
@@ -179,55 +165,47 @@ EOF
# Services # Services
cat <<- 'EOF' >> "$script"
# set column width
COLUMNS=2 COLUMNS=2
# colors
green="\e[1;32m"
red="\e[1;31m"
undim="\e[0m"
services=("fail2ban" "firewalld" "nmb" "motion" "btrfs-balance.timer" "btrfs-scrub.timer" "smb" "backup.timer" "btrbk.timer" "fstrim.timer" "dnf-automatic.timer" "smartd" "cockpit.socket" "generate-motd.timer") services=(
# sort services btrfs-balance.timer btrfs-scrub.timer backup.timer btrbk.timer fstrim.timer
IFS=$'\n' services=($(sort <<<"${services[*]}")) fail2ban firewalld smb nmb motion smartd cockpit.socket
unset IFS dnf-automatic.timer motd.timer
)
service_status=() service_status=()
# get status of all services # get status of all services
for service in "${services[@]}"; do for service in "${services[@]}"; do
service_status+=($(systemctl is-active "$service")) service_status+=("$(systemctl is-active "$service")")
done done
out="" out=""
for i in ${!services[@]}; do for i in "${!services[@]}"; do
# color green if service is active, else red # color green if service is active, else red
if [[ "${service_status[$i]}" == "active" ]]; then if [[ "${service_status[$i]}" == "active" ]]; then
out+="${services[$i]}:,${green}${service_status[$i]}${undim}," out+="${services[$i]%.*}:,${green}${service_status[$i]}${undim},"
else else
out+="${services[$i]}:,${red}${service_status[$i]}${undim}," out+="${services[$i]%.*}:,${red}${service_status[$i]}${undim},"
fi fi
# insert \n every $COLUMNS column # insert \n every $COLUMNS column
if [ $((($i+1) % $COLUMNS)) -eq 0 ]; then if [[ $(((i+1) % COLUMNS)) -eq 0 ]]; then
out+="\n" out+="\n"
fi fi
done done
out+="\n"
printf "\nServices\n" printf "\nServices\n"
printf "$out" | column -ts $',' | sed -e 's/^/ /' printf "%b\n" "$out" | column -ts $',' | sed -e 's/^/ /'
EOF
# Fail2Ban # Fail2Ban
cat <<- 'EOF' >> "$script"
# fail2ban-client status to get all jails, takes about ~70ms # fail2ban-client status to get all jails, takes about ~70ms
jails=($(fail2ban-client status | grep "Jail list:" | sed "s/ //g" | awk '{split($2,a,",");for(i in a) print a[i]}')) read -r -a jails <<< "$(fail2ban-client status | grep "Jail list:" | sed "s/ //g" | awk '{split($2,a,",");for(i in a) print a[i]}')"
out="jail,failed,total,banned,total\n" out="jail,failed,total,banned,total\n"
for jail in ${jails[@]}; do for jail in "${jails[@]}"; do
# slow because fail2ban-client has to be called for every jail (~70ms per jail) # slow because fail2ban-client has to be called for every jail (~70ms per jail)
status=$(fail2ban-client status ${jail}) status=$(fail2ban-client status "$jail")
failed=$(echo "$status" | grep -ioP '(?<=Currently failed:\t)[[:digit:]]+') failed=$(echo "$status" | grep -ioP '(?<=Currently failed:\t)[[:digit:]]+')
totalfailed=$(echo "$status" | grep -ioP '(?<=Total failed:\t)[[:digit:]]+') totalfailed=$(echo "$status" | grep -ioP '(?<=Total failed:\t)[[:digit:]]+')
banned=$(echo "$status" | grep -ioP '(?<=Currently banned:\t)[[:digit:]]+') banned=$(echo "$status" | grep -ioP '(?<=Currently banned:\t)[[:digit:]]+')
@@ -235,14 +213,11 @@ for jail in ${jails[@]}; do
out+="$jail,$failed,$totalfailed,$banned,$totalbanned\n" out+="$jail,$failed,$totalfailed,$banned,$totalbanned\n"
done done
printf "\nfail2ban status\n" printf "\nFail2ban\n"
printf $out | column -ts $',' | sed -e 's/^/ /' printf "%b\n" "$out" | column -ts $',' | sed -e 's/^/ /'
EOF
# Help links # Help links
cat <<- 'EOF' >> "$script" echo -e '
echo "\
Links (ctrl+click to follow) Links (ctrl+click to follow)
Server Manual.........: https://tinyurl.com/jjz9h6fr Server Manual.........: https://tinyurl.com/jjz9h6fr
Cockpit (for admins)..: http://localhost:9090 Cockpit (for admins)..: http://localhost:9090
@@ -251,78 +226,25 @@ Links (ctrl+click to follow)
RStudio Server........: http://localhost:8787 RStudio Server........: http://localhost:8787
Robot Computer........: vnc://192.168.16.101:5900 Robot Computer........: vnc://192.168.16.101:5900
Windows 10 VM.........: vnc://localhost:5900 (pw: hartman) Windows 10 VM.........: vnc://localhost:5900 (pw: hartman)
" '
EOF
# Scheduled reboot # Scheduled reboot
cat <<- "EOF" >> "$script"
if systemctl is-active scheduled-reboot.timer &>/dev/null; then if systemctl is-active scheduled-reboot.timer &>/dev/null; then
echo -n "Next scheduled reboot: " echo -n "Next scheduled reboot: "
time=$(systemctl cat scheduled-reboot.timer | grep OnCalendar=) time=$(systemctl cat scheduled-reboot.timer | grep OnCalendar=)
time=${time#*=} time=${time#*=}
echo "$time" echo "$time"
fi fi
EOF }
# cat <<- 'EOF' > "$script" install_services() {
# #!/usr/bin/env bash
# [[ -v NO_MOTD ]] && exit 0
# USER=$(whoami)
# HOSTNAME=$(uname -n)
# ARRAY=$(df -Ph | grep array | awk '{print $4}' | tr -d '\n')
# ROOT=$(df -Ph | grep sdb3 | awk '{print $4}' | tr -d '\n')
# BACKUP=$(df -Ph | grep backup | awk '{print $4}' | tr -d '\n')
# MEMUSED=$(free -t -m | grep "Mem:" | awk '{print $3" MB";}')
# MEMTOTAL=$(free -t -m | grep "Mem:" | awk '{print $2" MB";}')
# PSA=$(ps -Afl | wc -l)
# SWAPMEM=$(free -m | tail -n 1 | awk '{print $3}')
# #System uptime
# uptime=$(cut -f1 -d. /proc/uptime)
# upDays=$((uptime/60/60/24))
# upHours=$((uptime/60/60%24))
# upMins=$((uptime/60%60))
# upSecs=$((uptime%60))
# #System load
# LOAD1=$(awk {'print $1'} /proc/loadavg)
# LOAD5=$(awk {'print $2'} /proc/loadavg)
# LOAD15=$(awk {'print $3'} /proc/loadavg)
# echo "
# ===========================================================================
# - Hostname............: $HOSTNAME
# - Release.............: $(cat /etc/redhat-release)
# - Users...............: Currently $(users | wc -w) user(s) logged on
# ===========================================================================
# - Current user........: $USER
# - CPU usage...........: $LOAD1, $LOAD5, $LOAD15 (1, 5, 15 min)
# - Memory used.........: $MEMUSED / $MEMTOTAL
# - Processes...........: $PSA running
# - System uptime.......: $upDays days $upHours hours $upMins minutes $upSecs seconds
# - Disk space HOME|ROOT: $ROOT remaining
# - Disk space ARRAY....: $ARRAY remaining
# - Disk space BACKUP...: $BACKUP remaining
# ===========================================================================
# "
# EOF
generate-services() {
service="/usr/lib/systemd/system/generate-motd.service"
timer="/usr/lib/systemd/system/generate-motd.timer"
cat <<-EOF > "$service" cat <<-EOF > "$service"
[Unit] [Unit]
Description=Generate MoTD Description=Generate MoTD
[Service] [Service]
Type=simple Type=simple
ExecStart=/usr/bin/bash -c '$script > /etc/motd' ExecStart=/usr/bin/bash -c '$script --motd > /etc/motd'
[Install] [Install]
WantedBy=default.target WantedBy=default.target
@@ -338,18 +260,12 @@ generate-services() {
[Install] [Install]
WantedBy=timers.target WantedBy=timers.target
EOF EOF
chmod +x "$script" &&
systemctl daemon-reload &&
systemctl enable --now "${timer##*/}"
} }
chmod +x "$script" main "$@"
if (( DEBUG )); then
bash generate-motd.sh
cat -n generate-motd.sh
rm generate-motd.sh
else
generate-services && \
systemctl daemon-reload && \
systemctl enable --now generate-motd.timer
fi
exit $? exit $?