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.
Sar is provided by
sysstat tool and can be installed from official repositories various distributions.
$ sudo apt install sysstat
We will use the following syntax for
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 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.
%usercolumn is used to show user side load
%systemcolumns are used to show system-related load
%idlecolumn 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
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
$ 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 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
$ sar -S 1 3
As we can see from output there is no swap usage in this Linux server.
kbswpfreecolumn shows free swap size in kilobyte
kbswpusedcolumn shows used swap size in kilobyte
%swpusedcolumn 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
tpscolumn shows transactions per the second count
rtpdcolumn show read transactions per second count
wtpscolumn shows write transactions per second count
bread/scolumn shows byte read the second count
wread/scolumn 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
$ sar -d 1 3
DEVcolumn shows the devices minor and major numbers like dev252-0
tpscolumn shows transactions per the second count
rd_sec/scolumn shows read operation done in a second
we_sec/scolumn shows write operation done in a second
%utilcolumn 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
$ 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
$ 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
IFACEcolumn shows the related interface name
rxpck/scolumn 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 are 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 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