ansible-role-motd/templates/etc/update-motd.d/30-sysinfo.j2

56 lines
2.0 KiB
Plaintext
Raw Normal View History

2018-09-25 19:55:16 +07:00
#!/bin/bash
2019-02-22 15:57:09 +07:00
W="\e[0;39m"
G="\e[1;32m"
R="\e[1;31m"
2018-09-25 19:55:16 +07:00
# get load averages
IFS=" " read LOAD1 LOAD5 LOAD15 <<<$(cat /proc/loadavg | awk '{ print $1,$2,$3 }')
# get free memory
IFS=" " read USED FREE TOTAL <<<$(free -htm | grep "Mem" | awk {'print $3,$4,$2'})
# get processes
PROCESS=`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"| grep root | awk {'print $2'}`
PROCESS_USER=`echo "$PROCESS"| grep -v root | awk {'print $2'} | awk '{ SUM += $1} END { print SUM }'`
# get processors
PROCESSOR_NAME=`grep "model name" /proc/cpuinfo | cut -d ' ' -f3- | awk {'print $0'} | head -1`
PROCESSOR_COUNT=`grep -ioP 'processor\t:' /proc/cpuinfo | wc -l`
2019-02-22 15:57:09 +07:00
## Adapted from https://github.com/mrlesmithjr/ansible-motd/blob/master/templates/etc/update-motd.d/30-sysinfo.j2
# Capture DNS Servers
DNS_SERVER="$(cat /etc/resolv.conf | grep nameserver | awk '{print $2}' | tr '\r\n' ' ')"
# Capture Interfaces
NET_INTERFACES="$(ip link show | awk -F: '$1>0 {print $2}' | grep -v lo)"
# Using captured interfaces loop through them and capture:
# link state and ip address
# Only if IP address is defined
first=""
NET_IPS="$(for int in ${NET_INTERFACES[@]};
do
state="$(ip link show $int | awk '{print $9}')"
ip_addr="$(ip -4 add show $int | grep inet | awk '{print $2}'| awk -F/ '{print $1}')"
if [ ! -z $ip_addr ]; then
echo -e "$W$first$int [$R$state$W]: $G$ip_addr"
first=" "
fi
done
)"
2018-09-25 19:55:16 +07:00
echo -e "
${W}system info:
$W Distro......: $W`cat /etc/*release | grep "PRETTY_NAME" | cut -d "=" -f 2- | sed 's/"//g'`
$W Kernel......: $W`uname -sr`
$W Uptime......: $W`uptime -p`
$W Load........: $G$LOAD1$W (1m), $G$LOAD5$W (5m), $G$LOAD15$W (15m)
2019-02-22 15:57:09 +07:00
$W Processes...: $W$G$PROCESS_ROOT$W (root), $G$PROCESS_USER$W (user), $G$PROCESS_ALL$W (total)
2018-09-25 19:55:16 +07:00
$W CPU.........: $W$PROCESSOR_NAME ($G$PROCESSOR_COUNT$W vCPU)
2019-02-22 15:57:09 +07:00
$W Memory......: $G$USED$W used, $G$FREE$W free, $G$TOTAL$W total
$W IP Addresses: $NET_IPS
$W DNS_SERVER..: $G$DNS_SERVER$W"