Resource Monitoring
Resource monitoring - bu Linux tizimlarida CPU, memory, disk va network resurslarini kuzatish va tahlil qilish jarayoni. Bu system performance optimization va troubleshooting uchun juda muhim.
System Resource Overview
Resource Types
# CPU Resources
# - Processing power
# - Core utilization
# - Load average
# - Context switches
# Memory Resources
# - Physical RAM
# - Swap space
# - Buffers and cache
# - Memory leaks
# I/O Resources
# - Disk read/write
# - Network traffic
# - File descriptors
# - I/O wait time
# System Resources
# - Process count
# - Open files
# - Network connections
# - System calls
Quick System Overview
# Basic system information
uname -a # System information
uptime # System uptime and load
who # Logged in users
date # Current date and time
# Resource summary
free -h # Memory usage
df -h # Disk usage
lscpu # CPU information
cat /proc/version # Kernel version
cat /proc/meminfo | head -20 # Memory details
Process Monitoring with ps
Basic ps Usage
# Process status
ps # Current user's processes
ps aux # All processes (BSD format)
ps -ef # All processes (UNIX format)
ps -eLf # Include threads
# Process hierarchy
ps axjf # Process tree with job info
ps -ef --forest # Process tree format
pstree # Dedicated tree view
pstree -p # With PIDs
pstree username # User's processes
Advanced ps Filtering
# Filter by user
ps -u username # Specific user's processes
ps aux | grep "^username" # Alternative filtering
# Filter by process name
ps aux | grep nginx # Processes containing "nginx"
pgrep nginx # PIDs only
pgrep -f "nginx.*worker" # Full command line match
# Filter by CPU/Memory usage
ps aux --sort=-pcpu | head -10 # Top CPU consumers
ps aux --sort=-pmem | head -10 # Top memory consumers
ps aux --sort=+pcpu | head -10 # Lowest CPU usage
# Custom output format
ps -eo pid,ppid,cmd,pcpu,pmem # Custom columns
ps -eo pid,user,cmd,pcpu,pmem --sort=-pcpu | head -20
ps -eo pid,etime,cmd # Process runtime
ps -eo pid,nlwp,cmd # Number of threads
Process Resource Analysis
# Detailed process information
ps -p 1234 -o pid,ppid,cmd,pcpu,pmem,vsz,rss,etime,nlwp
# Process with specific criteria
ps aux | awk '$3 > 5.0 {print $0}' # Processes using >5% CPU
ps aux | awk '$4 > 10.0 {print $0}' # Processes using >10% memory
ps aux | awk '$6 > 100000 {print $0}' # Processes using >100MB memory
# Process state analysis
ps aux | awk '{print $8}' | sort | uniq -c # Process states count
# Process states:
# D - Uninterruptible sleep
# R - Running
# S - Sleeping
# T - Traced/Stopped
# Z - Zombie
Real-time Monitoring with top
Basic top Usage
# Start top
top # Default view
top -u username # Filter by user
top -p 1234,5678 # Monitor specific PIDs
top -n 1 # Single iteration (no interactive)
top -b # Batch mode (for scripts)
# Top header interpretation:
# Load average: 1-minute, 5-minute, 15-minute averages
# Tasks: total, running, sleeping, stopped, zombie
# CPU: user time, system time, nice time, idle time, I/O wait
# Memory: total, free, used, buff/cache
# Swap: total, free, used, available memory
Interactive top Commands
# While in top, press:
h # Help
q # Quit
k # Kill process (enter PID)
r # Renice process (change priority)
u # Filter by user
o # Add filter (e.g., o COMMAND=nginx)
f # Choose display fields
s # Change update interval
z # Toggle color
x # Highlight sort column
y # Highlight running tasks
# Sorting (while in top):
P # Sort by CPU usage
M # Sort by memory usage
T # Sort by time/cumulative
N # Sort by PID
top Configuration
# Save top configuration
# Press 'W' in top to save current settings to ~/.toprc
# Custom top startup
echo 'RCfile for "top with windows"' > ~/.toprc
# Configuration will be saved when you press 'W' in top
# Batch mode for monitoring
top -b -n 1 | head -20 # Single snapshot
top -b -d 5 -n 12 > top_output.txt # 12 iterations, 5 second delay
# Top output parsing
top -b -n 1 | grep "load average" | awk '{print $10, $11, $12}'
top -b -n 1 | grep "Cpu(s)" | awk '{print $2}' | sed 's/%us,//'
Enhanced Monitoring with htop
htop Features
# Install htop
sudo apt install htop # Ubuntu/Debian
sudo yum install htop # CentOS/RHEL (EPEL required)
# Start htop
htop # Enhanced top interface
htop -u username # Filter by user
htop -p 1234,5678 # Monitor specific PIDs
# htop advantages over top:
# - Color-coded display
# - Mouse support
# - Horizontal scrolling
# - Tree view
# - Easy killing and renicing
# - Visual CPU/memory bars
htop Interactive Commands
# Navigation:
↑↓ # Select process
←→ # Horizontal scroll
PgUp/PgDn # Page up/down
Home/End # First/last process
# Process management:
F9 or k # Kill process
F7/F8 or PageUp/PageDown # Change nice value
F6 # Sort by column
F5 # Tree view toggle
F4 # Filter processes
F3 or / # Search processes
# Display options:
F2 # Setup/configuration
F10 or q # Quit
h # Help
t # Tree view
H # Hide/show threads
K # Hide/show kernel threads
htop Configuration
# Configuration file
~/.config/htop/htoprc
# Common configuration options:
# Meter layout
# Color scheme
# Sort column
# Display options
# Update interval
# Save configuration
# Press F2 in htop, make changes, then F10 to save
Memory Monitoring
Memory Usage Analysis
# Basic memory information
free # Memory usage
free -h # Human readable
free -h -s 5 # Update every 5 seconds
free -h -c 10 # 10 iterations
# Memory breakdown
cat /proc/meminfo # Detailed memory info
cat /proc/meminfo | grep -E "MemTotal|MemFree|MemAvailable|Buffers|Cached|SwapTotal|SwapFree"
# Memory usage by process
ps aux --sort=-pmem | head -20 # Top memory consumers
ps -eo pid,ppid,cmd,pmem --sort=-pmem | head -20
# Memory map of process
pmap 1234 # Memory map for PID 1234
pmap -d 1234 # Detailed memory map
pmap -x 1234 # Extended format
Swap Usage Analysis
# Swap information
swapon --show # Active swap devices
cat /proc/swaps # Swap usage details
# Swap usage by process
find /proc -maxdepth 2 -name smaps -executable -exec awk '
/^Name:/ { name = $2 }
/^Swap:/ { swap += $2 }
END { if (swap > 0) printf "%s\t%s\n", swap, name }
' {} \; | sort -nr | head -20
# Memory pressure indicators
vmstat 1 # Virtual memory statistics
vmstat 1 10 # 10 iterations, 1 second interval
# Key vmstat columns:
# si/so - swap in/out
# bi/bo - blocks in/out
# in/cs - interrupts/context switches
Memory Monitoring Script
#!/bin/bash
# memory-monitor.sh - Memory usage monitoring
THRESHOLD_MEMORY=80
THRESHOLD_SWAP=50
LOG_FILE="/var/log/memory-monitor.log"
log_message() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE"
}
check_memory_usage() {
# Get memory usage percentage
memory_usage=$(free | awk 'NR==2{printf "%.1f", $3/$2*100}')
available_memory=$(free -h | awk 'NR==2{print $7}')
log_message "Memory usage: ${memory_usage}% (Available: $available_memory)"
if (( $(echo "$memory_usage > $THRESHOLD_MEMORY" | bc -l) )); then
log_message "WARNING: High memory usage detected!"
# Log top memory consumers
log_message "Top memory consumers:"
ps aux --sort=-pmem | head -10 | while read line; do
log_message " $line"
done
# Check for memory pressure
if [ -f /proc/pressure/memory ]; then
memory_pressure=$(cat /proc/pressure/memory | grep some | awk '{print $2}' | cut -d= -f2)
log_message "Memory pressure (some): $memory_pressure"
fi
fi
}
check_swap_usage() {
if [ -f /proc/swaps ] && [ "$(wc -l < /proc/swaps)" -gt 1 ]; then
swap_usage=$(free | awk 'NR==3{if($2>0) printf "%.1f", $3/$2*100; else print "0"}')
swap_used=$(free -h | awk 'NR==3{print $3}')
log_message "Swap usage: ${swap_usage}% (Used: $swap_used)"
if (( $(echo "$swap_usage > $THRESHOLD_SWAP" | bc -l) )); then
log_message "WARNING: High swap usage detected!"
# Find processes using swap
log_message "Processes using swap:"
find /proc -maxdepth 2 -name smaps 2>/dev/null | while read smaps; do
pid=$(echo "$smaps" | cut -d/ -f3)
if [ -r "$smaps" ]; then
swap_kb=$(awk '/^Swap:/ {sum += $2} END {print sum}' "$smaps" 2>/dev/null)
if [ -n "$swap_kb" ] && [ "$swap_kb" -gt 1024 ]; then
cmd=$(ps -p "$pid" -o comm= 2>/dev/null)
if [ -n "$cmd" ]; then
log_message " PID $pid ($cmd): ${swap_kb} KB swap"
fi
fi
fi
done | sort -k4 -nr | head -10
fi
else
log_message "No swap space configured"
fi
}
check_oom_killer() {
# Check for recent OOM killer activity
oom_count=$(dmesg | grep -c "Out of memory\|OOM killer\|Killed process")
if [ "$oom_count" -gt 0 ]; then
log_message "OOM killer activity detected ($oom_count events)"
# Show recent OOM events
log_message "Recent OOM events:"
dmesg | grep -E "Out of memory|OOM killer|Killed process" | tail -5 | while read line; do
log_message " $line"
done
fi
}
# Main execution
log_message "=== Memory Monitor Check ==="
check_memory_usage
check_swap_usage
check_oom_killer
log_message "Memory monitor check completed"
# Crontab entry:
# */5 * * * * /usr/local/bin/memory-monitor.sh
CPU Monitoring
CPU Usage Analysis
# CPU information
lscpu # CPU architecture info
cat /proc/cpuinfo # Detailed CPU info
nproc # Number of processing units
# CPU usage monitoring
top -bn1 | grep "Cpu(s)" # Current CPU usage
mpstat # Multi-processor statistics
mpstat 1 5 # 1 second interval, 5 times
sar -u 1 5 # CPU utilization
# Load average
uptime # Load averages
cat /proc/loadavg # Load average file
w # Who and load average
# CPU usage by process
ps aux --sort=-pcpu | head -20 # Top CPU consumers
pidstat 1 # Per-process CPU statistics
CPU Performance Monitoring
#!/bin/bash
# cpu-monitor.sh - CPU performance monitoring
THRESHOLD_CPU=80
THRESHOLD_LOAD=4.0
LOG_FILE="/var/log/cpu-monitor.log"
log_message() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE"
}
check_cpu_usage() {
# Get overall CPU usage
cpu_usage=$(top -bn1 | grep "Cpu(s)" | awk '{print $2}' | sed 's/%us,//')
cpu_idle=$(top -bn1 | grep "Cpu(s)" | awk '{print $8}' | sed 's/%id,//')
log_message "CPU usage: ${cpu_usage}% (Idle: ${cpu_idle}%)"
if (( $(echo "$cpu_usage > $THRESHOLD_CPU" | bc -l) )); then
log_message "WARNING: High CPU usage detected!"
# Log top CPU consumers
log_message "Top CPU consumers:"
ps aux --sort=-pcpu | head -10 | while read line; do
log_message " $line"
done
# Check CPU frequency scaling
if [ -f /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq ]; then
freq=$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq)
freq_mhz=$((freq / 1000))
log_message "CPU frequency: ${freq_mhz} MHz"
fi
fi
}
check_load_average() {
# Get load averages
load_1min=$(uptime | awk '{print $(NF-2)}' | sed 's/,//')
load_5min=$(uptime | awk '{print $(NF-1)}' | sed 's/,//')
load_15min=$(uptime | awk '{print $NF}')
log_message "Load average: $load_1min, $load_5min, $load_15min"
# Check against threshold (assuming 4-core system)
if (( $(echo "$load_1min > $THRESHOLD_LOAD" | bc -l) )); then
log_message "WARNING: High load average detected!"
# Show running processes
log_message "Running processes:"
ps aux | awk '$8 ~ /^R/ {print $0}' | while read line; do
log_message " $line"
done
# Show I/O wait
iowait=$(top -bn1 | grep "Cpu(s)" | awk '{print $10}' | sed 's/%wa,//')
log_message "I/O wait: ${iowait}%"
fi
}
check_context_switches() {
# Check context switches (high number indicates CPU thrashing)
context_switches=$(vmstat 1 2 | tail -1 | awk '{print $12}')
interrupts=$(vmstat 1 2 | tail -1 | awk '{print $11}')
log_message "Context switches/sec: $context_switches, Interrupts/sec: $interrupts"
if [ "$context_switches" -gt 10000 ]; then
log_message "WARNING: High context switch rate detected!"
fi
}
# Main execution
log_message "=== CPU Monitor Check ==="
check_cpu_usage
check_load_average
check_context_switches
log_message "CPU monitor check completed"
I/O Monitoring
Disk I/O Analysis
# I/O statistics
iostat # I/O statistics
iostat -x 1 5 # Extended stats, 1 sec interval, 5 times
iostat -d 2 # Disk stats every 2 seconds
# Per-process I/O
iotop # I/O usage by process (sudo required)
iotop -o # Show only processes doing I/O
iotop -a # Show accumulated I/O
# I/O wait analysis
vmstat 1 # Virtual memory stats with I/O wait
sar -d 1 5 # Disk activity
# File system I/O
df -h # Disk space usage
du -sh /* # Directory sizes
lsof | wc -l # Number of open files
Network Monitoring
# Network interface statistics
ip -s link # Interface statistics
cat /proc/net/dev # Network device stats
ifstat # Interface statistics (if available)
nload # Network load monitor
iftop # Network bandwidth usage by host
# Network connections
netstat -tulpn # Listening ports and connections
ss -tulpn # Modern netstat alternative
ss -s # Socket statistics summary
# Network traffic monitoring
tcpdump -i eth0 # Packet capture
nethogs # Network usage by process
Comprehensive System Monitoring
System Performance Dashboard
#!/bin/bash
# system-dashboard.sh - Comprehensive system monitoring dashboard
REFRESH_INTERVAL=5
LOG_FILE="/var/log/system-dashboard.log"
# Color codes for terminal output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
print_header() {
clear
echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE} SYSTEM PERFORMANCE DASHBOARD ${NC}"
echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE}Time: $(date)${NC}"
echo -e "${BLUE}Host: $(hostname)${NC}"
echo -e "${BLUE}Uptime: $(uptime | awk '{print $3,$4}' | sed 's/,//')${NC}"
echo
}
show_cpu_info() {
echo -e "${YELLOW}=== CPU Information ===${NC}"
# CPU usage
cpu_usage=$(top -bn1 | grep "Cpu(s)" | awk '{print $2}' | sed 's/%us,//')
cpu_idle=$(top -bn1 | grep "Cpu(s)" | awk '{print $8}' | sed 's/%id,//')
iowait=$(top -bn1 | grep "Cpu(s)" | awk '{print $10}' | sed 's/%wa,//')
printf "CPU Usage: %6.1f%% " "$cpu_usage"
if (( $(echo "$cpu_usage > 80" | bc -l) )); then
echo -e "${RED}HIGH${NC}"
elif (( $(echo "$cpu_usage > 50" | bc -l) )); then
echo -e "${YELLOW}MEDIUM${NC}"
else
echo -e "${GREEN}NORMAL${NC}"
fi
printf "CPU Idle: %6.1f%%\n" "$cpu_idle"
printf "I/O Wait: %6.1f%%\n" "$iowait"
# Load average
load_avg=$(uptime | awk '{print $(NF-2), $(NF-1), $NF}' | tr -d ',')
echo "Load Avg: $load_avg"
# Top CPU consumers
echo "Top CPU processes:"
ps aux --sort=-pcpu | head -6 | tail -5 | awk '{printf " %-20s %6.1f%%\n", $11, $3}'
echo
}
show_memory_info() {
echo -e "${YELLOW}=== Memory Information ===${NC}"
# Memory usage
memory_info=$(free -h | awk 'NR==2{printf "Used: %s/%s (%.1f%%), Available: %s", $3, $2, $3/$2*100, $7}')
echo "$memory_info"
# Memory usage percentage
memory_percent=$(free | awk 'NR==2{printf "%.1f", $3/$2*100}')
printf "Memory: %6.1f%% " "$memory_percent"
if (( $(echo "$memory_percent > 90" | bc -l) )); then
echo -e "${RED}HIGH${NC}"
elif (( $(echo "$memory_percent > 70" | bc -l) )); then
echo -e "${YELLOW}MEDIUM${NC}"
else
echo -e "${GREEN}NORMAL${NC}"
fi
# Swap usage
if [ -f /proc/swaps ] && [ "$(wc -l < /proc/swaps)" -gt 1 ]; then
swap_percent=$(free | awk 'NR==3{if($2>0) printf "%.1f", $3/$2*100; else print "0"}')
swap_used=$(free -h | awk 'NR==3{print $3}')
printf "Swap: %6.1f%% (Used: %s)\n" "$swap_percent" "$swap_used"
else
echo "Swap: Not configured"
fi
# Top memory consumers
echo "Top memory processes:"
ps aux --sort=-pmem | head -6 | tail -5 | awk '{printf " %-20s %6.1f%%\n", $11, $4}'
echo
}
show_disk_info() {
echo -e "${YELLOW}=== Disk Information ===${NC}"
# Disk usage
df -h | grep -vE '^Filesystem|tmpfs|udev|^/dev/loop' | while read filesystem size used avail percent mountpoint; do
percent_num=$(echo "$percent" | sed 's/%//')
printf "%-20s %8s " "$mountpoint" "$percent"
if [ "$percent_num" -gt 90 ]; then
echo -e "${RED}CRITICAL${NC}"
elif [ "$percent_num" -gt 80 ]; then
echo -e "${YELLOW}WARNING${NC}"
else
echo -e "${GREEN}OK${NC}"
fi
done
# I/O statistics
if command -v iostat >/dev/null 2>&1; then
echo "Recent I/O activity:"
iostat -d 1 2 | tail -n +4 | grep -E '^[a-z]' | head -3 | while read device tps read_per_sec write_per_sec read_total write_total; do
printf " %-10s %8s MB/s read, %8s MB/s write\n" "$device" "$read_per_sec" "$write_per_sec"
done
fi
echo
}
show_network_info() {
echo -e "${YELLOW}=== Network Information ===${NC}"
# Network interfaces
ip -s link | grep -E '^[0-9]+:|RX:|TX:' | paste - - - | while read line; do
interface=$(echo "$line" | awk '{print $2}' | sed 's/://')
rx_bytes=$(echo "$line" | awk '{print $3}')
tx_bytes=$(echo "$line" | awk '{print $10}')
if [ "$interface" != "lo" ] && [ -n "$rx_bytes" ] && [ -n "$tx_bytes" ]; then
rx_mb=$((rx_bytes / 1024 / 1024))
tx_mb=$((tx_bytes / 1024 / 1024))
printf " %-10s RX: %8d MB, TX: %8d MB\n" "$interface" "$rx_mb" "$tx_mb"
fi
done
# Active connections
connection_count=$(ss -tun | grep -c ESTAB)
echo "Active connections: $connection_count"
echo
}
show_system_info() {
echo -e "${YELLOW}=== System Status ===${NC}"
# System services
failed_services=$(systemctl --failed --no-legend | wc -l)
printf "Failed services: %d " "$failed_services"
if [ "$failed_services" -gt 0 ]; then
echo -e "${RED}ATTENTION NEEDED${NC}"
systemctl --failed --no-legend | head -3 | while read service rest; do
echo " $service"
done
else
echo -e "${GREEN}ALL OK${NC}"
fi
# Process count
process_count=$(ps aux | wc -l)
echo "Running processes: $process_count"
# Users logged in
user_count=$(who | wc -l)
echo "Logged in users: $user_count"
if [ "$user_count" -gt 0 ]; then
echo "Active users:"
who | awk '{print " " $1 " on " $2 " since " $3 " " $4}'
fi
echo
}
log_metrics() {
# Log key metrics for historical analysis
timestamp=$(date '+%Y-%m-%d %H:%M:%S')
cpu_usage=$(top -bn1 | grep "Cpu(s)" | awk '{print $2}' | sed 's/%us,//')
memory_usage=$(free | awk 'NR==2{printf "%.1f", $3/$2*100}')
load_1min=$(uptime | awk '{print $(NF-2)}' | sed 's/,//')
echo "$timestamp,CPU:$cpu_usage,MEM:$memory_usage,LOAD:$load_1min" >> "$LOG_FILE"
}
# Main dashboard loop
while true; do
print_header
show_cpu_info
show_memory_info
show_disk_info
show_network_info
show_system_info
log_metrics
echo -e "${BLUE}Press Ctrl+C to exit. Refreshing in $REFRESH_INTERVAL seconds...${NC}"
sleep "$REFRESH_INTERVAL"
done
Automated Performance Reports
#!/bin/bash
# performance-report.sh - Generate daily performance reports
REPORT_DIR="/var/log/performance-reports"
DATE=$(date +%Y%m%d)
REPORT_FILE="$REPORT_DIR/performance-report-$DATE.html"
mkdir -p "$REPORT_DIR"
generate_html_report() {
cat > "$REPORT_FILE" << EOF
<!DOCTYPE html>
<html>
<head>
<title>System Performance Report - $DATE</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
.header { background-color: #f0f0f0; padding: 10px; border-radius: 5px; }
.section { margin: 20px 0; padding: 15px; border: 1px solid #ddd; border-radius: 5px; }
.metric { display: inline-block; margin: 10px 20px 10px 0; }
.warning { color: #ff6600; font-weight: bold; }
.critical { color: #ff0000; font-weight: bold; }
.normal { color: #008000; }
table { border-collapse: collapse; width: 100%; }
th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
th { background-color: #f2f2f2; }
pre { background-color: #f8f8f8; padding: 10px; border-radius: 3px; overflow-x: auto; }
</style>
</head>
<body>
<div class="header">
<h1>System Performance Report</h1>
<p><strong>Date:</strong> $(date)</p>
<p><strong>Hostname:</strong> $(hostname)</p>
<p><strong>Uptime:</strong> $(uptime)</p>
</div>
EOF
# CPU Section
echo '<div class="section">' >> "$REPORT_FILE"
echo '<h2>CPU Performance</h2>' >> "$REPORT_FILE"
cpu_usage=$(top -bn1 | grep "Cpu(s)" | awk '{print $2}' | sed 's/%us,//')
load_avg=$(uptime | awk '{print $(NF-2), $(NF-1), $NF}' | tr -d ',')
echo "<div class='metric'>CPU Usage: <span class='normal'>${cpu_usage}%</span></div>" >> "$REPORT_FILE"
echo "<div class='metric'>Load Average: <span class='normal'>$load_avg</span></div>" >> "$REPORT_FILE"
echo '<h3>Top CPU Consumers</h3>' >> "$REPORT_FILE"
echo '<table>' >> "$REPORT_FILE"
echo '<tr><th>PID</th><th>User</th><th>CPU%</th><th>Command</th></tr>' >> "$REPORT_FILE"
ps aux --sort=-pcpu | head -11 | tail -10 | while read user pid cpu mem vsz rss tty stat start time cmd; do
echo "<tr><td>$pid</td><td>$user</td><td>$cpu%</td><td>$cmd</td></tr>" >> "$REPORT_FILE"
done
echo '</table>' >> "$REPORT_FILE"
echo '</div>' >> "$REPORT_FILE"
# Memory Section
echo '<div class="section">' >> "$REPORT_FILE"
echo '<h2>Memory Performance</h2>' >> "$REPORT_FILE"
memory_usage=$(free | awk 'NR==2{printf "%.1f", $3/$2*100}')
memory_total=$(free -h | awk 'NR==2{print $2}')
memory_used=$(free -h | awk 'NR==2{print $3}')
memory_available=$(free -h | awk 'NR==2{print $7}')
echo "<div class='metric'>Memory Usage: <span class='normal'>${memory_usage}%</span></div>" >> "$REPORT_FILE"
echo "<div class='metric'>Total: <span class='normal'>$memory_total</span></div>" >> "$REPORT_FILE"
echo "<div class='metric'>Used: <span class='normal'>$memory_used</span></div>" >> "$REPORT_FILE"
echo "<div class='metric'>Available: <span class='normal'>$memory_available</span></div>" >> "$REPORT_FILE"
echo '<h3>Top Memory Consumers</h3>' >> "$REPORT_FILE"
echo '<table>' >> "$REPORT_FILE"
echo '<tr><th>PID</th><th>User</th><th>MEM%</th><th>RSS</th><th>Command</th></tr>' >> "$REPORT_FILE"
ps aux --sort=-pmem | head -11 | tail -10 | while read user pid cpu mem vsz rss tty stat start time cmd; do
echo "<tr><td>$pid</td><td>$user</td><td>$mem%</td><td>$rss KB</td><td>$cmd</td></tr>" >> "$REPORT_FILE"
done
echo '</table>' >> "$REPORT_FILE"
echo '</div>' >> "$REPORT_FILE"
# Disk Section
echo '<div class="section">' >> "$REPORT_FILE"
echo '<h2>Disk Usage</h2>' >> "$REPORT_FILE"
echo '<table>' >> "$REPORT_FILE"
echo '<tr><th>Filesystem</th><th>Size</th><th>Used</th><th>Available</th><th>Use%</th><th>Mounted on</th></tr>' >> "$REPORT_FILE"
df -h | grep -vE '^Filesystem|tmpfs|udev' | while read filesystem size used avail percent mountpoint; do
echo "<tr><td>$filesystem</td><td>$size</td><td>$used</td><td>$avail</td><td>$percent</td><td>$mountpoint</td></tr>" >> "$REPORT_FILE"
done
echo '</table>' >> "$REPORT_FILE"
echo '</div>' >> "$REPORT_FILE"
# System Status
echo '<div class="section">' >> "$REPORT_FILE"
echo '<h2>System Status</h2>' >> "$REPORT_FILE"
failed_services=$(systemctl --failed --no-legend | wc -l)
echo "<div class='metric'>Failed Services: <span class='normal'>$failed_services</span></div>" >> "$REPORT_FILE"
if [ "$failed_services" -gt 0 ]; then
echo '<h3>Failed Services</h3>' >> "$REPORT_FILE"
echo '<pre>' >> "$REPORT_FILE"
systemctl --failed --no-legend >> "$REPORT_FILE"
echo '</pre>' >> "$REPORT_FILE"
fi
echo '</div>' >> "$REPORT_FILE"
echo '</body></html>' >> "$REPORT_FILE"
}
# Generate report
generate_html_report
echo "Performance report generated: $REPORT_FILE"
# Email report if mail is configured
if command -v mail >/dev/null 2>&1; then
{
echo "Daily performance report for $(hostname)"
echo "Report file: $REPORT_FILE"
echo ""
echo "Summary:"
echo "CPU Usage: $(top -bn1 | grep 'Cpu(s)' | awk '{print $2}' | sed 's/%us,//')"
echo "Memory Usage: $(free | awk 'NR==2{printf "%.1f%%", $3/$2*100}')"
echo "Load Average: $(uptime | awk '{print $(NF-2), $(NF-1), $NF}' | tr -d ',')"
} | mail -s "Daily Performance Report - $(hostname)" admin@company.com
fi
# Cleanup old reports (keep 30 days)
find "$REPORT_DIR" -name "performance-report-*.html" -mtime +30 -delete
Bu tutorial resource monitoring bo'yicha comprehensive guide beradi - basic process monitoring'dan tortib advanced system performance analysis va automated reporting bilan birga.