Set-up-Sar-command-to-monitor-CPU-RAM-Disk-on-VPS-Linux

When managing a Linux VPS, monitoring system resources such as CPU, RAM, Disk and network monitoring is extremely important. This monitoring ensures the server operates stably and efficiently. One of the useful tools to help you track these resources is the sar command. In this article, we will learn how to use the sar command to monitor system resources on a Linux VPS.

What is the Sar Order?

The sar (System Activity Report) command is a powerful tool used on Linux systems to collect and display reports on system performance. This command can monitor many important system factors, including CPU, RAM, Disk, and Network. Sar not only helps you monitor current resource status, but also helps store past performance data for trend analysis.

Main features of Sar command

  • CPU Monitor: Sar helps you check CPU usage. Includes system and user processing times, as well as the percentage of CPU idle time.
  • Monitor memory (RAM): You can use sar to monitor the amount of used and free memory on the system.
  • Disk Monitor: Sar provides detailed information about drive activity. Includes read and write data counts, wait time, and parameters related to drive performance.
  • Network Monitoring: The sar command also supports network activity monitoring. Includes traffic sent and received across the system’s network interfaces.

Benefits of Sar command

  • System Resource Monitoring: Sar helps monitor system performance in real-time or monitor history to detect problems.
  • Long-term performance analysis: Sar can be used to store and analyze performance data from days or weeks ago. Helps detect potential trends or problems.
  • Better Linux VPS Management: With sar, monitoring and optimizing VPS resources becomes easy. Avoid overload or instability.

The sar command is a powerful tool that helps system administrators maintain stable performance for  server and Linux VPS. At the same time, provides insight into the status of system resources.

Install Sar command on Linux VPS

To use the sar command, you need to install the sysstat package (containing the sar command) on your Linux VPS. Here’s how to install on some popular distributions:

On Ubuntu/Debian:

apt update sudo

sudo apt install sysstat

On CentOS/RHEL:

yum install sysstat

Once installed, you can use the sar command immediately.

How to use Sar command to monitor CPU, RAM, Disk.

Here’s how to use sar to monitor critical system resources on a Linux VPS:

CPU Monitoring:

To monitor CPU usage, use the following command:

sar -u 1 5

In there:

  • u: Monitor CPU (CPU usage).
  • 1: Limit the measurement time per second.
  • 5: Number of times to repeat the report (5 times).

The result of this command will display CPU usage through parameters:

  • %user: CPU usage by user processes.
  • %system: CPU usage by system processes.
  • %idle: Percentage of CPU time idle (idle).

Memory Monitor (RAM):

To monitor RAM memory status, use the command:

sar -r 1 5

In there:

  •  -r: Monitor RAM memory.
  • 1: Measure every second.
  • 5: Repeat 5 times.

This command will provide information about total memory, used memory and free memory in the system.

Monitor Disk (Drive):

To monitor disk usage status, use the command:

sar -d 1 5

In there:

  •  -d: Monitor disk activity.
  • 1: Measure every second.
  • 5: Repeat 5 times.

This command will display information about disk activity. Includes read/write counts per drive and their usage rates.

For example, run the Sar command on an Ubuntu 20.04 VPS and read the results

To better understand how to use the Sar command on a Linux VPS, this article details how to use the Sar command on an USA VPS Ubuntu 20.04  and analyze the results after running the command.

apt update sudo

sudo apt install sysstat

Monitor CPU

sar -u 1 5

The result received after running the command is as shown in the image below.

CPU-monitoring-with-Sar-command

Result:

  1. %user: CPU time spent on user tasks (user processes). Here it is 0.00, meaning no user processes are consuming CPU.
  2. %nice: CPU time spent on low priority tasks (nice processes). A value of 0.00 indicates that no nice processes are running.
  3. %system: CPU time spent on system tasks (kernel processes). Value 0.00, means the system is not using CPU resources.
  4. %iowait: CPU time waiting for I/O (read/write from disk, device). A value of 0.00 means there is no I/O congestion.
  5. %steal: CPU time “stolen” by the hypervisor (in a virtualized environment). A value of 0.00, means no resources are affected.
  6. %idle: Idle CPU time. Value 99.80 – CPU is hardly used, is in idle state.

Conclude: The machine’s CPU is almost completely idle (%idle is very high). There are no processes consuming significant CPU resources at the time of monitoring.

Monitor RAM

sar -r 1 5

The result received after running the command is as shown in the image below.

Track-RAM-with-Sar-command

Columns in the results:

  • kbmemfree: Free RAM memory, measured in kilobytes (kB).

Example: 180736 (approximately 176MB of free memory).

  • kbavail: Available memory that can be used by processes without requiring swapping or freeing cache.

Example: 457992 (approximately 447MB available).

  • kbmemused: Used RAM (total memory minus kbmemfree), measured in kilobytes.

Example: 218548 (approximately 213MB of used memory).

  • %memused: The percentage of used memory relative to total RAM.

Example: 24.41% (about 1/4 of total RAM is in use).

  • kbbuffers: Memory used for buffers (temporary storage before writing to disk).

Example: 49968 (approximately 48MB used as buffer).

  • kbcached: Memory used as cache (storing temporary data for faster access).

Example: 330228 (approximately 322MB used for cache).

  • kbcommit: Memory that has been “committed” for processes (including physical RAM and swap).

Example: 513104 (approximately 501MB committed).

  • %commit: Ratio of committed memory to total available memory.

Example: 18.78% (less than 1/5 of memory has been committed).

  • kbactive: Active memory

Example: 263348 (approximately 257MB active).

Conclude:

  •  Your machine is using 24.41% RAM (physical RAM) in total.
  • Free memory is about 176MB, and available memory is about 447MB.
  • The system has many caches and buffers to increase access speed, with a cache of about 322MB and a buffer of about 48MB.
  • Low memory usage, no signs of RAM shortage

Monitor Disk (Drive):

sar -d 1 5

The result received after running the command is as shown in the image below.

Monitor-Disk-using-Sar-command

The results from the command sar -d 1 5 is used to monitor the activity of disk devices at 1 second intervals and repeat 5 times. Detailed analysis is as follows:

Columns in the results:

  • DEV: Device name (e.g., dev7-0, dev7-1).
  • tps (transactions per second): The number of I/O requests sent to the device per second.
  • rkB/s (read KB per second): Data read speed from the device (KB/s).
  • wkB/s (write KB per second): Data write speed to the device (KB/s).
  • dkB/s (discard KB per second): Amount of discarded data (KB/s).
  • areq-sz (average request size): Average size of each I/O request (KB).
  • aqu-sz (average queue size): Average size of the I/O queue.
  • await (average wait time): Average time an I/O request waits to complete (ms).
  • %util (percent utilization): The percentage of time the device is busy performing I/O operations..

Analysis:

  • All values are 0: This indicates that there is no read/write activity or any other operations on the disk devices during the observed period (5 seconds).
  • %util = 0.00%: The device is completely idle and not in use.
  • aqu-sz = 0.00 and await = 0.00: No I/O requests are being processed or waiting in the queue.

Comment:

If this is a normal state, the system may not have any tasks requiring disk access at that time.

If you expect disk activity, this result may suggest that the application is not performing I/O access or that there is an error in the way it checks.

The sar command is a powerful and useful tool for monitoring system performance on a Linux VPS. Helps you monitor CPU, RAM, and Disk, quickly detect performance issues and optimize system resources to ensure your VPS operates stably. If you are managing a Linux VPS, don’t skip installing and using sar to maintain the best performance for your system.