MC, 2025
Ilustracja do artykułu: Polecenie Linux sar: Monitoring and Optimizing Your System Performance

Polecenie Linux sar: Monitoring and Optimizing Your System Performance

Linux is well-known for its stability, flexibility, and ability to handle complex server environments with ease. One of the many tools that can help system administrators monitor and optimize performance is the "sar" command. If you’ve ever wondered how to keep track of your system’s health, identify performance issues, or understand how your machine behaves under stress, the “sar” command might just be what you need. In this article, we will dive into the "Polecenie linux sar", exploring its functionality, syntax, and some practical examples of how to use it effectively.

What is the "sar" Command?

In Linux, "sar" stands for System Activity Report, and it's part of the sysstat package. The "sar" command collects and reports information about various system resources such as CPU usage, memory, disk I/O, network activity, and more. It is especially helpful for gathering historical data about the system’s performance, as it can store data collected over time and allow you to generate reports for further analysis. Unlike real-time monitoring tools like "top" or "htop", "sar" provides a way to analyze system performance over a period, which is essential for identifying trends and long-term performance issues.

The great thing about "sar" is that it can be used not just for real-time monitoring but also for generating reports on previous activity. If you're experiencing performance issues on your Linux server, "sar" might be your go-to tool for investigating what happened in the past and finding potential causes.

How to Install and Use "sar"

Before using the "sar" command, make sure the sysstat package is installed on your system. On most Linux distributions, you can install it using your package manager. Here's how to do it on some popular Linux distributions:

  • On Debian-based systems (like Ubuntu):
  •   sudo apt-get install sysstat
      
  • On Red Hat-based systems (like CentOS or Fedora):
  •   sudo yum install sysstat
      
  • On Arch Linux:
  •   sudo pacman -S sysstat
      

Once installed, the "sar" command can be used directly from the command line. To get a list of available options and usage, simply run:

sar --help

Basic Usage of "sar"

Now that you have "sar" installed, let’s dive into the basic usage. "sar" can collect and report a variety of system performance statistics. Here's how you can start using it:

1. Viewing CPU Usage

One of the most common things administrators want to monitor is CPU usage. The "sar" command allows you to easily check CPU utilization. To view CPU usage statistics, simply use the following command:

sar -u 1 5

This command will display the CPU usage every 1 second, and it will repeat this 5 times. The output will look something like this:

Linux 4.15.0-66-generic (server)  05/28/2021  _x86_64_    (4 CPU)

12:00:01 AM     CPU     %user     %system   %iowait   %steal   %idle
12:00:02 AM     all     5.00      2.00      1.00      0.00      92.00
12:00:03 AM     all     3.00      1.00      2.00      0.00      94.00
12:00:04 AM     all     4.00      2.00      1.00      0.00      93.00
12:00:05 AM     all     2.00      1.00      1.00      0.00      96.00

The columns show the percentage of CPU usage for user processes, system processes, I/O wait, steal time, and idle time. This data helps you identify if your system’s CPU is overworked or if it has plenty of idle time.

2. Viewing Memory Usage

Another key resource to monitor is memory. The "sar" command can show how much memory is being used and how much is available. To check memory usage, you can use:

sar -r 1 5

This command will show memory statistics every second for 5 repetitions. The output will look something like this:

Linux 4.15.0-66-generic (server)  05/28/2021  _x86_64_    (4 CPU)

12:00:01 AM kbmemfree  kbmemused  %memused  kbbuffers  kbcached
12:00:02 AM  999992   2099056    67.80     123456    654321
12:00:03 AM  999881   2099157    67.81     123457    654322
12:00:04 AM  999773   2099250    67.82     123458    654323
12:00:05 AM  999660   2099345    67.83     123459    654324

In this example, "kbmemfree" shows the amount of free memory, while "kbmemused" shows the amount of used memory, and "%memused" shows the percentage of memory usage. The "kbbuffers" and "kbcached" columns provide additional information about memory buffers and cached data.

3. Viewing Disk I/O Statistics

Monitoring disk performance is also crucial to ensure your system isn’t running into I/O bottlenecks. The "sar" command provides disk I/O statistics with the following command:

sar -d 1 5

This will display disk statistics every second for 5 iterations. The output will show information about read and write operations, as well as the average time for these operations:

Linux 4.15.0-66-generic (server)  05/28/2021  _x86_64_    (4 CPU)

12:00:01 AM  DEV  tps   rd_sec/s  wr_sec/s  avgrq-sz  avgqu-sz  await  svctm  %util
12:00:02 AM  sda  10    1024.00   2048.00   300.00    0.10      5.00   1.00   10.00
12:00:03 AM  sda  12    1024.50   2048.50   305.00    0.12      5.20   1.10   12.00

The columns show the number of transfers per second (tps), read and write sectors per second, average request size, average queue size, average wait time (await), service time (svctm), and the percentage of time the device is busy (%util). These metrics help you monitor disk performance and troubleshoot any slowdowns.

4. Viewing Network Statistics

Network performance is another critical area for any Linux system. You can monitor network traffic with the following command:

sar -n DEV 1 5

This will display network statistics every second for 5 iterations. The output will show information such as the number of packets transmitted and received:

Linux 4.15.0-66-generic (server)  05/28/2021  _x86_64_    (4 CPU)

12:00:01 AM IFACE   rxpck/s  txpck/s  rxkB/s  txkB/s  %ifutil
12:00:02 AM eth0    1000     2000     1024    2048    15.00
12:00:03 AM eth0    1100     2200     1152    2304    16.50

In this example, the columns display the number of packets received and transmitted per second (rxpck/s and txpck/s), the number of kilobytes received and transmitted per second (rxkB/s and txkB/s), and the interface utilization percentage (%ifutil). This data is useful for understanding network traffic and identifying potential bottlenecks.

Conclusion

In this article, we explored the powerful "Polecenie linux sar" and its ability to help system administrators monitor and analyze various system resources. The "sar" command is invaluable for obtaining historical system performance data, identifying trends, and troubleshooting potential issues. By using the examples provided in this article, you can start using "sar" to monitor your own system’s CPU, memory, disk, and network performance.

Komentarze (0) - Nikt jeszcze nie komentował - bądź pierwszy!

Imię:
Treść: