Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What Does The sar Command Do?
3.
What is sar command linux monitor system performance?
4.
Installing the sysstat Package
5.
Real-Time Statistics With sar
6.
Exploring CPU & I/O Statistics with sar
7.
Memory & Network Monitoring with sar
8.
Automating & Analyzing sar Reports
9.
Frequently Asked Questions
9.1.
Can sar monitor specific processes?
9.2.
How long does sar keep historical data?
9.3.
Is sar available on all Linux distributions?
10.
Conclusion
Last Updated: Mar 27, 2024
Medium

Sar Command in Linux

Roadmap to SDE career at Amazon
Speaker
Anubhav Sinha
SDE-2 @
25 Jun, 2024 @ 01:30 PM

Introduction

In the ever-evolving landscape of Linux system administration, the sar command emerges as a pivotal tool for monitoring system performance. This article aims to unravel the intricacies of sar, a utility that's crucial yet often overlooked by budding Linux enthusiasts. 

Sar Command in Linux

By the end of this piece, you'll have a firm grasp of what sar is, how to install it, and how to use it effectively to gather real-time system statistics.

What Does The sar Command Do?

The sar command stands for "System Activity Report." It's a powerful utility in Linux that provides a wealth of information about system activity. This includes CPU usage, memory usage, I/O transfer rates, and much more. It's part of the sysstat package and is essential for system diagnostics and performance tuning.

Understanding the sar command involves recognizing its ability to report historical data, helping in pinpointing the times when the system might have experienced performance issues. This retrospective analysis is invaluable for both learning and practical application in the real world of system administration.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

What is sar command linux monitor system performance?

The sar command monitors system performance in Linux by collecting, reporting, and saving system activity information. This utility allows you to track your system's health over time, offering insights into how various subsystems perform under different workloads.

For example, you can use sar to monitor CPU activity to identify times of high usage, or to keep an eye on memory consumption. This level of detail is crucial for understanding how your system behaves and for making informed decisions about system configuration and optimization.

Installing the sysstat Package

Before diving into the sar command, you need to install the sysstat package, which contains it. For most Linux distributions, sysstat isn't pre-installed. You can easily install it using your package manager. For instance, on Ubuntu or Debian-based systems, you would use:

sudo apt-get update
sudo apt-get install sysstat


Once installed, you can enable data collection for sar by editing the sysstat configuration file and restarting the service. This setup ensures that sar starts collecting data immediately, providing a historical perspective of your system’s performance.

Real-Time Statistics With sar

One of the most compelling features of the sar command is its ability to provide real-time statistics. This means you can observe the current performance of your system as it happens. For instance, to view real-time CPU usage, you would execute:

sar -u 1 10


This command displays CPU statistics every second for 10 seconds. Each line of output shows the percentage of CPU time spent in user mode, system mode, and idle time. This real-time monitoring is invaluable for detecting immediate performance issues and for learning how different processes affect system performance.

Exploring CPU & I/O Statistics with sar

A critical aspect of system monitoring is understanding CPU and I/O statistics, and sar provides detailed insights into these areas. By using the sar command, you can analyze how your CPU is being utilized - whether it's being overwhelmed by user processes or system processes. Similarly, for I/O statistics, sar helps in identifying bottlenecks in disk usage.

For CPU usage, the command sar -u provides a comprehensive view. It shows user time, system time, and idle time. For example:

sar -u 1 3


This command reports CPU statistics at one-second intervals, repeated three times. Each output line will display the percentage of time the CPU spends in different states.

On the I/O side, sar -d and sar -b are invaluable. sar -d reports activity for each block device, helping identify intensive disk operations, while sar -b shows the I/O transfer rate, essential for understanding the overall throughput of your system.

Memory & Network Monitoring with sar

Memory and network monitoring are also crucial for maintaining a healthy system. The sar command aids in tracking memory usage (including swap space) and network activity, ensuring you're aware of any potential issues.

To monitor memory usage, use sar -r, which provides details about free and used memory, as well as swap space usage. For network monitoring, sar -n DEV is the go-to option. It provides statistics on network devices, showing data like the number of bytes and packets transmitted and received.

Automating & Analyzing sar Reports

One of the strengths of sar is its ability to automate the collection of system data. This can be configured to run at regular intervals using cron jobs, ensuring a continuous stream of data for analysis.

Moreover, analyzing sar reports is critical for long-term system health. By regularly reviewing sar data, you can identify trends, spot anomalies, and make informed decisions to optimize system performance.

Frequently Asked Questions

Can sar monitor specific processes?

While sar is excellent for system-wide statistics, it doesn't monitor individual processes. For process-specific monitoring, tools like top or htop are more appropriate.

How long does sar keep historical data?

This depends on your configuration. By default, sar logs are rotated and kept for a certain period, usually 7-30 days, but this can be adjusted in the sysstat configuration.

Is sar available on all Linux distributions?

sar, part of the sysstat package, is available in the repositories of most major Linux distributions, but it may need to be installed manually.

Conclusion

The sar command is an indispensable tool for Linux users, especially for those involved in system administration and performance tuning. Its ability to provide detailed statistics about various system components like CPU, memory, I/O, and network makes it a versatile utility. Understanding and effectively using sar can lead to significant improvements in system performance and stability, a vital skill for any aspiring Linux professional.

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Previous article
Creating RAID 5 (Striping with Distributed Parity) in Linux
Next article
rm Command in Linux
Live masterclass