commit ea8a9e07339a2a869b33b9f79598342a397cd3e0
Author: Cem Keylan <cem@ckyln.com>
Date: Thu, 2 Jan 2020 11:25:42 +0300
initial commit
Diffstat:
7 files changed, 131 insertions(+), 0 deletions(-)
diff --git a/Makefile b/Makefile
@@ -0,0 +1,25 @@
+# See LICENSE for more information
+#
+
+VERSION = 0.1
+PREFIX = /usr/local
+BINDIR = ${PREFIX}/bin
+SHAREDIR = ${PREFIX}/share
+
+MOTDPROGS = 00-header.motd \
+ 50-systeminfo.motd \
+ 99-alert-scripts.motd
+
+install:
+ mkdir -p ${DESTDIR}${BINDIR}
+ sed 's#SHAREDIR#${SHAREDIR}#g' < gen-motd | sed 's#vnumber#${VERSION}#g' > ${DESTDIR}${BINDIR}/gen-motd
+ chmod 755 ${DESTDIR}${BINDIR}/gen-motd
+ for file in ${MOTDPROGS} ; do \
+ install -Dm644 SHAREDIR/$$file ${DESTDIR}${SHAREDIR}/motd-scripts/$$file ; done
+
+uninstall:
+ ${RM} ${DESTDIR}${BINDIR}/gen-motd
+ for file in ${MOTDPROGS} ; do \
+ ${RM} ${DESTDIR}${SHAREDIR}/motd-scripts/$$file ; done
+
+.PHONY: install uninstall
diff --git a/README b/README
@@ -0,0 +1,10 @@
+motd-gen
+========
+
+Message of the Day Generator
+
+
+Description
+-----------
+
+motd-gen is a set of scripts that are
diff --git a/SHAREDIR/00-header.motd b/SHAREDIR/00-header.motd
@@ -0,0 +1,3 @@
+printf "Welcome to $(hostname)!\\n"
+
+# vim:filetype=sh
diff --git a/SHAREDIR/50-systeminfo.motd b/SHAREDIR/50-systeminfo.motd
@@ -0,0 +1,33 @@
+storage() {
+ DISK="$1"; [ -z "$DISK" ] && DISK="/"
+ printf "$DISK: "
+ command
+ command df -h | grep "$DISK\$" | awk '{print $3}' | sed s/G// | tr -d '\n'
+ printf "/"
+ command df -h | grep "$DISK\$" | awk '{print $2}' | sed s/G/\ GB/
+}
+
+ram() {
+ USED="$(command free -m | grep Mem: | awk '{print $3}')"
+ TOTAL="$(command free -m | grep Mem: | awk '{print $2}')"
+ printf "RAM: "
+ expr 100 \* "$USED" / "$TOTAL" | tr -d '\n'
+ printf "%%\\n"
+}
+
+ipv4() {
+ [ -z "$1" ] && return 1
+ IPV4="$(ip a show "$1" 2>/dev/null | grep 'inet ' | cut -d : -f 2 | awk '{print $2}' | cut -d / -f 1)"
+ [ -z "$IPV4" ] && return 1
+ printf "IP: $IPV4\\n"
+}
+
+printf "System information as of $(date +%a\ %d\ %b\ %Y\ %H:%M:%S):\\n\\n"
+ipv4 eth0
+ram
+storage
+uptime --pretty
+
+printf "\\n"
+
+# vim:filetype=sh
diff --git a/SHAREDIR/90-package-updates.motd b/SHAREDIR/90-package-updates.motd
@@ -0,0 +1 @@
+#!/usr/bin/env sh
diff --git a/SHAREDIR/99-alert-scripts.motd b/SHAREDIR/99-alert-scripts.motd
@@ -0,0 +1,35 @@
+CHECKFILE=/etc/motd-check
+
+# So the checkfile is a csv file
+# the first value is what service it
+# is.
+
+# For a runit service, it starts with r
+# For a docker container it starts with d
+# For a systemd service, it starts with s
+
+systemdcheck() {
+ systemctl is-active "$1" >/dev/null 2>&1 || printf "ALERT: $1 is not active\\n"
+}
+
+runitcheck() {
+ sv stat "$1" | grep -q down && printf "ALERT: $1 is not active\\n"
+}
+
+dockercheck() {
+ docker container ls | grep -q "$1" || printf "ALERT: $1 is not up\\n"
+}
+
+check() {
+ while IFS=, read -r service program ; do
+ case "$service" in
+ s) systemdcheck "$program" ;;
+ r) runitcheck "$program" ;;
+ d) dockercheck "$program" ;;
+ esac
+ done < "$CHECKFILE"
+}
+
+[ -n "$CHECKFILE" ] && check
+
+# vim:filetype=sh
diff --git a/gen-motd b/gen-motd
@@ -0,0 +1,24 @@
+#!/usr/bin/env sh
+
+VER=vnumber
+
+usage() { printf "usage: ${0##*/} [-f motd file] -[h|v]\\n\\n" ; exit 1;}
+printversion() { printf "${0##*/}-$VER\\n" ; exit 0;}
+
+[ "$1" = "--help" ] || [ "$1" = "-h" ] && usage
+[ "$1" = "--version" ] || [ "$1" = "-v" ] && printversion
+
+if [ "$1" = "-f" ] ; then
+ [ -z "$2" ] && exit 1
+ file="$2"
+fi
+[ -z "$file" ] && file="/etc/motd"
+
+if ! :> "$file"; then
+ printf "ERROR: You do not have write access to $file\\n"
+ exit 1
+fi
+
+for motd in SHAREDIR/motd-scripts/*.motd; do
+ . "$motd"
+done > "$file"