2024-01-25
Before we begin, ensure sysstat
is installed on your system. For Debian/Ubuntu systems, use:
sudo apt-get update
sudo apt-get install sysstat
For Red Hat/CentOS/Fedora systems, the command is:
sudo yum update
sudo yum install sysstat
Once installed, sysstat
primarily operates through two core tools: sar
(System Activity Reporter) and sadc
(System Activity Data Collector).
sadc
– The Data Collectorsadc
is the unsung hero, quietly collecting performance data in the background. By default, it collects data every 10 minutes and stores it in /var/log/sa/
. You can customize this behavior. For instance, to collect data every 5 minutes and store it in a different directory:
sudo sadc -d /var/log/my_sa -i 300
This command specifies a 5-minute interval (-i 300
seconds) and the custom directory (-d /var/log/my_sa
). Remember to create the directory beforehand:
sudo mkdir -p /var/log/my_sa
You can also specify the types of data to collect using various options. Refer to the man sadc
page for a complete list.
sar
– The Data Analyzersar
is where the magic happens. It reads the data collected by sadc
and presents it in a user-friendly format. Let’s look at some common sar
commands:
1. CPU Utilization: This command shows CPU usage over time:
sar -u
This will display CPU usage statistics, including user, system, idle, and I/O wait times. To view data for a specific time range (e.g., the last hour):
sar -u -f /var/log/sa/sa16 # Replace sa16 with the appropriate file.
2. Memory Usage: Monitor memory usage with:
sar -r
This displays information on memory usage, including free memory, buffered memory, and cached memory. Similar to CPU usage, you can specify a time range using the -f
option.
3. I/O Statistics: Analyze disk I/O performance with:
sar -b
This shows block device statistics, including transfer rates and average queue lengths. You can further specify the device using the -d
option (e.g., sar -b -d sda
).
4. Network Statistics: Monitor network interface activity using:
sar -n DEV
This displays network interface statistics, such as received and transmitted packets and bytes. Replace DEV
with the specific interface name (e.g., eth0
, wlan0
). For network protocols, use sar -n EDEV
.
5. Customizing Output: You can tailor sar
’s output. For example, to display only CPU utilization and memory usage in a concise format, use:
sar -u -r -f /var/log/sa/sa16 | head -n 20
These examples demonstrate only a fraction of sar
’s capabilities. Exploring the man sar
page will allow you to fine-tune your performance monitoring strategies. By leveraging the data gathered by sadc
and analyzed by sar
, you can effectively identify and address potential performance issues, ensuring the optimal functioning of your Linux systems.