Posted on
Software

mpstat: CPU usage monitoring

Author
  • User
    Linux Bash
    Posts by this author
    Posts by this author

Harnessing the Power of mpstat: A Guide to CPU Usage Monitoring on Linux

When managing the performance of systems or servers, understanding CPU usage is critical. One highly-effective tool for administrators and performance analysts is mpstat, a utility in the sysstat package. This utility provides detailed information on CPU utilization, helping professionals diagnose bottlenecks and optimise efficiency. In this guide, we'll explore how to install and use mpstat across different Linux distributions and delve into leveraging its capabilities to monitor CPU usage effectively.

What is mpstat?

mpstat is a command-line utility part of the sysstat package which collects and shows information about CPU utilization. It is versatile enough to report CPU statistics on a per-processor basis which makes it an invaluable tool for systems with multiple CPUs or cores.

Installing mpstat

The installation process of mpstat involves installing the sysstat package which contains several utilities for performance monitoring. Here’s how you can install it based on your Linux distribution:

On Ubuntu and Debian-Based Systems:

On Ubuntu and other Debian-based systems, you can install sysstat using apt:

sudo apt update
sudo apt install sysstat

After installation, you might need to enable the collection of statistics by editing the /etc/default/sysstat file, changing ENABLED="false" to ENABLED="true" and starting the sysstat service:

sudo systemctl enable sysstat
sudo systemctl start sysstat

On Fedora, CentOS, and Red Hat-Based Systems:

For those using Fedora, CentOS, or other derivatives of Red Hat, dnf is your package manager:

sudo dnf install sysstat

Similar to Debian-based systems, ensure that data collection is enabled if it’s not started by default.

On openSUSE and SUSE-Based Systems:

For openSUSE or SUSE Enterprise Linux, the zypper package manager is used:

sudo zypper install sysstat

Check and ensure that the sysstat service is enabled and started as shown earlier.

Using mpstat

Once installed, running mpstat is straightforward. You can simply type mpstat followed by the interval in seconds and the number of times you want the report to be generated. For example:

mpstat 2 5

This command will output CPU statistics every 2 seconds, five times in succession. Here’s a breakdown of some key outputs:

  • %usr: CPU usage while executing at the user level (application).

  • %nice: CPU usage while executing at the user level with nice priority.

  • %sys: CPU usage while executing at the system level (kernel).

  • %iowait: Percentage of time the CPU is idle during which the system had an outstanding disk I/O request.

  • %irq: CPU usage while servicing interrupts.

  • %soft: CPU usage while servicing software interrupts.

  • %steal: Percentage of time a virtual CPU waits for a real CPU while the hypervisor is servicing another virtual processor.

Tips for Monitoring

  • Use mpstat to monitor CPU usage trends over time, which can help in capacity planning.

  • Combine mpstat with other utilities like iostat and vmstat for comprehensive system monitoring.

  • Script mpstat outputs at regular intervals and store them for historical performance analysis.

Conclusion

mpstat is a powerful tool for any system administrator's arsenal, providing deep insights into CPU behavior and helping tune systems for optimal performance. Whether you're a seasoned pro or just getting started, making mpstat part of your performance monitoring toolkit will certainly put you a step ahead in managing your IT infrastructure efficiently.