12.监控系统负载(从零开始学)

发布时间:2026/7/22 13:50:37
12.监控系统负载(从零开始学) 在开启一天的学习的时候先做好快照过程中如果出现意外的报错解决不了的就立即恢复快照作为初学者省时省力不要过于纠结哪里错了浪费时间1.系统负载介绍系统负载平均值Linux内核以活动请求数的指数移动平均值来表示。活动请求数不仅包含运行中进程还包含等待IO的进程对应于R和D。等待IO包括处于睡眠等待预期磁盘和网络响应的任务。指数移动平均值是一个数学公式可以平滑趋势数据的高值和低值更加准确地表示一段时间内系统负载并确定系统负载是随着时间增加还是减少。根据所有CPU活动请求数每5秒计算一次Load Average。通过汇总这些值可以得到最近1分钟5分钟和15分钟内的指数移动平均值。一些UNIX系统仅考虑CPU使用率或运行队列长度。Linux中负载平均值中还包含了对IO的考量遇到负载平均值很高但CPU活动很低时检查磁盘和网络活动。Linux将各个物理CPU核心和微处理器超线程计为独立执行单元。每个独立的执行单元拥有独立的请求队列。2.查看系统负载# 查看CPU [fengkaicentos7 ~ 19:52:30]$ lscpu Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Byte Order: Little Endian CPU(s): 2 On-line CPU(s) list: 0,1 Thread(s) per core: 1 Core(s) per socket: 1 座 2 NUMA 节点 1 厂商 ID GenuineIntel CPU 系列 6 型号 158 型号名称 Intel(R) Core(TM) i7-8750H CPU 2.20GHz 步进 10 CPU MHz 2208.006 BogoMIPS 4416.01 超管理器厂商 VMware 虚拟化类型 完全 L1d 缓存 32K L1i 缓存 32K L2 缓存 256K L3 缓存 9216K NUMA 节点0 CPU 0,1 Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon nopl xtopology tsc_reliable nonstop_tsc eagerfpu pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch invpcid_single ssbd ibrs ibpb stibp fsgsbase tsc_adjust bmi1 avx2 smep bmi2 invpcid rdseed adx smap clflushopt xsaveopt xsavec xgetbv1 arat md_clear spec_ctrl intel_stibp flush_l1d arch_capabilities # 查看负载 [fengkaicentos7 ~ 19:56:17]$ uptime 19:56:37 up 3:02, 2 users, load average: 0.02, 0.24, 0.21 # 给系统加负载 [fengkaicentos7 ~ 19:56:37]$ md5sum /dev/zero [1] 7683 [fengkaicentos7 ~ 19:56:55]$ md5sum /dev/zero [2] 768 # 等30秒左右 [fengkaicentos7 ~ 19:57:41]$ uptime 19:57:43 up 3:04, 2 users, load average: 1.06, 0.48, 0.293.负载解读示例4核心的CPU负载为 2.92 4.48 5.20每个cpu负载为0.73(2.92/4) 1.12(4.48/4) 1.30(5.20/4)比较理想的值为 75% 左右。4.top 命令作用动态查看进程信息包括不同状态任务数量CPU消耗和内存消耗。top命令快捷键常用的命令数字1PMkqh。5.stress 工具Linux 中的stress工具用于对系统进行压力测试可模拟 CPU、内存、I/O 和磁盘等资源的高负载状态。通过指定参数如-c压 CPU、-m压内存可创建负载帮助发现系统在压力下的稳定性问题常用于性能调优或硬件验证。# 安装 [rootcentos7 ~ 10:09:41]# yum install -y stress # 帮助信息 [rootcentos7 ~ 10:30:12]# stress --help stress imposes certain types of compute stress on your system Usage: stress [OPTION [ARG]] ... -?, --help show this help statement --version show version statement -v, --verbose be verbose -q, --quiet be quiet -n, --dry-run show what would have been done -t, --timeout N timeout after N seconds --backoff N wait factor of N microseconds before work starts -c, --cpu N spawn N workers spinning on sqrt() -i, --io N spawn N workers spinning on sync() -m, --vm N spawn N workers spinning on malloc()/free() --vm-bytes B malloc B bytes per vm worker (default is 256MB) --vm-stride B touch a byte every B bytes (default is 4096) --vm-hang N sleep N secs before free (default none, 0 is inf) --vm-keep redirty memory instead of freeing and reallocating -d, --hdd N spawn N workers spinning on write()/unlink() --hdd-bytes B write B bytes per hdd worker (default is 1GB) Example: stress --cpu 8 --io 4 --vm 2 --vm-bytes 128M --timeout 10s Note: Numbers may be suffixed with s,m,h,d,y (time) or B,K,M,G (size).5.1 压力测试-CPU# 消耗2个CPU [fengkaicentos7 ~ 10:17:37]$ stress -c 2 stress: info: [3997] dispatching hogs: 2 cpu, 0 io, 0 vm, 0 hdd # top 监控 [rootcentos7 ~ 10:31:30]# top top - 10:33:26 up 2:02, 2 users, load average: 1.32, 0.43, 0.32 Tasks: 191 total, 3 running, 185 sleeping, 3 stopped, 0 zombie %Cpu0 : 99.7 us, 0.0 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.3 si, 0.0 st %Cpu1 : 99.7 us, 0.0 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.3 si, 0.0 st KiB Mem : 3861256 total, 2706760 free, 530116 used, 624380 buff/cache KiB Swap: 4063228 total, 4063228 free, 0 used. 3073644 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME COMMAND 3999 fengkai 20 0 7312 100 0 R 100.0 0.0 1:01.31 stress 3998 fengkai 20 0 7312 100 0 R 99.7 0.0 1:01.40 stress 747 root 20 0 295564 5292 4044 S 0.3 0.1 0:27.17 vmtoolsd 3332 fengkai 20 0 161072 2536 1168 S 0.3 0.1 0:00.34 sshd 4008 root 20 0 162104 2340 1588 R 0.3 0.1 0:00.08 top ......5.2 压力测试-内存# 消耗前内存 [rootcentos7 ~ 10:34:57]# free -m total used free shared buff/cache available Mem: 3770 516 2643 20 610 3002 Swap: 3967 0 3967 # 消耗 1G 内存 [rootcentos7 ~ 14:19:11]# stress -m 1 --vm-bytes 1G stress: info: [2623] dispatching hogs: 0 cpu, 0 io, 1 vm, 0 hdd # 消耗后内存 [rootcentos7 ~ 10:36:07]# free -m total used free shared buff/cache available Mem: 3770 1542 1618 20 610 1976 Swap: 3967 0 39675.3压力测试-磁盘# 消耗磁盘IO [rootcentos7 ~]# stress -d 1 --hdd-bytes 2G # 监视活动状态百分比%util [rootcentos7 ~]# yum install -y sysstat iotop # 监控磁盘读写速度重点关注rd_sec/s和wr_sec/s单位是0.5k/每秒,每个secsector是512Byte。 [rootcentos7 ~ 10:39:09]# sar -dp 1 Linux 3.10.0-1160.71.1.el7.x86_64 (centos7.cloud) 2026年07月22日 _x86_64_ (2 CPU) 10时42分06秒 DEV tps rd_sec/s wr_sec/s avgrq-sz avgqu-sz await svctm %util 10时42分07秒 sda 630.00 0.00 645120.00 1024.00 142.79 228.48 1.59 99.90 10时42分07秒 sr0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 10时42分07秒 centos-root 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 10时42分07秒 centos-swap 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 10时42分07秒 centos-home 640.00 0.00 655360.00 1024.00 143.74 226.10 1.56 99.90 ...... # 监控进程读写左右方向键调整排序列 代表当前排序列 [rootcentos7 ~ 10:47:01]# iotop Total DISK READ : 0.00 B/s | Total DISK WRITE : 1365.29 M/s Actual DISK READ: 0.00 B/s | Actual DISK WRITE: 1338.78 M/s TID PRIO USER DISK READ DISK WRITE SWAPIN IO COMMAND 4166 be/4 fengkai 0.00 B/s 1365.29 M/s 0.00 % 6.02 % stress -d ~dd-bytes 2G 2048 be/4 gdm 0.00 B/s 0.00 B/s 0.00 % 0.00 % gsd-print-~tifications 1 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % systemd --~erialize 22 2 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [kthreadd] 2051 be/4 gdm 0.00 B/s 0.00 B/s 0.00 % 0.00 % gsd-print-~ons [gdbus] 4 be/0 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [kworker/0:0H] 2053 be/4 gdm 0.00 B/s 0.00 B/s 0.00 % 0.00 % gsd-screensaver-proxy 6 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [ksoftirqd/0] ...6.网络测试# 传送一个大size的文件 [rootcentos7 ~ 14:27:17]# wget http://192.168.50.200/course-materials/iso/CentOS-7-x86_64-DVD-2207-02.iso # 监控带宽 [rootcentos7 ~ 10:49:14]# sar -n DEV 1 ...... Linux 3.10.0-1160.71.1.el7.x86_64 (centos7.cloud) 2026年07月22日 _x86_64_ (2 CPU) 10时49分20秒 IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s 10时49分21秒 lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00 10时49分21秒 virbr0-nic 0.00 0.00 0.00 0.00 0.00 0.00 0.00 10时49分21秒 virbr0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 10时49分21秒 ens33 68990.00 1673.00 101929.47 98.72 0.00 0.00 0.00 10时49分21秒 IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s 10时49分22秒 lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00 10时49分22秒 virbr0-nic 0.00 0.00 0.00 0.00 0.00 0.00 0.00 10时49分22秒 virbr0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 10时49分22秒 ens33 59569.00 1443.00 88063.86 85.63 0.00 0.00 0.00 ......7.监控总结以下是针对 Linux 系统监控的几条实用建议涵盖关键监控维度和最佳实践核心指标实时监控重点跟踪 CPU 使用率用户态 / 系统态占比、内存占用含缓存 /swap 使用率、磁盘 I/O读写吞吐量、IOPS和网络流量带宽利用率、连接数。可通过top/htop实时、vmstat/iostat系统级等工具快速查看。部署专业监控工具对于服务器集群或长期监控需求建议使用 Prometheus Grafana可视化强、Zabbix全功能监控或 Nagios轻量告警等工具实现指标集中收集、趋势分析和历史数据查询。设置关键阈值告警针对核心指标配置告警阈值如 CPU 持续 5 分钟超 80%、磁盘空间不足 10%通过邮件、短信或即时通讯工具推送告警避免故障扩大。注意避免告警风暴可设置告警合并或延迟。监控进程与服务状态定期检查关键服务如 Nginx、MySQL的运行状态、进程数及资源消耗可通过systemctl status或自定义脚本实现。对异常退出的进程结合日志排查崩溃原因。日志集中管理与分析将系统日志/var/log/messages、应用日志集中存储如使用 ELK 栈关注错误信息ERROR级别、登录异常/var/log/auth.log和磁盘错误dmesg | grep error必要时配置日志告警规则。磁盘健康监控除空间使用率外需关注磁盘坏块smartctl工具检测 S.M.A.R.T 信息和文件系统完整性定期运行fsck非挂载状态下避免硬件故障导致数据丢失。网络与安全监控监控端口开放状态netstat/ss、异常连接尤其是外部 IP 的高频访问和防火墙规则生效情况。可结合tcpdump抓包分析可疑流量。定期性能基线分析记录系统正常运行时的指标基线如平均负载、内存使用峰值当指标偏离基线时及时排查避免微小异常累积成故障。自动化监控脚本对个性化需求如特定目录文件数、应用响应时间编写 Shell 或 Python 脚本定期执行检查输出结果至监控系统或直接触发告警。监控权限与安全限制监控工具的权限如仅授予读取日志和指标的权限加密传输监控数据防止监控系统本身成为安全薄弱点。通过分层监控基础指标→服务状态→业务性能和主动预警可显著提升系统稳定性和故障响应效率。