Cpustat – Monitors CPU Utilization by Running Processes in Linux


Cpustat is a command-line utility for monitoring CPU utilization by running processes in Linux. It allows users to view the amount of CPU time being used by individual processes and groups of processes, providing insights into how system resources are being utilized. With Cpustat, users can identify processes that are consuming too much CPU time and take action to optimize system performance.

Installing Cpustat

Cpustat is available for most Linux distributions and can be installed using the package manager. On Debian/Ubuntu, use the following command to install −

sudo apt-get install cpustat

On Fedora/CentOS, use the following command −

sudo dnf install cpustat

Using Cpustat

To use Cpustat, simply run the command cpustat followed by any optional parameters. By default, Cpustat will display a summary of CPU utilization by all running processes.

cpustat

The output provides a summary of CPU utilization by all running processes, including the total number of processes, the total amount of CPU time, and the percentage of CPU time used by each process.

Users can also specify a specific process or group of processes to monitor, as well as filter results based on CPU usage, process name, or other criteria. For example, to display CPU utilization for a specific process, use the -p option followed by the process ID.

cpustat -p PID

To monitor a group of processes, use the -g option followed by a process group name.

cpustat -g group_name

Cpustat also provides several options for customizing output, including changing the display interval, sorting by different criteria, and selecting the format of the output.

Display interval

To change the display interval, use the -i option followed by the number of seconds between updates.

cpustat -i 2

This will update the display every 2 seconds.

Sorting

To sort output by different criteria, use the -S option followed by the desired criteria.

cpustat -S %cpu

This will sort output by CPU usage percentage.

Output format

To select the format of the output, use the -o option followed by the desired format.

cpustat -o csv

This will output data in comma-separated value format.

Advanced Usage: Scripting with Cpustat

One of the most powerful features of Cpustat is its ability to be scripted and automated. By using Cpustat in conjunction with shell scripts or other programming languages, you can create custom monitoring solutions tailored to your specific needs. Here are some examples of scripts that can be created using Cpustat −

CPU Utilization Over Time

To monitor CPU utilization over time using Cpustat, you can create a shell script that runs Cpustat at regular intervals and appends the output to a log file. Here's an example −

#!/bin/bash

while true; do
   cpustat -o csv >> cpu_log.csv
   sleep 1
done

This script uses a while loop to continuously run Cpustat every second and append the output to a CSV file named cpu_log.csv. The -o csv option tells Cpustat to output the data in CSV format, which can be easily parsed and analyzed using tools like Excel or Python's pandas library.

Alerts for High CPU Usage

To create an alerting script that monitors CPU usage with Cpustat and sends an alert when certain thresholds are exceeded, you can use a tool like awk to parse the Cpustat output and compare the CPU usage against a predefined threshold. Here's an example −

#!/bin/bash

while true; do
   cpustat -o csv | awk -F ',' '{ if ($2 > 50) print "High CPU usage detected: " $0 }'
   sleep 1
done

This script uses a while loop to continuously run Cpustat every second and pipe the output to awk. The awk command uses the -F ',' option to specify that the input is in CSV format and compares the second column (which contains the CPU usage percentage) against a threshold of 50%. If the CPU usage exceeds the threshold, awk prints a message to the console.

You can modify this script to send an email or other type of notification instead of printing to the console, depending on your specific requirements.

Process Profiling

To profile the CPU usage of specific processes or groups of processes using Cpustat, you can use the -p option to specify a process ID or name. Here's an example −

#!/bin/bash

while true; do
   cpustat -o csv -p nginx >> nginx_cpu.csv
   cpustat -o csv -p postgres >> postgres_cpu.csv
   sleep 1
done

This script uses a while loop to continuously run Cpustat every second and monitor the CPU usage of two specific processes: nginx and postgres. The output for each process is written to a separate CSV file (nginx_cpu.csv and postgres_cpu.csv, respectively), which can be analyzed to identify performance issues or bottlenecks.

These are just a few examples of how Cpustat can be used for scripting and automation. With its customizable output and real-time monitoring capabilities, the possibilities are endless.

Cpustat vs. Other System Monitoring Tools

Cpustat is just one of many tools available for monitoring system performance on Linux. Other popular system monitoring tools include top, htop, and glances, each with their own strengths and weaknesses. Here are some of the advantages of using Cpustat over these other tools −

  • Process-level monitoring − Unlike top and htop, which show CPU utilization at the system level, Cpustat can monitor CPU utilization by individual processes or groups of processes. This makes it easier to identify which processes are consuming the most CPU resources.

  • Customizable output − Cpustat's output can be customized using command-line options to display only the information that is relevant to you. For example, you can choose to show or hide specific columns, adjust the width of the output, or sort the output based on different criteria.

  • Real-time monitoring − Cpustat can be used in real-time to monitor CPU utilization as it changes over time. This is especially useful for identifying processes that are causing spikes in CPU usage or for monitoring CPU usage during specific events or processes.

Overall, Cpustat is a powerful tool for monitoring CPU utilization in Linux, with several advantages over other popular system monitoring tools.

Conclusion

Cpustat is a powerful and flexible tool for monitoring CPU utilization by running processes in Linux. It provides a wealth of information on system performance, allowing users to identify performance bottlenecks and optimize system resources. Cpustat's interactive mode makes it easy to view the CPU usage of running processes in a convenient and easy-to-understand way.

With its ability to provide historical data on CPU usage, Cpustat can be used for troubleshooting as well. Additionally, the tool's scripting capabilities make it useful for automating tasks and generating custom reports on system performance.

With its intuitive interface and customizable options, Cpustat is an essential tool for any Linux user who needs to monitor system performance.

Updated on: 26-Jun-2023

236 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements