Last active 1705433129

Dynamic motd

motd-dynamic Raw
1#!/bin/bash
2
3# Dynamic MOTD generator
4
5USER=`whoami`
6HOSTNAME=`uname -n`
7NET_IF='eth0'
8IP4=`ip a show $NET_IF | grep 'inet ' | awk '{print $2}'i | tr -d '\n'`
9IP6=`ip a show $NET_IF | grep 'inet6 ' | head -1 | awk '{print $2}' | tr -d '\n'`
10ROOT_USED=`df -Ph | grep ' /$' | awk '{print $3}' | tr -d '\n'`
11ROOT_TOTAL=`df -Ph | grep ' /$' | awk '{print $2}' | tr -d '\n'`
12#HDD_USED=`zpool iostat | grep hdd | awk '{print $2}' | tr -d '\n'`
13#HDD_TOTAL=`df -Ph | grep 'hdd ' | awk '{print $2}' | tr -d '\n'`
14SSD_USED=`df -Ph | grep 'nextcloud ' | awk '{print $3}' | tr -d '\n'`
15SSD_TOTAL=`df -Ph | grep 'nextcloud ' | awk '{print $2}' | tr -d '\n'`
16
17MEMORY1=`free -t -m | grep "Mem" | awk '{print $3" MB";}'`
18MEMORY2=`free -t -m | grep "Mem" | awk '{print $2" MB";}'`
19PSA=`ps -Afl | wc -l`
20
21# time of day
22HOUR=$(date +"%H")
23if [ $HOUR -lt 12 -a $HOUR -ge 0 ]
24then TIME="morning"
25elif [ $HOUR -lt 17 -a $HOUR -ge 12 ]
26then TIME="afternoon"
27else
28 TIME="evening"
29fi
30
31#System uptime
32uptime=`cat /proc/uptime | cut -f1 -d.`
33upDays=$((uptime/60/60/24))
34upHours=$((uptime/60/60%24))
35upMins=$((uptime/60%60))
36upSecs=$((uptime%60))
37
38#System load
39LOAD1=`cat /proc/loadavg | awk {'print $1'}`
40LOAD5=`cat /proc/loadavg | awk {'print $2'}`
41LOAD15=`cat /proc/loadavg | awk {'print $3'}`
42
43# Failed logins
44#LASTLOGIN=$(cat /var/local/lastlogin)
45#LASTLOGIN_PRETTY=$(date +'%d %B %T' --date "$LASTLOGIN")
46#FAILS=$(journalctl -u sshd --since "$LASTLOGIN" | egrep "Failed|Invalid" | wc -l)
47# - Failed logins...........: $FAILS since $LASTLOGIN_PRETTY
48
49#Podman containers status
50if [ -f "/var/local/podman-status" ]; then
51 PODMAN_LAST_UPDATE=$(head -1 /var/local/podman-status)
52 TODAY=$(date --date="Today 00:00" +%s)
53 if [[ "$TODAY" == "$PODMAN_LAST_UPDATE" ]]; then
54 PODMAN_LAST_UPDATE_DAY='today'
55 else
56 DIFF=$(echo "${TODAY}-${PODMAN_LAST_UPDATE}" | bc)
57 if [[ "$DIFF" == 86400 ]];then
58 PODMAN_LAST_UPDATE_DAY='yesterday'
59 else
60 D=$(echo "${DIFF}/86400" | bc)
61 if [[ "$D" -lt 7 ]]; then
62 PODMAN_LAST_UPDATE_DAY="$(date --date=@$PODMAN_LAST_UPDATE +%A)"
63 else
64 PODMAN_LAST_UPDATE_DAY="$D days ago"
65 fi
66 fi
67 fi
68fi
69
70echo "
71Good $TIME $USER"
72
73echo "
74===========================================================================
75 - Hostname................: $HOSTNAME
76 - IPv4..................: $IP4
77 - IPv6..................: $IP6
78 - Release.................: `cat /etc/redhat-release`
79 - Users...................: `users | wc -w` user(s) logged in
80===========================================================================
81 - Current user............: $USER
82 - CPU usage...............: $LOAD1, $LOAD5, $LOAD15 (1, 5, 15 min)
83 - Memory used.............: $MEMORY1 / $MEMORY2
84 - Swap in use.............: `free -m | tail -n 1 | awk '{print $3}'` MB
85 - Processes...............: $PSA running
86 - System uptime...........: $upDays days $upHours hours $upMins minutes $upSecs seconds
87 - Usage of / .............: $ROOT_USED / $ROOT_TOTAL
88 - Usage of /opt...........: $SSD_USED / $SSD_TOTAL"
89
90[ -f "/var/local/podman-status" ] && tail -n +2 /var/local/podman-status | sed "s/WHEN/${PODMAN_LAST_UPDATE_DAY}/"
91printf "\
92===========================================================================\n"
93
94
95cat /var/local/nc-backup-status
96
97
98printf "\n"
99
100if [[ "$(curl -s localhost:8080/status.php | jq '.maintenance')" == "true" ]]; then
101 echo -e "
102 ** Nextcloud in maintenance mode **\n\n"
103fi
104
105
106if command -v mail > /dev/null && mail -e; then echo "You have mail."; fi
107
108if command -v tmux > /dev/null && tmux list-sessions > /dev/null; then echo "TMUX session running."; fi
podman-status Raw
1#!/bin/bash
2
3terminate() {
4 printf "\n===========================================================================
5 - Containers running......: (Waiting for first update)\n" > $STATUSFILE
6 exit 0
7}
8
9trap "terminate" 15
10
11
12STATUSFILE=/var/local/podman-status
13STATUSFILE_TMP=/tmp/podman-status
14
15echo "Starting podman status generator..." | systemd-cat -t podman-status
16echo "Waiting 60 seconds before acquiring status"
17sleep 60
18
19while true; do
20 #printf "" > $STATUSFILE_TMP
21 PODMAN_CONTAINERS=($(podman ps -q))
22 PODMAN_N_RUNNING=${#PODMAN_CONTAINERS[@]}
23 UPDATED_DATE=$(date --date="Today 00:00" +%s)
24 UPDATED=$(date +%s)
25
26 echo "$UPDATED_DATE" > $STATUSFILE_TMP
27 printf "===========================================================================
28 - Containers running......: $PODMAN_N_RUNNING (updated WHEN at $(date +%H:%M:%S))\n" >> $STATUSFILE_TMP
29
30 for c in ${PODMAN_CONTAINERS[@]}; do
31 C_NAME=$(podman ps --filter "id=$c" --format "{{.Names}}")
32 [[ "$(echo $C_NAME | grep -q infra)" == 0 ]]
33 C_UP=$(podman ps --filter "id=$c" --format "{{.RunningFor}}")
34 C_STATUS=$(podman ps --filter "id=$c" --format "{{.Status}}")
35 C_PORTS=$(podman ps --filter "id=$c" --format "{{.Ports}}")
36 printf "\r\ - .....................: Created $C_UP, $C_STATUS\r - $C_NAME\n" >> $STATUSFILE_TMP
37 done
38 #printf "===========================================================================\n" >> $STATUSFILE_TMP
39 cat $STATUSFILE_TMP > $STATUSFILE
40 rm -f $STATUSFILE_TMP
41 #echo "Updated podman status @$(date)" | systemd-cat -t podman-status
42
43 sleep 60
44
45done
podman-status.service Raw
1[Unit]
2Description=Podman status watcher for dynamic MOTD
3
4[Service]
5ExecStart=/usr/local/bin/podman-status
6RemainAfterExit=no
7
8[Install]
9WantedBy=default.target