123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355 |
- parent="${BASH_SOURCE[0]}"
- parent=${parent%/*}
- [[ -f "$parent"/script-functions ]] && . "$parent"/script-functions || exit 1
- is_root
- DEBUG=0
- [[ "$#" -gt 0 ]] && echo "Debug on" && DEBUG=1
- installdir="/usr/local/bin"
- [[ -d "$installdir" ]] || mkdir -p "$installdir"
- if (( DEBUG )); then
- script="generate-motd.sh"
- else
- script="$installdir/generate-motd.sh"
- fi
- cat <<- 'EOF' > "$script"
- echo -n '
- _ _ _ _ _
- | | | | | | | | | |
- | |__| | __ _ _ __| |_ _ __ ___ __ _ _ __ | | __ _| |__
- | __ |/ _` | `__| __| `_ ` _ \ / _` | `_ \ | | / _` | `_ \
- | | | | (_| | | | |_| | | | | | (_| | | | | | |___| (_| | |_) |
- |_| |_|\__,_|_| \__|_| |_| |_|\__,_|_| |_| |______\__,_|_.__/
- '
- EOF
- cat <<- 'EOF' >> "$script"
- IFS=" " read LOAD1 LOAD5 LOAD15 <<<$(cat /proc/loadavg | awk '{ print $1,$2,$3 }')
- IFS=" " read USED AVAIL TOTAL <<<$(free -htm | grep "Mem" | awk {'print $3,$7,$2'})
- 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_ROOT=`echo "$PROCESS"| grep root | awk {'print $2'}`
- PROCESS_USER=`echo "$PROCESS"| grep -v root | awk {'print $2'} | awk '{ SUM += $1} END { print SUM }'`
- 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`
- W="\e[0;39m"
- G="\e[1;32m"
- echo -e "
- $W Distro......: $W`cat /etc/*release | grep "PRETTY_NAME" | cut -d "=" -f 2- | sed 's/"//g'`
- $W Kernel......: $W`uname -sr`
- $W Uptime......: $W`uptime -p`
- $W Load........: $G$LOAD1$W (1m), $G$LOAD5$W (5m), $G$LOAD15$W (15m)
- $W Processes...: $W$G$PROCESS_ROOT$W (root), $G$PROCESS_USER$W (user), $G$PROCESS_ALL$W (total)
- $W CPU.........: $W$PROCESSOR_NAME ($G$PROCESSOR_COUNT$W vCPU)
- $W Memory......: $G$USED$W used, $G$AVAIL$W avail, $G$TOTAL$W total$W"
- EOF
- # Disk usage
- cat <<- 'EOF' >> "$script"
- # config
- max_usage=90
- 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
- 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)
- printf "\nDisk usage\n"
- for line in "${dfs[@]}"; do
- # get disk usage
- usage=$(echo "$line" | awk '{print $2}' | sed 's/%//')
- used_width=$((($usage*$bar_width)/100))
- if [ "${usage}" -ge "${max_usage}" ]; then
- color=$red
- else
- color=$green
- fi
- bar="[${color}"
- for ((i=0; i<$used_width; i++)); do
- bar+="="
- done
- bar+="${white}${dim}"
- for ((i=$used_width; i<$bar_width; i++)); do
- bar+="="
- done
- bar+="${undim}]"
- echo "${line}" | awk '{ printf("%-31s%+3s used out of %+4s\n", $1, $2, $3); }' | sed -e 's/^/ /'
- echo -e "${bar}" | sed -e 's/^/ /'
- done
- EOF
- cat <<- 'EOF' >> "$script"
- COLUMNS=2
- 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")
- IFS=$'\n' services=($(sort <<<"${services[*]}"))
- unset IFS
- service_status=()
- for service in "${services[@]}"; do
- service_status+=($(systemctl is-active "$service"))
- done
- out=""
- for i in ${!services[@]}; do
- if [[ "${service_status[$i]}" == "active" ]]; then
- out+="${services[$i]}:,${green}${service_status[$i]}${undim},"
- else
- out+="${services[$i]}:,${red}${service_status[$i]}${undim},"
- fi
- if [ $((($i+1) % $COLUMNS)) -eq 0 ]; then
- out+="\n"
- fi
- done
- out+="\n"
- printf "\nServices\n"
- printf "$out" | column -ts $',' | sed -e 's/^/ /'
- EOF
- cat <<- 'EOF' >> "$script"
- 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"
- for jail in ${jails[@]}; do
- status=$(fail2ban-client status ${jail})
- failed=$(echo "$status" | grep -ioP '(?<=Currently failed:\t)[[:digit:]]+')
- totalfailed=$(echo "$status" | grep -ioP '(?<=Total failed:\t)[[:digit:]]+')
- banned=$(echo "$status" | grep -ioP '(?<=Currently banned:\t)[[:digit:]]+')
- totalbanned=$(echo "$status" | grep -ioP '(?<=Total banned:\t)[[:digit:]]+')
- out+="$jail,$failed,$totalfailed,$banned,$totalbanned\n"
- done
- printf "\nfail2ban status\n"
- printf $out | column -ts $',' | sed -e 's/^/ /'
- EOF
- cat <<- 'EOF' >> "$script"
- echo "\
- Links (ctrl+click to follow)
- Server Manual.........: https://tinyurl.com/jjz9h6fr
- Cockpit (for admins)..: http://localhost:9090
- Robot Camera..........: http://localhost:9999
- JupyterLab............: http://localhost:8888
- RStudio Server........: http://localhost:8787
- Robot Computer........: vnc://192.168.16.101:5900
- Windows 10 VM.........: vnc://localhost:5900 (pw: hartman)
- "
- EOF
- cat <<- "EOF" >> "$script"
- if systemctl is-active scheduled-reboot.timer &>/dev/null; then
- echo -n "Next scheduled reboot: "
- time=$(systemctl cat scheduled-reboot.timer | grep OnCalendar=)
- time=${time#*=}
- echo "$time"
- fi
- EOF
- generate-services() {
- service="/usr/lib/systemd/system/generate-motd.service"
- timer="/usr/lib/systemd/system/generate-motd.timer"
- cat <<-EOF > "$service"
- [Unit]
- Description=Generate MoTD
- [Service]
- Type=simple
- ExecStart=/usr/bin/bash -c '$script > /etc/motd'
- [Install]
- WantedBy=default.target
- EOF
- cat <<-'EOF' > "$timer"
- [Unit]
- Description=Generate MoTD every minute on a timer
- [Timer]
- OnCalendar=*:0/1
- OnBootSec=10s
- [Install]
- WantedBy=timers.target
- EOF
- }
- chmod +x "$script"
- 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 $?
|