1. 首页 > Linux教程 > 正文

Linux教程FG165-sar命令系统监控

内容大纲

内容简介:本文风哥教程参考Linux官方文档、Red Hat Enterprise Linux官方文档、Ansible Automation Platform官方文档、Docker官方文档、Kubernetes官方文档和Podman官方文档等内容,详细介绍了相关技术的配置和使用方法。

1. sar命令概述

sar命令是Linux系统中的系统活动报告工具,它可以收集、报告和保存系统活动信息,包括CPU、内存、I/O、网络等。

from PG视频:www.itpux.com

# sar命令的主要功能
# CPU统计:用户、系统、等待、空闲时间
# 内存统计:内存使用、缓冲区、缓存
# I/O统计:磁盘读写速率、IOPS
# 网络统计:网络流量、错误率
# 进程统计:进程创建、上下文切换
# 历史数据:保存历史数据供分析

2. sar命令基本使用

使用sar命令查看系统活动信息。

学习交流加群风哥微信: itpux-com

# sar命令基本使用

# 1. 查看CPU统计信息
[root@localhost ~]# sar -u
Linux 5.14.0-362.el9.x86_64 (localhost.localdomain) 04/03/2026 _x86_64_ (4 CPU)

10:00:00 AM CPU %user %nice %system %iowait %steal %idle
10:00:01 AM all 2.50 0.00 1.00 0.50 0.00 96.00
10:00:02 AM all 3.00 0.00 1.50 0.00 0.00 95.50
10:00:03 AM all 2.00 0.00 1.00 0.00 0.00 97.00
Average: all 2.50 0.00 1.17 0.17 0.00 96.17

# 2. 查看内存统计信息
[root@localhost ~]# sar -r
Linux 5.14.0-362.el9.x86_64 (localhost.localdomain) 04/03/2026 _x86_64_ (4 CPU)

10:00:00 AM kbmemfree kbmemused %memused kbbuffers kbcached kbcommit %commit kbactive kbinact kbdirty
10:00:01 AM 4096000 4096000 50.00 102400 2048000 512000 6.25 2048000 2048000 0
10:00:02 AM 4096000 4096000 50.00 102400 2048000 512000 6.25 2048000 2048000 0
10:00:03 AM 4096000 4096000 50.00 102400 2048000 512000 6.25 2048000 2048000 0
Average: 4096000 4096000 50.00 102400 2048000 512000 6.25 2048000 2048000 0

# 3. 查看I/O统计信息
[root@localhost ~]# sar -b
Linux 5.14.0-362.el9.x86_64 (localhost.localdomain) 04/03/2026 _x86_64_ (4 CPU)

10:00:00 AM tps rtps wtps bread/s bwrtn/s
10:00:01 AM 1.00 0.50 0.50 10.00 5.00
10:00:02 AM 0.00 0.00 0.00 0.00 0.00
10:00:03 AM 0.00 0.00 0.00 0.00 0.00
Average: 0.33 0.17 0.17 3.33 1.67

# 4. 查看网络统计信息
[root@localhost ~]# sar -n DEV
Linux 5.14.0-362.el9.x86_64 (localhost.localdomain) 04/03/2026 _x86_64_ (4 CPU)

10:00:00 AM IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s %ifutil
10:00:01 AM eth0 100.00 50.00 10.00 5.00 0.00 0.00 0.00 0.10
10:00:02 AM eth0 100.00 50.00 10.00 5.00 0.00 0.00 0.00 0.10
10:00:03 AM eth0 100.00 50.00 10.00 5.00 0.00 0.00 0.00 0.10
Average: eth0 100.00 50.00 10.00 5.00 0.00 0.00 0.00 0.10

# 5. 查看进程统计信息
[root@localhost ~]# sar -w
Linux 5.14.0-362.el9.x86_64 (localhost.localdomain) 04/03/2026 _x86_64_ (4 CPU)

10:00:00 AM proc/s cswch/s
10:00:01 AM 1.00 100.00
10:00:02 AM 0.00 50.00
10:00:03 AM 0.00 50.00
Average: 0.33 66.67

3. sar命令参数详解

学习交流加群风哥QQ113257174

详解sar命令的各个参数。

# sar命令参数详解

# 1. CPU统计参数
# -u:显示CPU使用率
# -P:显示每个CPU的使用率
# -I:显示中断统计

# 2. 内存统计参数
# -r:显示内存使用情况
# -R:显示内存页统计

# 3. I/O统计参数
# -b:显示I/O传输速率
# -d:显示磁盘统计
# -p:显示块设备统计

# 4. 网络统计参数
# -n DEV:显示网络接口统计
# -n EDEV:显示网络错误统计
# -n TCP:显示TCP统计
# -n UDP:显示UDP统计

# 5. 进程统计参数
# -w:显示进程创建和上下文切换
# -q:显示运行队列和负载

# 6. 其他参数
# -o:将数据保存到文件
# -f:从文件读取数据
# -s:指定开始时间
# -e:指定结束时间

# 查看详细参数说明
[root@localhost ~]# sar –help
Usage: sar [ options ] [ [ ] ]
Main options and reports:
-B Paging statistics
-b I/O and transfer rate statistics
-d Block device statistics
-F Custom filesystem statistics
-H Hugepages utilization statistics
-I { SUM | CPU | SCPU | ALL | XALL }
Interrupts statistics
-m Power management statistics
-n { DEV | EDEV | NFS | NFSD | SOCK | IP | EIP | ICMP | EICMP | TCP | ETCP | UDP | SOFT | ALL }
Network statistics
-q Queue length and load average statistics
-r Memory utilization statistics
-R Memory statistics
-S Swap space utilization statistics
-u [ ALL ]
CPU utilization statistics
-v Kernel tables statistics
-W Swapping statistics
-w Task creation and system switching statistics
-y TTY devices statistics

4. sar命令数据收集

使用sar命令收集和保存系统活动数据。

# sar命令数据收集

# 1. 启动sar数据收集服务
[root@localhost ~]# systemctl enable –now sysstat
Created symlink /etc/systemd/system/sysstat.service → /usr/lib/systemd/system/sysstat.service.
Created symlink /etc/systemd/system/sysstat.timer → /usr/lib/systemd/system/sysstat.timer.

# 2. 配置sar数据收集
[root@localhost ~]# cat /etc/sysconfig/sysstat
# sysstat-12.5.2 configuration file.

# How long to keep log files (in days).
# If value is greater than 28, then log files are kept in
# multiple directories, one for each month.
HISTORY=7

# Compress (using xz) sa files after they have been moved?
# This is set to “false” by default because compression can
# make sar slower, especially if the CPU is slow.
COMPRESSAFTER=31

# Parameters for the system activity data collector (see sadc manual page)
# which are used for the generation of log files.
SADC_OPTIONS=”-S DISK”

# 3. 手动收集数据
[root@localhost ~]# /usr/lib64/sa/sadc 1 10 /tmp/sa.data

# 4. 查看收集的数据
[root@localhost ~]# sar -f /tmp/sa.data
Linux 5.14.0-362.el9.x86_64 (localhost.localdomain) 04/03/2026 _x86_64_ (4 CPU)

10:00:00 AM CPU %user %nice %system %iowait %steal %idle
10:00:01 AM all 2.50 0.00 1.00 0.50 0.00 96.00
10:00:02 AM all 3.00 0.00 1.50 0.00 0.00 95.50
10:00:03 AM all 2.00 0.00 1.00 0.00 0.00 97.00
Average: all 2.50 0.00 1.17 0.17 0.00 96.17

# 5. 查看历史数据
[root@localhost ~]# sar -f /var/log/sa/sa03
Linux 5.14.0-362.el9.x86_64 (localhost.localdomain) 04/03/2026 _x86_64_ (4 CPU)

10:00:00 AM CPU %user %nice %system %iowait %steal %idle
10:00:01 AM all 2.50 0.00 1.00 0.50 0.00 96.00
10:00:02 AM all 3.00 0.00 1.50 0.00 0.00 95.50
10:00:03 AM all 2.00 0.00 1.00 0.00 0.00 97.00
Average: all 2.50 0.00 1.17 0.17 0.00 96.17

# 6. 查看特定时间范围的数据
[root@localhost ~]# sar -s 10:00:00 -e 10:00:05 -f /var/log/sa/sa03
Linux 5.14.0-362.el9.x86_64 (localhost.localdomain) 04/03/2026 _x86_64_ (4 CPU)

10:00:00 AM CPU %user %nice %system %iowait %steal %idle
10:00:01 AM all 2.50 0.00 1.00 0.50 0.00 96.00
10:00:02 AM all 3.00 0.00 1.50 0.00 0.00 95.50
10:00:03 AM all 2.00 0.00 1.00 0.00 0.00 97.00
Average: all 2.50 0.00 1.17 0.17 0.00 96.17

更多学习教程公众号风哥教程itpux_com

5. 实战案例

使用sar命令监控系统性能。

# 实战案例:使用sar命令监控系统性能

# 1. 查看CPU统计信息
[root@localhost ~]# sar -u 1 3
Linux 5.14.0-362.el9.x86_64 (localhost.localdomain) 04/03/2026 _x86_64_ (4 CPU)

10:00:00 AM CPU %user %nice %system %iowait %steal %idle
10:00:01 AM all 2.50 0.00 1.00 0.50 0.00 96.00
10:00:02 AM all 3.00 0.00 1.50 0.00 0.00 95.50
10:00:03 AM all 2.00 0.00 1.00 0.00 0.00 97.00
Average: all 2.50 0.00 1.17 0.17 0.00 96.17

# 2. 查看内存统计信息
[root@localhost ~]# sar -r 1 3
Linux 5.14.0-362.el9.x86_64 (localhost.localdomain) 04/03/2026 _x86_64_ (4 CPU)

10:00:00 AM kbmemfree kbmemused %memused kbbuffers kbcached kbcommit %commit kbactive kbinact kbdirty
10:00:01 AM 4096000 4096000 50.00 102400 2048000 512000 6.25 2048000 2048000 0
10:00:02 AM 4096000 4096000 50.00 102400 2048000 512000 6.25 2048000 2048000 0
10:00:03 AM 4096000 4096000 50.00 102400 2048000 512000 6.25 2048000 2048000 0
Average: 4096000 4096000 50.00 102400 2048000 512000 6.25 2048000 2048000 0

# 3. 创建监控脚本
[root@localhost ~]# cat > /usr/local/bin/sar-monitor.sh << 'EOF' #!/bin/bash # script.sh # from:www.itpux.com.qq113257174.wx:itpux-com # web: http://www.fgedu.net.cn # sar监控脚本 LOG_FILE="/var/log/sar-monitor.log" ALERT_EMAIL="admin@fgedu.net.cn" # 记录日志函数 log() { echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" >> $LOG_FILE
}

# 检查CPU使用率
check_cpu() {
CPU_USAGE=$(sar -u 1 2 | tail -1 | awk ‘{print $100}’)
CPU_IDLE=$(sar -u 1 2 | tail -1 | awk ‘{print $8}’)
CPU_USAGE=$(echo “100 – $CPU_IDLE” | bc)

if [ $(echo “$CPU_USAGE > 80” | bc) -eq 1 ]; then
log “WARNING: CPU usage is ${CPU_USAGE}%”
echo “CPU usage is ${CPU_USAGE}%” | mail -s “WARNING: CPU usage alert” $ALERT_EMAIL
fi
}

# 检查内存使用率
check_memory() {
MEMORY_USAGE=$(sar -r 1 2 | tail -1 | awk ‘{print $3}’)

if [ $(echo “$MEMORY_USAGE > 80” | bc) -eq 1 ]; then
log “WARNING: Memory usage is ${MEMORY_USAGE}%”
echo “Memory usage is ${MEMORY_USAGE}%” | mail -s “WARNING: Memory usage alert” $ALERT_EMAIL
fi
}

# 检查I/O等待
check_io_wait() {
IO_WAIT=$(sar -u 1 2 | tail -1 | awk ‘{print $5}’)

if [ $(echo “$IO_WAIT > 20” | bc) -eq 1 ]; then
log “WARNING: I/O wait is ${IO_WAIT}%”
echo “I/O wait is ${IO_WAIT}%” | mail -s “WARNING: I/O wait alert” $ALERT_EMAIL
fi
}

# 主函数
main() {
log “Starting sar monitoring…”
check_cpu
check_memory
check_io_wait
log “Sar monitoring completed.”
}

# 执行主函数
main
EOF

# 4. 设置脚本执行权限
[root@localhost ~]# chmod +x /usr/local/bin/sar-monitor.sh

# 5. 创建日志文件
[root@localhost ~]# touch /var/log/sar-monitor.log
[root@localhost ~]# chmod 644 /var/log/sar-monitor.log

# 6. 配置定时任务
[root@localhost ~]# echo “*/5 * * * * root /usr/local/bin/sar-monitor.sh” > /etc/cron.d/sar-monitor

# 7. 重启cron服务
[root@localhost ~]# systemctl restart crond

# 8. 测试监控脚本
[root@localhost ~]# /usr/local/bin/sar-monitor.sh

# 9. 查看日志
[root@localhost ~]# tail -f /var/log/sar-monitor.log
[2026-04-03 10:00:00] Starting sar monitoring…
[2026-04-03 10:00:05] Sar monitoring completed.

# 10. 配置日志轮转
[root@localhost ~]# cat > /etc/logrotate.d/sar-monitor << 'EOF' /var/log/sar-monitor.log { daily rotate 7 compress delaycompress missingok notifempty create 0644 root root } EOF # 11. 测试日志轮转 [root@localhost ~]# logrotate -f /etc/logrotate.d/sar-monitor

联系我们

在线咨询:点击这里给我发消息

微信号:itpux-com

工作日:9:30-18:30,节假日休息