Mastering Linux sar: Practical CPU and Memory Monitoring

Overview

Maintaining production server reliability hinges on two operational pillars: tracking system metrics as they unfold in real time and auditing historical performance trends to uncover recurring issues. Within the venerable sysstat suite, the System Activity Reporter (sar) stands out as an indispensable diagnostic utility. This hands-on tutorial walks through setting up the daemon, inspecting live CPU, RAM, disk I/O, and interface metrics, as well as dumping and replaying binary audit logs for post-mortem investigations.

Installing sysstat and Activating the Background Data Collector

Before querying any telemetry data, install the underlying package containing the toolset:

Installing sysstat on Ubuntu using apt-get

Debian-based distributions leave background activity recording turned off out of the box. Modify /etc/default/sysstat so the collection engine boots automatically:

Enabling automatic data collection in sysstat default configuration

With configuration changes in place, launch the system daemon right away and register it with systemd to persist across reboots:

Starting and enabling the sysstat systemd unit

Verify that the binary functions properly and inspect its deployed version string.
⚙️ [Core Command Flags]

-u : Surfaces processor utilization breakdowns across execution spaces.
-r : Exposes physical memory consumption and operational swap levels.
-b : Reports overall I/O rates and physical transfer counters.
-d : Breaks down activity profiles by individual block devices.
-n : Emits networking throughput statistics.
(Keyword targets include DEV, EDEV, among others)
-q : Displays processor run-queue sizing along with load averages.
-f : Extracts recorded statistical sets from an existing binary archive.
-o : Directs output samples into a persistent binary file.

Checking installed sar binary version

Tracking Real-Time Processor Activity and System Load (sar -u, sar -q)

To obtain an instantaneous snapshot of overall processor execution, execute sar -u 1 3.
This invocation captures three consecutive samples spaced by one second, breaking down user-space execution (%user), kernel-level handling (%system), storage wait latency (%iowait), and available capacity (%idle) alongside an aggregate summary:

Inspecting real-time CPU utilization with sar -u

When pinpointing single-thread starvation or unbalanced workloads, run sar -P ALL 1 2 to audit every core separately.
Evaluating individual logical processing units (such as cores 0 through 5) over two 1-second intervals lets you uncover threads pinned to a single execution engine:

Monitoring per-core CPU utilization via sar -P ALL

To assess thread scheduling backlogs and overall cluster pressure, employ sar -q 1 3.
Every second, the tool polls currently queued threads awaiting processor time (runq-sz), active process counts (plist-sz), and standard 1-, 5-, and 15-minute operational load benchmarks (ldavg-1, ldavg-5, ldavg-15):

Auditing run queue length and load averages with sar -q

Evaluating Physical RAM Footprint and Swap Pressure (sar -r, sar -S)

A thorough assessment of host memory allocation can be initiated using sar -r 1 3.
Over three sampling passes, this inspects unallocated RAM (kbmemfree), truly allocatable memory (kbavail), proportional utilization (%memused), kernel filesystem cache buffers (kbbuffers, kbcached), and the total memory commitment ratio (%commit) to highlight impending memory exhaustion:

Reviewing physical RAM allocation and buffer pools with sar -r

When investigating page-thrashing and secondary backing storage behavior, issue sar -S 1 3.
Gathering three iterative metrics provides free swap storage (kbswpfree), engaged swap blocks (kbswpused), and the net swap consumption ratio (%swpused), pinpointing performance degradation driven by insufficient RAM:

Tracking swap space consumption using sar -S

Gauging Block Storage I/O and Network Throughput (sar -b, sar -n)

Inspect overall storage subsystem throughput by invoking sar -b 1 3.
By recording three sequential data points, you can review total transfer transactions (tps), read operations (rtps), write commits (wtps), and block generation throughput (bwrtn/s) to identify storage tier saturation:

Evaluating storage subsystem throughput and TPS with sar -b

To track packets traversing physical and virtual interfaces, run sar -n DEV 1 3.
This yields per-second telemetry across both the loopback device (lo) and primary adapter (eth0), detailing packet flow rates (rxpck/s, txpck/s), data transfer volumes (rxkB/s, txkB/s), and effective adapter utilization (%ifutil) to detect saturated links:

Auditing real-time network adapter bandwidth with sar -n DEV

Preserving Metrics to Binary Archives and Replaying Logs (sar -o, sar -f)

Set up a dedicated target folder to store operational metric snapshots by running mkdir -p ~/sar_reports:

Creating a dedicated metrics report storage folder

To capture live workload events directly into a binary archive for forensic inspection, run sar -o ~/sar_reports/system_stats.dat 1 3.
This stream simultaneously renders three 1-second CPU status lines to standard output while logging raw counters to ~/sar_reports/system_stats.dat:

Capturing live diagnostic output into a binary log file using sar -o

To re-examine processor activity from that saved session, load the log using sar -u -f ~/sar_reports/system_stats.dat.
Supplying the -f flag points the tool at your binary dump, while -u filters out everything except recorded processor allocations across user (%user), kernel (%system), and wait states (%iowait):

Extracting recorded historical CPU statistics with sar -u -f

Similarly, query the exact same archive for memory behavior by executing sar -r -f ~/sar_reports/system_stats.dat.
The utility extracts recorded memory attributes—including free capacity (kbmemfree), available buffers (kbavail), and consumption percentage (%memused)—allowing you to reconstruct resource pressure during that snapshot window:

Replaying historical RAM utilization statistics with sar -r -f

This walkthrough demonstrated how Linux administrators can leverage sar to monitor compute and memory resources in real time and preserve critical metric streams for offline post-mortems. When diagnosing production bottlenecks, combining these parameters gives you complete visibility into system state and performance recovery.