Use the sar tool to detect system performance bottlenecks
sar The command is used to collect, report, or save UNIX/Linux system activity information. It saves selected counters to the operating system's /var/log/sa/sadd file. From the collected data, you can get a lot of information about your server:
- CPU Usage
- Memory pages and usage
- Network I/O and Transfer Statistics
- Process Creation Activity
- All block device activities
- Interruptions per second, etc.
sar The output of the command can be used to identify server bottlenecks. However, it can be difficult to analyze the information provided by the sar command, so use the kSar tool. The kSar tool can plot the output of the sar command into an easy-to-understand chart based on time periods.
sar, sa1, and sa2 commands are part of the sysstat package. It is a collection of performance monitoring tools included with Linux.
- sar:Display data
- sa1 and sa2: Collect and save data for later analysis. sa2 The shell script writes a daily report in the /var/log/sa directory. sa1 The shell script writes daily system activity information to a file in the form of binary data.
- sadc - System activity data collector. You can configure various options by modifying the sa1 and sa2 scripts. They are located in the following directories:
- /usr/lib64/sa/sa1 (64-bit) or /usr/lib/sa/sa1 (32-bit) - it calls sadcGo to log reports to /var/log/sa/sadX format.
- /usr/lib64/sa/sa2 (64-bit) or /usr/lib/sa/sa2 (32-bit) - it calls sarGo and log reports to /var/log/sa/sarX format.
在一个基于 CentOS/RHEL 的系统上,输入如下的 yum 命令[1] 去安装 sysstat:
# yum install sysstat
示例输出如下:
Loaded plugins: downloadonly, fastestmirror, priorities, : protectbase, security Loading mirror speeds from cached hostfile * addons: mirror.cs.vt.edu * base: mirror.ash.fastserv.com * epel: serverbeach1.fedoraproject.org * extras: mirror.cogentco.com * updates: centos.mirror.nac.net 0 packages excluded due to repository protections Setting up Install Process Resolving Dependencies --> Running transaction check ---> Package sysstat.x86_64 0:7.0.2-3.el5 set to be updated --> Finished Dependency Resolution Dependencies Resolved ==================================================================== Package Arch Version Repository Size ==================================================================== Installing: sysstat x86_64 7.0.2-3.el5 base 173 k Transaction Summary ==================================================================== Install 1 Package(s) Update 0 Package(s) Remove 0 Package(s) Total download size: 173 k Is this ok [y/N]: y Downloading Packages: sysstat-7.0.2-3.el5.x86_64.rpm | 173 kB 00:00 Running rpm_check_debug Running Transaction Test Finished Transaction Test Transaction Test Succeeded Running Transaction Installing : sysstat 1/1 Installed: sysstat.x86_64 0:7.0.2-3.el5 Complete!
编辑 /etc/sysconfig/sysstat 文件去指定日志文件保存多少天(最长为一个月):
# vi /etc/sysconfig/sysstat
示例输出如下 :
# keep log for 28 days # the default is 7 HISTORY=28
保存并关闭这个文件。
默认的 cron 作业位于[2] /etc/cron.d/sysstat:
# cat /etc/cron.d/sysstat
示例输出如下:
# run system activity accounting tool every 10 minutes */10 * * * * root /usr/lib64/sa/sa1 1 1 # generate a daily summary of process accounting at 23:53 53 23 * * * root /usr/lib64/sa/sa2 -A
使用一个文本编辑器去编辑 /etc/cron.d/sysstat 文件,比如使用 vim 命令,输入如下:
# vi /etc/cron.d/sysstat
像下面的示例那样更新这个文件,以记录所有的硬盘统计数据(-d 选项强制记录每个块设备的统计数据,而 -I 选项强制记录所有系统中断的统计数据):
# run system activity accounting tool every 10 minutes */10 * * * * root /usr/lib64/sa/sa1 -I -d 1 1 # generate a daily summary of process accounting at 23:53 53 23 * * * root /usr/lib64/sa/sa2 -A
在 CentOS/RHEL 7.x 系统上你需要传递 -S DISK 选项去收集块设备的数据。传递 -S XALL 选项去采集如下所列的数据:
- 磁盘
- 分区
- 系统中断
- SNMP
- IPv6
# Run system activity accounting tool every 10 minutes */10 * * * * root /usr/lib64/sa/sa1 -S DISK 1 1 # 0 * * * * root /usr/lib64/sa/sa1 600 6 & # Generate a daily summary of process accounting at 23:53 53 23 * * * root /usr/lib64/sa/sa2 -A # Run system activity accounting tool every 10 minutes
保存并关闭这个文件。
输入如下命令:
chkconfig sysstat on service sysstat start
示例输出如下:
Calling the system activity data collector (sadc):
对于 CentOS/RHEL 7.x,运行如下的命令:
# systemctl enable sysstat # systemctl start sysstat.service # systemctl status sysstat.service
示例输出:
● sysstat.service - Resets System Activity Logs Loaded: loaded (/usr/lib/systemd/system/sysstat.service; enabled; vendor preset: enabled) Active: active (exited) since Sat 2018-01-06 16:33:19 IST; 3s ago Process: 28297 ExecStart=/usr/lib64/sa/sa1 --boot (code=exited, status=0/SUCCESS) Main PID: 28297 (code=exited, status=0/SUCCESS) Jan 06 16:33:19 centos7-box systemd[1]: Starting Resets System Activity Logs... Jan 06 16:33:19 centos7-box systemd[1]: Started Resets System Activity Logs.
使用 sar 命令去显示操作系统中选定的累积活动计数器输出。在这个示例中,运行 sar 命令行,去实时获得 CPU 使用率的报告:
# sar -u 3 10
示例输出:
Linux 2.6.18-164.2.1.el5 (www-03.nixcraft.in) 12/14/2009 09:49:47 PM CPU %user %nice %system %iowait %steal %idle 09:49:50 PM all 5.66 0.00 1.22 0.04 0.00 93.08 09:49:53 PM all 12.29 0.00 1.93 0.04 0.00 85.74 09:49:56 PM all 9.30 0.00 1.61 0.00 0.00 89.10 09:49:59 PM all 10.86 0.00 1.51 0.04 0.00 87.58 09:50:02 PM all 14.21 0.00 3.27 0.04 0.00 82.47 09:50:05 PM all 13.98 0.00 4.04 0.04 0.00 81.93 09:50:08 PM all 6.60 6.89 1.26 0.00 0.00 85.25 09:50:11 PM all 7.25 0.00 1.55 0.04 0.00 91.15 09:50:14 PM all 6.61 0.00 1.09 0.00 0.00 92.31 09:50:17 PM all 5.71 0.00 0.96 0.00 0.00 93.33 Average: all 9.24 0.69 1.84 0.03 0.00 88.20
其中:
- 3 表示间隔时间
- 10 表示次数
查看进程创建的统计数据,输入:
# sar -c 3 10
查看 I/O 和传输率统计数据,输入:
# sar -b 3 10
查看内存页面统计数据,输入:
# sar -B 3 10
查看块设备统计数据,输入:
# sar -d 3 10
查看所有中断的统计数据,输入:
# sar -I XALL 3 10
查看网络设备特定的统计数据,输入:
# sar -n DEV 3 10 # sar -n EDEV 3 10
查看 CPU 特定的统计数据,输入:
# sar -P ALL # Only 1st CPU stats # sar -P 1 3 10
查看队列长度和平均负载的统计数据,输入:
# sar -q 3 10
查看内存和交换空间的使用统计数据,输入:
# sar -r 3 10 # sar -R 3 10
查看 inode、文件、和其它内核表统计数据状态,输入:
# sar -v 3 10
查看系统切换活动统计数据,输入:
# sar -w 3 10
查看交换统计数据,输入:
# sar -W 3 10
查看一个 PID 为 3256 的 Apache 进程,输入:
# sar -x 3256 3 10
sar 和 sadf 提供了基于命令行界面的输出。这种输出可能会使新手用户/系统管理员感到无从下手。因此,你需要使用 kSar,它是一个图形化显示你的 sar 数据的 Java 应用程序。它也允许你以 PDF/JPG/PNG/CSV 格式导出数据。你可以用三种方式去加载数据:本地文件、运行本地命令、以及通过 SSH 远程运行的命令。kSar 可以处理下列操作系统的 sar 输出:
- Solaris 8, 9 和 10
- Mac OS/X 10.4+
- Linux (Systat Version >= 5.0.5)
- AIX (4.3 & 5.3)
- HPUX 11.00+
访问 官方[3] 网站去获得最新版本的源代码。使用 wget[4] 去下载源代码,输入:
$ wget https://github.com/vlsi/ksar/releases/download/v5.2.4-snapshot-652bf16/ksar-5.2.4-SNAPSHOT-all.jar
首先要确保你的机器上 JAVA jdk[5] 已安装并能够正常工作。输入下列命令去启动 kSar:
$ java -jar ksar-5.2.4-SNAPSHOT-all.jar
kSar welcome screen
接下来你将看到 kSar 的主窗口,和有两个菜单的面板。
kSar - the main window
左侧有一个列表,是 kSar 根据数据已经解析出的可用图表的列表。右侧窗口将展示你选定的图表。
首先,你需要从命名为 server1 的服务器上采集 sar 命令的统计数据。输入如下的命令:
[ server1 ]# LC_ALL=C sar -A > /tmp/sar.data.txt
接下来,使用 scp 命令从本地桌面拷贝到远程电脑上:
[ desktop ]$ scp user@server1.nixcraft.com:/tmp/sar.data.txt /tmp/
切换到 kSar 窗口,点击 “Data” > “Load data from text file” > 从 /tmp/ 中选择 sar.data.txt> 点击 “Open” 按钮。
现在,图表类型树已经出现在左侧面板中并选定了一个图形:
Linux kSar Processes for server1
Linux Disk I/O Stats Using kSar
Linux Memory paging and its utilization stats
放大和缩小
通过移动你可以交互式缩放图像的一部分。在要缩放的图像的左上角点击并按下鼠标,移动到要缩放区域的右下角,可以选定要缩放的区域。返回到未缩放状态,点击并拖动鼠标到除了右下角外的任意位置,你也可以点击并选择 zoom 选项。
了解 kSar 图像和 sar 数据
我强烈建议你去阅读 sar 和 sadf 命令的 man 页面:
$ man sar $ man sadf
使用 sar 命令和 kSar 工具,可以得到内存、CPU、以及其它子系统的详细快照。例如,如果 CPU 使用率在一个很长的时间内持续高于 80%,有可能就是出现了一个 CPU 瓶颈。使用 sar -x ALL 你可以找到大量消耗 CPU 的进程。
mpstat 命令[6] 的输出(sysstat 包的一部分)也会帮你去了解 CPU 的使用率。但你可以使用 kSar 很容易地去分析这些信息。
Perform the following adjustments to the CPU:
- Make sure there are no unnecessary processes running in the background. Turn off all unnecessary services on Linux[7].
- Use cron[8] Run a task (for example, backup) at an off-peak time.
- Use top and ps commands[9] to find all non-critical background jobs/services. Use the renice command [10] to adjust low priority jobs.
- Use the taskset command to set the CPU used by the process [11] (offload the CPU used), that is, bind the process to a different CPU. For example, running MySQL database on CPU #2 and Apache on CPU #3.
- Make sure your system is using the latest drivers and firmware.
- Add additional CPUs to the system if possible.
- Use a faster CPU for single-threaded applications (e.g., Lighttpd web server application).
- Use multiple CPUs for multi-threaded applications (e.g., MySQL database server applications).
- Use multiple compute nodes and set up a load balancer for a web application[12].
isag command graphically displays system activity data stored in a binary file from previous runs of the sar command. The isag command references sar and extracts its data to plot the graph. isag has fewer options compared to kSar.
Fig.06: isag CPU utilization graphs
The above is the detailed content of Use the sar tool to detect system performance bottlenecks. For more information, please follow other related articles on the PHP Chinese website!

The Internet does not rely on a single operating system, but Linux plays an important role in it. Linux is widely used in servers and network devices and is popular for its stability, security and scalability.

The core of the Linux operating system is its command line interface, which can perform various operations through the command line. 1. File and directory operations use ls, cd, mkdir, rm and other commands to manage files and directories. 2. User and permission management ensures system security and resource allocation through useradd, passwd, chmod and other commands. 3. Process management uses ps, kill and other commands to monitor and control system processes. 4. Network operations include ping, ifconfig, ssh and other commands to configure and manage network connections. 5. System monitoring and maintenance use commands such as top, df, du to understand the system's operating status and resource usage.

Introduction Linux is a powerful operating system favored by developers, system administrators, and power users due to its flexibility and efficiency. However, frequently using long and complex commands can be tedious and er

Linux is suitable for servers, development environments, and embedded systems. 1. As a server operating system, Linux is stable and efficient, and is often used to deploy high-concurrency applications. 2. As a development environment, Linux provides efficient command line tools and package management systems to improve development efficiency. 3. In embedded systems, Linux is lightweight and customizable, suitable for environments with limited resources.

Introduction: Securing the Digital Frontier with Linux-Based Ethical Hacking In our increasingly interconnected world, cybersecurity is paramount. Ethical hacking and penetration testing are vital for proactively identifying and mitigating vulnerabi

The methods for basic Linux learning from scratch include: 1. Understand the file system and command line interface, 2. Master basic commands such as ls, cd, mkdir, 3. Learn file operations, such as creating and editing files, 4. Explore advanced usage such as pipelines and grep commands, 5. Master debugging skills and performance optimization, 6. Continuously improve skills through practice and exploration.

Linux is widely used in servers, embedded systems and desktop environments. 1) In the server field, Linux has become an ideal choice for hosting websites, databases and applications due to its stability and security. 2) In embedded systems, Linux is popular for its high customization and efficiency. 3) In the desktop environment, Linux provides a variety of desktop environments to meet the needs of different users.

The disadvantages of Linux include user experience, software compatibility, hardware support, and learning curve. 1. The user experience is not as friendly as Windows or macOS, and it relies on the command line interface. 2. The software compatibility is not as good as other systems and lacks native versions of many commercial software. 3. Hardware support is not as comprehensive as Windows, and drivers may be compiled manually. 4. The learning curve is steep, and mastering command line operations requires time and patience.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version
Visual web development tools

Atom editor mac version download
The most popular open source editor