Linux System Activity Reporter (SAR) Command Tutorial With Examples
Monitoring Linux servers is important part of the system administration. There are a lot of way and tools to monitor Linux servers. System Activity Reporter or simply sar is one of the most popular and useful tool. Sar can save system stats from various subsystems like disk, CPU, memory, network etc. to file and used to review historical performance metrics. There is also different tools to provide graphical presentation from Sar stats. Sar saves systems stats in specified intervals. We will look them in details in this tutorial.
Sar is provided by
sysstat tool and can be installed from official repositories various distributions.
$ sudo apt install sysstat
sar [options] [interval] [count]
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 metric to track in monitoring purposes is CPU usage. We will provide
-u options for CPU stats. Also numbers
1 specifies time interval and
3 count which is how many time stats will be printed.
$ sar -u 1 3
In the example CPU column provides information that all CPU’s metrics are provided. There is also time information for the performance metrics.
%usercolumn is used to show user side load
%systemcolumns is used to show system related load
%idlecolumn shows how many CPU resource is do not used. In this case load is very low and idle value is range of %97
Show Specific All CPU Stats
In the previous example we have looked some stats of the CPU those are popular and most useful. But in some situations this can not be enough and we may need more stat about CPU like
iowait . This 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
Show Specific CPU Stats
As we know modern CPU’s provides more than one core for more performance. While providing CPU stats by default all cores metrics are provided. But there is
-P option which will filter and provide only specified core metrics like below. In this example we want to get second core information. Keep in mind that core index starts from 0 which means first core is numbered as 0 .
$ sar -P 1 1 3
We can see from screenshot that first core is specified with
-P 1 and other parameters like interval and count are provided too. If we look 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 memory. Memory performance can be monitored with the
$ sar -r 1 3
We can see a lot of information about the memory is provided.
kbmemfreecolumn shows free memory as kilobyte
kbmemusedcolumn shows used memory as kilobyte
%memusedcolumns shows percentage of used memory
Show Swap Stats
Swap is a memory increment mechanisms used years ago. Currently system administrator do not prefer this method because of the memory prices a low and the swap performance is not good. Swap stats can be get with
$ sar -S 1 3
As we can see from output there is no swap usage in this Linux server.
kbswpfreecolumns shows free swap size in kilobyte
kbswpusedcolumns shows used swap size in kilobyte
%swpusedcolumn shows used swap size as 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
tpscolumn shows transactions per second count
rtpdcolumn show read transactions per second count
wtpscolumn shows write transactions per second count
bread/scolumn shows byte read second count
wread/scolumn shows byte write 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
$ sar -d 1 3
DEVcolumn show the devices minor and major numbers like dev252-0
tpscolumn shows transactions per second count
rd_sec/scolumn shows read operation done in a second
we_sec/scolumn shows write operation done in a second
%utilcolumn show total utilization percentage
Show Disk Names
In 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
$ sar -p -d 1 3
Show Context Switch Stats
As we know processes do not all ways run in their life time. Some times they sleep or wait some io. This status change is named as context switching . Information about context switching can be get with
$ sar -w 1 3
Show Network Statistics
Network statistics can be printed with 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
IFACEcolumns shows related interface name
rxpck/scolumns shows received packets count
txpck/scolumn shows transmitted packets count
rxkB/scolumn shows received packets sizes
txkB/scolumn shows transmitted packets sizes
%ifutilcolumn show interface usage or load percentage
We have provided
-n DEV to get device related stats. But there is other subsystems other than DEV to get information. Here are some of them
IPdisplays IP statistics
TCPdisplays TCP statistics
UDPdisplays UDP statistics
ALLdisplays 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 have 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 save sar data into a file. One of the sar usage scenario 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