Skip to content

Commit

Permalink
revamp
Browse files Browse the repository at this point in the history
  • Loading branch information
classabbyamp committed Mar 9, 2022
1 parent 2d0f405 commit 4235c9a
Show file tree
Hide file tree
Showing 15 changed files with 232 additions and 69 deletions.
29 changes: 29 additions & 0 deletions LICENCE
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
BSD 3-Clause License

Copyright (c) 2022 classabbyamp
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47 changes: 26 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,32 +1,37 @@
# motd
# motdd

My little dynamic motd scripts.
A small shell script to manage `/etc/motd`.

![an example of the output of the script](./example-output.png)
## Usage

## Requirements
Install `bin/update-motd` somewhere on your `$PATH`.

- lolcat
- figlet
- curl
- bash
- docker
Create `/etc/update-motd.d/` and put the scripts to generate the MOTD in it. Ensure they are executable.
If packaging this program, `/usr/share/update-motd.d/` can be used as a fallback path for system defaults.

## Usage
On systems with runit and [snooze](https://github.com/leahneukirchen/snooze) (like Void Linux), the service in `runit/` can be used.
Otherwise, a cronjob, systemd timer, or similar method for running `update-motd` periodically works.

See [here](https://github.com/classabbyamp/void-packages/blob/motdd/srcpkgs/motdd/template) for an example package template for motdd.

On systems that include `/etc/update-motd.d/`: copy the contents of `update-motd.d/` to that directory.
## Examples

On other systems:
Two examples of scripts are provided:

```
# cp ./update-motd.sh /usr/local/bin/
# cp -r ./update-motd.d /usr/local/share/
# crontab -e
```
### Basic

In the crontab, add something like this (this runs every hour at 0 minutes past the hour):
A simple MOTD.

```
0 * * * * /usr/local/bin/update-motd.sh
```
![an example of the output of the basic example scripts](doc/basic-example.png)

### Fancy

A showy MOTD. Requires:

- lolcat
- figlet
- curl
- docker (optional)
- xbps (optional)

![an example of the output of the fancy example scripts](doc/fancy-example.png)
88 changes: 88 additions & 0 deletions bin/update-motd
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#!/bin/sh

RELEASE=1.0.0

USRDIR="/usr/share/update-motd.d"
ETCDIR="/etc/update-motd.d"
MOTDFILE="/etc/motd"

VERBOSE=
VERSION=
HELP=
DRYRUN=

while getopts "vVdh" arg; do
case $arg in
v) VERBOSE=1 ;;
V) VERSION=1 ;;
d) DRYRUN=1 ;;
h|?) HELP=1 ;;
esac
done

version() {
env echo "motdd v${RELEASE}"
exit 0
}

help() {
env echo "Usage: update-motd [OPTION]...
update-motd runs scripts to generate a message of the day in ${MOTDFILE}.
Default scripts are stored in ${USRDIR}. User-defined scripts
are stored in ${ETCDIR}. If a file exists in ${ETCDIR},
it overrides the file with the same name in ${USRDIR}.
Ensure all scripts executable and have a shebang.
OPTIONS
-h Show this message
-V Show the version of motd
-v Prints more verbose messages to stderr
-d Print to stdout and do not write to ${MOTDFILE}
Copyright (c) 2022 classabbyamp, released under the BSD 3-Clause licence"
exit 0
}

err() {
env echo -e "$@" >&2
}

run() {
fn="$1"
if [ -f "$fn" ]; then
if [ -x "$fn" ]; then
[ -n "$VERBOSE" ] && err "Running \"$fn\" ..."
[ -n "$DRYRUN" ] && $fn
[ -z "$DRYRUN" ] && $fn >> $MOTDFILE
else
err "Cannot execute \"$fn\""
fi
ret=0
else
ret=1
fi
return "$ret"
}

[ -n "$HELP" ] && help
[ -n "$VERSION" ] && version

[ -n "$DRYRUN" ] || touch $MOTDFILE

# list all files in USRDIR and ETCDIR
# sort them to get the unique values in order
# for each file:
# if it exists in ETCDIR, check if it's executable, then execute it
# otherwise, check if it exists and is executable in USRDIR and execute it
while read -r f; do
run "$ETCDIR/$f" || run "$USRDIR/$f"
done <<EOF
$(
sort -u <<SORTEOF
$(ls -1 $USRDIR; ls -1 $ETCDIR)
SORTEOF
)
EOF
Binary file added doc/basic-example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/fancy-example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed example-output.png
Binary file not shown.
23 changes: 23 additions & 0 deletions examples/basic/00-header
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/sh

[ -r /etc/hostname ] && HOSTNAME=$(cat /etc/hostname) || HOSTNAME=$(hostname)

[ -r /usr/lib/os-release ] && . /usr/lib/os-release || . /etc/os-release

if [ -n "$PRETTY_NAME" ]; then
OS_NAME="$PRETTY_NAME"
elif [ -n "$NAME" ]; then
OS_NAME="$NAME"
else
[ -r /etc/lsb-release ] && . /etc/lsb-release

if [ -n "$DISTRIB_DESCRIPTION" ]; then
OS_NAME="$DISTRIB_DESCRIPTION"
elif command -v lsb_release >/dev/null; then
OS_NAME=$(lsb_release -s -d)
else
OS_NAME="Unknown System"
fi
fi

env echo -e "Welcome to ${OS_NAME} $(uname -r) on ${HOSTNAME}"
28 changes: 28 additions & 0 deletions examples/fancy/00-header
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/sh

[ -r /etc/hostname ] && HOSTNAME=$(cat /etc/hostname) || HOSTNAME=$(hostname)
HOST_PFX="${HOSTNAME%%.*}"
HOST_DOMAIN="${HOSTNAME#*.}"

[ -r /usr/lib/os-release ] && . /usr/lib/os-release || . /etc/os-release

if [ -n "$PRETTY_NAME" ]; then
OS_NAME="$PRETTY_NAME"
elif [ -n "$NAME" ]; then
OS_NAME="$NAME"
else
[ -r /etc/lsb-release ] && . /etc/lsb-release

if [ -n "$DISTRIB_DESCRIPTION" ]; then
OS_NAME="$DISTRIB_DESCRIPTION"
elif command -v lsb_release >/dev/null; then
OS_NAME=$(lsb_release -s -d)
else
OS_NAME="Unknown System"
fi
fi

[ -n "$ANSI_COLOR" ] && OS_NAME="\033[${ANSI_COLOR}m${OS_NAME}\033[m"

figlet "$HOST_PFX" | sed -e 's/[[:space:]]*$//;${s/$/.'"${HOST_DOMAIN}"'/}' | lolcat -f
env echo -e "\nWelcome to ${OS_NAME} $(uname -r)\n"
16 changes: 16 additions & 0 deletions examples/fancy/10-sysinfo
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/sh

date=$(date)
load=$(awk '{print $1}' </proc/loadavg)
root_usage=$(df -h / | awk '/\// {print $(NF-1)}')
memory_usage=$(free -m | awk '/Mem:/ { total=$2; used=$3 } END { printf("%3.1f%%", used/total*100)}')
time=$(uptime | grep -ohe 'up .*' | sed 's/,/\ hours/g' | awk '{ printf $2" "$3 }')
processes=$(ps aux | wc -l)
public_ip=$(curl -s ipinfo.io/ip)

env echo -e "System information as of: \033[0;32m${date}\033[0m
System load: \033[0;32m${load}\033[0m System uptime: \033[0;32m${time}\033[0m
Memory usage: \033[0;32m${memory_usage}\033[0m IP Address: \033[0;32m${public_ip}\033[0m
Usage on /: \033[0;32m${root_usage}\033[0m Processes: \033[0;32m${processes}\033[0m
"
6 changes: 6 additions & 0 deletions examples/fancy/20-docker
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh
if command -v docker >/dev/null; then
env echo -e "$(docker info -f 'Running Docker \033[0;32mv{{.ServerVersion}}\033[0m
Containers: \033[0;32m{{.Containers}}\033[0m (\033[0;32m{{.ContainersRunning}}\033[0m running, \033[0;32m{{.ContainersPaused}}\033[0m paused, \033[0;32m{{.ContainersStopped}}\033[0m stopped)
Images: \033[0;32m{{.Images}}\033[0m')"
fi
13 changes: 13 additions & 0 deletions examples/fancy/30-xbps
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/sh
if command -v xbps-install >/dev/null; then
xbps-install -MSun | awk 'BEGIN { FS=" "; inscount = 0; upcount = 0; rmcount = 0 }
$2 == "install" { inscount++ }
$2 == "update" { upcount++ }
$2 == "remove" { rmcount++ }
END {
total = inscount + upcount + rmcount
if (total > 0) {
print "\n\033[0;32m" total "\033[0m updates available (\033[0;32m+" inscount " \033[0;34m~" upcount " \033[0;31m-" rmcount "\033[0m)"
}
}'
fi
3 changes: 3 additions & 0 deletions runit/motdd/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh
# update the motd every 10 minutes
exec snooze -d* -m* -w* -D* -W* -H* -M/10 -S0 update-motd
15 changes: 0 additions & 15 deletions update-motd.d/00-header

This file was deleted.

29 changes: 0 additions & 29 deletions update-motd.d/10-sysinfo

This file was deleted.

4 changes: 0 additions & 4 deletions update-motd.sh

This file was deleted.

0 comments on commit 4235c9a

Please sign in to comment.