Skip to content

Instantly share code, notes, and snippets.

@NQevxvEtg
Last active May 26, 2026 16:59
Show Gist options
  • Select an option

  • Save NQevxvEtg/fbcf8b29d35dcf43913d1f8ca46c4994 to your computer and use it in GitHub Desktop.

Select an option

Save NQevxvEtg/fbcf8b29d35dcf43913d1f8ca46c4994 to your computer and use it in GitHub Desktop.
#!/bin/bash
# --- INITIALIZATION ---
# Hide cursor for a clean look
tput civis
# Ensure cursor returns on exit
trap "tput cnorm; echo -e '\nExiting...'; exit" SIGINT
# --- DATA GATHERING ---
get_stats() {
# 1. CPU (From /proc/loadavg)
read -r load1 _ _ _ _ < /proc/loadavg
cores=$(grep -c ^processor /proc/cpuinfo)
# Fast integer math: Load * 100 / Cores
cpu_util=$(( (10#${load1/./} * 100) / (cores * 100) ))
# 2. DISK & NET (Initial Snapshots)
# Finds all physical disks skipping loop/ram
readarray -t disks < <(lsblk -dno NAME | grep -v 'loop\|ram')
for i in "${!disks[@]}"; do
d="${disks[$i]}"
io_start[$i]=$(awk -v d="$d" '$3==d {print $13}' /proc/diskstats)
done
# Finds active network interface
net_dev=$(ip route get 8.8.8.8 2>/dev/null | sed -n 's/.* dev \([^ ]*\).*/\1/p')
net_dev=${net_dev:-eth0}
read rx1 tx1 < <(awk -v d="$net_dev:" '$1==d {print $2, $10}' /proc/net/dev)
sleep 1 # Synchronized 1-second sample window
# 3. DISK & NET (Final Snapshots)
for i in "${!disks[@]}"; do
d="${disks[$i]}"
io_end[$i]=$(awk -v d="$d" '$3==d {print $13}' /proc/diskstats)
disk_util[$i]=$(( (io_end[$i] - io_start[$i]) / 10 ))
[ "${disk_util[$i]}" -gt 100 ] && disk_util[$i]=100
done
read rx2 tx2 < <(awk -v d="$net_dev:" '$1==d {print $2, $10}' /proc/net/dev)
# Calculations
rx_kb=$(( (rx2 - rx1) / 1024 ))
tx_kb=$(( (tx2 - tx1) / 1024 ))
}
# --- ATOMIC RENDERING ---
render() {
# Move cursor to top-left
tput cup 0 0
# Build the entire screen in a variable first for zero-flicker
local out=""
out+="\e[1;33mSYSTEM MONITOR: $(hostname) | $(date +%H:%M:%S)\e[0m\n"
out+="----------------------------------------------------\n"
out+="\e[1;34m--- CPU & LOAD ---\e[0m\n"
out+="$(printf "Cores: %-4s | 1m Load: %-5s | Usage: %s%%" "$cores" "$load1" "$cpu_util")\e[K\n"
out+="\n\e[1;32m--- MEMORY & SWAP ---\e[0m\n"
out+="$(free -h | grep -E 'Mem|Swap' | column -t)\e[K\n"
out+="\n\e[1;36m--- DISK I/O (% BUSY) ---\e[0m\n"
for i in "${!disks[@]}"; do
out+="$(printf "Device: %-10s | Utilization: %s%%" "${disks[$i]}" "${disk_util[$i]}")\e[K\n"
done
out+="\n\e[1;36m--- NETWORK ACTIVITY ---\e[0m\n"
out+="$(printf "Interface: %-10s | IN: %s KB/s | OUT: %s KB/s" "$net_dev" "$rx_kb" "$tx_kb")\e[K\n"
out+="\n\e[1;35m--- TOP PROCESSES (CPU) ---\e[0m\n"
out+="$(ps -eo pid,pcpu,pmem,comm --sort=-pcpu | head -n 6 | tail -n +2 | column -t)\e[K\n"
# Print the entire buffer at once
echo -e "$out"
}
# --- MAIN ---
clear # Initial clear
while true; do
get_stats
render
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment