sar Command: Tutorial & Examples
Monitoring performance
The sar
command, also known as the System Activity Reporter, is an incredibly useful tool when
it comes to monitoring the performance of a Linux server. It comes from the sysstat package and provides a comprehensive
view of your system's activity. It works by collecting, reporting, and saving system activity information for later
analysis.
Understanding sar
is crucial for server administrators because it helps to identify any
potential high load issues and allows them to tune their servers accordingly for optimal
performance.
How Does sar
Work?
The sar
command collects and displays data by reading binary data files in
the /var/log/sa
directory. These files are created by the sadc (system activity data
collector) utility, which is a part of the sysstat package.
The data collected includes CPU activity, memory/paging, interrupts, device load, network and more. The command is typically used in combination with cron jobs to gather data throughout the day.
Typical Problems Solved by sar
The sar
command can help solve a variety of performance-related issues on a Linux server. For instance, it can be used
to identify high CPU usage over time, observe trends
in memory usage, or diagnose disk I/O bottlenecks. An administrator can then use this
data to optimize server performance and troubleshoot issues such as slow servers or system crashes.
Examples of Using sar
Command
Here are a few examples of how you can use the sar
command:
1. Display CPU Usage
To display the CPU usage, you can use the sar
command followed by the interval and the number of times you want the
data to be updated. For instance, if you want to display CPU usage every 2 seconds for a total of 3 times, the command
would be:
sar 2 3
The output might look something like this:
Linux 4.15.0-54-generic (ubuntu) Thursday 13 June 2019 _x86_64_ (2 CPU)
10:51:51 CPU %user %nice %system %iowait %steal %idle
10:51:53 all 0.50 0.00 0.50 0.00 0.00 99.00
10:51:55 all 0.50 0.00 0.50 0.00 0.00 99.00
Average: all 0.50 0.00 0.50 0.00 0.00 99.00
2. Display Memory Usage
To display memory usage, you can use the -r
option. For instance:
sar -r 2 3
The output might look like this:
10:51:51 kbmemfree kbmemused %memused kbbuffers kbcached kbcommit %commit kbactive kbinact
10:51:53 1023840 3004172 74.57 123440 2518752 23060 0.56 1620016 1141204
10:51:55 1023840 3004172 74.57 123440 2518752 23060 0.56 1620016 1141204
Average: 1023840 3004172 74.57 123440 2518752 23060 0.56 1620016 1141204
Conclusion
The sar
command is a powerful tool for monitoring system performance and diagnosing issues in a Linux server
environment. By understanding how to use it effectively, you can ensure your server is running optimally and
troubleshoot any issues that may arise.