summaryrefslogtreecommitdiffstats
path: root/motd
diff options
context:
space:
mode:
Diffstat (limited to 'motd')
-rwxr-xr-xmotd106
1 files changed, 106 insertions, 0 deletions
diff --git a/motd b/motd
new file mode 100755
index 0000000..448c260
--- /dev/null
+++ b/motd
@@ -0,0 +1,106 @@
+#!/bin/sh
+#
+# *nix system agnostic motd
+# Automatically detect motd_* functions,
+# then output all key/value
+
+# @show_line
+# Show full motd line with prefix
+show_line() {
+ echo " ▌${@}"
+}
+
+# @show_label
+# Show a line with key/value(s)
+# - 1 : line description
+# - 2 : value
+show_label() {
+ separator_length=$(expr ${SEPARATOR_LENGTH} - ${#1})
+ separator_full=$(printf %${separator_length}s | tr " " "${SEPARATOR}")
+ show_line " ${1}${separator_full}: ${2}"
+}
+
+# @show_progress
+# Show a line with key/progress bar
+# - 1 : line description
+# - 2 : used value
+# - 3 : maximum value
+show_progress() {
+ progress_used_length=$((200*${2}/${3} % 2 + 100*${2}/${3} / 5))
+ progress_remaining_length=$((${PROGRESS_LENGTH} - ${progress_used_length}))
+ line="${ORANGE}${BOLD}"$(printf %${progress_used_length}s | tr " " "${PROGRESS_USED}")
+ line="${line}${CLEAR}"$(printf %${progress_remaining_length}s | tr " " "${PROGESS_REMAINING}")
+ show_label "${1}" "${line} ${2} on ${3}"
+}
+
+# *constants
+KEY_LIST=$(typeset -f | grep "^get_" | cut -d "(" -f1)
+SEPARATOR_LENGTH="36"
+SEPARATOR="."
+PROGRESS_USED="="
+PROGESS_REMAINING="-"
+PROGRESS_LENGTH="32"
+
+# *tput colors
+# Declare color sequences,
+# interpreted in motd rendering
+GREEN=$(tput setaf 34 0 0)
+ORANGE=$(tput setaf 208 0 0)
+RED=$(tput setaf 196 0 0)
+BOLD=$(tput bold)
+CLEAR=$(tput sgr0 ; tput setaf 15 0 0)
+
+kernel=$(uname -a | cut -d " " -f1 | tr "[:upper:]" "[:lower:]")
+kernel_rev=$(uname -a | cut -d " " -f3)
+hostname=$(uname -n)
+hostnick=$(uname -n | rev | cut -d "-" -f1 | rev)
+
+case "${kernel}" in
+ linux)
+ os=$(cat /etc/os-release | grep NAME | head -n1 | cut -d "=" -f2 | sed 's/"//g;s/ //g' | tr "[:upper:]" "[:lower:]")
+ ;;
+ openbsd)
+ os="openbsd"
+ ;;
+esac
+
+# *memory
+# Find and process memory usage
+case "${kernel}" in
+ linux)
+ memory_max=$(grep MemTotal /proc/meminfo | tr -s " " | cut -d " " -f2)
+ memory_free=$(grep MemFree /proc/meminfo | tr -s " " | cut -d " " -f2)
+ disks="$(df -x squashfs -x tmpfs -x devtmpfs)"
+ ;;
+ openbsd)
+ memory_max=$(sysctl hw.physmem | cut -d "=" -f2)
+ memory_free=$(vmstat | tail -n 1 | tr -s " " | cut -d " " -f5 | sed "s/M/000000/g")
+ disks="$(df)"
+ ;;
+esac
+memory_used=$(expr ${memory_max} - ${memory_free})
+disks=$(echo "${disks}" | tail +2 | tr -s " ")
+
+# *motd output
+
+echo
+figlet -f slant -w 180 "${hostnick}" | while IFS="" read -r line; do
+ echo " ▌ ${BOLD}${line}${CLEAR}"
+done
+echo
+show_line "system info:"
+show_label "os" "${os}"
+show_label "kernel" "${kernel}"
+show_label "kernel rev" "${kernel_rev}"
+show_label "user" "$(whoami)"
+show_label "hostname" "${hostname}"
+echo
+show_line "runtime:"
+show_progress "memory" "${memory_used}" "${memory_max}"
+echo "${disks}" | while IFS="" read -r disk; do
+ disk_name=$(echo "${disk}" | cut -d " " -f1)
+ disk_full=$(echo "${disk}" | cut -d " " -f2)
+ disk_used=$(echo "${disk}" | cut -d " " -f3)
+ show_progress "disk ${disk_name}" "${disk_used}" "${disk_full}"
+done
+echo
remember that computers suck.