Website/motd.sh

416 lines
14 KiB
Bash

#! /bin/bash
##########################################################################
## My spin on the MOTD ##
## version 4.2.3-71423 ##
## ##
## Copy this script to the /usr/local/bin directory and name it motd ##
## ##
## chmod 755 /usr/local/bin/motd and run it "motd" and it will make ##
## the the changes automatically and install the pkgs required ##
## to run at each login ##
## ##
## Questions or Comments: pconnor@ara.com ##
## ##
## TODO: add config file for some settings ##
##########################################################################
if [ "$(command -v lsb_release)" ]; then
OS=$(lsb_release -i | awk '{print $3}')
OSVER=$(lsb_release -r | awk '{print $2}')
CODENAME=$(lsb_release -c | awk '{print $2}' | tr '[:upper:]' '[:lower:]')
else
OS=$(grep PRETTY_NAME /etc/os-release | sed 's/PRETTY_NAME=//g' | tr -d '="' | awk '{print $1}')
OSVER=$(grep VERSION_ID /etc/os-release | sed 's/VERSION_ID=//g' | tr -d '="')
if ! grep CODENAME /etc/os-release; then
CODENAME=$(cat < /etc/system-release | awk '{print $5}' | tr -d '("' | tr -d ')"')
else
CODENAME=$(grep VERSION_CODENAME /etc/os-release | sed 's/VERSION_CODENAME=//g' | tr -d '="' | awk -F. '{print $1}')
fi
fi
###########################################################
#### Detect Package Manger from OS and OSVer Variables ####
###########################################################
if [ "${OS}" = Amazon ]; then
PAKMGR="yum -y"
elif [ "${OS}" = Red ] || [ "${OS}" = Rocky ]; then
if [ "${OSVER}" = 7 ]; then
PAKMGR="yum -y"
else
PAKMGR="dnf -y"
fi
elif [ "${OS}" = Debian ] || [ "${OS}" = Ubuntu ]; then
PAKMGR="apt -y"
fi
##############################
#### Terminal Setup Start ####
##############################
export TERM=xterm-256color
tput rmam
######################
#### Script Setup ####
######################
if ! grep -q motd /etc/profile; then
echo '/usr/local/bin/motd' >> /etc/profile
fi
if [ ! -f "/etc/profile.d/colorprompt.sh" ]; then
# shellcheck disable=SC2016,SC2028
{
echo '#########################################################################'
echo '#### This script is to setup and color the system prompt and can ####'
echo '#### be removed by a chmod 000 or deleting the file. Questions or ####'
echo '#### changes please contact Phil Connor pconnor@ara.com ####'
echo '#########################################################################'
echo '# UID 0 is the root user'
echo 'if [[ $UID == 0 ]];then'
echo ' # prompt for root user'
echo " PS1='\t [\[\033[01;31m\]\u\[\033[0m\]@\h: \[\033[01;34m\]\W\[\033[0m\]]# '"
echo 'else'
echo ' # prompt for all logged in users'
echo " PS1='[\[\033[38;5;221m\]\u\[\033[0m\]@\h: \[\033[01;34m\]\W\[\033[0m\]]% '"
echo 'fi'
} > /etc/profile.d/colorprompt.sh
fi
if [[ "${OS}" = Debian || "${OS}" = Ubuntu ]]; then
if ! grep -q colorprompt.sh ~/.bashrc; then
echo 'source /etc/profile.d/colorprompt.sh' >> ~/.bashrc
fi
fi
if [ ! -f "/etc/banner" ]; then
echo "
------------------------------------------------------------------------------
(########################## **** WARNING! **** ############################)
#) (#
(# This system is the property of Linda's.Work Servers, and is to be #)
#) used in accordance with applicable LWS Policies. Unauthorized access or (#
(# activity is a violation of LWS Policies and may be a violation of law. #)
#) Use of this system constitutes consent to monitoring for unauthorized (#
(# use, in accordance with LWS Policies, local laws, and regulations. #)
#) Unauthorized use may result in penalties including, but not limited to, (#
(# reprimand, dismissal, financial penalties, and legal action. #)
#) (#
(##############################################################################)
------------------------------------------------------------------------------
" > /etc/banner
fi
if [ ! "$(command -v figlet)" ]; then
if [ "${OS}" = Debian ] || [ "${OS}" = Ubuntu ]; then
$PAKMGR update
$PAKMGR install figlet
elif [ "${OS}" = Red ] || [ "${OS}" = Rocky ]; then
$PAKMGR install epel-release
$PAKMGR install figlet
else
$PAKMGR install figlet
fi
fi
if [ ! "$(command -v gem)" ]; then
$PAKMGR install gem
fi
if [ ! "$(command -v lolcat)" ]; then
if [ "${OS}" = Debian ]; then
gem install lolcat
$PAKMGR update
$PAKMGR install lolcat
elif [ "${OS}" = Ubuntu ]; then
$PAKMGR update
$PAKMGR install lolcat
else
gem install lolcat
chmod 755 /usr/local/bin/lolcat
fi
fi
# ------- TODO START -------- #
###############################
#### Script directory path ####
###############################
if [ -n "${BASH_SOURCE[0]}" ]; then
DIR=$(dirname "${BASH_SOURCE[0]}")
elif [ -n "${0}" ]; then
DIR=$(dirname "$(readlink -f "$0")")
fi
##########################################
#### Configuration file and fallbacks ####
##########################################
CONFIG_FILE="${DIR}/motd.conf"
# shellcheck source=/dev/null
if test -f "${CONFIG_FILE}"; then
. "${DIR}/motd.conf"
fi
if [ -z ${DATE_FORMAT+x} ]; then
DATE_FORMAT="%x %X"
fi
# ------- TODO END ------- #
########################
### Banner Functions ###
########################
function banner() {
lolcat -f '/etc/banner'
}
function hostn() {
hname=$(hostname -s)
figlet -f slant -c "$hname" | lolcat -f
}
function logo() {
figlet -cf mini A Linda\'s Work Server | lolcat -f
}
################
#### Colors ####
################
BW="\033[38;5;15m" # Bold White
CD="\033[0m" # Default
CRB="\033[1;31m" # Red bold
CG="\033[0;32m" # Green
CYB="\033[1;33m" # Yellow bold
# CYL="\033[3;33m" # Yellow light
CBB="\033[1;34m" # Blue bold
# CML="\033[3;35m" # Magenta light
# CCL="\033[3;36m" # Cyan light
NC="\033[00m"
###############################
#### OS - (System Section) ####
###############################
DATE=$(date +"${DATE_FORMAT}")
DISTRIBUTION_NAME=$OS
DISTRIBUTION_VERSION=$OSVER
DISTRIBUTION_CODENAME=$CODENAME
UPTIME=$(uptime | awk '{print $3 " " $4}' | sed s'/.$//')
USER_COUNT=$(users | wc -w)
PROCESSES_RUNNING=$(ps aux | wc -l)
if [ -z ${SYSTEM_NAME+x} ]; then
SYSTEM_NAME=$(hostname)
else
SYSTEM_NAME+=" ($(hostname))"
fi
if [ "$(command -v timedatectl)" ]; then
TIMEZONE=$(timedatectl | grep "Time" | awk '{print $3" "$4" UTC"$5}')
else
TIMEZONE=$(date +"%Z %z")
fi
#####################
#### Time of Day ####
#####################
HOUR=$(date +"%H")
if [ "$HOUR" -lt 12 ] && [ "$HOUR" -ge 0 ]; then
TIME="Morning User"
elif [ "$HOUR" -lt 17 ] && [ "$HOUR" -ge 12 ]; then
TIME="Afternoon User"
else
TIME="Evening User"
fi
###############################
#### CPU - (Usage Section) ####
###############################
CPU_MODEL=$(grep -m 1 "model name" < /proc/cpuinfo | awk '{a="";for (i=4;i<=NF;i++){a=a$i" "}print a}')
CPU_LOAD=$(awk '{print $1*100}'< /proc/loadavg)
CPU_LOAD_AVG=$(awk '{print $1" "$2" "$3}'< /proc/loadavg)
CPU_CORES=$(nproc)
CPU_USAGE=$((CPU_LOAD / CPU_CORES))
CPU_SPEED=$(lscpu | grep -m 1 "MHz" | awk '{for(i=NF;i>=1;i--) printf "%s ", $i;print ""}' | awk '{print $1}' | cut -f1 -d".")
##################################
#### Memory - (Usage Section) ####
##################################
MEMORY_TOTAL=$(free -m | grep "Mem" | awk '{print $2}')
MEMORY_USAGE=$(free -m | grep "Mem" | awk '{print $3}')
MEMORY_USAGE_PERCENT=$(( MEMORY_USAGE * 100 / MEMORY_TOTAL ))
################################
#### Swap - (Usage Section) ####
################################
SWAP_TOTAL=$(free -m | grep "Swap" | awk '{print $2}')
SWAP_USAGE=$(free -m | grep "Swap" | awk '{print $3}')
# SWAP space is optional, so it needs to be checked if it exist.
# shellcheck disable=2140
if [[ "${SWAP_TOTAL}" -gt 0 ]]; then
SWAP_USAGE_PERCENT=$(( SWAP_USAGE * 100 / SWAP_TOTAL ))
else
SWAP_USAGE_PERCENT='-'
fi
################################
#### Disk - (Space Section) ####
################################
function drive_space_used() {
{
W="\e[0;39m"
G="\e[1;32m"
R="\e[1;31m"
Y="\e[1;33m"
dim="\e[2m"
undim="\e[0m"
max_usage=90
bar_width=26
medium_usage=70
mapfile -t dfs < <(df -H -x zfs -x squashfs -x tmpfs -x devtmpfs -x overlay --output=target,pcent,size | tail -n+2)
for line in "${dfs[@]}"
do
usage=$(echo "$line" | awk '{print $2}' | sed 's/%//')
used_width=$((( usage * bar_width) / 100 ))
if [ "${usage}" -ge "${max_usage}" ]; then
color=$R
elif [ "${usage}" -ge "${medium_usage}" ]; then
color=$Y
else
color=$G
fi
bar="[${color}"
for (( i = 0; i < used_width; i++ ))
do
bar+="="
done
bar+="${W}${dim}"
for (( i = used_width; i < bar_width; i++))
do
bar+="·"
done
bar+="${undim}]"
echo "${line}" | awk '{ printf("%-16s%+3s used out of %+4s", $1, $2, $3); }' | sed -e 's/^/ /' | lolcat -f
echo -e "${bar}" | sed -e 's/^/ /'
done
}
}
#######################
#### Network Block ####
#######################
if [ "$(command -v ip)" ]; then
mapfile -t INTERFACES < <(ip -o link show | awk -F': ' '{print $2}' | grep -v -E "lo|veth")
# IP V4
for INTERFACE in "${INTERFACES[@]}"
do
if [[ "${INTERFACE}" != "${INTERFACES[0]}" ]] ; then
IP_V4="${IP_V4}\n ${CG}"
fi
mapfile -t IPS < <(ip addr show "${INTERFACE}" | grep -oP '(?<=inet\s)\d+(\.\d+){3}')
INTERFACE_IPS=""
for IP in "${IPS[@]}"
do
INTERFACE_IPS="${INTERFACE_IPS} ${IP}"
done
IP_V4="${IP_V4}${INTERFACE}${INTERFACE_IPS}"
done
# IP V6
for INTERFACE in "${INTERFACES[@]}"
do
if [[ "${INTERFACE}" != "${INTERFACES[0]}" ]] ; then
IP_V6="${IP_V6}\n ${CG}"
fi
mapfile -t IPS < <(ip addr show "${INTERFACE}" | grep -oP '(?<=inet6\s)\w+(:?:\w+){4}')
INTERFACE_IPS=""
for IP in "${IPS[@]}"
do
INTERFACE_IPS="${INTERFACE_IPS} ${IP}"
done
IP_V6="${IP_V6}${INTERFACE}${INTERFACE_IPS}"
done
else
IP1=$(hostname -I | awk '{print $1}')
IP2=$(hostname -I | awk '{print $2}')
IP3=$(hostname -I | awk '{print $3}')
if [[ $IP1 =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
IP1R="0"
else
IP1R="1"
fi
if [[ $IP2 =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
IP2R="1"
IP2PIP="1"
else
IP2R="0"
fi
if [[ $IP3 =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
IP3R="1"
else
IP3R="0"
fi
fi
##########################
### Host Display Block ###
##########################
banner
hostn
printf "\n"
printf " %b%b\n\n" "${CYB}" "${SYSTEM_NAME}${NC}"
printf " %bSYSTEM %b\n" "${CRB}" "${CD}"
echo ''
echo -e "${CD}${CBB}Distribution ${CG} ${DISTRIBUTION_NAME} ${DISTRIBUTION_VERSION} (${DISTRIBUTION_CODENAME})" | lolcat -f
echo -e "${CD}${CBB}CPU ${CG} ${CPU_MODEL}x ${CPU_CORES} cores" | lolcat -f
echo -e "${CD}${CBB}Timezone ${CG} ${TIMEZONE}" | lolcat -f
echo -e "${CD}${CBB}Date ${CG} ${DATE}" | lolcat -f
echo -e "${CD}${CBB}Uptime ${CG} ${UPTIME}" | lolcat -f
echo -e "${CD}${CBB}Users ${CG} ${USER_COUNT} Currently Logged in" | lolcat -f
echo ''
printf " %bUSAGE %b\n" "${CRB}" "${CD}"
echo ''
echo -e "${CD}${CBB}CPU ${BW} ${CPU_USAGE}% ${CG}(${CPU_LOAD_AVG}) @ ${CPU_SPEED} MHz" | lolcat -f
echo -e "${CD}${CBB}Memory ${CG} ${MEMORY_USAGE_PERCENT}% (${MEMORY_USAGE} MB of ${MEMORY_TOTAL} MB)" | lolcat -f
echo -e "${CD}${CBB}Swap ${CG} ${SWAP_USAGE_PERCENT}% (${SWAP_USAGE} MB of ${SWAP_TOTAL} MB)" | lolcat -f
echo -e "${CD}${CBB}Processes ${CG} ${PROCESSES_RUNNING} (running)" | lolcat -f
echo ''
printf " %bSPACE %b\n" "${CRB}" "${CD}"
echo ''
printf "${CD}${CBB}Drive Usage ${CG}%b\n"
drive_space_used
echo ''
printf " %bNETWORK %b\n" "${CRB}" "${CD}"
echo ''
if [ "$IP1R" != 1 ]; then
printf "${CD}${CBB}IPv4 ${CG}%b\n" "${IP_V4}""${IP1}" | lolcat -f
fi
if [ "$IP2R" != 1 ]; then
printf "${CD}${CBB}IPv6 ${CG}%b\n" "${IP_V6}""${IP2}" | lolcat -f
fi
if [ "$IP2PIP" == 1 ]; then
printf "${CD}${CBB}Private IP ${CG}%b\n" "${IP2}" | lolcat -f
elif [ "$IP3R" == 1 ]; then
printf "${CD}${CBB}Private IP ${CG}%b\n" "${IP3}" | lolcat -f
fi
printf " %b" "${CD}"
printf "\n"
logo
echo -e "${BW}Good $TIME ${CYB}$USER${NC}"
######################
### Terminal Reset ###
######################
tput smam