summaryrefslogtreecommitdiffstats
path: root/motd
blob: 448c260448fb597ee2762d0b7f28bff867e84bed (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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.