A Complete Guide to Monitoring Linux System Metrics with the atop Tool: Installation, Configuration, and Usage

Real-time and comprehensive monitoring of system resources and process status is crucial in Linux system maintenance. As a powerful monitoring tool, atop can help us easily achieve this goal. This article will detail how to install, configure, and use the atop monitoring tool on a Linux instance.

Introduction to atop tool

Atop is a tool specifically designed for monitoring Linux system resources and processes. It records system and process activity, reporting the status of all running processes. The data collected covers resource usage such as CPU, memory, disk, and network, as well as process states. This data can also be saved to disk in log file format. For each process, we can obtain key information such as CPU utilization, memory growth, disk usage, priority, username, status, and exit code. Furthermore, through the atop configuration file, we can customize parameters such as logging frequency, log file storage path, and rotation policy.

II. Installing the atop tool

The installation methods of atop vary slightly across different Linux distributions; the following introduces it using a common operating system as an example

  1. Alibaba Cloud Linux 3/2、CentOS 7/8、Fedora、Rocky Linux 9

    • Execute installation command: sudo yum install -y atop
    • Start the atop service: sudo systemctl start atop
  2. Ubuntu / Debian

    • Update software sources: sudo apt update
    • Execute the installation command: sudo apt install -y atop
    • Start atop service: sudo systemctl start atop
  3. CentOS Stream 9

    • Download and install: sudo wget https://www.atoptool.nl/download/atop-2.11.0-1.el9.x86_64.rpm && sudo rpm -i atop-2.11.0-1.el9.x86_64.rpm
    • Start atop service: sudo systemctl start atop
  4. openSUSE

    • Execute installation command: sudo zypper install -y atop atop-daemon
    • Start the atop service: sudo systemctl start atop

If the operating systems listed above do not include your distribution, you can visit the atop official website for installation information

III. Configuring Monitoring Cycle and Log Retention Time

  1. Configuration file location: On Alibaba Cloud Linux 3/2, CentOS 7/8, and Fedora systems, the atop configuration file is /etc/sysconfig/atop; on Ubuntu, Debian, and openSUSE systems, the configuration file is /etc/default/atop
  2. Default Configuration Parameters Description
    • LOGOPTS: Used to control log file recording options, defaults to empty
    • The monitoring cycle, default is 600 seconds. If you need to collect historical logs to track issues, adjust this frequency according to your actual needs.
    • LOGGENERATIONS: Log retention time, default 28 days
    • LOGPATH: The path where log files are stored, default /var/log/atop
  3. Configuration Steps
    • Execute the command to open the configuration file:
      • On Alibaba Cloud Linux 3/2, CentOS 7/8, Fedora systems: sudo vim /etc/sysconfig/atop
      • In Ubuntu, Debian, openSUSE, CentOS Stream 9, Rocky Linux 9 systems: sudo vim /etc/default/atop
    • Press i to enter edit mode and adjust configuration parameters as needed. For example, change the monitoring cycle to 30 seconds, set the log retention time to 7 days, and keep the log path at its default setting:
    • Press the Esc key, enter :wq, save and exit editing
    • Restarting the atop service will apply the configuration: sudo systemctl restart atop
LOGOPTS=""
LOGINTERVAL=30
LOGGENERATIONS=7
LOGPATH=/var/log/atop 

Four, Using the atop tool

  1. Introduction to Common Commands: In interactive command mode, the following are common commands:

    • Switch back to the default composite output view
    • Displays the full command line of processes
    • Sort by process memory usage in descending order
    • Sort by process disk usage in descending order
    • Sort in descending order based on the comprehensive utilization rate of processes and resources
    • Sort by process network usage in descending order
    • Go to the next monitoring point
    • Go to the previous monitoring point
    • b: Specifies a point in time, formatted as YYYYMMDDhhmm
  2. Introduction to Resource Monitoring Fields

    • Host name, information sampling date and time
    • Overall process runtime status, including kernel and user space running time, total number of processes, and the number of processes in different states
    • The CPU utilization shows the overall usage, with each field’s numerical sum resulting in N*100% (where N is the number of CPU cores), including kernel time, user time, interrupt time, idle time, and wait for disk I/O time proportions
    • CPL: CPU load information, such as the average number of processes in the run queue over the past 1 minute, 5 minutes, and 15 minutes, context switch count, and interrupt occurrence count
    • MEM: Memory usage, including total physical memory, idle memory, page cache memory, file cache memory, and kernel occupied memory
    • SWP: Swap space usage, including total swap area and free swap space size
    • PAG: Virtual memory paging status, such as the number of pages swapped in and out
    • DSK: Disk Usage, each disk device corresponds to a row, displaying device identifier, busy time ratio, and read/write request count
    • NET: Network conditions, displaying receive and send packet sizes for the transport layer TCP and UDP, IP layer, and active network interfaces
  3. View real-time system metrics

    • Check system metrics every 5 seconds: atop 5
    • Check system metrics within the next 5 minutes (total of 30 times, with a 10-second interval): atop -M 10 30
    • Check system metrics after 10 minutes (10 times, with a 60-second interval) and write the results to a file: atop -M 60 10 > /log/atop.mem
  4. To view historical indicator logs: After atop starts, the collection records are stored by default in the /var/log/atop directory. When viewing, be sure to check that the log file for the specified date exists; otherwise, an error will occur.

    • View daily history indicators log: atop -r
    • View yesterday’s historical indicator logs: atop -r y
    • View historical indicator logs for a specified date, such as November 6, 2024: atop -r 20241106
    • View historical indicator logs from a specified time within a specified date, such as starting at 14:00 on November 6, 2024: atop -r 20241106 -b 14:00
    • View the historical indicator logs for a specified time period within a specific date, such as November 5, 2024 from 00:04 to 00:08: atop -r 20241105 -b 00:04 -e 00:08
  5. View System Activity Report

    • Check the CPU utilization report of the current system within 1 minute (12 times, interval 5 seconds): atopsar -c 5 12
    • To view the memory metrics report for a specified time period on that day, such as from 18:00 to 18:01: atopsar -m -b 18:00 -e 18:01
    • View memory metrics reports for a specified date and time period, such as November 5, 2024, from 18:00 to 18:01: atopsar -m -r 20241105 -b 18:00 -e 18:01

V. Other Operations

  1. Configure a Tian-level log rotation policy: If you want to generate an atop index log file every day, you can perform the following operations:

    • (Optional) Adjust the monitoring cycle, log retention time, and log storage path as needed
    • Run command to set daily log rotation related services to start on boot and start the services: sudo systemctl enable --now atop atopacct atop-rotate.timer
    • If the business has more complex requirements for log processing, it can be combined with logrotate or custom scripts to implement log management
  2. Load the optional netatop kernel module: If you need to monitor network usage, you can install the netatop module (this module is not installed by default in atop). Taking Alibaba Cloud Linux 3 as an example:

    • Install the kernel development package and software environment required for compilation: sudo yum install -y kernel-devel dkms elfutils-libelf-devel
    • Download the latest version of netatop source code to a designated directory: cd /usr/src/ && sudo wget https://www.atoptool.nl/download/netatop-3.2.2.tar.gz --no-check-certificate
    • Extract the source code and enter the source directory: sudo tar -zxvf netatop-3.2.2.tar.gz && cd netatop-3.2.2
    • Build and install the module and daemon from source code: sudo make && sudo make install
    • Start the netatop service: sudo systemctl start netatop

The atop tool is powerful and flexible. With proper installation, configuration, and usage, we can better understand the operating status of the Linux system, promptly discover and resolve potential issues. We hope this article will help everyone improve their skills in Linux system monitoring.

References

  • [atop official website]
  • Installing, Configuring, and Using the atop Monitoring Tool
A financial IT programmer's tinkering and daily life musings
Built with Hugo
Theme Stack designed by Jimmy