Skip to content

Instantly share code, notes, and snippets.

@principis
Last active August 3, 2024 12:43
Show Gist options
  • Save principis/5c71258260c64ecaa62e3b1d376c8bfd to your computer and use it in GitHub Desktop.
Save principis/5c71258260c64ecaa62e3b1d376c8bfd to your computer and use it in GitHub Desktop.
#!/bin/bash
# Only print for interactive shells.
if [[ $- != *il* ]]; then
return 0
fi
function get_zpool_status {
pool_name="$1"
res=$(zpool list | grep "$pool_name" | awk '{print $3 " / " $2 "\t " $10}')
pool_status=$(echo "$res" | awk '{print $4}')
if [ "$pool_status" == "ONLINE" ]; then
color="\033[0;32m"
else
color="\033[0;31m"
fi
echo -e "${res//$pool_status/${color}${pool_status}\033[0m}"
}
FAILED=$(systemctl list-units --state=failed --no-legend --plain)
sensor="coretemp-isa-0000"
USER=$(whoami)
HOSTNAME=$(uname -n)
ROOT=$(get_zpool_status "rpool")
TANK=$(get_zpool_status "tank")
TEMPERATURE=$(sensors $sensor | awk 'NR==3{print $4}')
MEMORY1=$(free -t -m | grep "Mem" | awk '{print $3;}')
MEMORY2=$(free -t -m | grep "Mem" | awk '{print $2 " MB";}')
PSA=$(ps -Afl | wc -l)
#System uptime
uptime=$(cut </proc/uptime -f1 -d.)
upDays=$((uptime / 60 / 60 / 24))
upHours=$((uptime / 60 / 60 % 24))
upMins=$((uptime / 60 % 60))
upSecs=$((uptime % 60))
#System load
LOAD=$(awk '{print $1", "$2", "$3}' /proc/loadavg)
IP_BR0=$(ifconfig br0 | grep -E 'inet ' | awk '{print $2}')
# disk temperature
DISKS=($(lsblk -dl | tail -n +2 | grep -F -v "SWAP" | cut -d' ' -f1))
DISK_TEMPS=''
for D in "${DISKS[@]}"; do
DEV="/dev/${D}"
temp=$(smartctl -A $DEV | grep 'Temperature' | head -n 1 | grep -oE '\s[0-9]{2}(\s|$)' | xargs)
health=$(smartctl -H $DEV | grep 'test result' | cut -d':' -f2 | xargs)
DISK_TEMPS+="$D,$temp,$health\n"
done
# VMs
VMS=$(virsh list --all --name)
VM_OUTPUT=''
for N in $VMS; do
state=$(virsh domstate "$N" --reason | head -n 1)
domif=$(virsh domiflist "$N" | awk 'NR==3')
if=$(echo "$domif" | awk '{print $1}')
mac=$(echo "$domif" | awk '{print $5}')
ip=$(arp -n | grep "$mac" | awk '{print $1}')
#memory
memstat=$(virsh dommemstat "$N" 2>/dev/null)
avail=$(echo "$memstat" | grep -oE 'available [0-9]+' | awk '{print $2}')
usabl=$(virsh dommemstat "$N" 2>/dev/null | grep -oE 'usable [0-9]+' | awk '{print $2}')
mem="-"
[ -z "$avail" ] || [ -z "$usabl" ] || mem="$((($avail - $usabl) / 1024)) / $(($avail / 1024))"
VM_OUTPUT+="$N,$state,$if,$ip,$mem\n"
done
#Return message
[ -z "$TEMPERATURE" ] && TEMPERATURE="not available"
[ -x "$(command -v sensors)" ] || TEMPERATURE="lm_sensors not found"
echo -ne "
System information
------------------
Hostname: $HOSTNAME
Release: $(</etc/redhat-release)
Users: Currently $(users | wc -w) user(s) logged on
IPv4 address for br0: $IP_BR0
System load: $LOAD (1, 5, 15 min)
Memory usage: $MEMORY1 / $MEMORY2
Swap usage: $(free -m | tail -n 1 | awk '{print $3}') MB
Processes: $PSA
Uptime: $upDays days $upHours hours $upMins minutes $upSecs seconds
zpool root: $ROOT
zpool tank: $TANK
CPU Temperature: $TEMPERATURE
Disks
------------
"
printf "$DISK_TEMPS" | column --table --table-columns='Device,Temp (°C),Health' --separator=',' | awk '{print " " $0}'
echo -ne "
VM Information
--------------
"
printf "$VM_OUTPUT" | column --table --table-columns='Name,State,Interface,IPv4,Memory (MB)' --separator=',' | awk '{print " " $0}'
echo
if [[ -n "${FAILED}" ]]; then
COUNT=$(wc -l <<<"${FAILED}")
echo -e " => Systemd: \033[31m${COUNT}\033[39m failed units:"
awk '{ print " " $1 }' <<<"${FAILED}"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment