From 80aab5e486df55ec66b831919bd5ba7165d5bf2d Mon Sep 17 00:00:00 2001 From: Bryan Roessler Date: Mon, 29 Jul 2024 18:15:27 -0400 Subject: [PATCH] Rename scripts old dir to .old --- .old/script-install-motd | 355 ++++++++++++++++++++++++++++++++++++ .old/script-install-motd.bk | 27 +++ .old/set-subuid-subgid | 10 + 3 files changed, 392 insertions(+) create mode 100755 .old/script-install-motd create mode 100755 .old/script-install-motd.bk create mode 100755 .old/set-subuid-subgid diff --git a/.old/script-install-motd b/.old/script-install-motd new file mode 100755 index 00000000..fcd1427b --- /dev/null +++ b/.old/script-install-motd @@ -0,0 +1,355 @@ +#!/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 $? \ No newline at end of file diff --git a/.old/script-install-motd.bk b/.old/script-install-motd.bk new file mode 100755 index 00000000..1ac526a4 --- /dev/null +++ b/.old/script-install-motd.bk @@ -0,0 +1,27 @@ +#!/usr/bin/env bash +# Install motd scripts + +[[ -f functions ]] && . functions || exit 1 + +is_root + +scripts=("10-hostname-color" "20-sysinfo" "35-diskspace" "36-diskstatus" "40-services" "50-fail2ban") + +git clone "https://github.com/yboetz/motd.git" + +systemdir="/etc/update-mot.d" + +[[ ! -d "$systemdir" ]] && mkdir -p "$systemdir" + +cd motd || exit $? + +for script in "${scripts[@]}"; do + echo "$script" + cp "$script" "$systemdir" +done + +cd .. + +rm -rf motd + +exit 0 \ No newline at end of file diff --git a/.old/set-subuid-subgid b/.old/set-subuid-subgid new file mode 100755 index 00000000..747cd5ec --- /dev/null +++ b/.old/set-subuid-subgid @@ -0,0 +1,10 @@ +#!/usr/bin/env bash +users=$(for s in /home/*; do echo "${s##*/}"; done) +subuid_start=100000 +incr=65536 +for user in ${users[@]}; do + uid=$(id -u "$user") + echo "$user:$subuid_start:$incr" >> /etc/subuid + echo "$user:$subuid_start:$incr" >> /etc/subgid + subuid_start=$((subuid_start + 100000)) +done \ No newline at end of file