Skip to main content

Debian Resource Monitoring

Introduction

Resource monitoring is a critical aspect of system administration that helps ensure your Debian system operates efficiently. By monitoring resources like CPU, memory, disk usage, and network traffic, you can identify performance bottlenecks, plan for capacity upgrades, troubleshoot issues, and maintain optimal system health.

This guide explores the essential tools and techniques for monitoring system resources on Debian-based systems. Whether you're managing a personal server or enterprise infrastructure, these monitoring practices will help you maintain reliable system performance.

Understanding System Resources

Before diving into monitoring tools, let's understand the key resources we need to monitor:

  1. CPU Usage: How much processing power is being utilized
  2. Memory (RAM) Usage: How memory is allocated and used
  3. Disk Usage and I/O: Storage capacity and read/write operations
  4. Network Traffic: Data transfer across network interfaces
  5. Process Activity: Which processes are consuming resources

Basic Monitoring Commands

Debian comes with several built-in command-line tools for monitoring resources. Let's explore the most useful ones:

1. top - Process Activity Monitor

The top command provides a dynamic real-time view of the system's processes:

bash
top

Sample Output:

top - 14:32:18 up 5 days, 2:13, 1 user, load average: 0.15, 0.10, 0.09
Tasks: 213 total, 1 running, 212 sleeping, 0 stopped, 0 zombie
%Cpu(s): 2.3 us, 0.7 sy, 0.0 ni, 96.7 id, 0.3 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem: 7962.8 total, 4521.1 used, 3441.7 free, 1024.2 buffers
MiB Swap: 2048.0 total, 10.5 used, 2037.5 free, 3102.5 cached Mem

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1256 www-data 20 0 721880 141540 73608 S 2.0 1.7 12:32.13 apache2
1892 mysql 20 0 1722388 352668 37832 S 1.7 4.3 8:45.56 mysqld
812 root 20 0 14512 9772 5076 S 0.3 0.1 1:23.45 sshd

Key features of top:

  • Displays CPU, memory, and swap usage
  • Shows running processes sorted by resource usage
  • Interactive commands (press 'h' for help while running)
  • Press 'q' to exit

2. htop - Enhanced Process Viewer

htop is an improved version of top with a more user-friendly interface:

bash
# Install htop if not already available
sudo apt install htop

# Run htop
htop

Key features of htop:

  • Color-coded interface
  • Vertical and horizontal scrolling
  • Mouse operation support
  • Visual indicators for CPU, memory, and swap usage

3. free - Memory Usage

The free command displays the amount of used and free memory:

bash
free -h

Sample Output:

              total        used        free      shared  buff/cache   available
Mem: 7.8Gi 2.1Gi 3.4Gi 284Mi 2.3Gi 5.2Gi
Swap: 2.0Gi 10Mi 2.0Gi

The -h flag shows the output in human-readable format (G for gigabytes, M for megabytes).

4. df - Disk Space Usage

The df command reports file system disk space usage:

bash
df -h

Sample Output:

Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1 30G 12G 17G 42% /
/dev/sda2 915G 208G 661G 24% /home
tmpfs 3.9G 0 3.9G 0% /dev/shm

5. du - Directory Space Usage

The du command estimates file space usage:

bash
# Show the size of the current directory and subdirectories
du -sh *

Sample Output:

16K     bin
24K boot
4.0K dev
8.0K etc
4.2G home
16K lib
4.0K media
4.0K mnt

6. iostat - I/O Statistics

iostat reports CPU and input/output statistics for devices and partitions:

bash
# Install sysstat package for iostat
sudo apt install sysstat

# Display I/O statistics
iostat -xz 5

Sample Output:

Linux 5.10.0-8-amd64 (debian)         03/13/2025      _x86_64_        (4 CPU)

avg-cpu: %user %nice %system %iowait %steal %idle
2.32 0.00 0.74 0.25 0.00 96.69

Device r/s w/s rkB/s wkB/s rrqm/s wrqm/s %util
sda 0.45 1.21 25.40 42.35 0.00 0.32 0.28
sdb 0.00 0.00 0.00 0.00 0.00 0.00 0.00

The command refreshes every 5 seconds. Press Ctrl+C to exit.

7. vmstat - Virtual Memory Statistics

vmstat reports information about processes, memory, paging, block I/O, and CPU activity:

bash
vmstat 3

Sample Output:

procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
1 0 10752 3525916 1048576 3177088 0 0 5 6 88 142 2 1 97 0 0
0 0 10752 3525788 1048576 3177096 0 0 0 35 126 252 1 0 99 0 0
0 0 10752 3525788 1048576 3177096 0 0 0 0 125 242 0 1 99 0 0

The command refreshes every 3 seconds. Press Ctrl+C to exit.

8. netstat - Network Statistics

netstat displays network connections, routing tables, and interface statistics:

bash
# Install net-tools package for netstat
sudo apt install net-tools

# Display all listening ports
netstat -tuln

Sample Output:

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN
tcp6 0 0 :::22 :::* LISTEN
tcp6 0 0 ::1:25 :::* LISTEN
udp 0 0 0.0.0.0:68 0.0.0.0:*

Advanced Monitoring Tools

Let's explore more sophisticated monitoring solutions for Debian systems:

1. Glances - System Monitoring Tool

Glances is an advanced cross-platform monitoring tool:

bash
# Install Glances
sudo apt install glances

# Run Glances
glances

Key features of Glances:

  • All-in-one monitoring (CPU, memory, network, disks, processes)
  • Web interface option (glances -w to start the web server)
  • Export data to various monitoring systems
  • Alerting capabilities

2. Prometheus and Grafana

For enterprise-level monitoring, Prometheus (for data collection) and Grafana (for visualization) form a powerful combination:

bash
# Install Prometheus
sudo apt install prometheus

# Install Grafana
sudo apt install apt-transport-https software-properties-common
wget -q -O - https://packages.grafana.com/gpg.key | sudo apt-key add -
sudo add-apt-repository "deb https://packages.grafana.com/oss/deb stable main"
sudo apt update
sudo apt install grafana

# Start and enable the services
sudo systemctl start prometheus
sudo systemctl enable prometheus
sudo systemctl start grafana-server
sudo systemctl enable grafana-server

Setting up a basic monitoring dashboard with these tools is beyond the scope of this guide, but resources are provided in the "Additional Resources" section.

Monitoring Specific Resources

CPU Monitoring with mpstat

Monitor CPU performance with detailed statistics:

bash
# Install sysstat if not already installed
sudo apt install sysstat

# Show CPU statistics updated every 2 seconds
mpstat -P ALL 2

Sample Output:

Linux 5.10.0-8-amd64 (debian)         03/13/2025      _x86_64_        (4 CPU)

03:15:42 PM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %idle
03:15:44 PM all 2.13 0.00 0.75 0.25 0.00 0.13 0.00 0.00 96.75
03:15:44 PM 0 2.48 0.00 0.99 0.00 0.00 0.00 0.00 0.00 96.53
03:15:44 PM 1 1.49 0.00 0.50 0.50 0.00 0.00 0.00 0.00 97.52
03:15:44 PM 2 2.49 0.00 0.50 0.50 0.00 0.50 0.00 0.00 96.02
03:15:44 PM 3 2.00 0.00 1.00 0.00 0.00 0.00 0.00 0.00 97.00

Memory Monitoring with smem

smem is a memory reporting tool with unique features:

bash
# Install smem
sudo apt install smem

# Show process memory usage sorted by PSS (Proportional Set Size)
smem -t -k

Sample Output:

  PID User     Command                         Swap      USS      PSS      RSS
1256 www-data /usr/sbin/apache2 -k start 0 22124 31298 73608
1892 mysql /usr/sbin/mysqld 0 314836 337468 352668
1057 root /usr/bin/containerd 0 45712 47893 54340
812 root /usr/sbin/sshd -D 0 4696 6734 9772
-------------------------------------------------------------------------------
42 13 0 1876136 2053417 2901140

Network Monitoring with iftop

Monitor network bandwidth usage:

bash
# Install iftop
sudo apt install iftop

# Monitor traffic on the default interface
sudo iftop

Key features of iftop:

  • Shows current bandwidth usage by host pairs
  • Displays cumulative transfer totals
  • Multiple display options (press 'h' for help)

Disk I/O Monitoring with iotop

Monitor disk I/O usage by processes:

bash
# Install iotop
sudo apt install iotop

# Monitor disk I/O
sudo iotop

Sample Output:

Total DISK READ:       0.00 B/s | Total DISK WRITE:      23.50 K/s
Current DISK READ: 0.00 B/s | Current DISK WRITE: 0.00 B/s
TID PRIO USER DISK READ DISK WRITE SWAPIN IO> COMMAND
1892 20 mysql 0.00 B 15.70 K/s 0.00 % 0.00 % mysqld
1256 20 www-data 0.00 B 7.80 K/s 0.00 % 0.00 % apache2
1 20 root 0.00 B 0.00 B/s 0.00 % 0.00 % systemd --system

Continuous Monitoring with sysstat

The sysstat package provides tools for long-term monitoring and data collection:

bash
# Install sysstat if not already installed
sudo apt install sysstat

# Enable sysstat data collection
sudo systemctl enable sysstat
sudo systemctl start sysstat

Using sar to View Historical Data

sar (System Activity Reporter) displays performance data:

bash
# View CPU usage for the current day
sar -u

# View memory usage
sar -r

# View specific time period
sar -u -f /var/log/sysstat/sa01 -s 08:00:00 -e 10:00:00

# View network statistics
sar -n DEV

Sample Output for CPU usage:

Linux 5.10.0-8-amd64 (debian)         03/13/2025      _x86_64_        (4 CPU)

12:00:01 AM CPU %user %nice %system %iowait %steal %idle
12:10:01 AM all 2.25 0.00 0.78 0.31 0.00 96.67
12:20:01 AM all 2.40 0.00 0.81 0.25 0.00 96.54
12:30:01 AM all 2.38 0.00 0.79 0.28 0.00 96.55
...

Setting Up Automated Monitoring Alerts

Using cron and Custom Scripts

Create a simple bash script to check system load and send email alerts:

bash
#!/bin/bash
# /usr/local/bin/check_load.sh

THRESHOLD=4.0
LOAD=$(cat /proc/loadavg | awk '{print $1}')

if (( $(echo "$LOAD > $THRESHOLD" | bc -l) )); then
echo "System load is high: $LOAD" | mail -s "High load alert on $(hostname)" [email protected]
fi

Set up a cron job to run this script every 5 minutes:

bash
# Make the script executable
sudo chmod +x /usr/local/bin/check_load.sh

# Add to crontab
echo "*/5 * * * * /usr/local/bin/check_load.sh" | sudo tee -a /etc/cron.d/system-monitor

Using Monit

Monit is a utility for monitoring services, files, directories, and devices:

bash
# Install Monit
sudo apt install monit

# Configure basic monitoring
sudo nano /etc/monit/conf.d/system

Add this configuration:

check system localhost
if loadavg (5min) > 3 then alert
if memory usage > 80% then alert
if cpu usage (user) > 70% for 5 cycles then alert
if cpu usage (system) > 40% for 5 cycles then alert

check filesystem rootfs with path /
if space usage > 80% then alert

Restart Monit to apply changes:

bash
sudo systemctl restart monit

Visualizing System Resource Data

Using gnuplot for Basic Visualization

Create a simple script to visualize CPU data:

bash
#!/bin/bash
# /usr/local/bin/plot_cpu.sh

# Collect data
sar -u | grep -v Average | grep -v Linux | grep -v CPU > cpu_data.txt

# Create gnuplot script
cat > plot_script.gp << EOF
set terminal png
set output "cpu_usage.png"
set title "CPU Usage"
set xlabel "Time"
set ylabel "Percentage"
set xdata time
set timefmt "%I:%M:%S %p"
set format x "%H:%M"
set yrange [0:100]
plot "cpu_data.txt" using 1:3 title "%user" with lines, \
"cpu_data.txt" using 1:5 title "%system" with lines, \
"cpu_data.txt" using 1:9 title "%idle" with lines
EOF

# Generate plot
gnuplot plot_script.gp

Make it executable and run:

bash
sudo chmod +x /usr/local/bin/plot_cpu.sh
./plot_cpu.sh

Creating a Monitoring Dashboard with Cockpit

Cockpit provides a web-based monitoring dashboard:

bash
# Install Cockpit
sudo apt install cockpit

# Start and enable Cockpit
sudo systemctl enable --now cockpit.socket

Access the dashboard at https://your-server-ip:9090 in your web browser.

System Monitoring Best Practices

  1. Establish Baselines: Monitor your system during normal operations to establish baseline metrics.

  2. Set Meaningful Thresholds: Configure alerts based on your specific system requirements.

  3. Implement Tiered Monitoring: Use simple tools for basic checks and advanced tools for detailed analysis.

  4. Automate Routine Checks: Use cron jobs or monitoring services for continuous monitoring.

  5. Maintain Historical Data: Keep historical performance data for trend analysis.

  6. Monitor All Critical Resources: CPU, memory, disk, network, and application-specific metrics.

  7. Document Normal Patterns: Know what "normal" looks like for your system.

Troubleshooting Common Issues

High CPU Usage

If top shows high CPU usage:

  1. Identify the process using most CPU with top or htop
  2. Check if it's a legitimate workload or a runaway process
  3. Use strace -p [PID] to see what the process is doing
  4. Check system logs with journalctl for errors

Memory Leaks

If memory usage keeps increasing:

  1. Use free -m to monitor memory usage over time
  2. Check which processes are consuming memory with ps aux --sort=-%mem
  3. Use smem for detailed memory analysis
  4. Consider restarting leaking services

Disk Space Issues

If disk space is running low:

  1. Find large files with find / -type f -size +100M -exec ls -lh {} \; 2>/dev/null | sort -k5,5hr
  2. Check log files in /var/log
  3. Use ncdu for an interactive disk usage analyzer: sudo apt install ncdu && sudo ncdu /

Slow Disk I/O

If disk operations are slow:

  1. Check I/O wait time with iostat -xz 1
  2. Identify processes causing I/O with iotop
  3. Check disk health with smartctl -a /dev/sda

Summary

Effective resource monitoring is essential for maintaining a healthy Debian system. This guide has covered:

  • Basic command-line tools like top, free, and df
  • Advanced monitoring tools such as Glances, Prometheus, and Grafana
  • Techniques for monitoring specific resources (CPU, memory, disk, network)
  • Setting up automated monitoring and alerts
  • Troubleshooting common resource-related issues

By implementing these monitoring practices, you'll be better equipped to maintain optimal system performance, plan for capacity needs, and quickly address issues before they affect your users.

Additional Resources

Exercises

  1. Set up a simple monitoring system using top, free, and df with a cron job that saves outputs to a log file every hour.

  2. Install and configure Glances to monitor a Debian system. Explore its web interface.

  3. Create a bash script that checks for processes using more than 80% CPU and sends an alert.

  4. Install Cockpit and explore its features for monitoring system resources.

  5. Set up a simple Prometheus and Grafana installation to monitor basic system metrics.



If you spot any mistakes on this website, please let me know at [email protected]. I’d greatly appreciate your feedback! :)