linux命令 sar_带有示例Linux系统活动报告程序(SAR)命令教程
linux命令 sar
Monitoring Linux servers is important part of the system administration. There are a lot of ways and tools to monitor Linux servers. System Activity Reporter or simply sar is one of the most popular and useful tools. Sar can save system stats from various subsystems like disk, CPU, memory, network, etc. to file and used to review historical performance metrics. There are also different tools to provide graphical presentations from Sar stats. Sar saves systems stats in specified intervals. We will look at them in detail in this tutorial.
监视Linux服务器是系统管理的重要部分。 有很多方法和工具可以监视Linux服务器。 System Activity Reporter或简称为sar是最受欢迎和最有用的工具之一。 Sar可以将磁盘,CPU,内存,网络等各种子系统的系统统计信息保存到文件中,并用于查看历史性能指标。 也有不同的工具可以根据Sar统计数据提供图形表示。 Sar会按指定的时间间隔保存系统统计信息。 我们将在本教程中详细介绍它们。
安装 (Install)
Sar is provided by sysstat
tool and can be installed from official repositories various distributions.
Sar由sysstat
工具提供,可以从官方存储库中的各种发行版中安装。
$ sudo apt install sysstat
句法 (Syntax)
We will use the following syntax for sar
command.
我们将对sar
命令使用以下语法。
sar [options] [interval] [count]
帮帮我(Help)
Simple and fast way to get help with sar command is providing -h
option like below.
获得sar命令帮助的简单快捷方法是提供-h
选项,如下所示。
$ sar -h
显示CPU状态(Show CPU Stats)
One of the most wanted server metrics to track in monitoring purposes is CPU usage. We will provide -u
options for CPU stats. Also, numbers 1
specify a time interval and 3
count which is how many time stats will be printed.
监视用途中最需要跟踪的服务器指标之一是CPU使用率。 我们将为CPU统计信息提供-u
选项。 此外,数字1
指定的时间间隔和3
计数是多少时间统计数据将被打印。
$ sar -u 1 3
In the example, the CPU column provides information that all CPU’s metrics are provided. There is also time information for the performance metrics.
在示例中,“ CPU”列提供了有关已提供所有CPU指标的信息。 也有性能指标的时间信息。
-
%user
column is used to show user side load%user
列用于显示用户端负载 -
%system
columns are used to show system-related load%system
列用于显示与系统相关的负载 -
%idle
column shows how many CPU resources are do not used. In this caseload is very low and idle value is the range of %97%idle
列显示未使用多少CPU资源。 在这种情况下,负载非常低,空闲值在%97的范围内
显示特定的所有CPU统计信息(Show Specific All CPU Stats)
In the previous example, we have looked at some stats of the CPU that are popular and most useful. But in some situations, this can not be enough and we may need more stat about CPU like iowait
. These stats can be printed with ALL
option after -u
like below.
在前面的示例中,我们查看了一些流行且最有用的CPU统计信息。 但是在某些情况下,这还不够,我们可能需要有关iowait
CPU的更多统计信息。 这些统计信息可以在-u
之后用ALL
选项打印,如下所示。
$ sar -u ALL 1 3
In this example, we can see the extra columns like %iowaint
%irq
, %gnice
.
在此示例中,我们可以看到额外的列,例如%iowaint
%irq
, %gnice
。
显示特定的CPU统计信息 (Show Specific CPU Stats)
As we know modern CPUs provide more than one core for more performance. While providing CPU stats by default all core metrics are provided. But there is -P
option that will filter and provide only specified core metrics like below. In this example, we want to get second core information. Keep in mind that the core index starts from 0 which means the first core is numbered as 0.
众所周知,现代CPU提供了多个内核来提高性能。 默认情况下,在提供CPU统计信息时,会提供所有核心指标。 但是有-P
选项将过滤并仅提供指定的核心指标,如下所示。 在此示例中,我们希望获得第二个核心信息。 请记住,核心索引从0开始,这意味着第一个核心编号为0。
$ sar -P 1 1 3
We can see from the screenshot that the first core is specified with -P 1
and other parameters like interval and count are provided too. If we look at the CPU column we can see that the core index number 1 is printed as we expected.
从屏幕截图中我们可以看到,第一个内核是用-P 1
指定的,并且还提供了诸如interval和count之类的其他参数。 如果我们查看CPU列,我们可以看到核心索引号1已按预期方式打印。
显示内存统计 (Show Memory Stats)
Another important performance metric for Linux servers is a memory. Memory performance can be monitored with the -r
parameter.
Linux服务器的另一个重要性能指标是内存。 可以使用-r
参数监视内存性能。
$ sar -r 1 3
We can see a lot of information about the memory is provided.
我们可以看到很多有关内存的信息。
-
kbmemfree
column shows free memory as kilobytekbmemfree
列将可用内存显示为千字节 -
kbmemused
column shows used memory as kilobytekbmemused
列将kbmemused
用内存显示为千字节 -
%memused
columns show the percentage of used memory%memused
列显示已用内存的百分比
显示掉期统计(Show Swap Stats)
Swap is a memory increment mechanism used years ago. Currently, the system administrator does not prefer this method because the memory prices a low and the swap performance is not good. Swap stats can get with -S
option.
交换是几年前使用的内存增加机制。 当前,系统管理员不喜欢这种方法,因为内存价格低且交换性能不好。 交换统计信息可以通过-S
选项获得。
$ sar -S 1 3
As we can see from output there is no swap usage in this Linux server.
从输出中可以看到,此Linux服务器中没有交换使用。
-
kbswpfree
column shows free swap size in kilobytekbswpfree
列显示可用交换大小(以千字节为单位) -
kbswpused
column shows used swap size in kilobytekbswpused
列显示已使用的交换大小(以千字节为单位) -
%swpused
column shows used swap size as a percentage%swpused
列以百分比显示了已使用的交换大小
显示I / O统计(Show I/O Stats)
IO is another monitored metric for Linux systems. IO parameter -b
provides information about transactions per second, read and write transactions, etc.
IO是Linux系统的另一个受监视指标。 IO参数-b
提供有关每秒事务,读取和写入事务等信息。
$ sar -b 1 3
-
tps
column shows transactions per the second counttps
列显示每秒的交易数 -
rtpd
column show read transactions per second countrtpd
列显示每秒读取的事务数 -
wtps
column shows write transactions per second countwtps
列显示每秒写入事务数 -
bread/s
column shows byte read the second countbread/s
栏显示字节读取第二个计数 -
wread/s
column shows byte write the second countwread/s
列显示字节写入的第二个计数
显示特定的磁盘I / O统计信息(Show Specific Disk I/O Stats)
Block devices or disk IO stats can be gathered with sar command too. To get this information required option is -d
.
块设备或磁盘IO统计信息也可以使用sar命令收集。 要获取此信息,必需的选项是-d
。
$ sar -d 1 3
-
DEV
column shows the devices minor and major numbers like dev252-0DEV
列显示设备的次要和主要编号,例如dev252-0 -
tps
column shows transactions per the second counttps
列显示每秒的交易数 -
rd_sec/s
column shows read operation done in a secondrd_sec/s
列显示了在一秒钟内完成的读取操作 -
we_sec/s
column shows write operation done in a secondwe_sec/s
列显示写操作在一秒钟内完成 -
%util
column shows the total utilization percentage%util
列显示总利用率百分比
显示磁盘名称 (Show Disk Names)
In the previous example, we have listed the stats with related disk device major and minor numbers by default. This is not readable from human point of view if we have a lot of disks on the Linux system. -p
option can be used to list disk devices with their native names like sda
, vda
etc.
在前面的示例中,我们默认列出了统计信息以及相关的磁盘设备主设备号和次设备号。 如果我们在Linux系统上有很多磁盘,那么从人类的角度来看这是不可读的。 -p
选项可用于列出具有本地名称的磁盘设备,例如sda
, vda
等。
$ sar -p -d 1 3
显示上下文切换状态(Show Context Switch Stats)
As we know processes do not all ways run in their lifetime. Some times they sleep or wait for some io. This status change is named as context switching. Information about context switching can get with -w
option.
众所周知,过程并非在生命周期中都以某种方式运行。 有时他们睡觉或等待一些东西。 此状态更改称为上下文切换。 可以使用-w
选项获取有关上下文切换的信息。
$ sar -w 1 3
显示网络统计(Show Network Statistics)
Network statistics can be printed with the option -n
. Also after the option, a keyword must be provided. In this example, we will print the device-related metrics.
可以使用选项-n
来打印网络统计信息。 此外,在选项之后,还必须提供关键字。 在此示例中,我们将打印与设备相关的指标。
$ sudo sar -n DEV 1 3
-
IFACE
column shows the related interface nameIFACE
列显示相关的接口名称 -
rxpck/s
column shows received packets countrxpck/s
列显示接收到的数据包计数 -
txpck/s
column shows transmitted packets counttxpck/s
列显示已发送的数据包计数 -
rxkB/s
column shows received packets sizesrxkB/s
列显示接收到的数据包大小 -
txkB/s
column shows transmitted packets sizestxkB/s
列显示传输的数据包大小 -
%ifutil
column show interface usage or load percentage%ifutil
列显示接口使用情况或负载百分比
We have provided -n DEV
to get device-related stats. But there are other subsystems other than DEV to get information. Here are some of them
我们提供了-n DEV
来获取与设备相关的统计信息。 但是除DEV之外,还有其他子系统可以获取信息。 这里是其中的一些
-
IP
displays IP statisticsIP
显示IP统计信息 -
TCP
displays TCP statisticsTCP
显示TCP统计信息 -
UDP
displays UDP statisticsUDP
显示UDP统计信息 -
ALL
displays all statics from above and those we have not mentionedALL
显示上面的所有静态信息以及我们未提及的所有静态信息
$ sudo sar -n IP 1 3
显示分页统计 (Show Paging Stats)
As we know Linux memory management has some mechanisms to effectively and reliable way manage the memory. Paging is used to manage the memory system.
众所周知,Linux内存管理具有一些机制,可以有效,可靠地管理内存。 分页用于管理内存系统。
$ sar -b 1 3
将Sar统计数据写入文件(Write Sar Stats To A File)
Up to now, we have printed collected statistics and stats into the terminal. But in some situations, we may need to save data and shot in a more shiny way or store historical data for inspection. -o
option with a file name can be used to save collected information into a file.
到目前为止,我们已经将收集到的统计数据和统计信息打印到终端中。 但是在某些情况下,我们可能需要保存数据并以更闪亮的方式拍摄或存储历史数据以进行检查。 带有文件名的-o
选项可用于将收集的信息保存到文件中。
$ sar -o sar-data -u 1 3
从文件中显示历史性Sar统计数据(Show Historic Sar Stats From File)
In the previous example, we have to save sar data into a file. One of the sar usage scenarios is saving this data and playing graphics according to this data. Sar can also read all ready saved data and show accordingly. -f
with a data file name can be used for this purpose.
在前面的示例中,我们必须将sar数据保存到文件中。 sar使用情况之一是保存此数据并根据此数据播放图形。 Sar还可以读取所有准备好的已保存数据并进行相应显示。 具有数据文件名的-f
可以用于此目的。
$ sar -f sar-data
翻译自: https://www.poftut.com/linux-system-activity-reporter-sar-command-tutorial-examples/
linux命令 sar