Bash System Monitor Script

bash 1 views 2 hours, 19 minutes ago Public
Raw Download
bash 2292 characters
#!/bin/bash
# System Monitor Script
# Displays system information and resource usage

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color

# Function to print section headers
print_header() {
    echo -e "\n${BLUE}========================================${NC}"
    echo -e "${CYAN}$1${NC}"
    echo -e "${BLUE}========================================${NC}"
}

# System Information
print_header "SYSTEM INFORMATION"
echo -e "${GREEN}Hostname:${NC} $(hostname)"
echo -e "${GREEN}OS:${NC} $(uname -s)"
echo -e "${GREEN}Kernel:${NC} $(uname -r)"
echo -e "${GREEN}Architecture:${NC} $(uname -m)"
echo -e "${GREEN}Uptime:${NC} $(uptime -p)"

# CPU Information
print_header "CPU INFORMATION"
echo -e "${GREEN}CPU Model:${NC} $(lscpu | grep 'Model name' | cut -d ':' -f 2 | xargs)"
echo -e "${GREEN}CPU Cores:${NC} $(nproc)"
echo -e "${GREEN}CPU Usage:${NC}"
top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print "  " 100 - $1"%"}'

# Memory Information
print_header "MEMORY USAGE"
free -h | awk 'NR==2{printf "  Total: %s\n  Used: %s (%.2f%%)\n  Free: %s\n", $2, $3, $3*100/$2, $4}'

# Disk Usage
print_header "DISK USAGE"
df -h | awk 'NR==1 || /^\/dev/ {printf "  %-20s %10s %10s %10s %6s\n", $1, $2, $3, $4, $5}'

# Network Information
print_header "NETWORK INFORMATION"
echo -e "${GREEN}IP Addresses:${NC}"
ip -4 addr show | grep -oP '(?<=inet\s)\d+(\.\d+){3}' | while read ip; do
    echo "  $ip"
done

# Top Processes by CPU
print_header "TOP 5 PROCESSES BY CPU"
ps aux --sort=-%cpu | awk 'NR<=6{printf "  %-10s %5s %5s %s\n", $1, $3"%", $4"%", $11}'

# Top Processes by Memory
print_header "TOP 5 PROCESSES BY MEMORY"
ps aux --sort=-%mem | awk 'NR<=6{printf "  %-10s %5s %5s %s\n", $1, $3"%", $4"%", $11}'

# Logged in Users
print_header "LOGGED IN USERS"
who | awk '{printf "  %-15s %-15s %s\n", $1, $2, $3" "$4}'

# System Load
print_header "SYSTEM LOAD"
uptime | awk -F'load average:' '{print "  " $2}'

# Check if running as root for additional info
if [ "$EUID" -eq 0 ]; then
    print_header "RECENT LOGINS (Last 10)"
    last -n 10 | awk '{printf "  %-15s %-15s %-20s %s\n", $1, $3, $4" "$5" "$6" "$7, $9}'
fi

echo -e "\n${GREEN}Monitoring complete!${NC}\n"