MOTDfetch/motdfetch.d/01-sysinfo

149 lines
5.6 KiB
Plaintext
Raw Permalink Normal View History

2023-02-07 10:10:46 +01:00
#!/usr/bin/env bash
#
# display a list of system informations
# + dig and curl have shorts timeouts to not hang the script
#
# requirements: sudo apt install dnsutils
# GENERAL ###########################################################
# locale env
unset LC_ALL
export LC_MESSAGES=C
# check if module was disabled
module_disable=${module_sysinfo_disable:=0}
if (($module_disable == 1)); then
exit 1
fi
# colors
c_txt=${c_txt:="39"}
c_txt_emphase=${c_txt_emphase:="35"}
c_txt_deco=${c_txt_deco:="97"}
c_txt_invert=${c_txt_invert:="30"}
c_bg=${c_bg_sec:="47"}
c_danger=${c_danger:="31"}
c_warning=${c_warning:="33"}
c_success=${c_success:="32"}
c_title=${c_title:="${c_bg};1;${c_txt_invert}m"}
# api website name for plain public IP4/6 check
publicip_api=${global_publicip_api:="icanhazip.com"}
# PREPARATIONS ######################################################
# output header
echo -e "\nWaiting for DNS and IP checks\e[5m...\e[0m"
# get hostname
HOSTNAME=`hostname -f`
# get distribution name
DISTRO=`/usr/bin/cat /etc/*release | /usr/bin/grep "PRETTY_NAME" | cut -d "=" -f 2- | sed 's/"//g'`
# get name of main interface with a route for internet
INTERNETIFACE="$(ip -o route get 8/32 2>/dev/null)"
if [[ "$INTERNETIFACE" ]]; then
INTERNETIFACE=`echo -e "$INTERNETIFACE" | sed -e 's/.*dev //' -e 's/ .*//'`
else
INTERNETIFACE="\e[1;${c_warning}mno interface with a route to 8/32\e[0;${c_txt}m"
fi
# get DNS servers ips from resolv.conf
DNSSERVERS=`/usr/bin/grep '^nameserver' /etc/resolv.conf | cut -d' ' -f2 | paste -sd ','`
if [[ ! $DNSSERVERS ]];then
DNSSERVERS="\e[1;${c_warning}mno ns in resolv.conf\e[0;${c_txt}m"
fi
# get current output public IPs and their revers DNS record
PUBLICIP4=`timeout 6 curl -s -4 $publicip_api 2>/dev/null`
if [[ $PUBLICIP4 ]];then
IP4PTR=`timeout 4 dig +short -x $PUBLICIP4 @1.1.1.1 2>/dev/null`
if [[ ! $IP4PTR ]];then
IP4PTR="\e[0;${c_txt}mno data"
fi
else
IP4PTR="\e[0;${c_txt}mno data"
PUBLICIP4="\e[1;5;${c_warning}mno IPv4 internet\e[0;${c_txt}m"
INTERNET_IP4=0
fi
PUBLICIP6=`timeout 6 curl -s -6 $publicip_api 2>/dev/null`
if [[ $PUBLICIP6 ]];then
IP6PTR=`timeout 4 dig +short -x $PUBLICIP6 @1.1.1.1 2>/dev/null`
if [[ ! $IP6PTR ]];then
IP6PTR="\e[0;${c_txt}mno data"
fi
else
IP6PTR="\e[0;${c_txt}mno data"
PUBLICIP6="\e[1;${c_warning}mno ipv6 internet\e[0;${c_txt}m"
INTERNET_IP6=0
fi
INTERNET_OUT=0
if [[ $INTERNET_IP4 && $INTERNET_IP6 ]]; then
INTERNET_OUT=1
fi
# get DNS records of the FQDN
HOSTNAMEIP4=`timeout 4 dig +short A $HOSTNAME @1.1.1.1 2>/dev/null`
if [[ ! $HOSTNAMEIP4 ]];then
HOSTNAMEIP4="\e[0;${c_txt}mno data"
fi
HOSTNAMEIP6=`timeout 4 dig +short AAAA $HOSTNAME @1.1.1.1 2>/dev/null`
if [[ ! $HOSTNAMEIP6 ]];then
HOSTNAMEIP6="\e[0;${c_txt}mno data"
fi
# get processes
PROCESS=`/usr/bin/ps -eo user= | sort | uniq -c | awk '{ print $2 " " $1 }'`
PROCESS_ALL=`echo "$PROCESS" | awk {'print $2'} | awk '{ SUM += $1} END { print SUM }'`
PROCESS_ROOT=`echo "$PROCESS" | /usr/bin/grep root | awk {'print $2'}`
PROCESS_USER=`echo "$PROCESS" | /usr/bin/grep -v root | awk {'print $2'} | awk '{ SUM += $1} END { print SUM }'`
# get processors
PROCESSOR_NAME=`/usr/bin/grep "model name" /proc/cpuinfo | cut -d ' ' -f3- | awk {'print $0'} | head -1 | sed -e 's/([^()]*)/ /g' -e 's/\s\+/ /g'`
PROCESSOR_COUNT=`/usr/bin/grep -ioPc 'processor\t:' /proc/cpuinfo 2>/dev/null`
# get load averages
IFS=" " read LOAD1 LOAD5 LOAD15 <<<$(cat /proc/loadavg | awk '{ print $1,$2,$3 }')
if [[ $(echo "$LOAD1 > $(echo "$PROCESSOR_COUNT + ($PROCESSOR_COUNT / 3)" | bc -l )" | bc) == 1 ]]; then
LOAD1="\e[1;5;${c_danger}m$LOAD1\e[0;${c_txt}m"
elif [[ $(echo "$LOAD1 > $(echo "$PROCESSOR_COUNT - ($PROCESSOR_COUNT / 3)" | bc -l )" | bc) == 1 ]]; then
LOAD1="\e[1;5;${c_warning}m$LOAD1\e[0;${c_txt}m"
fi
if [[ $(echo "$LOAD5 > $(echo "$PROCESSOR_COUNT * 2" | bc -l )" | bc) == 1 ]]; then
LOAD2="\e[1;5;${c_warning}m$LOAD5\e[0;${c_txt}m"
fi
if [[ $(echo "$LOAD15 > $(echo "$PROCESSOR_COUNT * 2" | bc -l )" | bc) == 1 ]]; then
LOAD3="\e[1;5;${c_warning}m$LOAD15\e[0;${c_txt}m"
fi
# get free memory
IFS=" " read USED AVAIL TOTAL <<<$(free -htm | /usr/bin/grep "Mem" | awk {'print $3,$7,$2'})
# get free swap
IFS=" " read SUSED SAVAIL STOTAL <<<$(free -htm | /usr/bin/grep "Swap" | awk {'print $3,$4,$2'})
# OUTPUT ############################################################
W="\e[0;${c_txt}m"
X="\e[1;${c_txt}m"
G="\e[1;${c_txt_emphase}m"
N="\e[0m"
echo -e "\e[1A\e[${c_title} System information $N
$X Distro$W..........: $DISTRO
$X Kernel$W..........: `uname -sr`
$X Uptime$W..........: $G`uptime -p`$W
$X CPU$W.............: $PROCESSOR_NAME
$X CPU cores$W.......: $G$PROCESSOR_COUNT$W
$X Load$W............: $G$LOAD1$W (1m), $G$LOAD5$W (5m), $G$LOAD15$W (15m)
$X Processes$W.......: $G$PROCESS_ROOT$W (root), $G$PROCESS_USER$W (user), $G$PROCESS_ALL$W (total)
$X Memory$W..........: $G$USED$W used, $G$AVAIL$W avail, $G$TOTAL$W total$N"
if [[ ${SUSED} ]]; then
echo -e "$X Swap$W............: $G$SUSED$W used, $G$SAVAIL$W avail, $G$STOTAL$W total$W"
else
echo -e "$X Swap$W............: \e[1;${c_warning}mno Swap$W"
fi
echo -e "
$X Hostname/FQDN$W...: $G$(hostname)$W / $G$(hostname -f)$W
$X Internet Iface$W..: $G$INTERNETIFACE$W
$X DNS servers$W.....: $G$(echo -e "${DNSSERVERS}" | sed "s/,/\\\e[0m, \\\e[1;${c_txt_emphase}m/g")$N"
if [[ ${INTERNET_OUT} == 0 ]]; then
echo -e "$X Public IP4/IP6$W..: $G$PUBLICIP4$W / $G$PUBLICIP6$N"
else
echo -e "$X Public IP4/IP6$W..: \e[1;${c_warning}mno internet or "${publicip_api}" timeout$N"
fi
echo -e "$X PTR IP4/IP6$W.....: $G$IP4PTR$W / $G$IP6PTR$W
$X FQDN A/AAAA$W.....: $G$HOSTNAMEIP4$W / $G$HOSTNAMEIP6$N"