123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355 |
- #!/usr/bin/env bash
- # Install motd scripts
- # Bryan C. Roessler
- 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"
- #!/usr/bin/env bash
- echo -n '
- _ _ _ _ _
- | | | | | | | | | |
- | |__| | __ _ _ __| |_ _ __ ___ __ _ _ __ | | __ _| |__
- | __ |/ _` | `__| __| `_ ` _ \ / _` | `_ \ | | / _` | `_ \
- | | | | (_| | | | |_| | | | | | (_| | | | | | |___| (_| | |_) |
- |_| |_|\__,_|_| \__|_| |_| |_|\__,_|_| |_| |______\__,_|_.__/
- '
- EOF
- # System info
- cat <<- 'EOF' >> "$script"
- # get load averages
- IFS=" " read LOAD1 LOAD5 LOAD15 <<<$(cat /proc/loadavg | awk '{ print $1,$2,$3 }')
- # get free memory
- IFS=" " read USED AVAIL TOTAL <<<$(free -htm | grep "Mem" | awk {'print $3,$7,$2'})
- # get processes
- 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 }'`
- # get processors
- 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))
- # color is green if usage < max_usage, else red
- if [ "${usage}" -ge "${max_usage}" ]; then
- color=$red
- else
- color=$green
- fi
- # print green/red bar until used_width
- bar="[${color}"
- for ((i=0; i<$used_width; i++)); do
- bar+="="
- done
- # print dimmmed bar until end
- bar+="${white}${dim}"
- for ((i=$used_width; i<$bar_width; i++)); do
- bar+="="
- done
- bar+="${undim}]"
- # print usage line & bar
- 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
- # # Disk health
- # cat <<- 'EOF' >> "$script"
- # # config
- # MAX_TEMP=40
- # # set column width
- # COLUMNS=2
- # # colors
- # white="\e[39m"
- # green="\e[1;32m"
- # red="\e[1;31m"
- # dim="\e[2m"
- # undim="\e[0m"
- # # disks to check
- # disks=(sda sdb sdc sdd sde sdf sdg sdi)
- # disknames=(sda sdb sdc sdd sde sdf sdg sdi)
- # # hddtemp
- # hddtemp_host=localhost
- # hddtemp_port=7634
- # # logfiles to check
- # logfiles='/var/log/syslog /var/log/syslog.1'
- # # get all lines with smartd entries from syslog
- # lines=$(tac $logfiles | grep -hiP 'smartd\[[[:digit:]]+\]:' | grep -iP "previous self-test")
- # # use nc to query temps from hddtemp daemon
- # hddtemp=$(timeout 0.01 nc $hddtemp_host $hddtemp_port | sed 's/|//m' | sed 's/||/ \n/g')
- # out=""
- # for i in "${!disks[@]}"; do
- # disk=${disks[$i]}
- # # use disknames if given
- # diskname=${disknames[$i]}
- # if [ -z "${diskname}" ]; then
- # diskname=$disk
- # fi
- # uuid=$(blkid -s UUID -o value "/dev/${disk}")
- # status=$( (grep "${uuid}" <<< "${lines}") | grep -m 1 -oP "previous self-test.*" | awk '{ print $4 " " $5 }')
- # temp=$( (grep "${disk}" <<< "${hddtemp}") | awk -F'|' '{ print $3 }')
- # # color green if temp <= MAX_TEMP, else red
- # if [[ "${temp}" -gt "${MAX_TEMP}" ]]; then
- # color=$red
- # else
- # color=$green
- # fi
- # # add "C" if temp is numeric
- # if [[ "$temp" =~ ^[0-9]+$ ]]; then
- # temp="${temp}C"
- # fi
- # # color green if status is "without error", else red
- # if [[ "${status}" == "without error" ]]; then
- # status_color=$green
- # else
- # status_color=$red
- # fi
- # # print temp & smartd error
- # out+="${diskname}:,${color}${temp}${undim} | ${status_color}${status}${undim},"
- # # insert \n every $COLUMNS column
- # if [ $((($i+1) % $COLUMNS)) -eq 0 ]; then
- # out+="\n"
- # fi
- # done
- # out+="\n"
- # printf "\ndisk status:\n"
- # printf "$out" | column -ts $',' | sed -e 's/^/ /'
- # EOF
- # Services
- cat <<- 'EOF' >> "$script"
- # set column width
- 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")
- # 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=""
- 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},"
- 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/^/ /'
- EOF
- # Fail2Ban
- cat <<- 'EOF' >> "$script"
- # 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]}'))
- out="jail,failed,total,banned,total\n"
- for jail in ${jails[@]}; do
- # slow because fail2ban-client has to be called for every jail (~70ms per jail)
- 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
- # Help links
- 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
- # Scheduled reboot
- 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
- # cat <<- 'EOF' > "$script"
- # #!/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"
- [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 $?
|