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