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

Syntax
We will use the following syntax for sar
command.
sar [options] [interval] [count]

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

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

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

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

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.
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

We can see a lot of information about the memory is provided.
kbmemfree
column shows free memory as kilobytekbmemused
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

As we can see from output there is no swap usage in this Linux server.
kbswpfree
column shows free swap size in kilobytekbswpused
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

tps
column shows transactions per the second countrtpd
column show read transactions per second countwtps
column shows write transactions per second countbread/s
column shows byte read the second countwread/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

DEV
column shows the devices minor and major numbers like dev252-0tps
column shows transactions per the second countrd_sec/s
column shows read operation done in a secondwe_sec/s
column shows write operation done in a second%util
column shows the total utilization percentage
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 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 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 namerxpck/s
column shows received packets counttxpck/s
column shows transmitted packets countrxkB/s
column shows received packets sizestxkB/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 statisticsTCP
displays TCP statisticsUDP
displays UDP statisticsALL
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

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

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
