Skip to main content

Monitoring System Resources

1. Viewing Memory Usage

Command Description Notes
free -h Displays memory usage in a human-readable format Shows total, used, and free memory including swap
cat /proc/meminfo Displays detailed memory statistics from kernel Provides extensive memory metrics like buffers, cache, and slab
top Real-time view of system processes and memory usage Press 'M' to sort by memory usage
htop Enhanced version of top with better UI Interactive process viewer with color-coded memory display

Example: Using free -h to view memory usage

$ free -h
              total        used        free      shared  buff/cache   available
Mem:           15Gi       3.2Gi       6.1Gi       204Mi       6.2Gi       11Gi
Swap:          2.0Gi       0B        2.0Gi

Example: Using cat /proc/meminfo to view detailed memory info

$ cat /proc/meminfo | grep -E "(MemTotal|MemFree|Buffers|Cached)"
MemTotal:        16384000 kB
MemFree:          6456320 kB
Buffers:           123456 kB
Cached:          5432100 kB

2. Monitoring CPU Usage

Command Description Notes
top Real-time display of running processes and system resource usage Press Shift+M to sort by memory usage, Shift+P to sort by CPU usage
htop Enhanced version of top with better UI and features Requires installation; more user-friendly than top
ps aux Report a snapshot of current processes Use ps aux | grep <process> to filter results
vmstat Report virtual memory statistics Use vmstat 1 5 to show stats every second for 5 times
sar System Activity Reporter from sysstat package Can log and analyze historical data; requires sysstat package

Example: Using top to monitor CPU usage

$ top

Example: Using ps aux to get CPU usage of a specific process

$ ps aux | grep "nginx"

Example: Using vmstat to monitor CPU statistics

$ vmstat 1 5

3. Displaying Load Average

Command Description Example Output
uptime Displays system uptime, number of users, and load averages over 1, 5, and 15 minutes 14:23:45 up 2 days, 3:15, 2 users, load average: 0.15, 0.20, 0.25
w Shows logged-in users and their processes, including load averages
14:23:45 up 2 days,  3:15,  2 users,  load average: 0.15, 0.20, 0.25
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
john     pts/0    192.168.1.100    14:10   12:05   0.05s  0.03s -bash
mary     pts/1    192.168.1.101    14:15    0.00s  0.02s  0.01s w
loadavg Displays load average from /proc/loadavg file 0.15 0.20 0.25 2/345 12345

Example: Interpreting Load Average

uptime
# Output:
# 14:23:45 up 2 days,  3:15,  2 users,  load average: 0.15, 0.20, 0.25

# Interpretation:
# - Load average over last minute: 0.15 (low)
# - Load average over 5 minutes: 0.20 (low)
# - Load average over 15 minutes: 0.25 (low)
Note: Load average is the number of processes in the run queue (waiting for CPU or I/O) averaged over 1, 5, and 15 minutes. A load average of 1.0 means one process is running or waiting on a CPU.

4. Monitoring I/O Statistics

Command Description Example
iostat Reports CPU and I/O statistics for devices and partitions iostat 1 5 - Reports stats every second for 5 times
iotop Shows I/O usage by processes in real-time iotop -a - Shows accumulated I/O by processes
vmstat Reports virtual memory statistics including I/O vmstat 2 5 - Reports every 2 seconds for 5 times
pidstat Reports I/O statistics for specific processes pidstat -d 1 3 - Reports I/O stats every second for 3 times

Example: Using iostat to Monitor Disk I/O

iostat -x 1 3

Example: Using iotop to Monitor I/O by Processes

iotop -a -p 1234
Note: iostat requires the sysstat package to be installed.
Warning: iotop may not be available on all Linux distributions.

5. Viewing Real-Time Disk Activity

Command Description Notes
iostat Reports CPU and I/O statistics for devices and partitions Part of sysstat package
iotop Shows real-time I/O usage by processes Requires root or CAP_SYS_ADMIN capability
vmstat Reports virtual memory statistics including disk I/O Part of sysstat package
dstat Aggregates system statistics including disk I/O More flexible than iostat, can combine multiple stats

Example: Using iostat to view disk activity

iostat -x 1

Example: Using iotop to view real-time I/O by process

iotop -a
Note: iostat and iotop require the sysstat package to be installed on most distributions.
Warning: iotop may not show accurate data if the system is under heavy load or if processes are not actively I/O bound.

6. Checking System Temperature

Command Description Notes
sensors Display sensor readings from hardware monitors Part of lm-sensors package
cat /sys/class/thermal/thermal_zone*/temp Read raw temperature values from thermal zones Provides Celsius temperature in millidegree units
watch -n 1 'cat /sys/class/thermal/thermal_zone*/temp' Continuously monitor temperature changes Useful for real-time monitoring

Example: Using sensors command

sensors
Adapter: Virtual device
coretemp-isa-0000
Package id 0:  +45.0°C  (high = +80.0°C, critical = +100.0°C)
Core 0:       +43.0°C  (high = +80.0°C, critical = +100.0°C)
Core 1:       +45.0°C  (high = +80.0°C, critical = +100.0°C)

Example: Reading raw temperature values

cat /sys/class/thermal/thermal_zone0/temp
45000

7. Monitoring Network Bandwidth

Tool Command Description Notes
iftop iftop -i eth0 Real-time bandwidth usage per connection Requires root or CAP_NET_ADMIN
nethogs nethogs eth0 Shows bandwidth per process Displays data in KB/s
vnstat vnstat -d Daily network statistics Needs database setup with vnstat -u
ss ss -tuln Socket statistics including network usage Can filter by protocol or state
netstat netstat -i Network interface statistics Shows packet counts and errors

Example: Using iftop to monitor bandwidth

iftop -i eth0
# Displays real-time bandwidth usage per connection
# Shows total bandwidth and individual connections

Example: Using nethogs to view per-process bandwidth

nethogs eth0
# Shows bandwidth usage per process
# Displays KB/s for each process
Note: iftop and nethogs require root privileges or specific capabilities to capture network traffic.

Key Features of Network Monitoring Tools

  • iftop: Real-time view of bandwidth usage per connection
  • nethogs: Shows bandwidth usage per process
  • vnstat: Provides historical daily network statistics
  • ss/netstat: Displays socket and interface statistics

8. Using System Activity Reporter

Example: Basic system activity report

sar -u 1 5
Option Description Notes
sar -u Reports CPU utilization Shows %idle, %user, %system
sar -r Reports memory utilization Shows free, used, and cached memory
sar -d Reports disk I/O activity Shows device read/write stats
sar -w Reports context switches and process activity Shows system load and process creation rate
Note: The sar command requires the sysstat package to be installed.

Example: Monitor disk I/O every 2 seconds for 3 times

sar -d 2 3

Example: Display network statistics

sar -n DEV 1 3

9. Viewing System Statistics

Command Description Example Output
top Real-time view of running processes and system statistics
top - 14:23:01 up 2 days,  3:45,  2 users,  load average: 0.15, 0.10, 0.05
Tasks: 123 total,   1 running, 122 sleeping,   0 stopped,   0 zombie
%Cpu(s):  5.2 us,  1.8 sy,  0.0 ni, 93.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
MiB Mem :   8192.0 total,   1234.5 free,   3456.7 used,   3500.8 buff/cache
MiB Swap:   2048.0 total,   2048.0 free,      0.0 used.   4567.8 avail Mem
htop Enhanced version of top with better UI and process management
htop - 14:23:01 up 2 days,  3:45,  2 users,  load average: 0.15, 0.10, 0.05
Tasks: 123 total,   1 running, 122 sleeping,   0 stopped,   0 zombie
%Cpu(s):  5.2 us,  1.8 sy,  0.0 ni, 93.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
MiB Mem :   8192.0 total,   1234.5 free,   3456.7 used,   3500.8 buff/cache
MiB Swap:   2048.0 total,   2048.0 free,      0.0 used.   4567.8 avail Mem
vmstat Virtual memory statistics including CPU, memory, and I/O
procs -----------memory---------- ---system-- -----io---- --cpu----- 
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 1  0      0  123456   7890  350080    0    0     1     2   12    5  5  2 93  0  0
iostat Input/output statistics for devices and partitions
iostat -x 1 2
Linux 5.4.0-42-generic (hostname)  06/15/2023

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           5.20    0.00    1.80    0.00    0.00   93.00

Device:         rrqm/s wrqm/s   r/s   w/s avgrq-sz avgqu-sz await svctm %util
sda               0.00     0.00  1.20  0.80    1.50     0.00   0.50   0.50  0.10
sar System activity reporter from sysstat package
Sysstat version 12.5.7 (2023-06-15)
Linux 5.4.0-42-generic (hostname)  06/15/2023

14:23:01        CPU     %user     %nice   %system   %iowait    %steal     %idle
14:23:01        all      5.20      0.00      1.80      0.00      0.00     93.00

Example: Using top to monitor CPU usage

top -p 1234,5678

Example: Using vmstat to check memory usage

vmstat -s
Note: The top command updates in real-time. Use q to quit.
Warning: iostat requires the sysstat package to be installed.

10. Analyzing System Performance

Metric Description Command Notes
CPU Usage Percentage of CPU time used by processes top Real-time view of system processes
Memory Usage Amount of RAM used and available free -h Human-readable format with units
Disk I/O Input/output operations on storage devices iostat Requires sysstat package
Network Traffic Data transfer rates over network interfaces iftop Real-time bandwidth monitoring
Load Average Average system load over 1, 5, and 15 minutes uptime Indicates system workload intensity

Example: Using top to analyze CPU usage

# Run top command
top

# Sample output:
#   PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
#  1234 user      20   0  256789 123456  45678 S  12.5  0.3   0:05.67 firefox
#  5678 root      20   0   12345   6789   1234 S   5.0  0.1   0:01.23 systemd

Example: Monitoring disk I/O with iostat

# Install sysstat if not present
sudo apt install sysstat

# Run iostat to monitor I/O stats every 2 seconds
iostat -x 2

# Sample output:
#   Device:         rrqm/s   wrqm/s     r/s     w/s   rMB/s   wMB/s avgrq-sz avgqu-sz await svctm %util
#   sda              0.00     0.00    2.00    1.00    0.01    0.02     1.50     0.00   1.00   0.50  0.30
Note: Use htop for a more interactive and user-friendly version of top.
Warning: iostat requires the sysstat package to be installed on some distributions.

11. Viewing Open Files and Processes

Command Description Example
ps Report or control processes ps aux | grep firefox
top Display and update sorted process information top -p 1234
htop Enhanced version of top with better UI htop
lsof List open files and associated processes lsof -i :80
netstat Display network connections, routing tables, interface statistics netstat -tuln | grep :80

Example: View all processes using port 80

sudo netstat -tuln | grep :80

Example: Find process ID by name

ps aux | grep nginx

Example: List all open files for a specific process

lsof -p 1234
Note: The ps command can be used with various options like aux, ef, or o to customize output format.
Warning: Using sudo with commands like netstat or lsof may be required to view all network connections and file descriptors.

Was this article helpful?