Skip to content

Commit 03d7b99

Browse files
committed
Version 0.0.2 adding SElinux info
1 parent 684307c commit 03d7b99

File tree

9 files changed

+190
-40
lines changed

9 files changed

+190
-40
lines changed

rpmbuild/BUILD/sysmotd/README.txt

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
Summary
2+
=======
3+
4+
sysmotd is essentially a shell script that produces a Message Of The Day
5+
(MOTD) including system information and statistics.
6+
7+
It has been developed and tested on Fedora Linux and it might work on
8+
other Red Hat-based distributions with very small tweaks. It should also
9+
be relatively simple to port to other distributions that do not have
10+
their own dynamic MOTD generator.
11+
12+
As of time of writting, Fedora only uses pam_motd.so for ssh log ins.
13+
14+
How it looks like
15+
=================
16+
17+
A picture is worth a thousand words.
18+
19+
Background
20+
==========
21+
22+
I am a long term Fedora user. Having worked with some Ubuntu servers
23+
lately, I found myself missing the system information and statistics
24+
that you get when you log in. When I installed Fedora 37 on a Raspberry
25+
Pi4, I saw that cockpit would generate a MOTD, and I decided to research
26+
a bit to find out if I could add my own message with some system statistics in
27+
it.
28+
29+
Out of the various sources I found, I would like to give credit to a
30+
couple that helped me a lot in getting the information that I wanted and
31+
what tools to use.
32+
33+
[1] https://github.com/angela-d/motd-for-centos
34+
35+
[2] https://gist.github.com/cha55son/6042560
36+
37+
While those sources gave me a good start, I wanted to reduce the number
38+
of dependencies to a minimum and to make the dynamic part of the MOTD as
39+
efficient as possible. Hence I re-wrote re-wrote those and started
40+
looking into how to avoid using the user's profile to trigger the
41+
script. That leads us to the next section.
42+
43+
Structure and how it works
44+
==========================
45+
46+
My understanding is that Debian and Ubuntu distribute a patched version
47+
of pam_motd.so. Theirs runs scripts in /etc/update-motd.d. Also, their
48+
PAM configuration includes lines for static and dyanamic MOTDs.
49+
50+
[3] https://wiki.debian.org/motd
51+
52+
Using a patched version of pam\_motd.so would have been the preferred
53+
approach since the script updating the dynamic part of the MOTD would only
54+
run when a user logged in. However, I didn't know how difficult it would
55+
be to get a change like that approved for Fedora. My assumption was, and
56+
still is, that the people behind Fedora or Red Hat know that Debian and
57+
Ubuntu are using this dynamic MOTD approach and if they have not
58+
implemented it yet, it is because they have some valid reasons.
59+
60+
Having made that assumption, I resorted to use a systemd service to call
61+
the script and a timer to call the service, every minute. However, not all the
62+
parts of the script that generate the corresponding part of the MOTD run
63+
every minute.
64+
65+
So, sysmotd is comprised of:
66+
67+
### /usr/libexec/sysmotd
68+
69+
The shell script. It produces 3 files which are saved under
70+
/run/motd.d
71+
72+
- 00-sysmotd-header.motd: colorful header that changes every 60
73+
minutes.
74+
- 01-sysmotd-sysinfo.motd: system information which is updated every
75+
60 seconds.
76+
- 02-sysmotd-updates.motd: updates available which is updated every 60
77+
minutes. It uses Fedora's own dnf cache which runs whenever it runs.
78+
79+
### /usr/lib/systemd/system/sysmotd.service
80+
81+
A systemd service to run the script. It is disabled and it should stay
82+
like that.
83+
84+
### /usr/lib/systemd/system/sysmotd.timer
85+
86+
A systemd timer to run the service every 60 seconds. The script works
87+
out which parts of the MOTD to update. It is enabled and started when
88+
the dnf package is installed.
89+
90+
### /usr/lib/systemd/system-preset/50-sysmotd.preset
91+
92+
A systemd preset file to disable sysmotd.service and enable
93+
sysmotd.timer by default.
94+
95+
In summary, what you get when you install sysmotd is a systemd timer
96+
that every 60 seconds runs a service, that runs a script, which in turn
97+
updates the header every 60 minutes, system's information every 60
98+
seconds, and the updates available every 60 minutes too.
99+
100+
How to install sysmotd and dependencies
101+
=======================================
102+
103+
These are the Fedora packages that you need to have installed for
104+
sysmotd to work:
105+
106+
bash
107+
coreutils
108+
dnf
109+
figlet
110+
findutils
111+
gawk
112+
lolcat
113+
procps-ng
114+
systemd
115+
116+
Most systems will have all of them installed apart from figlet and
117+
lolcat.
118+
119+
To install sysmotd and its dependencies, my recommendation is to
120+
download the rpm package under rpmbuild/RPMS/noarch in this repo, and
121+
install it with dnf.
122+
123+
sudo dnf install sysmotd-0.0.1-1.fc37.noarch.rpm
124+
125+
If you rather not install a package from an unknown source, and I
126+
wouldn't blame you for that, you can clone this repo and, after
127+
inspecting the files listed under the structure section, copy them to
128+
their respective folders. Please note that if you do this, and you are
129+
running selinux enforced, you will have to fix the context of the files.

rpmbuild/BUILD/sysmotd/sysmotd

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121

2222
# directory where the motd files will live
2323
rundir="/run/motd.d"
24-
header="$rundir/00-header.motd"
25-
sysinfo="$rundir/01-sysinfo.motd"
26-
updates="$rundir/02-updates.motd"
24+
header="$rundir/00-sysmotd-header.motd"
25+
sysinfo="$rundir/01-sysmotd-sysinfo.motd"
26+
updates="$rundir/02-sysmotd-updates.motd"
2727

2828
# some variables
2929
. /etc/os-release
@@ -33,15 +33,15 @@ hostname=$(uname -n)
3333

3434
# uptime
3535
uptime=$(awk '{print int($1)}' /proc/uptime)
36-
upDays=$((uptime/60/60/24))
37-
upHours=$((uptime/60/60%24))
38-
upMins=$((uptime/60%60))
39-
upSecs=$((uptime%60))
36+
up_days=$((uptime/60/60/24))
37+
up_hours=$((uptime/60/60%24))
38+
up_mins=$((uptime/60%60))
39+
up_secs=$((uptime%60))
4040

4141
# avg load
42-
load1=$(awk '{print $1}' /proc/loadavg)
43-
load5=$(awk '{print $2}' /proc/loadavg)
44-
load15=$(awk '{print $3}' /proc/loadavg)
42+
load_1=$(awk '{print $1}' /proc/loadavg)
43+
load_5=$(awk '{print $2}' /proc/loadavg)
44+
load_15=$(awk '{print $3}' /proc/loadavg)
4545

4646
# memory
4747
mem_total=$(awk '/MemTotal/ {print $2;}' /proc/meminfo)
@@ -62,6 +62,13 @@ users=$(users | wc -w)
6262
# processes
6363
processes=$(ps aux --no-headers | wc -l)
6464

65+
# SELinux
66+
se_status=$(sestatus | awk '/^SELinux status:/ {print $3;}')
67+
if [[ "$se_status" != "disabled" ]]; then
68+
semode=$(sestatus | awk '/^Current mode/ {print ($3);}')
69+
se_status="${se_status} (${semode})"
70+
fi
71+
6572
### MOTD ###
6673

6774
# colourful header. Updated every 60 minutes only but no real performance gain
@@ -77,13 +84,14 @@ echo -e "Welcome to ${PRETTY_NAME} ($(uname -o) $(uname -r))
7784
7885
System information as of $(date)
7986
80-
Hostname:\t${hostname}\t\tMemory usage:\t$((${mem_used}/1024))/$((${mem_total}/1024))M (${mem_perc}%)
81-
Uptime:\t\t${upDays}d ${upHours}h ${upMins}m ${upSecs}s\t\t\tSwap usage:\t$((${swap_used}/1024))/$((${swap_total}/1024))M (${swap_perc}%)
82-
Load 1-5-15min:\t${load1}-${load5}-${load15}\t\t\tUsage of /:\t${root_used}/${root_total} (${root_perc})
83-
Processes:\t${processes}\t\t\t\tUsers:\t\t${users}
87+
Hostname:\t${hostname}
88+
Uptime:\t\t${up_days}d ${up_hours}h ${up_mins}m ${up_secs}s\t\t\tUsage of /:\t${root_used}/${root_total} (${root_perc})
89+
Load 1-5-15min:\t${load_1}-${load_5}-${load_15}\t\t\tSElinux:\t${se_status}
90+
Memory usage:\t$((${mem_used}/1024))/$((${mem_total}/1024))M (${mem_perc}%)\t\t\tProcesses:\t${processes}
91+
Swap usage:\t$((${swap_used}/1024))/$((${swap_total}/1024))M (${swap_perc}%)\t\t\tUsers:\t\t${users}
8492
" > $sysinfo
8593

86-
# updates available updated every 60 minutes using CACHE.
94+
# available updates refreshes every 60 minutes using CACHE.
8795
# dnf's cache is updated by the system using its own service and timer. The conservative
8896
# approach is to not to change this.
8997
NEED_UPDATE="FALSE"
@@ -98,4 +106,4 @@ if [[ "$NEED_UPDATE" = "TRUE" ]]; then
98106
else
99107
echo -e "There were ${n_updates} updates available when last checked.\n" > $updates
100108
fi
101-
fi
109+
fi
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
disable sysmotd.service
2+
enable sysmotd.timer
22.4 KB
Binary file not shown.
14.3 KB
Binary file not shown.

rpmbuild/SPECS/sysmotd.spec

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Name: sysmotd
2-
Version: 0.0.1
2+
Version: 0.0.2
33
Release: 1%{?dist}
44
Summary: Generates a MOTD (Message Of The Day) including system information on Fedora Linux
55
BuildArch: noarch
@@ -67,5 +67,8 @@ rm -f /run/motd.d/*%{name}*
6767

6868

6969
%changelog
70+
* Thu Apr 27 2023 Manuel Fombuena <mfombuena@innovara.co.uk>
71+
- Version 0.0.2 adding SElinux info
72+
7073
* Mon Dec 26 2022 Manuel Fombuena <mfombuena@innovara.co.uk>
7174
- First version being packaged
24.5 KB
Binary file not shown.

sysmotd/README.txt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Summary
22
=======
33

44
sysmotd is essentially a shell script that produces a Message Of The Day
5-
(MOTD) which includes system information and statistics.
5+
(MOTD) including system information and statistics.
66

77
It has been developed and tested on Fedora Linux and it might work on
88
other Red Hat-based distributions with very small tweaks. It should also
@@ -23,7 +23,7 @@ I am a long term Fedora user. Having worked with some Ubuntu servers
2323
lately, I found myself missing the system information and statistics
2424
that you get when you log in. When I installed Fedora 37 on a Raspberry
2525
Pi4, I saw that cockpit would generate a MOTD, and I decided to research
26-
a bit to find out if I could add my own with some system statistics on
26+
a bit to find out if I could add my own message with some system statistics in
2727
it.
2828

2929
Out of the various sources I found, I would like to give credit to a
@@ -34,9 +34,9 @@ what tools to use.
3434

3535
[2] https://gist.github.com/cha55son/6042560
3636

37-
While these sources gave me a good start, I wanted to reduce the number
38-
of dependencies to a minimum and make the dynamic part of the MOTD as
39-
efficient as possible, so I soon re-wrote most of the stuff and started
37+
While those sources gave me a good start, I wanted to reduce the number
38+
of dependencies to a minimum and to make the dynamic part of the MOTD as
39+
efficient as possible. Hence I re-wrote re-wrote those and started
4040
looking into how to avoid using the user's profile to trigger the
4141
script. That leads us to the next section.
4242

@@ -50,15 +50,15 @@ PAM configuration includes lines for static and dyanamic MOTDs.
5050
[3] https://wiki.debian.org/motd
5151

5252
Using a patched version of pam\_motd.so would have been the preferred
53-
approach, since the script updating the dynamic part of the MOTD only
54-
runs when a user logs in. However, I didn't know how difficult it would
53+
approach since the script updating the dynamic part of the MOTD would only
54+
run when a user logged in. However, I didn't know how difficult it would
5555
be to get a change like that approved for Fedora. My assumption was, and
5656
still is, that the people behind Fedora or Red Hat know that Debian and
5757
Ubuntu are using this dynamic MOTD approach and if they have not
5858
implemented it yet, it is because they have some valid reasons.
5959

60-
In that context, I resorted to use a systemd service to call the script
61-
and a timer to call the service, every minute. However, not all the
60+
Having made that assumption, I resorted to use a systemd service to call
61+
the script and a timer to call the service, every minute. However, not all the
6262
parts of the script that generate the corresponding part of the MOTD run
6363
every minute.
6464

@@ -126,4 +126,4 @@ If you rather not install a package from an unknown source, and I
126126
wouldn't blame you for that, you can clone this repo and, after
127127
inspecting the files listed under the structure section, copy them to
128128
their respective folders. Please note that if you do this, and you are
129-
running selinux enforced, you will have to fix the context of the files.
129+
running selinux enforced, you will have to fix the context of the files.

sysmotd/sysmotd

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ hostname=$(uname -n)
3333

3434
# uptime
3535
uptime=$(awk '{print int($1)}' /proc/uptime)
36-
upDays=$((uptime/60/60/24))
37-
upHours=$((uptime/60/60%24))
38-
upMins=$((uptime/60%60))
39-
upSecs=$((uptime%60))
36+
up_days=$((uptime/60/60/24))
37+
up_hours=$((uptime/60/60%24))
38+
up_mins=$((uptime/60%60))
39+
up_secs=$((uptime%60))
4040

4141
# avg load
42-
load1=$(awk '{print $1}' /proc/loadavg)
43-
load5=$(awk '{print $2}' /proc/loadavg)
44-
load15=$(awk '{print $3}' /proc/loadavg)
42+
load_1=$(awk '{print $1}' /proc/loadavg)
43+
load_5=$(awk '{print $2}' /proc/loadavg)
44+
load_15=$(awk '{print $3}' /proc/loadavg)
4545

4646
# memory
4747
mem_total=$(awk '/MemTotal/ {print $2;}' /proc/meminfo)
@@ -62,6 +62,13 @@ users=$(users | wc -w)
6262
# processes
6363
processes=$(ps aux --no-headers | wc -l)
6464

65+
# SELinux
66+
se_status=$(sestatus | awk '/^SELinux status:/ {print $3;}')
67+
if [[ "$se_status" != "disabled" ]]; then
68+
semode=$(sestatus | awk '/^Current mode/ {print ($3);}')
69+
se_status="${se_status} (${semode})"
70+
fi
71+
6572
### MOTD ###
6673

6774
# colourful header. Updated every 60 minutes only but no real performance gain
@@ -77,13 +84,14 @@ echo -e "Welcome to ${PRETTY_NAME} ($(uname -o) $(uname -r))
7784
7885
System information as of $(date)
7986
80-
Hostname:\t${hostname}\t\tMemory usage:\t$((${mem_used}/1024))/$((${mem_total}/1024))M (${mem_perc}%)
81-
Uptime:\t\t${upDays}d ${upHours}h ${upMins}m ${upSecs}s\t\t\tSwap usage:\t$((${swap_used}/1024))/$((${swap_total}/1024))M (${swap_perc}%)
82-
Load 1-5-15min:\t${load1}-${load5}-${load15}\t\t\tUsage of /:\t${root_used}/${root_total} (${root_perc})
83-
Processes:\t${processes}\t\t\t\tUsers:\t\t${users}
87+
Hostname:\t${hostname}
88+
Uptime:\t\t${up_days}d ${up_hours}h ${up_mins}m ${up_secs}s\t\t\tUsage of /:\t${root_used}/${root_total} (${root_perc})
89+
Load 1-5-15min:\t${load_1}-${load_5}-${load_15}\t\t\tSElinux:\t${se_status}
90+
Memory usage:\t$((${mem_used}/1024))/$((${mem_total}/1024))M (${mem_perc}%)\t\t\tProcesses:\t${processes}
91+
Swap usage:\t$((${swap_used}/1024))/$((${swap_total}/1024))M (${swap_perc}%)\t\t\tUsers:\t\t${users}
8492
" > $sysinfo
8593

86-
# updates available updated every 60 minutes using CACHE.
94+
# available updates refreshes every 60 minutes using CACHE.
8795
# dnf's cache is updated by the system using its own service and timer. The conservative
8896
# approach is to not to change this.
8997
NEED_UPDATE="FALSE"
@@ -98,4 +106,4 @@ if [[ "$NEED_UPDATE" = "TRUE" ]]; then
98106
else
99107
echo -e "There were ${n_updates} updates available when last checked.\n" > $updates
100108
fi
101-
fi
109+
fi

0 commit comments

Comments
 (0)