10 tools to Monitor Linux Server Resource Usage and Statistics
Maintaining a Linux server’s performance requires continuous resource usage and statistics monitoring. Resources here mean memory, CPU, disk, network usage, etc. You must consider these aspects to ensure your server runs smoothly.
So, Linux monitoring tools can give you valuable insights into your server activities. They provide important information such as processes using the most CPU or memory availability.
Monitoring tools can help you spot any problems early. You can keep a Linux server healthy and efficient with the essential tools discussed in this article.
What are the Prerequisites Before You Start?
You need a properly set up Linux server. So, first, deploy a Linux server on Cantech or any other cloud service to test monitoring tools. Log in using SSH, as your server is up and running. You must use a non-root user with sudo privileges for security reasons. Next, update your system so that all the required tools can be installed without any issues.
Checking GPU Usage with NVIDIA-SMI
For a Linux server running on a GPU, you must monitor its usage for better performance. NVIDIA provides a command-line tool called nvidia-smi that helps track GPU activity. It comes together with NVIDIA drivers, and you can check VRAM usage and GPU load using it.
Below are the different commands for various purposes –
=> To see GPU usage details –
$ nvidia-smi
=> Make this more dynamic with the watch command. It refreshes the data every few seconds to track real-time usage. For example, the below command will refresh the output every five seconds; you need not re-enter the command. To stop the process, press Ctrl + C.
$ watch -n 5 nvidia-smi
The official NVIDIA documentation can give you additional options and configurations.
Checking Disk Space with the DF Command
Monitoring disk usage ensures prevention and solutions for storage-related problems. Linux has a built-in tool called df that helps you to track the availability and usage of disk space on different storage devices on your server.
Below are different commands in df –
=> Check your server’s current disk space and storage with the below command.
$ df
It tells you the storage device name in the “Filesystem” section. For instance, the main server storage is often listed as `/dev/vda2`, while the boot volume appears as `/dev/vda1`. Other volumes like `/dev/vda3` or `/dev/vda4` may be secondary partitions or external storage.
The command also shows the storage size in 1-kilobyte blocks under “1K-blocks.” The “Type” section displays the filesystem format. “Size” shows the total space of the storage in MB or GB, and “Used” shows how much space is already used.
It also tells you how much space is still available under “Available.” “Use%” shows the percentage of space used. Finally, “Mounted on” shows where the volume is mounted in the system, like `/` for the root directory.
However, the default output is in kilobytes (it is difficult to read).
=> Make the output more readable using the -h flag. This command displays disk space in KB, GB or MB, which is easier to understand.
$ df -h
=> Check a specific storage volume and specify the filesystem path for that. For example, check /dev/vda1 with –
$ df -Th /dev/vda1
=> Access the manual page for more details –:
$ man df
Checking Memory Usage with Free Command
A Linux server relies on RAM and swap memory for smooth operation. The free command helps you track memory usage in real time. It comes with the default procps utilities package.
Below are the commands for different purposes –
=> Check memory details (The output includes used memory, free memory, and buffer/cache details.) –
$ free
Change the memory measurement value with options like –
- b for Bytes
- -k for Kibibytes
- -m for Mebibytes
- -g for Gibibytes
- -kilo for Kilobytes
- -mega for Megabytes
- -giga for Gigabytes
=> See memory usage in MB instead of kilobytes with the -h option –
$ free -h
The output includes:
- Total = Total available memory.
- Used = Amount of memory currently in use.
- Free = Memory that is completely free.
- Available = Memory that can still be used by applications without affecting performance.
- Swap = Swap memory, used when RAM is full.
=> Monitor memory usage continuously by running the below. This updates the memory details every three seconds.
$ watch -n 3 free -h
=> Check swap memory details –
$ swapon
=> Read the manual for more information.
$ man free
Monitoring System Processes with Top Command
The top command is an essential tool for tracking server processes, CPU usage, and memory consumption in real time. It provides detailed insights into how the server is handling resources.
Below are the Commands for specific purposes –
=> Start the top interface.
$ top
This will display a dynamic list of active processes. The output includes:
- System Information = uptime, logged-in users, and system load averages.
- CPU Usage = percentage of CPU used by different tasks.
- Memory Usage = RAM and swap memory usage.
- Processes List = details like process ID (PID), CPU usage, memory consumption, and the command running the process.
=> See processes from a specific user using the -u option, followed by the username –
$ top -u linuxuser
=> Close the top interface by pressing Q.
=> If you want more details, check the manual.
$ man top
Using VMStat for Detailed Performance Monitoring
The vmstat command helps track CPU, memory, and disk activity. It provides a detailed breakdown of how your server manages resources. It is a great tool for checking overall performance.
=> Check system statistics.
$ vmstat
The output includes details about:
- Processes (procs) = number of running and blocked processes.
- Memory Usage = free, used, and cached memory.
- Swap Memory = swap memory usage.
- Disk I/O (io) = number of blocks read and written to storage.
- CPU Usage = the percentage of time spent on different CPU tasks.
=> Get continuous updates every two seconds –
$ vmstat 2
=> Generate a fixed number of reports (E.g. 10) –
$ vmstat 2 10
=> See a memory and CPU usage summary –
$ vmstat -s
=> Read the manual for further options –
$ man vmstat
Tracking Active Processes with the PS Command
You can use the ps command to list currently running processes on your Linux server.
=> See active processes in your session with the below command –
$ ps
=> In case you want to see all running processes under all users on the whole system, you can use –
$ ps aux
The output includes:
- PID = Process ID of the task.
- USER = user who started the process.
- %CPU = percentage of CPU the process is using.
- %MEM = percentage of memory the process is using.
- COMMAND = The name of the command running the process.
=> You can sort processes by memory usage with –-
$ ps aux --sort=-%mem
This will list processes sorted by the highest memory consumption. You can replace %mem with %cpu if you need to sort by CPU usage.
=> To see processes started by a particular user with the command –
$ ps -u username **For a more detailed view of a process, use – ps -f
=> Explore more options in the manual.
$ man ps
Using Htop for an Interactive View of System Processes
Htop is a better alternative to the top command because it provides an easy-to-read interface with color-coded details. Check out the below commands to see how you can use it –
=> Installation command
Htop on Debian/Ubuntu - $ sudo apt install htop Htop on CentOS/RHEL-based systems - $ sudo dnf install htop
=> Launch Htop
$ htop
- The htop interface displays:
- CPU Usage = A real-time graph of CPU usage.
- Memory Usage = RAM and swap memory usage.
Process List = all running processes with details like PID, user, CPU, and memory usage.
=> Navigate through the interface using arrow keys.
=> To exit, press Q.
=>You can get a tree view to see process hierarchies.
$ htop -t
=> Check the manual for more options.
$ man htop
Checking Open Files with the Lsof Command3
The lsof command lists all open files and the processes using them.
=> See all open files with the below command –
$ sudo lsof
The output includes:
- COMMAND = name of the process using the file.
- PID = process ID.
- USER = owner of the process.
- FD = The file descriptor type (e.g., read, write).
- NAME = The file path.
=> Check which files are open in a specific directory.
sudo lsof /
=> Check files opened by a specific user –
$ sudo lsof -u username
=> Find which files a program like Apache is using –
sudo lsof /usr/sbin/apache2
=> You can find processes using port 80 (HTTP) –
$ sudo lsof -i :80
=> Check the manual with the below command –
$ man lsof
Monitoring Disk Activity with Iotop
The iotop command helps track disk input and output (I/O) activities.
=> Installation
- On Debian/Ubuntu –
$ sudo apt install iotop
- CentOS/RHEL-based systems
$ sudo dnf install iotop
=> Start monitoring disk activity
$ sudo iotop
The output includes:
- TID = thread ID of the process.
- USER = user who started the process.
- DISK READ = amount of data read from disk.
- DISK WRITE = amount of data written to disk.
- COMMAND = name of the running process.
=> View only active processes performing disk operations$ sudo iotop -o38
=> Include a timestamp in the output.
sudo iotop -t
=> Monitor disk usage for a specific user.
sudo iotop -u username
=> Check the manual for further customization.
$ man iotop
Checking Server Uptime and Kernel Messages
=> The below uptime command helps see how long your server has been running –
$ uptime
The output includes:
- Current Time
- Uptime Duration
- Logged-in Users = number of active users
- Load Average = average system load over 1, 5, and 15 minutes.
=> See uptime in hours and minutes with the following command –
$ uptime -p
=> Check when the server was last rebooted with –
$ uptime -s
=> Use the strace command to trace system calls when debugging or analyzing a program. The strace command helps track these calls in real time. It troubleshoots performance or debugging errors.
- Install strace on Debian or Ubuntu –
sudo apt install strace
** For CentOS or RHEL-based systems, use sudo dnf install strace
- Trace a specific command with –
sudo strace command_name
** For example, trace Apache using –
sudo strace apache2
The output will show system calls made by the process.
- Trace a running process by its Process ID (PID)
sudo strace -p 1234
** Replace 1234 with the actual PID of the process you want to monitor.
For more details, check the manual: - Check the manual and get more details.
$ man strace
Conclusion
Use right commands and tools to optimize your server and avoid unexpected failures. Always refer to manual pages for detailed options and keep tracking server usage for better efficiency.