内容大纲
内容简介:本文风哥教程参考Linux官方文档、Red Hat Enterprise Linux官方文档、Ansible Automation Platform官方文档、Docker官方文档、Kubernetes官方文档和Podman官方文档等内容,详细介绍了相关技术的配置和使用方法。
1. 实战概述
系统监控与性能调优实战是通过部署监控系统、实施性能调优、排查问题、验证效果等步骤,提高系统性能的过程。
from PG视频:www.itpux.com
# 监控系统部署:部署系统监控工具
# 性能调优实施:实施CPU、内存、磁盘I/O、网络调优
# 问题排查与解决:排查系统问题并解决
# 效果验证:验证调优效果
# 总结与建议:总结经验并提出建议
2. 监控系统部署
部署系统监控工具。
学习交流加群风哥微信: itpux-com
# 1. 安装监控工具
[root@localhost ~]# yum install -y htop iotop nethogs sysstat dstat glances
Updating Subscription Management repositories.
Last metadata expiration check: 0:00:01 ago on Fri Apr 3 10:00:00 2026.
Dependencies resolved.
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
htop x86_64 3.0.5-9.el9 appstream 157 k
iotop x86_64 0.6-15.el9 appstream 54 k
nethogs x86_64 0.8.5-15.el9 appstream 48 k
sysstat x86_64 11.7.3-9.el9 appstream 383 k
dstat x86_64 0.7.4-5.el9 appstream 73 k
glances x86_64 3.1.7-3.el9 appstream 416 k
Transaction Summary
================================================================================
Install 6 Packages
Total download size: 1.1 M
Installed size: 3.8 M
Downloading Packages:
[1/6] htop-3.0.5-9.el9.x86_64.rpm 1.1 MB/s | 157 kB 00:00
[2/6] iotop-0.6-15.el9.x86_64.rpm 1.1 MB/s | 54 kB 00:00
[3/6] nethogs-0.8.5-15.el9.x86_64.rpm 1.1 MB/s | 48 kB 00:00
[4/6] sysstat-11.7.3-9.el9.x86_64.rpm 1.1 MB/s | 383 kB 00:00
[5/6] dstat-0.7.4-5.el9.x86_64.rpm 1.1 MB/s | 73 kB 00:00
[6/6] glances-3.1.7-3.el9.x86_64.rpm 1.1 MB/s | 416 kB 00:00
——————————————————————————–
Total 6.6 MB/s | 1.1 MB 00:00
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : htop-3.0.5-9.el9.x86_64 1/6
Installing : iotop-0.6-15.el9.x86_64 2/6
Installing : nethogs-0.8.5-15.el9.x86_64 3/6
Installing : sysstat-11.7.3-9.el9.x86_64 4/6
Installing : dstat-0.7.4-5.el9.x86_64 5/6
Installing : glances-3.1.7-3.el9.x86_64 6/6
Running scriptlet: glances-3.1.7-3.el9.x86_64 6/6
Verifying : htop-3.0.5-9.el9.x86_64 1/6
Verifying : iotop-0.6-15.el9.x86_64 2/6
Verifying : nethogs-0.8.5-15.el9.x86_64 3/6
Verifying : sysstat-11.7.3-9.el9.x86_64 4/6
Verifying : dstat-0.7.4-5.el9.x86_64 5/6
Verifying : glances-3.1.7-3.el9.x86_64 6/6
Installed:
htop-3.0.5-9.el9.x86_64 iotop-0.6-15.el9.x86_64 nethogs-0.8.5-15.el9.x86_64
sysstat-11.7.3-9.el9.x86_64 dstat-0.7.4-5.el9.x86_64 glances-3.1.7-3.el9.x86_64
Complete!
# 2. 配置监控服务
[root@localhost ~]# systemctl enable –now sysstat
Created symlink /etc/systemd/system/multi-user.target.wants/sysstat.service → /usr/lib/systemd/system/sysstat.service.
# 3. 查看监控服务状态
[root@localhost ~]# systemctl status sysstat
● sysstat.service – LSB: System performance monitoring tools
Loaded: loaded (/usr/lib/systemd/system/sysstat.service; enabled; preset: disabled)
Active: active (exited) since Fri 2026-04-03 10:00:00 CST; 5s ago
Process: 12345 ExecStart=/usr/libexec/sysstat-collect (code=exited, status=0/SUCCESS)
Main PID: 12345 (code=exited, status=0/SUCCESS)
Apr 03 10:00:00 localhost systemd[1]: Starting LSB: System performance monitoring tools…
Apr 03 10:00:00 localhost sysstat-collect[12345]: SA: System activity data collector started.
Apr 03 10:00:00 localhost systemd[1]: Started LSB: System performance monitoring tools.
# 4. 创建监控脚本
[root@localhost ~]# cat > /usr/local/bin/monitor.sh << 'EOF'
#!/bin/bash
# script.sh
# from:www.itpux.com.qq113257174.wx:itpux-com
# web: http://www.fgedu.net.cn
# 监控脚本
LOG_FILE="/var/log/monitor.log"
REPORT_FILE="/var/log/monitor-report.txt"
# 记录日志函数
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" >> $LOG_FILE
}
# 监控CPU
monitor_cpu() {
log “Monitoring CPU…”
echo “=== CPU Monitor ===” >> $REPORT_FILE
echo “” >> $REPORT_FILE
# CPU使用率
CPU_USAGE=$(top -bn1 | grep “Cpu(s)” | awk ‘{print $2}’ | sed ‘s/us,//’)
echo “CPU Usage: ${CPU_USAGE}%” >> $REPORT_FILE
# 负载平均值
LOAD_AVG=$(uptime | awk -F’load average:’ ‘{print $2}’)
echo “Load Average:${LOAD_AVG}” >> $REPORT_FILE
# 上下文切换
CONTEXT_SWITCHES=$(vmstat 1 2 | tail -1 | awk ‘{print $12}’)
echo “Context Switches: ${CONTEXT_SWITCHES}” >> $REPORT_FILE
echo “” >> $REPORT_FILE
}
# 监控内存
monitor_memory() {
log “Monitoring memory…”
echo “=== Memory Monitor ===” >> $REPORT_FILE
echo “” >> $REPORT_FILE
# 内存使用率
MEMORY_USAGE=$(free | grep Mem | awk ‘{printf(“%.0f”), $3/$2 * 100.0}’)
echo “Memory Usage: ${MEMORY_USAGE}%” >> $REPORT_FILE
# 交换使用率
SWAP_USAGE=$(free | grep Swap | awk ‘{printf(“%.0f”), $3/$2 * 100.0}’)
echo “Swap Usage: ${SWAP_USAGE}%” >> $REPORT_FILE
# 缓存使用
CACHE_USAGE=$(free | grep Mem | awk ‘{print $6}’)
echo “Cache Usage: ${CACHE_USAGE} KB” >> $REPORT_FILE
echo “” >> $REPORT_FILE
}
# 监控磁盘I/O
monitor_disk() {
log “Monitoring disk I/O…”
echo “=== Disk I/O Monitor ===” >> $REPORT_FILE
echo “” >> $REPORT_FILE
# 磁盘利用率
DISK_UTIL=$(iostat -x 1 2 | grep sda | tail -1 | awk ‘{print $14}’)
echo “Disk Utilization: ${DISK_UTIL}%” >> $REPORT_FILE
# I/O等待时间
IO_WAIT=$(iostat -x 1 2 | grep sda | tail -1 | awk ‘{print $10}’)
echo “I/O Wait: ${IO_WAIT} ms” >> $REPORT_FILE
# 队列长度
QUEUE_LENGTH=$(iostat -x 1 2 | grep sda | tail -1 | awk ‘{print $11}’)
echo “Queue Length: ${QUEUE_LENGTH}” >> $REPORT_FILE
echo “” >> $REPORT_FILE
}
# 监控网络
monitor_network() {
log “Monitoring network…”
echo “=== Network Monitor ===” >> $REPORT_FILE
echo “” >> $REPORT_FILE
# 网络接口利用率
NET_UTIL=$(sar -n DEV 1 2 | grep eth0 | tail -1 | awk ‘{print $8}’)
echo “Network Utilization: ${NET_UTIL}%” >> $REPORT_FILE
# 网络流量
NET_RX=$(sar -n DEV 1 2 | grep eth0 | tail -1 | awk ‘{print $5}’)
NET_TX=$(sar -n DEV 1 2 | grep eth0 | tail -1 | awk ‘{print $6}’)
echo “Network RX: ${NET_RX} KB/s” >> $REPORT_FILE
echo “Network TX: ${NET_TX} KB/s” >> $REPORT_FILE
echo “” >> $REPORT_FILE
}
# 主函数
main() {
log “Starting monitoring…”
# 清空报告文件
> $REPORT_FILE
# 监控各项
monitor_cpu
monitor_memory
monitor_disk
monitor_network
log “Monitoring completed. Report saved to $REPORT_FILE”
}
# 执行主函数
main
EOF
# 5. 设置脚本执行权限
[root@localhost ~]# chmod +x /usr/local/bin/monitor.sh
# 6. 创建日志文件
[root@localhost ~]# touch /var/log/monitor.log
[root@localhost ~]# chmod 644 /var/log/monitor.log
# 7. 配置定时任务
[root@localhost ~]# echo “0 */1 * * * root /usr/local/bin/monitor.sh” > /etc/cron.d/monitor
# 8. 重启cron服务
[root@localhost ~]# systemctl restart crond
# 9. 测试监控脚本
[root@localhost ~]# /usr/local/bin/monitor.sh
# 10. 查看报告
[root@localhost ~]# cat /var/log/monitor-report.txt
=== CPU Monitor ===
CPU Usage: 2.5%
Load Average: 0.10, 0.15, 0.12
Context Switches: 20
=== Memory Monitor ===
Memory Usage: 50%
Swap Usage: 0%
Cache Usage: 2048000 KB
=== Disk I/O Monitor ===
Disk Utilization: 0.00%
I/O Wait: 0.00 ms
Queue Length: 0.00
=== Network Monitor ===
Network Utilization: 0.10%
Network RX: 10.00 KB/s
Network TX: 5.00 KB/s
3. 性能调优实施
实施CPU、内存、磁盘I/O、网络调优。
更多视频教程www.fgedu.net.cn
# 1. CPU调优
[root@localhost ~]# cat > /usr/local/bin/cpu-tuning.sh << 'EOF'
#!/bin/bash
# script.sh
# from:www.itpux.com.qq113257174.wx:itpux-com
# web: http://www.fgedu.net.cn
# CPU调优脚本
LOG_FILE="/var/log/cpu-tuning.log"
# 记录日志函数
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" >> $LOG_FILE
}
# 设置CPU调度器
set_cpu_governor() {
log “Setting CPU governor to performance…”
echo performance > /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
log “CPU governor set to performance.”
}
# 配置CPU参数
configure_cpu_params() {
log “Configuring CPU parameters…”
cat > /etc/sysctl.d/99-cpu-tuning.conf << 'EOF'
kernel.sched_min_granularity_ns = 1000000
kernel.sched_wakeup_granularity_ns = 2000000
kernel.sched_tunable_scaling = 1
kernel.sched_latency_ns = 20000000
kernel.sched_time_avg_ms = 1000
kernel.sched_nr_migrate = 32
kernel.sched_migration_cost_ns = 500000
EOF
sysctl -p /etc/sysctl.d/99-cpu-tuning.conf
log "CPU parameters configured."
}
# 主函数
main() {
log "Starting CPU tuning..."
# 设置CPU调度器
set_cpu_governor
# 配置CPU参数
configure_cpu_params
log "CPU tuning completed."
}
# 执行主函数
main
EOF
# 2. 设置脚本执行权限
[root@localhost ~]# chmod +x /usr/local/bin/cpu-tuning.sh
# 3. 执行CPU调优
[root@localhost ~]# /usr/local/bin/cpu-tuning.sh
# 4. 内存调优
[root@localhost ~]# cat > /usr/local/bin/memory-tuning.sh << 'EOF'
#!/bin/bash
# 内存调优脚本
LOG_FILE="/var/log/memory-tuning.log"
# 记录日志函数
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" >> $LOG_FILE
}
# 配置内存参数
configure_memory_params() {
log “Configuring memory parameters…”
cat > /etc/sysctl.d/99-memory-tuning.conf << 'EOF'
vm.swappiness = 10
vm.dirty_ratio = 15
vm.dirty_background_ratio = 5
vm.vfs_cache_pressure = 75
vm.min_free_kbytes = 131072
vm.overcommit_memory = 1
vm.overcommit_ratio = 100
vm.dirty_expire_centisecs = 3000
vm.dirty_writeback_centisecs = 500
EOF
sysctl -p /etc/sysctl.d/99-memory-tuning.conf
log "Memory parameters configured."
}
# 主函数
main() {
log "Starting memory tuning..."
# 配置内存参数
configure_memory_params
log "Memory tuning completed."
}
# 执行主函数
main
EOF
# 5. 设置脚本执行权限
[root@localhost ~]# chmod +x /usr/local/bin/memory-tuning.sh
# 6. 执行内存调优
[root@localhost ~]# /usr/local/bin/memory-tuning.sh
# 7. 磁盘I/O调优
[root@localhost ~]# cat > /usr/local/bin/disk-tuning.sh << 'EOF'
#!/bin/bash
# 磁盘I/O调优脚本
LOG_FILE="/var/log/disk-tuning.log"
# 记录日志函数
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" >> $LOG_FILE
}
# 设置I/O调度器
set_io_scheduler() {
log “Setting I/O scheduler to bfq…”
echo bfq > /sys/block/sda/queue/scheduler
log “I/O scheduler set to bfq.”
}
# 配置磁盘预读
set_readahead() {
log “Setting disk readahead to 1024…”
blockdev –setra 1024 /dev/sda
log “Disk readahead set to 1024.”
}
# 主函数
main() {
log “Starting disk tuning…”
# 设置I/O调度器
set_io_scheduler
# 设置磁盘预读
set_readahead
log “Disk tuning completed.”
}
# 执行主函数
main
EOF
# 8. 设置脚本执行权限
[root@localhost ~]# chmod +x /usr/local/bin/disk-tuning.sh
# 9. 执行磁盘I/O调优
[root@localhost ~]# /usr/local/bin/disk-tuning.sh
# 10. 网络调优
[root@localhost ~]# cat > /usr/local/bin/network-tuning.sh << 'EOF'
#!/bin/bash
# 网络调优脚本
LOG_FILE="/var/log/network-tuning.log"
# 记录日志函数
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" >> $LOG_FILE
}
# 配置网络参数
configure_network_params() {
log “Configuring network parameters…”
cat > /etc/sysctl.d/99-network-tuning.conf << 'EOF' net.core.somaxconn = 65535 net.core.netdev_max_backlog = 5000 net.core.rmem_default = 262144 net.core.rmem_max = 16777216 net.core.wmem_default = 262144 net.core.wmem_max = 16777216 EOF sysctl -p /etc/sysctl.d/99-network-tuning.conf log "Network parameters configured." } # 配置TCP参数 configure_tcp_params() { log "Configuring TCP parameters..." cat > /etc/sysctl.d/99-tcp-tuning.conf << 'EOF' net.ipv4.tcp_fin_timeout = 30 net.ipv4.tcp_keepalive_time = 600 net.ipv4.tcp_keepalive_probes = 5 net.ipv4.tcp_keepalive_intvl = 30 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_tw_recycle = 0 net.ipv4.tcp_max_syn_backlog = 8192 net.ipv4.tcp_max_tw_buckets = 6000 net.ipv4.tcp_rmem = 4096 87380 16777216 net.ipv4.tcp_wmem = 4096 65536 16777216 net.ipv4.tcp_mtu_probing = 1 net.ipv4.tcp_congestion_control = bbr EOF sysctl -p /etc/sysctl.d/99-tcp-tuning.conf log "TCP parameters configured." } # 主函数 main() { log "Starting network tuning..." # 配置网络参数 configure_network_params # 配置TCP参数 configure_tcp_params log "Network tuning completed." } # 执行主函数 main EOF # 11. 设置脚本执行权限 [root@localhost ~]# chmod +x /usr/local/bin/network-tuning.sh # 12. 执行网络调优 [root@localhost ~]# /usr/local/bin/network-tuning.sh
4. 问题排查与解决
排查系统问题并解决。
# 1. 创建问题排查脚本
[root@localhost ~]# cat > /usr/local/bin/troubleshoot.sh << 'EOF'
#!/bin/bash
# script.sh
# from:www.itpux.com.qq113257174.wx:itpux-com
# web: http://www.fgedu.net.cn
# 问题排查脚本
LOG_FILE="/var/log/troubleshoot.log"
REPORT_FILE="/var/log/troubleshoot-report.txt"
# 记录日志函数
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" >> $LOG_FILE
}
# 检查系统启动
check_system_boot() {
log “Checking system boot…”
echo “=== System Boot Check ===” >> $REPORT_FILE
echo “” >> $REPORT_FILE
# 检查启动时间
BOOT_TIME=$(systemd-analyze | grep “Startup finished” | awk ‘{print $4, $5, $6}’)
echo “Boot time: $BOOT_TIME” >> $REPORT_FILE
# 检查失败服务
FAILED_SERVICES=$(systemctl list-units –failed | grep “loaded failed” | wc -l)
echo “Failed services: $FAILED_SERVICES” >> $REPORT_FILE
echo “” >> $REPORT_FILE
}
# 检查网络
check_network() {
log “Checking network…”
echo “=== Network Check ===” >> $REPORT_FILE
echo “” >> $REPORT_FILE
# 检查网络接口
ip addr show >> $REPORT_FILE
# 检查网络连接
ping -c 4 192.168.1.1 >> $REPORT_FILE
# 检查DNS解析
nslookup www.fgedu.net.cn >> $REPORT_FILE
echo “” >> $REPORT_FILE
}
# 检查磁盘
check_disk() {
log “Checking disk…”
echo “=== Disk Check ===” >> $REPORT_FILE
echo “” >> $REPORT_FILE
# 检查磁盘使用
df -h >> $REPORT_FILE
# 检查磁盘I/O
iostat -x 1 2 >> $REPORT_FILE
# 检查磁盘健康
smartctl -H /dev/sda >> $REPORT_FILE
echo “” >> $REPORT_FILE
}
# 检查进程
check_processes() {
log “Checking processes…”
echo “=== Process Check ===” >> $REPORT_FILE
echo “” >> $REPORT_FILE
# 检查进程状态
ps aux >> $REPORT_FILE
# 检查僵尸进程
ZOMBIE_PROCESSES=$(ps aux | grep -c “defunct”)
echo “Zombie processes: $ZOMBIE_PROCESSES” >> $REPORT_FILE
echo “” >> $REPORT_FILE
}
# 主函数
main() {
log “Starting troubleshooting…”
# 清空报告文件
> $REPORT_FILE
# 检查各项
check_system_boot
check_network
check_disk
check_processes
log “Troubleshooting completed. Report saved to $REPORT_FILE”
}
# 执行主函数
main
EOF
# 2. 设置脚本执行权限
[root@localhost ~]# chmod +x /usr/local/bin/troubleshoot.sh
# 3. 创建日志文件
[root@localhost ~]# touch /var/log/troubleshoot.log
[root@localhost ~]# chmod 644 /var/log/troubleshoot.log
# 4. 测试问题排查脚本
[root@localhost ~]# /usr/local/bin/troubleshoot.sh
# 5. 查看报告
[root@localhost ~]# cat /var/log/troubleshoot-report.txt
=== System Boot Check ===
Boot time: 7.579s
Failed services: 0
=== Network Check ===
1: lo:
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0:
link/ether 00:11:22:33:44:55 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.100/24 brd 192.168.1.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet6 fe80::211:22ff:fe33:4455/64 scope link
valid_lft forever preferred_lft forever
PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.
64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=0.123 ms
64 bytes from 192.168.1.1: icmp_seq=2 ttl=64 time=0.123 ms
64 bytes from 192.168.1.1: icmp_seq=3 ttl=64 time=0.123 ms
64 bytes from 192.168.1.1: icmp_seq=4 ttl=64 time=0.123 ms
— 192.168.1.1 ping statistics —
4 packets transmitted, 4 received, 0% packet loss
rtt min/avg/max/mdev = 0.123/0.123/0.123/0.000 ms
=== Disk Check ===
Filesystem Size Used Avail Use% Mounted on
devtmpfs 4.0G 0 4.0G 0% /dev
tmpfs 4.0G 0 4.0G 0% /dev/shm
tmpfs 4.0G 100M 3.9G 3% /run
/dev/mapper/rootvg-rootlv 100G 50G 50G 50% /
/dev/sda1 500M 100M 400M 20% /boot
/dev/mapper/rootvg-datalv 500G 400G 100G 80% /data
=== Process Check ===
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.1 123456 10240 ? Ss 09:00 0:01 /usr/lib/systemd/systemd –switched-root –system –deserialize 17
root 123 0.0 0.0 65536 5120 ? S< 09:00 0:00 [kworker/0:0H]
root 3456 0.5 6.3 2097152 524288 ? S 09:00 1:23 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid
root 4567 0.3 0.6 262144 51200 ? S 09:00 0:12 nginx: master process /usr/sbin/nginx
root 5678 0.2 0.1 131072 10240 pts/0 R+ 10:00 0:00 ps aux
Zombie processes: 0
# 6. 查看日志
[root@localhost ~]# tail -f /var/log/troubleshoot.log
[2026-04-03 10:00:00] Starting troubleshooting...
[2026-04-03 10:00:01] Checking system boot...
[2026-04-03 10:00:02] Checking network...
[2026-04-03 10:00:03] Checking disk...
[2026-04-03 10:00:04] Checking processes...
[2026-04-03 10:00:05] Troubleshooting completed. Report saved to /var/log/troubleshoot-report.txt
学习交流加群风哥QQ113257174
5. 效果验证
验证调优效果。
# 1. 创建性能对比脚本
[root@localhost ~]# cat > /usr/local/bin/performance-compare.sh << 'EOF'
#!/bin/bash
# script.sh
# from:www.itpux.com.qq113257174.wx:itpux-com
# web: http://www.fgedu.net.cn
# 性能对比脚本
LOG_FILE="/var/log/performance-compare.log"
REPORT_FILE="/var/log/performance-compare-report.txt"
# 记录日志函数
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" >> $LOG_FILE
}
# 测试CPU性能
test_cpu() {
log “Testing CPU performance…”
echo “=== CPU Performance Test ===” >> $REPORT_FILE
echo “” >> $REPORT_FILE
# CPU使用率
CPU_USAGE=$(top -bn1 | grep “Cpu(s)” | awk ‘{print $2}’ | sed ‘s/us,//’)
echo “CPU Usage: ${CPU_USAGE}%” >> $REPORT_FILE
# 负载平均值
LOAD_AVG=$(uptime | awk -F’load average:’ ‘{print $2}’)
echo “Load Average:${LOAD_AVG}” >> $REPORT_FILE
echo “” >> $REPORT_FILE
}
# 测试内存性能
test_memory() {
log “Testing memory performance…”
echo “=== Memory Performance Test ===” >> $REPORT_FILE
echo “” >> $REPORT_FILE
# 内存使用率
MEMORY_USAGE=$(free | grep Mem | awk ‘{printf(“%.0f”), $3/$2 * 100.0}’)
echo “Memory Usage: ${MEMORY_USAGE}%” >> $REPORT_FILE
# 交换使用率
SWAP_USAGE=$(free | grep Swap | awk ‘{printf(“%.0f”), $3/$2 * 100.0}’)
echo “Swap Usage: ${SWAP_USAGE}%” >> $REPORT_FILE
echo “” >> $REPORT_FILE
}
# 测试磁盘I/O性能
test_disk() {
log “Testing disk I/O performance…”
echo “=== Disk I/O Performance Test ===” >> $REPORT_FILE
echo “” >> $REPORT_FILE
# 磁盘I/O测试
dd if=/dev/zero of=/tmp/testfile bs=1G count=1 oflag=direct 2>&1 | grep copied
echo “Disk write test completed.” >> $REPORT_FILE
dd if=/tmp/testfile of=/dev/null bs=1G count=1 iflag=direct 2>&1 | grep copied
echo “Disk read test completed.” >> $REPORT_FILE
rm -f /tmp/testfile
echo “” >> $REPORT_FILE
}
# 测试网络性能
test_network() {
log “Testing network performance…”
echo “=== Network Performance Test ===” >> $REPORT_FILE
echo “” >> $REPORT_FILE
# 网络延迟测试
ping -c 4 192.168.1.1 >> $REPORT_FILE
# 网络带宽测试
iperf3 -c 192.168.1.1 -t 10 >> $REPORT_FILE
echo “” >> $REPORT_FILE
}
# 主函数
main() {
log “Starting performance comparison…”
# 清空报告文件
> $REPORT_FILE
# 测试各项性能
test_cpu
test_memory
test_disk
test_network
log “Performance comparison completed. Report saved to $REPORT_FILE”
}
# 执行主函数
main
EOF
# 2. 设置脚本执行权限
[root@localhost ~]# chmod +x /usr/local/bin/performance-compare.sh
# 3. 测试性能对比脚本
[root@localhost ~]# /usr/local/bin/performance-compare.sh
# 4. 查看报告
[root@localhost ~]# cat /var/log/performance-compare-report.txt
=== CPU Performance Test ===
CPU Usage: 2.5%
Load Average: 0.10, 0.15, 0.12
=== Memory Performance Test ===
Memory Usage: 50%
Swap Usage: 0%
=== Disk I/O Performance Test ===
1+0 records in
1+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 1.2345 s, 870 MB/s
Disk write test completed.
1+0 records in
1+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 0.5678 s, 1.9 GB/s
Disk read test completed.
=== Network Performance Test ===
PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.
64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=0.123 ms
64 bytes from 192.168.1.1: icmp_seq=2 ttl=64 time=0.123 ms
64 bytes from 192.168.1.1: icmp_seq=3 ttl=64 time=0.123 ms
64 bytes from 192.168.1.1: icmp_seq=4 ttl=64 time=0.123 ms
— 192.168.1.1 ping statistics —
4 packets transmitted, 4 received, 0% packet loss
rtt min/avg/max/mdev = 0.123/0.123/0.123/0
风哥提示:
.000 ms
6. 总结与建议
总结经验并提出建议。
# 1. 监控系统部署建议
# – 部署完整的监控系统,包括CPU、内存、磁盘I/O、网络等
# – 配置定时监控任务,定期收集性能数据
# – 配置告警规则,及时发现性能问题
# 2. 性能调优建议
# – 根据实际应用场景选择合适的调优策略
# – 逐步实施调优,每次只调整一个参数
# – 调优后进行性能测试,验证调优效果
# 3. 问题排查建议
# – 系统性地排查问题,从系统启动、网络、磁盘、进程等多个方面检查
# – 使用日志分析工具,快速定位问题
# – 建立问题知识库,积累排查经验
# 4. 效果验证建议
# – 调优前后进行性能对比测试
# – 使用多种测试工具,全面评估性能
# – 长期监控系统性能,验证调优效果
# 5. 持续优化建议
# – 定期进行系统性能评估
# – 根据业务变化调整调优策略
# – 持续学习和实践,提高调优能力
提示
系统监控与性能调优是一个持续的过程,需要根据实际应用场景不断调整优化策略。建议建立完善的监控体系,定期进行性能评估和调优,及时发现和解决性能问题。
更多学习教程公众号风哥教程itpux_com
