Linux has the powerful command sar to view hardware information, and the visualization tool ksar
I. Overview
sar (System Activity Reporter, system activity report) is a system running status statistics tool under Linux. It can report system activities from many aspects, including: file reading and writing, system call usage, disk I/O, CPU efficiency, memory usage, process activity and IPC-related activities, etc. He is a versatile little expert.
2. Installation
Under ubuntu:
root@ubuntu:/home/peng# apt-get install sysstat
root@ubuntu:/home/peng# sar -r
Cannot open /var/log/sysstat/sa07: No such file or directory
Please check if data collecting is enabled
After execution, you will encounter the following error. The 07 in sa07 is today's date. The reason is that the file has not been created. Solution:
root@ubuntu:/home/peng# chmod o+w /etc/default/sysstat
root@ubuntu:/home/peng# vim /etc/default/sysstat
3. Orders
grammar
-
The type is the type of indicator data we want to obtain. The -n here represents monitoring some network information.
-a:文件读写情况
-A:所有报告的总和
-B:分页状况
-b:显示I/O和传送速率的统计信息
-c:输出进程统计信息,每秒创建的进程数
-d:块设备状况
-F [ MOUNT ]:文件系统统计信息
-H:交换空间利用率
-I { <中断> | SUM | ALL | XALL }:中断信息状况
-n:汇报网络情况
-P:设定CPU
-q:队列长度和平均负载
-R:输出内存页面的统计信息
-r [ ALL ]:输出内存和交换空间的统计信息
-S:交换空间利用率信息
-u [ ALL ]:输出CPU使用情况的统计信息
-v:输出inode、文件和其他内核表的统计信息
-W:输出系统交换活动信息
-w:任务创建与系统转换信息
-y:终端设备活动情况
-
Type Parameters Some types have parameters, and some do not. DEV here represents monitoring network card information
-
Interval time The interval between each report (seconds)
-
Number of times
Displays the number of times reported. For example: the time interval is 2 and the number of samples is 3, then the sar command will block for 12 seconds.
help
root@ubuntu:/home/peng# sar --help
Usage: sar [ options ] [ <interval> [ <count> ] ]
Options are:
[ -A ] [ -B ] [ -b ] [ -C ] [ -D ] [ -d ] [ -F [ MOUNT ] ] [ -H ] [ -h ]
[ -p ] [ -q ] [ -R ] [ -r [ ALL ] ] [ -S ] [ -t ] [ -u [ ALL ] ] [ -V ]
[ -v ] [ -W ] [ -w ] [ -y ] [ --sadc ]
[ -I { <int> [,...] | SUM | ALL | XALL } ] [ -P { <cpu> [,...] | ALL } ]
[ -m { <keyword> [,...] | ALL } ] [ -n { <keyword> [,...] | ALL } ]
[ -j { ID | LABEL | PATH | UUID | ... } ]
[ -f [ <filename> ] | -o [ <filename> ] | -[0-9]+ ]
[ -i <interval> ] [ -s [ <hh:mm[:ss]> ] ] [ -e [ <hh:mm[:ss]> ] ]
4. Examples
Resource monitoring under Linux mainly includes the following types: disk, CPU, network, memory, IO, etc.
1. CPU information
Let’s start with the cpu information. As the brain of the computer, as a commander, we have to monitor its every move. In fact, there are three types of monitoring for the CPU: utilization, load, and interrupts.
(1)CPU utilization
Using sar -u, let's take a look at its execution results. You can see that the usage of each type is the same as the meaning in the top command.
root@ubuntu:/home/peng# sar -u 1 1
Linux 4.15.0-112-generic (ubuntu) 03/07/2021 _x86_64_ (1 CPU)
05:28:09 AM CPU %user %nice %system %iowait %steal %idle
05:28:10 AM all 1.01 0.00 2.02 0.00 0.00 96.97
Average: all 1.01 0.00 2.02 0.00 0.00 96.97
Field description
-
%user #User space CPU usage
-
%nice CPU usage of processes that have changed priorities
-
%system CPU usage of kernel space
-
%iowait Percentage of CPU waiting for IO
-
%steal The CPU used by the virtual machine's virtual machine CPU
-
%idle idle CPU
-
In the above display, mainly look at %iowait and %idle. If %iowait is too high, it indicates that there is an I/O bottleneck, that is, disk IO cannot meet business needs. If %idle is too low, it indicates that the CPU usage is serious and needs to be combined with memory usage, etc. Determine whether the CPU is a bottleneck.
(2) Report the usage status of each CPU:
root@ubuntu:/home/peng# sar -p 1 3
Linux 4.15.0-112-generic (ubuntu) 03/07/2021 _x86_64_ (1 CPU)
05:29:21 AM CPU %user %nice %system %iowait %steal %idle
05:29:22 AM all 1.00 0.00 0.00 0.00 0.00 99.00
05:29:23 AM all 1.02 0.00 0.00 0.00 0.00 98.98
05:29:24 AM all 1.01 0.00 1.01 0.00 0.00 97.98
Average: all 1.01 0.00 0.34 0.00 0.00 98.65
Field description
-
CPU: Statistics for all CPUs
-
%user User mode CPU usage statistics
-
%nice CPU usage statistics of processes whose priorities have been changed
-
%iowait The percentage of CPU waiting for IO data
-
%steal The percentage of physical CPU occupied by the vCPU of the virtual machine
-
%idle idle CPU percentage
(3) Use sar -q for CPU load. Similarly, the meaning of the parameters of top is similar. In addition to the load value, it also displays the length of the waiting queue, which is very helpful for troubleshooting queuing problems.
root@ubuntu:/home/peng# sar -q 1 1
Linux 4.15.0-112-generic (ubuntu) 03/07/2021 _x86_64_ (1 CPU)
05:30:20 AM runq-sz plist-sz ldavg-1 ldavg-5 ldavg-15 blocked
05:30:21 AM 0 440 0.01 0.02 0.00 0
Average: 0 440 0.01 0.02 0.00 0
Field description
-
runq-sz The length of the run queue (the number of processes waiting to run, the CP of each core cannot exceed 3)
-
plist-sz The number of processes (processes) and threads (threads) in the process list
-
ldavg-1 The average CPU load in the last minute, which is the average value obtained by adding the load of the multi-core CPU in the past minute and dividing it by the number of cores, and so on for 5 minutes and 15 minutes.
-
ldavg-5 CPU load average for the last 5 minutes
-
ldavg-15 CPU load average for the last 15 minutes
(4) Interruption
Use sar -I, note that the i is capitalized. Since there are different conversion methods, the interrupt parameters are divided into default, SUM, ALL, etc.
root@ubuntu:/home/peng# sar -I SUM 1 2
Linux 4.15.0-112-generic (ubuntu) 03/07/2021 _x86_64_ (1 CPU)
05:31:01 AM INTR intr/s
05:31:02 AM sum 250.52
05:31:03 AM sum 338.38
Average: sum 294.90
(5) Context switching
Use sar -w, which is often confused with sar -W (note case) which monitors swap partition usage, so be careful.
root@ubuntu:/home/peng# sar -w 1
Linux 4.15.0-112-generic (ubuntu) 03/07/2021 _x86_64_ (1 CPU)
05:31:53 AM proc/s cswch/s
05:31:54 AM 0.00 433.67
05:31:55 AM 0.00 734.38
05:31:56 AM 0.00 582.65
05:31:57 AM 0.00 886.46
2. Memory information
Memory is mainly divided into the following parts. What we usually monitor are physical memory, virtual memory, kernel, etc.
(1) Memory utilization
Use the sar -r command. Some sar versions may have sar -R, but generally lowercase is enough.
root@ubuntu:/home/peng# sar -r 1 1
Linux 4.15.0-112-generic (ubuntu) 03/07/2021 _x86_64_ (1 CPU)
05:32:54 AM kbmemfree kbmemused %memused kbbuffers kbcached kbcommit %commit kbactive kbinact kbdirty
05:32:55 AM 281108 1736408 86.07 109040 675176 3345488 110.93 730964 591392 0
Average: 281108 1736408 86.07 109040 675176 3345488 110.93 730964 591392 0
Field description
-
kbmemfree: available free memory size
-
kbmemused: used memory size (excluding memory used by the kernel)
-
%memused: Percentage of used memory
-
kbbuffers: the memory size used by the kernel buffer (buffer)
-
kbcached: The memory size used by the kernel cache (cache) data
-
kbswpfree: available free swap space size
-
kbswpused: used swap space size
-
%swpused: Percentage of used swap space
-
kbswpcad: memory size used by swap space cache
-
kbcommit ensures the minimum memory required to ensure the normal operation of the current system, that is, the minimum memory required to ensure that the memory does not overflow (physical memory + Swap partition)
-
commit This value is a percentage of kbcommit and the total memory (physical memory + swap partition)
(2) swap swap partition
For swap partitions, you can use sar -S. The effect is as follows. If you want to see the usage of the swap partition (not the capacity), you need to switch to the sar -W command.
root@ubuntu:/home/peng# sar -S 1 1
Linux 4.15.0-112-generic (ubuntu) 03/07/2021 _x86_64_ (1 CPU)
05:34:15 AM kbswpfree kbswpused %swpused kbswpcad %swpcad
05:34:16 AM 962556 35840 3.59 2808 7.83
Average: 962556 35840 3.59 2808 7.83
(3) Kernel usage
Mainly use the sar -v command. v is generally used for version display in other commands, and the sar command is used to output some information in the slab area. It can be said to be unique and unconventional.
root@ubuntu:/home/peng# sar -v 1
Linux 4.15.0-112-generic (ubuntu) 03/07/2021 _x86_64_ (1 CPU)
05:34:46 AM dentunusd file-nr inode-nr pty-nr
05:34:47 AM 47183 6816 53938 17
05:34:48 AM 47183 6816 53938 17
Field description
-
dentunusd The number of unused entries in the cache directory entries
-
file-nr The number of file handles used by the system
-
inode-nr The number of indexes used
-
pty-nr The number of pty used
(4) Monitor memory paging information, mainly using the sar -B command. (Pay attention to his pronunciation! It’s awesome!) The execution results are as follows:
root@ubuntu:/home/peng# sar -B
Linux 4.15.0-112-generic (ubuntu) 03/07/2021 _x86_64_ (1 CPU)
01:51:34 AM LINUX RESTART (1 CPU)
01:55:01 AM pgpgin/s pgpgout/s fault/s majflt/s pgfree/s pgscank/s pgscand/s pgsteal/s %vmeff
02:05:01 AM 0.00 0.29 23.98 0.00 10.50 0.00 0.00 0.00 0.00
02:15:01 AM 0.00 0.23 1.03 0.00 1.90 0.00 0.00 0.00 0.00
02:25:01 AM 0.00 0.47 1.73 0.00 2.71 0.00 0.00 0.00 0.00
(5) View the statistics of the system swap partition:
root@ubuntu:/home/peng# sar -W
Linux 4.15.0-112-generic (ubuntu) 03/07/2021 _x86_64_ (1 CPU)
01:51:34 AM LINUX RESTART (1 CPU)
01:55:01 AM pswpin/s pswpout/s
02:05:01 AM 0.00 0.00
02:15:01 AM 0.00 0.00
02:25:01 AM 0.00 0.00
02:35:01 AM 0.00 0.00
02:45:01 AM 0.00 0.00
02:55:01 AM 0.00 0.00
Field description
-
pswpin/s The number of swap pages from the swap partition to the system per second
-
pswpout/s The number of swap pages swapped from the system to swap per second
(6) View statistics on I/O and transfer rates
root@ubuntu:/home/peng# sar -b
Linux 4.15.0-112-generic (ubuntu) 03/07/2021 _x86_64_ (1 CPU)
01:51:34 AM LINUX RESTART (1 CPU)
01:55:01 AM tps rtps wtps bread/s bwrtn/s
02:05:01 AM 0.04 0.00 0.04 0.00 0.59
02:15:01 AM 0.03 0.00 0.03 0.00 0.47
Field description
-
tps The total number of disk IOs per second, equal to tps in iostat
-
rtps total number of IOs read from disk per second
-
wtps Total IO written to disk per second
-
bread/s Total number of blocks read from disk per second
-
bwrtn/s Total number of blocks written to disk per second
(7) Disk usage details statistics
root@ubuntu:/home/peng# sar -d
Linux 4.15.0-112-generic (ubuntu) 03/07/2021 _x86_64_ (1 CPU)
01:51:34 AM LINUX RESTART (1 CPU)
01:55:01 AM DEV tps rd_sec/s wr_sec/s avgrq-sz avgqu-sz await svctm %util
02:05:01 AM dev7-0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
02:05:01 AM dev8-0 0.04 0.00 0.59 13.54 0.00 0.00 0.00 0.00
Field description
-
DEV The name of the disk device. If -p is not added, a device name similar to dev253-0 will be displayed. Therefore, the name displayed by adding -p is more direct.
-
tps: Total number of I/O transmissions per second
-
rd_sec/s Total number of sectors read per second
-
wr_sec/s Total number of sectors written per second
-
avgrq-sz 平均每次次磁盘I/O操作的数据大小(扇区)
-
avgqu-sz 磁盘请求队列的平均长度
-
await 从请求磁盘操作到系统完成处理,每次请求的平均消耗时间,包括请求队列等待时间,单位是毫秒(1秒等于1000毫秒),等于寻道时间+队列时间+服务时间
-
svctm I/O的服务处理时间,即不包括请求队列中的时间
-
%util I/O请求占用的CPU百分比,值越高,说明I/O越慢
3. I/O信息
IO信息监控,同样是一个响亮的sar -b,不过这里的b,变成了小写的。
root@ubuntu:/home/peng# sar -b 1 2
Linux 4.15.0-112-generic (ubuntu) 03/07/2021 _x86_64_ (1 CPU)
05:41:22 AM tps rtps wtps bread/s bwrtn/s
05:41:23 AM 0.00 0.00 0.00 0.00 0.00
05:41:24 AM 2.06 0.00 2.06 0.00 65.98
Average: 1.02 0.00 1.02 0.00 32.65
字段说明
-
tps 磁盘每秒钟的IO总数,等于iostat中的tps
-
rtps 每秒钟从磁盘读取的IO总数
-
wtps 每秒钟从写入到磁盘的IO总数
-
bread/s 每秒钟从磁盘读取的块总数
-
bwrtn/s 每秒钟写入到磁盘的块总数
sar -d命令非常类似于iostat命令,结果更多。
root@ubuntu:/home/peng# sar -d 1
Linux 4.15.0-112-generic (ubuntu) 03/07/2021 _x86_64_ (1 CPU)
05:42:03 AM DEV tps rd_sec/s wr_sec/s avgrq-sz avgqu-sz await svctm %util
05:42:04 AM dev7-0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
05:42:04 AM dev8-0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
字段说明
-
DEV 磁盘设备的名称,如果不加-p,会显示dev253-0类似的设备名称,因此加上-p显示的名称更直接
-
tps:每秒I/O的传输总数
-
rd_sec/s 每秒读取的扇区的总数
-
wr_sec/s 每秒写入的扇区的 总数
-
avgrq-sz 平均每次次磁盘I/O操作的数据大小(扇区)
-
avgqu-sz 磁盘请求队列的平均长度
-
await 从请求磁盘操作到系统完成处理,每次请求的平均消耗时间,包括请求队列等待时间,单位是毫秒(1秒等于1000毫秒),等于寻道时间+队列时间+服务时间
-
svctm I/O的服务处理时间,即不包括请求队列中的时间
-
%util I/O请求占用的CPU百分比,值越高,说明I/O越慢
4. 网络信息
(1) 统计网络信息
sar -n
接下来,我们看最复杂的网络信息。说它复杂,是因为它的参数非常的多,比如上面说到的DEV,就表示的网络流量。
要命的是,这些参数的每个输出,还都不是一样的。可能是26个字母已经无法涵盖这么多参数了吧,所以sar命令统一把它加在了sar -n下面。好在我们平常使用的时候,只和DEV参数打交道既可以了。
#sar -n选项使用6个不同的开关:DEV,EDEV,NFS,NFSD,SOCK,IP,EIP,ICMP,EICMP,TCP,ETCP,UDP,SOCK6,IP6,EIP6,ICMP6,EICMP6和UDP6 ,DEV显示网络接口信息,EDEV显示关于网络错误的统计数据,NFS统计活动的NFS客户端的信息,NFSD统计NFS服务器的信息,SOCK显示套接字信息,ALL显示所有5个开关。它们可以单独或者一起使用。
(2) 每间隔1秒统计一次,总计统计1次
root@ubuntu:/home/peng# sar -n DEV 1 1
Linux 4.15.0-112-generic (ubuntu) 03/07/2021 _x86_64_ (1 CPU)
05:45:36 AM IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s %ifutil
05:45:37 AM ens33 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
05:45:37 AM lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
Average: IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s %ifutil
Average: ens33 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
Average: lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
字段说明 下面的average是在多次统计后的平均值
-
IFACE 本地网卡接口的名称
-
rxpck/s 每秒钟接受的数据包
-
txpck/s 每秒钟发送的数据库
-
rxKB/S 每秒钟接受的数据包大小,单位为KB
-
txKB/S 每秒钟发送的数据包大小,单位为KB
-
rxcmp/s 每秒钟接受的压缩数据包
-
txcmp/s 每秒钟发送的压缩包
-
rxmcst/s 每秒钟接收的多播数据包
(3) 统计网络设备通信失败信息:
root@ubuntu:/home/peng# sar -n EDEV 1 1
Linux 4.15.0-112-generic (ubuntu) 03/07/2021 _x86_64_ (1 CPU)
05:46:22 AM IFACE rxerr/s txerr/s coll/s rxdrop/s txdrop/s txcarr/s rxfram/s rxfifo/s txfifo/s
05:46:23 AM ens33 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
05:46:23 AM lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
Average: IFACE rxerr/s txerr/s coll/s rxdrop/s txdrop/s txcarr/s rxfram/s rxfifo/s txfifo/s
Average: ens33 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
Average: lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
字段说明
-
IFACE 网卡名称
-
rxerr/s 每秒钟接收到的损坏的数据包
-
txerr/s 每秒钟发送的数据包错误数
-
coll/s 当发送数据包时候,每秒钟发生的冲撞(collisions)数,这个是在半双工模式下才有
-
rxdrop/s 当由于缓冲区满的时候,网卡设备接收端每秒钟丢掉的网络包的数目
-
txdrop/s 当由于缓冲区满的时候,网络设备发送端每秒钟丢掉的网络包的数目
-
txcarr/s 当发送数据包的时候,每秒钟载波错误发生的次数
-
rxfram 在接收数据包的时候,每秒钟发生的帧对其错误的次数
-
rxfifo 在接收数据包的时候,每秒钟缓冲区溢出的错误发生的次数
-
txfifo 在发生数据包 的时候,每秒钟缓冲区溢出的错误发生的次数
(4) 统计socket连接信息
root@ubuntu:/home/peng# sar -n SOCK 1 1
Linux 4.15.0-112-generic (ubuntu) 03/07/2021 _x86_64_ (1 CPU)
05:47:21 AM totsck tcpsck udpsck rawsck ip-frag tcp-tw
05:47:22 AM 1393 2 6 0 0 0
Average: 1393 2 6 0 0 0
字段说明
-
totsck 当前被使用的socket总数
-
tcpsck 当前正在被使用的TCP的socket总数
-
udpsck 当前正在被使用的UDP的socket总数
-
rawsck 当前正在被使用于RAW的skcket总数
-
ip-frag 当前的IP分片的数目
-
tcp-tw TCP套接字中处于TIME-WAIT状态的连接数量
使用FULL关键字,相当于上述DEV、EDEV和SOCK三者的综合。
(5) TCP连接的统计
root@ubuntu:/home/peng# sar -n TCP 1 3
Linux 4.15.0-112-generic (ubuntu) 03/07/2021 _x86_64_ (1 CPU)
05:48:05 AM active/s passive/s iseg/s oseg/s
05:48:06 AM 0.00 0.00 0.00 0.00
05:48:07 AM 0.00 0.00 0.00 0.00
05:48:08 AM 0.00 0.00 0.00 0.00
Average: 0.00 0.00 0.00 0.00
字段说明
-
active/s 新的主动连接
-
passive/s 新的被动连接
-
iseg/s 接受的段
-
oseg/s 输出的段
(6) sar -n 使用总结
-
DEV 网络接口统计信息
-
EDEV 网络接口错误
-
NFS NFS 客户端
-
NFSD NFS 服务器
-
SOCK Sockets (套接字) (v4)套接字使用
-
IP IP 流 (v4) IP数据报统计信息
-
EIP IP 流 (v4) (错误) IP错误统计信息
-
ICMP ICMP 流 (v4)
-
EICMP ICMP 流 (v4) (错误)
-
TCP TCP 流 (v4) TCP统计信息
-
ETCP TCP 流 (v4) (错误)TCP错误统计信息
-
UDP UDP 流 (v4)
-
SOCK6 Sockets (套接字) (v6)
-
IP6 IP 流 (v6)
-
EIP6 IP 流 (v6) (错误)
-
ICMP6 ICMP 流 (v6)
-
EICMP6 ICMP 流 (v6) (错误)
-
UDP6 UDP 流 (v6)
五、ksar
Ksar可以用来分析系统性能数据,其优势在于不需要单独去收集性能数据,系统自带有sar包,通过命令转换即可使用Ksar展现。
安装该软件需要先安装java,如果已经安装调到第5步。
1. 下载java
jdk-8u202-linux-x64.tar.gz下载地址如下:
https://ch1-ctc-dd.tv002.com/down/d743c682d992b1c39f64ba78ce0d86e8/jdk-8u202-linux-x64.tar.gz?cts=dx-f-D121A237A240A189F43372&ctp=121A237A240A189& ctt=1615408146&limit=1&spd=65000&ctk=d743c682d992b1c39f64ba78ce0d86e8&chk=3bf885e6cb28751b351749779a461744- 194042837&mtd=1
2. Unzip
Copy jdk-8u202-linux-x64.tar.gz to /home/peng/jdk in ubuntu
tar -zxvf jdk-8u202-linux-x64.tar.gz
3. Set environment variables
$sudo vim /etc/profile
Add the following content at the end of the file
28 #set java env
29 export JAVA_HOME=/home/peng/jdk/jdk1.8.0_202
30 export JRE_HOME=${JAVA_HOME}/jre
31 export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
32 export PATH=${JAVA_HOME}/bin:$PATH
Make environment variables effective
$sudo source /etc/profile
4. Test java
The java version is 1.8.0_202
5. Download ksar source code
wget http://jaist.dl.sourceforge.net/project/ksar/ksar/5.0.6/ksar-5.0.6.zip
unzip ksar-5.0.6.zip
Then unzip and enter the source code root directory, and execute the script:
sh run.sh
This software can be launched
6. Operation
To execute the command, click Data->Run local command
You can execute the following commands:
sar -A
Click on the corresponding hardware information to view the corresponding hardware information in a graphical form. Very intuitive and convenient.
·················· END ···················
Click to follow the official account and reply [1024] to receive free study materials