前言:
Linux中的sar命令,全称是(System Activity Reporter系统活动情况报告),它是Linux下的系统运行状态监控工具,可以从多个方面对系统活动进行报告。
包括:网络收发包情况、文件读写情况、磁盘I/O、内存使用情况等。
堪称全能小王子,下面我们一起来看下sar命令的使用。
1.sar命令安装笔者使用的是Ubuntu机器,安装比较简单
root@91230cc467cc:/usr/local/tomcat# apt-get install sar
在使用sar命令时经常会报错,如下所示:
root@93de58bae514:/proc/net# sar
Cannot open /var/log/sysstat/sa04: No such file or directory
Please check if data collecting is enabled
这时的处理方案如下:
# 1.修改/etc/default/sysstat权限
root@91230cc467cc:/usr/local/tomcat# chmod u+x /etc/default/sysstat
# 2.修改/etc/default/sysstat内容,将ENABLED设置为true(原为false)
root@91230cc467cc:/usr/local/tomcat# vim /etc/default/sysstat
# Default settings for /etc/init.d/sysstat, /etc/cron.d/sysstat
# and /etc/cron.daily/sysstat files
#
# Should sadc collect system activity informations? Valid values
# are "true" and "false". Please do not put other values, they
# will be overwritten by debconf!
ENABLED="true"
# 3.重启sysstat
root@91230cc467cc:/usr/local/tomcat# /etc/init.d/sysstat restart
2.sar命令的那些参数
使用sar -help来展示选项信息
root@91230cc467cc:/usr/local/tomcat# sar -h
# 用法就是 sar [选项信息] [间隔时间 次数]
Usage: /usr/bin/sar [ options ] [ [ ] ]
Main options and reports:
-B Paging statistics # 分页统计信息
-b I/O and transfer rate statistics # I/O传输速率
-d Block devices statistics # 块设备情况
-F Filesystems statistics # 文件系统统计
-H Hugepages utilization statistics # 交换空间利用统计
-I { | SUM | ALL | XALL }
Interrupts statistics # 中断信息统计
-m { [,...] | ALL }
Power management statistics # 电源管理统计信息
Keywords are:
CPU CPU instantaneous clock frequency
FAN Fans speed
FREQ CPU average clock frequency
IN Voltage inputs
TEMP Devices temperature
USB USB devices plugged into the system
-n { [,...] | ALL }
Network statistics # 网络信息统计
Keywords are:
DEV Network interfaces # 网卡
EDEV Network interfaces (errors)
NFS NFS client
NFSD NFS server
SOCK Sockets (v4) # 套接字
IP IP traffic (v4) # IP流
EIP IP traffic (v4) (errors)
ICMP ICMP traffic (v4)
EICMP ICMP traffic (v4) (errors)
TCP TCP traffic (v4) # TCP流
ETCP TCP traffic (v4) (errors)
UDP UDP traffic (v4) # UDP流
SOCK6 Sockets (v6)
IP6 IP traffic (v6)
EIP6 IP traffic (v6) (errors)
ICMP6 ICMP traffic (v6)
EICMP6 ICMP traffic (v6) (errors)
UDP6 UDP traffic (v6)
-q Queue length and load average statistics # CPU负载信息统计
-R Memory statistics # 内存信息统计
-r Memory utilization statistics # 内存使用率信息统计
-S Swap space utilization statistics # SWAP空间利用率统计
-u [ ALL ] # CPU统计
CPU utilization statistics
-v Kernel tables statistics # 内核表统计
-W Swapping statistics # 交换分区状态统计
下面我们来看些重点信息
3.网络信息查看(sar -n DEV)查看网络接口信息
# 查看网卡流量信息,每3秒展示一次,无限循环,直到用户中断
root@e05fc0c9b15c:/# sar -n DEV 3
Linux 5.10.76-linuxkit (e05fc0c9b15c) 03/18/22 _aarch64_ (4 CPU)
15:01:24 IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s %ifutil
15:01:27 lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
15:01:27 tunl0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
15:01:27 ip6tnl0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
15:01:27 eth0 7097.33 3900.67 470.98 264.66 0.00 0.00 0.00 0.04
15:01:27 IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s %ifutil
15:01:30 lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
15:01:30 tunl0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
15:01:30 ip6tnl0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
15:01:30 eth0 5176.67 2786.00 343.73 189.47 0.00 0.00 0.00 0.03
就15:01:30 这行记录
rxpck/s(每秒接收的网络帧)为5176,txpck/s(每秒发送的网络帧)为2786,
rxkB/s(每秒接收的千字节数)为343,txkB/s(每秒发送的千字节数)为189
有关于输出项的具体含义我们可以通过man sar 找到-n,如下所示:
-n { keyword [,...] | ALL }
Report network statistics.
Possible keywords are DEV, EDEV, NFS, NFSD, SOCK, IP, EIP, ICMP, EICMP, TCP, ETCP, UDP, SOCK6, IP6, EIP6, ICMP6, EICMP6 and UDP6.
With the DEV keyword, statistics from the network devices are reported. The following values are displayed:
IFACE
Name of the network interface for which statistics are reported.
rxpck/s
Total number of packets received per second.
txpck/s
Total number of packets transmitted per second.
rxkB/s
Total number of kilobytes received per second.
txkB/s
Total number of kilobytes transmitted per second.
rxcmp/s
Number of compressed packets received per second (for cslip etc.).
txcmp/s
Number of compressed packets transmitted per second.
rxmcst/s
Number of multicast packets received per second.
%ifutil
Utilization percentage of the network interface. For half-duplex interfaces, utilization is calculated using the sum of rxkB/s and txkB/s as
a percentage of the interface speed. For full-duplex, this is the greater of rxkB/S or txkB/s.
具体含义这里的英文说明已经很详细了,不再赘述
4.socket连接信息统计(sar -n SOCK)root@e05fc0c9b15c:/# sar -n SOCK 2
Linux 5.10.76-linuxkit (e05fc0c9b15c) 04/04/22 _aarch64_ (4 CPU)
04:35:10 totsck tcpsck udpsck rawsck ip-frag tcp-tw
04:35:12 11 1 0 0 0 0
04:35:14 12 2 0 0 0 0
04:35:16 11 1 0 0 0 0
04:35:18 11 1 0 0 0 0
04:35:20 11 1 0 0 0 0
totsck当前被使用的socket总数tcpsck当前被使用的TCP socket总数udpsck当前被使用的UDP socket总数rawsck当前处于RWA状态的socket总数ip-frag当前ip分配的数目tcp-twTCP套接字中处于TIME_WAIT状态的连接总数
5.磁盘使用情况(sar -d)
root@e05fc0c9b15c:/# sar -d 2
Linux 5.10.76-linuxkit (e05fc0c9b15c) 04/04/22 _aarch64_ (4 CPU)
04:41:26 DEV tps rkB/s wkB/s dkB/s areq-sz aqu-sz await %util
04:41:28 vda 2.50 38.00 0.00 0.00 15.20 0.00 1.00 0.15
04:41:30 vda 0.50 2.00 0.00 0.00 4.00 0.00 0.00 0.05
04:41:32 vda 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
DEV磁盘设备名称tps每秒I/O的传输总数rkB/s每秒读总数量(KB单位)wkB/s每秒写总数量(KB单位)areq-sz平均每次I/O操作的数据大小(KB单位)aqu-sz磁盘请求队列的平均长度await每次I/O请求的平均耗时(ms单位)%utilI/O请求占用的CPU时间百分比
6.查看内存情况(sar -r)
root@e05fc0c9b15c:/# sar -r 3
Linux 5.10.76-linuxkit (e05fc0c9b15c) 04/04/22 _aarch64_ (4 CPU)
04:51:25 kbmemfree kbavail kbmemused %memused kbbuffers kbcached kbcommit %commit kbactive kbinact kbdirty
04:51:28 513764 1070688 659536 32.39 136096 609548 4497728 145.79 497816 842500 8
04:51:31 513732 1070656 659568 32.39 136096 609548 4497728 145.79 497816 843016 8
笔者个人还是更喜欢vmstat 和 pidstat 来展示
总结:其他笔者不再赘述,在使用的时候主要还是想查看网络接口信息(sar -n DEV),有助于我们分析网卡包传入情况。
更多其他知识点可以参考:
linux 性能监控 sar命令_liyongbing1122的博客-CSDN博客_linux sar