Linux System Activity Reporter (SAR) Command Tutorial With Examples – POFTUT

Linux System Activity Reporter (SAR) Command Tutorial With Examples


Monitoring Linux servers is important part of the system administration. There are a lot of ways and tools to monitor Linux servers. System Activity Reporter or simply sar is one of the most popular and useful tools. Sar can save system stats from various subsystems like disk, CPU, memory, network, etc. to file and used to review historical performance metrics. There are also different tools to provide graphical presentations from Sar stats. Sar saves systems stats in specified intervals. We will look at them in detail in this tutorial.

Install

Sar is provided by sysstat tool and can be installed from official repositories various distributions.

$ sudo apt install sysstat
Install Sar
Install Sar

Syntax

We will use the following syntax for sar command.

sar [options] [interval] [count]
Syntax
Syntax

Help

Simple and fast way to get help with sar command is providing -h option like below.

$ sar -h
Help
Help

Show CPU Stats

One of the most wanted server metrics to track in monitoring purposes is CPU usage. We will provide -u options for CPU stats. Also, numbers 1 specify a time interval and 3 count which is how many time stats will be printed.

$ sar -u 1 3
Show CPU Stats
Show CPU Stats

In the example, the CPU column provides information that all CPU’s metrics are provided. There is also time information for the performance metrics.

  • %user column is used to show user side load
  • %system columns are used to show system-related load
  • %idle column shows how many CPU resources are do not used. In this caseload is very low and idle value is the range of %97

Show Specific All CPU Stats

In the previous example, we have looked at some stats of the CPU that are popular and most useful. But in some situations, this can not be enough and we may need more stat about CPU like iowait . These stats can be printed with ALL option after -u like below.

$ sar -u ALL 1 3
Show Specific All CPU Stats
Show Specific All CPU Stats

In this example, we can see the extra columns like %iowaint %irq , %gnice .

Show Specific CPU Stats

As we know modern CPUs provide more than one core for more performance. While providing CPU stats by default all core metrics are provided. But there is -P option that will filter and provide only specified core metrics like below. In this example, we want to get second core information. Keep in mind that the core index starts from 0 which means the first core is numbered as 0.

$ sar -P 1 1 3
Show Specific CPU Stats
Show Specific CPU Stats

We can see from the screenshot that the first core is specified with -P 1 and other parameters like interval and count are provided too. If we look at the CPU column we can see that the core index number 1 is printed as we expected.

LEARN MORE  What Is DisplayPort?

Show Memory Stats

Another important performance metric for Linux servers is a memory. Memory performance can be monitored with the -r parameter.

$ sar -r 1 3
Show Memory Stats
Show Memory Stats

We can see a lot of information about the memory is provided.

  • kbmemfree column shows free memory as kilobyte
  • kbmemused column shows used memory as kilobyte
  • %memused columns show the percentage of used memory

Show Swap Stats

Swap is a memory increment mechanism used years ago. Currently, the system administrator does not prefer this method because the memory prices a low and the swap performance is not good. Swap stats can get with -S option.

$ sar -S 1 3
Show Swap Stats
Show Swap Stats

As we can see from output there is no swap usage in this Linux server.

  • kbswpfree column shows free swap size in kilobyte
  • kbswpused column shows used swap size in kilobyte
  • %swpused column shows used swap size as a percentage

Show I/O Stats

IO is another monitored metric for Linux systems.  IO parameter -b provides information about transactions per second, read and write transactions, etc.

$ sar -b 1 3
Show I/O Stats
Show I/O Stats
  • tps column shows transactions per the second count
  • rtpd column show read transactions per second count
  • wtps column shows write transactions per second count
  • bread/s column shows byte read the second count
  • wread/s column shows byte write the second count

Show Specific Disk I/O Stats

Block devices or disk IO stats can be gathered with sar command too. To get this information required option is -d .

$ sar -d 1 3
Show Specific Disk I/O Stats
Show Specific Disk I/O Stats
  • DEV column shows the devices minor and major numbers like dev252-0
  • tps column shows transactions per the second count
  • rd_sec/s column shows  read operation done in a second
  • we_sec/s column shows  write operation done in a second
  • %util column shows the total utilization percentage
LEARN MORE  How To Use vmstat To Monitor Linux Performance

Show Disk Names

In the previous example, we have listed the stats with related disk device major and minor numbers by default. This is not readable from human point of view if we have a lot of disks on the Linux system. -p option can be used to list disk devices with their native names like sda , vda etc.

$ sar -p -d 1 3
Show Disk Names
Show Disk Names

Show Context Switch Stats

As we know processes do not all ways run in their lifetime. Some times they sleep or wait for some io. This status change is named as context switching. Information about context switching can get with -w option.

$ sar -w 1 3
Show Context Switch Stats
Show Context Switch Stats

Show Network Statistics

Network statistics can be printed with the option -n . Also after the option, a keyword must be provided. In this example, we will print the device-related metrics.

$ sudo sar -n DEV 1 3

  • IFACE column shows the related interface name
  • rxpck/s column shows received packets count
  • txpck/s column shows transmitted packets count
  • rxkB/s column shows received packets sizes
  • txkB/s column shows transmitted packets sizes
  • %ifutil column show interface usage or load percentage

We have provided -n DEV to get device-related stats. But there are other subsystems other than DEV to get information. Here are some of them

  • IP displays IP statistics
  • TCP displays TCP statistics
  • UDP displays UDP statistics
  • ALL displays all statics from above and those we have not mentioned
$ sudo sar -n IP 1 3

Show Paging Stats

As we know Linux memory management has some mechanisms to effectively and reliable way manage the memory. Paging is used to manage the memory system.

$ sar -b 1 3
Show Paging Stats
Show Paging Stats

Write Sar Stats To A File

Up to now, we have printed collected statistics and stats into the terminal. But in some situations, we may need to save data and shot in a more shiny way or store historical data for inspection. -o option with a file name can be used to save collected information into a file.

$ sar -o sar-data -u 1 3
Write Sar Stats To A File
Write Sar Stats To A File

Show Historic Sar Stats From File

In the previous example, we have to save sar data into a file. One of the sar usage scenarios is saving this data and playing graphics according to this data. Sar can also read all ready saved data and show accordingly. -f with a data file name can be used for this purpose.

$ sar -f sar-data
Show Historic Sar Stats
Show Historic Sar Stats

Leave a Comment