script-install-motd 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. #!/usr/bin/env bash
  2. # Install motd scripts
  3. # Bryan C. Roessler
  4. parent="${BASH_SOURCE[0]}"
  5. parent=${parent%/*}
  6. [[ -f "$parent"/script-functions ]] && . "$parent"/script-functions || exit 1
  7. is_root
  8. DEBUG=0
  9. [[ "$#" -gt 0 ]] && echo "Debug on" && DEBUG=1
  10. installdir="/usr/local/bin"
  11. [[ -d "$installdir" ]] || mkdir -p "$installdir"
  12. if (( DEBUG )); then
  13. script="generate-motd.sh"
  14. else
  15. script="$installdir/generate-motd.sh"
  16. fi
  17. cat <<- 'EOF' > "$script"
  18. #!/usr/bin/env bash
  19. echo -n '
  20. _ _ _ _ _
  21. | | | | | | | | | |
  22. | |__| | __ _ _ __| |_ _ __ ___ __ _ _ __ | | __ _| |__
  23. | __ |/ _` | `__| __| `_ ` _ \ / _` | `_ \ | | / _` | `_ \
  24. | | | | (_| | | | |_| | | | | | (_| | | | | | |___| (_| | |_) |
  25. |_| |_|\__,_|_| \__|_| |_| |_|\__,_|_| |_| |______\__,_|_.__/
  26. '
  27. EOF
  28. # System info
  29. cat <<- 'EOF' >> "$script"
  30. # get load averages
  31. IFS=" " read LOAD1 LOAD5 LOAD15 <<<$(cat /proc/loadavg | awk '{ print $1,$2,$3 }')
  32. # get free memory
  33. IFS=" " read USED AVAIL TOTAL <<<$(free -htm | grep "Mem" | awk {'print $3,$7,$2'})
  34. # get processes
  35. PROCESS=`ps -eo user=|sort|uniq -c | awk '{ print $2 " " $1 }'`
  36. PROCESS_ALL=`echo "$PROCESS"| awk {'print $2'} | awk '{ SUM += $1} END { print SUM }'`
  37. PROCESS_ROOT=`echo "$PROCESS"| grep root | awk {'print $2'}`
  38. PROCESS_USER=`echo "$PROCESS"| grep -v root | awk {'print $2'} | awk '{ SUM += $1} END { print SUM }'`
  39. # get processors
  40. PROCESSOR_NAME=`grep "model name" /proc/cpuinfo | cut -d ' ' -f3- | awk {'print $0'} | head -1`
  41. PROCESSOR_COUNT=`grep -ioP 'processor\t:' /proc/cpuinfo | wc -l`
  42. W="\e[0;39m"
  43. G="\e[1;32m"
  44. echo -e "
  45. $W Distro......: $W`cat /etc/*release | grep "PRETTY_NAME" | cut -d "=" -f 2- | sed 's/"//g'`
  46. $W Kernel......: $W`uname -sr`
  47. $W Uptime......: $W`uptime -p`
  48. $W Load........: $G$LOAD1$W (1m), $G$LOAD5$W (5m), $G$LOAD15$W (15m)
  49. $W Processes...: $W$G$PROCESS_ROOT$W (root), $G$PROCESS_USER$W (user), $G$PROCESS_ALL$W (total)
  50. $W CPU.........: $W$PROCESSOR_NAME ($G$PROCESSOR_COUNT$W vCPU)
  51. $W Memory......: $G$USED$W used, $G$AVAIL$W avail, $G$TOTAL$W total$W"
  52. EOF
  53. # Disk usage
  54. cat <<- 'EOF' >> "$script"
  55. # config
  56. max_usage=90
  57. bar_width=50
  58. # colors
  59. white="\e[39m"
  60. green="\e[1;32m"
  61. red="\e[1;31m"
  62. dim="\e[2m"
  63. undim="\e[0m"
  64. # disk usage: ignore zfs, squashfs & tmpfs
  65. 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)
  66. printf "\nDisk usage\n"
  67. for line in "${dfs[@]}"; do
  68. # get disk usage
  69. usage=$(echo "$line" | awk '{print $2}' | sed 's/%//')
  70. used_width=$((($usage*$bar_width)/100))
  71. # color is green if usage < max_usage, else red
  72. if [ "${usage}" -ge "${max_usage}" ]; then
  73. color=$red
  74. else
  75. color=$green
  76. fi
  77. # print green/red bar until used_width
  78. bar="[${color}"
  79. for ((i=0; i<$used_width; i++)); do
  80. bar+="="
  81. done
  82. # print dimmmed bar until end
  83. bar+="${white}${dim}"
  84. for ((i=$used_width; i<$bar_width; i++)); do
  85. bar+="="
  86. done
  87. bar+="${undim}]"
  88. # print usage line & bar
  89. echo "${line}" | awk '{ printf("%-31s%+3s used out of %+4s\n", $1, $2, $3); }' | sed -e 's/^/ /'
  90. echo -e "${bar}" | sed -e 's/^/ /'
  91. done
  92. EOF
  93. # # Disk health
  94. # cat <<- 'EOF' >> "$script"
  95. # # config
  96. # MAX_TEMP=40
  97. # # set column width
  98. # COLUMNS=2
  99. # # colors
  100. # white="\e[39m"
  101. # green="\e[1;32m"
  102. # red="\e[1;31m"
  103. # dim="\e[2m"
  104. # undim="\e[0m"
  105. # # disks to check
  106. # disks=(sda sdb sdc sdd sde sdf sdg sdi)
  107. # disknames=(sda sdb sdc sdd sde sdf sdg sdi)
  108. # # hddtemp
  109. # hddtemp_host=localhost
  110. # hddtemp_port=7634
  111. # # logfiles to check
  112. # logfiles='/var/log/syslog /var/log/syslog.1'
  113. # # get all lines with smartd entries from syslog
  114. # lines=$(tac $logfiles | grep -hiP 'smartd\[[[:digit:]]+\]:' | grep -iP "previous self-test")
  115. # # use nc to query temps from hddtemp daemon
  116. # hddtemp=$(timeout 0.01 nc $hddtemp_host $hddtemp_port | sed 's/|//m' | sed 's/||/ \n/g')
  117. # out=""
  118. # for i in "${!disks[@]}"; do
  119. # disk=${disks[$i]}
  120. # # use disknames if given
  121. # diskname=${disknames[$i]}
  122. # if [ -z "${diskname}" ]; then
  123. # diskname=$disk
  124. # fi
  125. # uuid=$(blkid -s UUID -o value "/dev/${disk}")
  126. # status=$( (grep "${uuid}" <<< "${lines}") | grep -m 1 -oP "previous self-test.*" | awk '{ print $4 " " $5 }')
  127. # temp=$( (grep "${disk}" <<< "${hddtemp}") | awk -F'|' '{ print $3 }')
  128. # # color green if temp <= MAX_TEMP, else red
  129. # if [[ "${temp}" -gt "${MAX_TEMP}" ]]; then
  130. # color=$red
  131. # else
  132. # color=$green
  133. # fi
  134. # # add "C" if temp is numeric
  135. # if [[ "$temp" =~ ^[0-9]+$ ]]; then
  136. # temp="${temp}C"
  137. # fi
  138. # # color green if status is "without error", else red
  139. # if [[ "${status}" == "without error" ]]; then
  140. # status_color=$green
  141. # else
  142. # status_color=$red
  143. # fi
  144. # # print temp & smartd error
  145. # out+="${diskname}:,${color}${temp}${undim} | ${status_color}${status}${undim},"
  146. # # insert \n every $COLUMNS column
  147. # if [ $((($i+1) % $COLUMNS)) -eq 0 ]; then
  148. # out+="\n"
  149. # fi
  150. # done
  151. # out+="\n"
  152. # printf "\ndisk status:\n"
  153. # printf "$out" | column -ts $',' | sed -e 's/^/ /'
  154. # EOF
  155. # Services
  156. cat <<- 'EOF' >> "$script"
  157. # set column width
  158. COLUMNS=2
  159. # colors
  160. green="\e[1;32m"
  161. red="\e[1;31m"
  162. undim="\e[0m"
  163. 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")
  164. # sort services
  165. IFS=$'\n' services=($(sort <<<"${services[*]}"))
  166. unset IFS
  167. service_status=()
  168. # get status of all services
  169. for service in "${services[@]}"; do
  170. service_status+=($(systemctl is-active "$service"))
  171. done
  172. out=""
  173. for i in ${!services[@]}; do
  174. # color green if service is active, else red
  175. if [[ "${service_status[$i]}" == "active" ]]; then
  176. out+="${services[$i]}:,${green}${service_status[$i]}${undim},"
  177. else
  178. out+="${services[$i]}:,${red}${service_status[$i]}${undim},"
  179. fi
  180. # insert \n every $COLUMNS column
  181. if [ $((($i+1) % $COLUMNS)) -eq 0 ]; then
  182. out+="\n"
  183. fi
  184. done
  185. out+="\n"
  186. printf "\nServices\n"
  187. printf "$out" | column -ts $',' | sed -e 's/^/ /'
  188. EOF
  189. # Fail2Ban
  190. cat <<- 'EOF' >> "$script"
  191. # fail2ban-client status to get all jails, takes about ~70ms
  192. jails=($(fail2ban-client status | grep "Jail list:" | sed "s/ //g" | awk '{split($2,a,",");for(i in a) print a[i]}'))
  193. out="jail,failed,total,banned,total\n"
  194. for jail in ${jails[@]}; do
  195. # slow because fail2ban-client has to be called for every jail (~70ms per jail)
  196. status=$(fail2ban-client status ${jail})
  197. failed=$(echo "$status" | grep -ioP '(?<=Currently failed:\t)[[:digit:]]+')
  198. totalfailed=$(echo "$status" | grep -ioP '(?<=Total failed:\t)[[:digit:]]+')
  199. banned=$(echo "$status" | grep -ioP '(?<=Currently banned:\t)[[:digit:]]+')
  200. totalbanned=$(echo "$status" | grep -ioP '(?<=Total banned:\t)[[:digit:]]+')
  201. out+="$jail,$failed,$totalfailed,$banned,$totalbanned\n"
  202. done
  203. printf "\nfail2ban status\n"
  204. printf $out | column -ts $',' | sed -e 's/^/ /'
  205. EOF
  206. # Help links
  207. cat <<- 'EOF' >> "$script"
  208. echo "\
  209. Links (ctrl+click to follow)
  210. Server Manual.........: https://tinyurl.com/jjz9h6fr
  211. Cockpit (for admins)..: http://localhost:9090
  212. Robot Camera..........: http://localhost:9999
  213. JupyterLab............: http://localhost:8888
  214. RStudio Server........: http://localhost:8787
  215. Robot Computer........: vnc://192.168.16.101:5900
  216. Windows 10 VM.........: vnc://localhost:5900 (pw: hartman)
  217. "
  218. EOF
  219. # Scheduled reboot
  220. cat <<- "EOF" >> "$script"
  221. if systemctl is-active scheduled-reboot.timer &>/dev/null; then
  222. echo -n "Next scheduled reboot: "
  223. time=$(systemctl cat scheduled-reboot.timer | grep OnCalendar=)
  224. time=${time#*=}
  225. echo "$time"
  226. fi
  227. EOF
  228. # cat <<- 'EOF' > "$script"
  229. # #!/usr/bin/env bash
  230. # [[ -v NO_MOTD ]] && exit 0
  231. # USER=$(whoami)
  232. # HOSTNAME=$(uname -n)
  233. # ARRAY=$(df -Ph | grep array | awk '{print $4}' | tr -d '\n')
  234. # ROOT=$(df -Ph | grep sdb3 | awk '{print $4}' | tr -d '\n')
  235. # BACKUP=$(df -Ph | grep backup | awk '{print $4}' | tr -d '\n')
  236. # MEMUSED=$(free -t -m | grep "Mem:" | awk '{print $3" MB";}')
  237. # MEMTOTAL=$(free -t -m | grep "Mem:" | awk '{print $2" MB";}')
  238. # PSA=$(ps -Afl | wc -l)
  239. # SWAPMEM=$(free -m | tail -n 1 | awk '{print $3}')
  240. # #System uptime
  241. # uptime=$(cut -f1 -d. /proc/uptime)
  242. # upDays=$((uptime/60/60/24))
  243. # upHours=$((uptime/60/60%24))
  244. # upMins=$((uptime/60%60))
  245. # upSecs=$((uptime%60))
  246. # #System load
  247. # LOAD1=$(awk {'print $1'} /proc/loadavg)
  248. # LOAD5=$(awk {'print $2'} /proc/loadavg)
  249. # LOAD15=$(awk {'print $3'} /proc/loadavg)
  250. # echo "
  251. # ===========================================================================
  252. # - Hostname............: $HOSTNAME
  253. # - Release.............: $(cat /etc/redhat-release)
  254. # - Users...............: Currently $(users | wc -w) user(s) logged on
  255. # ===========================================================================
  256. # - Current user........: $USER
  257. # - CPU usage...........: $LOAD1, $LOAD5, $LOAD15 (1, 5, 15 min)
  258. # - Memory used.........: $MEMUSED / $MEMTOTAL
  259. # - Processes...........: $PSA running
  260. # - System uptime.......: $upDays days $upHours hours $upMins minutes $upSecs seconds
  261. # - Disk space HOME|ROOT: $ROOT remaining
  262. # - Disk space ARRAY....: $ARRAY remaining
  263. # - Disk space BACKUP...: $BACKUP remaining
  264. # ===========================================================================
  265. # "
  266. # EOF
  267. generate-services() {
  268. service="/usr/lib/systemd/system/generate-motd.service"
  269. timer="/usr/lib/systemd/system/generate-motd.timer"
  270. cat <<-EOF > "$service"
  271. [Unit]
  272. Description=Generate MoTD
  273. [Service]
  274. Type=simple
  275. ExecStart=/usr/bin/bash -c '$script > /etc/motd'
  276. [Install]
  277. WantedBy=default.target
  278. EOF
  279. cat <<-'EOF' > "$timer"
  280. [Unit]
  281. Description=Generate MoTD every minute on a timer
  282. [Timer]
  283. OnCalendar=*:0/1
  284. OnBootSec=10s
  285. [Install]
  286. WantedBy=timers.target
  287. EOF
  288. }
  289. chmod +x "$script"
  290. if (( DEBUG )); then
  291. bash generate-motd.sh
  292. cat -n generate-motd.sh
  293. rm generate-motd.sh
  294. else
  295. generate-services && \
  296. systemctl daemon-reload && \
  297. systemctl enable --now generate-motd.timer
  298. fi
  299. exit $?