script-install-motd 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. #!/usr/bin/env bash
  2. # Install and generate motd
  3. # Bryan C. Roessler
  4. main() {
  5. if [[ " $1 " == " --motd " ]]; then
  6. print_motd
  7. else
  8. parent="${BASH_SOURCE[0]}"
  9. parent=${parent%/*}
  10. [[ -f "$parent"/script-functions ]] && . "$parent"/script-functions || exit 1
  11. is_root
  12. script="/usr/local/bin/${0##*/}"
  13. service="/usr/lib/systemd/system/motd.service"
  14. timer="/usr/lib/systemd/system/motd.timer"
  15. [[ -f $script ]] || cp "$0" /usr/local/bin/
  16. install_services
  17. fi
  18. }
  19. print_motd() {
  20. # colors
  21. default='\e[0m'
  22. green='\e[32m'
  23. red='\e[31m'
  24. dim='\e[2m'
  25. undim='\e[0m'
  26. # shellcheck disable=SC2016
  27. echo -e \
  28. ' _ _ _ _ _
  29. | | | | | | | | | |
  30. | |__| | __ _ _ __| |_ _ __ ___ __ _ _ __ | | __ _| |__
  31. | __ |/ _` | `__| __| `_ ` _ \ / _` | `_ \ | | / _` | `_ \
  32. | | | | (_| | | | |_| | | | | | (_| | | | | | |___| (_| | |_) |
  33. |_| |_|\__,_|_| \__|_| |_| |_|\__,_|_| |_| |______\__,_|_.__/ '
  34. # System info
  35. # get load averages
  36. IFS=" " read -r LOAD1 LOAD5 LOAD15 <<<"$(awk '{ print $1,$2,$3 }' /proc/loadavg)"
  37. # get free memory
  38. IFS=" " read -r USED AVAIL TOTAL <<<"$(free -htm | grep "Mem" | awk '{print $3,$7,$2}')"
  39. # get processes
  40. PROCESS=$(ps -eo user=|sort|uniq -c | awk '{ print $2 " " $1 }')
  41. PROCESS_ALL=$(echo "$PROCESS"| awk '{print $2}' | awk '{ SUM += $1} END { print SUM }')
  42. PROCESS_ROOT=$(echo "$PROCESS"| grep root | awk '{print $2}')
  43. PROCESS_USER=$(echo "$PROCESS"| grep -v root | awk '{print $2}' | awk '{ SUM += $1} END { print SUM }')
  44. # get processors
  45. PROCESSOR_NAME=$(grep "model name" /proc/cpuinfo | cut -d ' ' -f3- | awk '{print $0}' | head -1)
  46. PROCESSOR_COUNT=$(grep -ioP 'processor\t:' /proc/cpuinfo | wc -l)
  47. echo -e "
  48. ${default}Distro......: $default$(cat /etc/*release | grep "PRETTY_NAME" | cut -d "=" -f 2- | sed 's/"//g')
  49. ${default}Kernel......: $default$(uname -sr)
  50. ${default}Uptime......: $default$(uptime -p)
  51. ${default}Load........: $green$LOAD1$default (1m), $green$LOAD5$default (5m), $green$LOAD15$default (15m)
  52. ${default}Processes...: $default$green$PROCESS_ROOT$default (root), $green$PROCESS_USER$default (user), $green$PROCESS_ALL$default (total)
  53. ${default}CPU.........: $default$PROCESSOR_NAME ($green$PROCESSOR_COUNT$default vCPU)
  54. ${default}Memory......: $green$USED$default used, $green$AVAIL$default avail, $green$TOTAL$default total$default"
  55. # Disk usage
  56. # config
  57. max_usage=90
  58. bar_width=50
  59. # disk usage: ignore zfs, squashfs & tmpfs
  60. 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)
  61. printf "\nDisk usage\n"
  62. for line in "${dfs[@]}"; do
  63. # get disk usage
  64. usage=$(echo "$line" | awk '{print $2}' | sed 's/%//')
  65. used_width=$(((usage*bar_width)/100))
  66. # color is green if usage < max_usage, else red
  67. if [ "${usage}" -ge "${max_usage}" ]; then
  68. color=$red
  69. else
  70. color=$green
  71. fi
  72. # print green/red bar until used_width
  73. bar="[${color}"
  74. for ((i=0; i<used_width; i++)); do
  75. bar+="="
  76. done
  77. # print dimmmed bar until end
  78. bar+="${default}${dim}"
  79. for ((i=used_width; i<bar_width; i++)); do
  80. bar+="="
  81. done
  82. bar+="${undim}]"
  83. # print usage line & bar
  84. echo "${line}" | awk '{ printf("%-31s%+3s used out of %+4s\n", $1, $2, $3); }' | sed -e 's/^/ /'
  85. echo -e "${bar}" | sed -e 's/^/ /'
  86. done
  87. # # Disk health
  88. # cat <<- 'EOF' >> "$script"
  89. # # config
  90. # MAX_TEMP=40
  91. # # set column width
  92. # COLUMNS=2
  93. # # colors
  94. # # disks to check
  95. # disks=(sda sdb sdc sdd sde sdf sdg sdi)
  96. # disknames=(sda sdb sdc sdd sde sdf sdg sdi)
  97. # # hddtemp
  98. # hddtemp_host=localhost
  99. # hddtemp_port=7634
  100. # # logfiles to check
  101. # logfiles='/var/log/syslog /var/log/syslog.1'
  102. # # get all lines with smartd entries from syslog
  103. # lines=$(tac $logfiles | grep -hiP 'smartd\[[[:digit:]]+\]:' | grep -iP "previous self-test")
  104. # # use nc to query temps from hddtemp daemon
  105. # hddtemp=$(timeout 0.01 nc $hddtemp_host $hddtemp_port | sed 's/|//m' | sed 's/||/ \n/g')
  106. # out=""
  107. # for i in "${!disks[@]}"; do
  108. # disk=${disks[$i]}
  109. # # use disknames if given
  110. # diskname=${disknames[$i]}
  111. # if [ -z "${diskname}" ]; then
  112. # diskname=$disk
  113. # fi
  114. # uuid=$(blkid -s UUID -o value "/dev/${disk}")
  115. # status=$( (grep "${uuid}" <<< "${lines}") | grep -m 1 -oP "previous self-test.*" | awk '{ print $4 " " $5 }')
  116. # temp=$( (grep "${disk}" <<< "${hddtemp}") | awk -F'|' '{ print $3 }')
  117. # # color green if temp <= MAX_TEMP, else red
  118. # if [[ "${temp}" -gt "${MAX_TEMP}" ]]; then
  119. # color=$red
  120. # else
  121. # color=$green
  122. # fi
  123. # # add "C" if temp is numeric
  124. # if [[ "$temp" =~ ^[0-9]+$ ]]; then
  125. # temp="${temp}C"
  126. # fi
  127. # # color green if status is "without error", else red
  128. # if [[ "${status}" == "without error" ]]; then
  129. # status_color=$green
  130. # else
  131. # status_color=$red
  132. # fi
  133. # # print temp & smartd error
  134. # out+="${diskname}:,${color}${temp}${undim} | ${status_color}${status}${undim},"
  135. # # insert \n every $COLUMNS column
  136. # if [ $((($i+1) % $COLUMNS)) -eq 0 ]; then
  137. # out+="\n"
  138. # fi
  139. # done
  140. # out+="\n"
  141. # printf "\ndisk status:\n"
  142. # printf "$out" | column -ts $',' | sed -e 's/^/ /'
  143. # EOF
  144. # Services
  145. COLUMNS=2
  146. services=(
  147. btrfs-balance.timer btrfs-scrub.timer backup.timer btrbk.timer fstrim.timer
  148. fail2ban firewalld smb nmb motion smartd cockpit.socket
  149. dnf-automatic.timer motd.timer
  150. )
  151. service_status=()
  152. # get status of all services
  153. for service in "${services[@]}"; do
  154. service_status+=("$(systemctl is-active "$service")")
  155. done
  156. out=""
  157. for i in "${!services[@]}"; do
  158. # color green if service is active, else red
  159. if [[ "${service_status[$i]}" == "active" ]]; then
  160. out+="${services[$i]%.*}:,${green}${service_status[$i]}${undim},"
  161. else
  162. out+="${services[$i]%.*}:,${red}${service_status[$i]}${undim},"
  163. fi
  164. # insert \n every $COLUMNS column
  165. if [[ $(((i+1) % COLUMNS)) -eq 0 ]]; then
  166. out+="\n"
  167. fi
  168. done
  169. printf "\nServices\n"
  170. printf "%b\n" "$out" | column -ts $',' | sed -e 's/^/ /'
  171. # Fail2Ban
  172. # fail2ban-client status to get all jails, takes about ~70ms
  173. read -r -a jails <<< "$(fail2ban-client status | grep "Jail list:" | sed "s/ //g" | awk '{split($2,a,",");for(i in a) print a[i]}')"
  174. out="jail,failed,total,banned,total\n"
  175. for jail in "${jails[@]}"; do
  176. # slow because fail2ban-client has to be called for every jail (~70ms per jail)
  177. status=$(fail2ban-client status "$jail")
  178. failed=$(echo "$status" | grep -ioP '(?<=Currently failed:\t)[[:digit:]]+')
  179. totalfailed=$(echo "$status" | grep -ioP '(?<=Total failed:\t)[[:digit:]]+')
  180. banned=$(echo "$status" | grep -ioP '(?<=Currently banned:\t)[[:digit:]]+')
  181. totalbanned=$(echo "$status" | grep -ioP '(?<=Total banned:\t)[[:digit:]]+')
  182. out+="$jail,$failed,$totalfailed,$banned,$totalbanned\n"
  183. done
  184. printf "\nFail2ban\n"
  185. printf "%b\n" "$out" | column -ts $',' | sed -e 's/^/ /'
  186. # Help links
  187. echo -e '
  188. Links (ctrl+click to follow)
  189. Server Manual.........: https://tinyurl.com/jjz9h6fr
  190. Cockpit (for admins)..: http://localhost:9090
  191. Robot Camera..........: http://localhost:9999
  192. JupyterLab............: http://localhost:8888
  193. RStudio Server........: http://localhost:8787
  194. Robot Computer........: vnc://192.168.16.101:5900
  195. Windows 10 VM.........: vnc://localhost:5900 (pw: hartman)
  196. '
  197. # Scheduled reboot
  198. if systemctl is-active scheduled-reboot.timer &>/dev/null; then
  199. echo -n "Next scheduled reboot: "
  200. time=$(systemctl cat scheduled-reboot.timer | grep OnCalendar=)
  201. time=${time#*=}
  202. echo "$time"
  203. fi
  204. }
  205. install_services() {
  206. cat <<-EOF > "$service"
  207. [Unit]
  208. Description=Generate MoTD
  209. [Service]
  210. Type=simple
  211. ExecStart=/usr/bin/bash -c '$script --motd > /etc/motd'
  212. [Install]
  213. WantedBy=default.target
  214. EOF
  215. cat <<-'EOF' > "$timer"
  216. [Unit]
  217. Description=Generate MoTD every minute on a timer
  218. [Timer]
  219. OnCalendar=*:0/1
  220. OnBootSec=10s
  221. [Install]
  222. WantedBy=timers.target
  223. EOF
  224. chmod +x "$script" &&
  225. systemctl daemon-reload &&
  226. systemctl enable --now "${timer##*/}"
  227. }
  228. main "$@"
  229. exit $?