转载自:
https://blog.csdn.net/chenggong2dm/article/details/8555127
https://blog.csdn.net/msdnchina/article/details/80639978
安装sar
#yum -y install sysstat #或者 yum install sysstat (自己按y)
#whereis sar
#whereis iostat
安装完成。
使用sar
//每隔一秒往屏幕输出一次cpu使用信息,共输出5次:
#sar 1 5 #或者 sar -u 1 5
//每2秒采样1次,共10次,把CPU使用数据保存到/opt/cpu.log文件中。
#sar 2 10 > /opt/cpu.log
//当前所有核心的数据显示。
#sar -P ALL 1 1
//每隔1秒记录CPU的使用情况,直到11点02分,数据将保存到/opt/cpu.log文件中。(-e 参数表示结束时间,注意时间格式:必须为hh:mm:ss格式)
#sar 1 0 -u -e 11:02:00 > /opt/cpu.log
//每隔1秒记录内存使用情况,直到11点02分,数据将保存到/opt/memory.log文件中。
#sar 1 0 -r -e 11:02:00 > /opt/memory.log
//每隔1秒记录网络使用情况,直到11点02分,数据将保存到/opt/network.log文件中。
#sar 1 0 -n DEV -e 11:02:00 > /opt/network.log
附注:
一般格式:
sar [ options... ] [ <interval> [ <count> ] ]
常用选项:
-A :包括绝大多数选项。
-b :报告I/O和传输速率统计。
-B :报告分页统计。
-c :报告进程创建活动。
-d :报告每个活动的块设备。(指磁盘)
-e [ hh:mm:ss ] :设置sar命令执行的结束时间,时间为24小时制格式。
-I { irq | SUM | ALL | XALL } :报告给定中断的统计。
-n { DEV | EDEV | NFS | NFSD | SOCK | ALL } :报告网络统计。
-P { cpu | ALL } :报告每个处理器的统计信息,可以统计指定或全部处理器。
-q :报告队列长度和平均负载。
-r :报告内存和交换空间的利用情况统计。
-R :报告内存统计。
-u :报告CPU利用率。
interval :两次输出之间的间隔时间。
count :按照这个时间间隔统计的次数。
[root@abc ~]# man sar
-u [ ALL ]
Report CPU utilization. The ALL keyword indicates that all the CPU fields should be displayed. The report may show the following fields:
%user
Percentage of CPU utilization that occurred while executing at the user level (application). Note that this field includes time spent running virtual processors.
%usr
Percentage of CPU utilization that occurred while executing at the user level (application). Note that this field does NOT include time spent running virtual processors.
%nice
Percentage of CPU utilization that occurred while executing at the user level with nice priority.
%system
Percentage of CPU utilization that occurred while executing at the system level (kernel). Note that this field includes time spent servicing hardware and software interrupts.
%sys
Percentage of CPU utilization that occurred while executing at the system level (kernel). Note that this field does NOT include time spent servicing hardware or soft-ware interrupts.
%iowait
Percentage of time that the CPU or CPUs were idle during which the system had an outstanding disk I/O request.
%steal
Percentage of time spent in involuntary wait by the virtual CPU or CPUs while the hypervisor was servicing another virtual processor.
%irq
Percentage of time spent by the CPU or CPUs to service hardware interrupts.
%soft
Percentage of time spent by the CPU or CPUs to service software interrupts.
%guest
Percentage of time spent by the CPU or CPUs to run a virtual processor.
%idle
Percentage of time that the CPU or CPUs were idle and the system did not have an outstanding disk I/O request.
Note: On SMP machines a processor that does not have any activity at all (0.00 for every field) is a disabled (offline) processor.
[root@abc ~]# sar -u ALL 1 10
Linux 2.6.32-642.el6.x86_64 (abc) 06/06/2018 _x86_64_ (4 CPU)
01:56:08 PM CPU %usr %nice %sys %iowait %steal %irq %soft %guest %idle
01:56:09 PM all 0.00 0.00 0.25 0.00 0.00 0.00 0.00 0.00 99.75
01:56:10 PM all 0.25 0.00 0.25 0.00 0.00 0.00 0.00 0.00 99.50
01:56:11 PM all 0.00 0.00 0.25 0.00 0.00 0.00 0.00 0.00 99.75
01:56:12 PM all 0.00 0.00 0.25 0.00 0.00 0.00 0.00 0.00 99.75
01:56:13 PM all 0.25 0.00 0.50 0.00 0.00 0.00 0.00 0.00 99.25
01:56:14 PM all 0.00 0.00 0.25 0.00 0.00 0.00 0.00 0.00 99.75
01:56:15 PM all 0.00 0.00 0.25 0.25 0.00 0.00 0.00 0.00 99.50
01:56:16 PM all 0.00 0.00 0.25 0.00 0.00 0.00 0.00 0.00 99.75
01:56:17 PM all 0.00 0.00 0.50 0.00 0.00 0.00 0.00 0.00 99.50
01:56:18 PM all 0.00 0.00 0.25 0.00 0.00 0.00 0.00 0.00 99.75
Average: all 0.05 0.00 0.30 0.03 0.00 0.00 0.00 0.00 99.62
[root@abc ~]#
Spending time in %user is expected behavior, as this is where all non-system tasks are accounted for.
If cycles are actively being spent in %system then much of the execution time is being spent in lower-level code.
If %iowait is high then it indicates processes are actively waiting due to disk accesses being a bottleneck on the system.