How to display top K nodes w.r.t CPU usage?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Monitoring CPU usage on individual nodes within a distributed system is crucial for performance optimization, load balancing, and system health maintenance. Whether you are administering a small network of computers, a corporate data center, or a cluster of nodes in a cloud environment, being able to quickly ascertain which nodes are consuming the most CPU resources can help in making informed decisions about resource allocation and troubleshooting. Below, we explore several methods and tools to display the top K nodes with respect to CPU usage.
1. Using Command Line Tools
1.1. top and htop
On Unix-like systems, the top command provides a dynamic real-time view of a running system. It can display system summary information and a list of processes or threads currently being managed by the Linux kernel. The usage as per CPU for individual nodes can typically be seen in clusters where each node runs its instance of the system.
To check the CPU usage:
However, top cannot aggregate data from multiple nodes. For such needs, tools like htop can be installed, which provide a more colorful and manageable interface. Note that htop also does not natively support aggregation across nodes but offers a more straightforward interface for managing processes and viewing system stats.
1.2. Custom Scripts for Aggregation
To display the top K nodes, you might need to write custom scripts that run top or gather CPU usage information from each node, then aggregate and sort this information. An example using a hypothetical scenario where you have SSH access and the same credentials on each node:
This script connects to each node listed, runs top in batch mode for one iteration, and appends the first ten lines (usually where CPU info resides) into a single file.
2. Using Monitoring Tools
2.1. Prometheus and Grafana
For more sophisticated monitoring across multiple systems and aggregation of data, you can use tools like Prometheus and Grafana.
- Prometheus is an open-source system monitoring and alerting toolkit. It can collect and store its metrics as time-series data. You can configure it to pull CPU usage metrics from each node at specific intervals.
- Grafana is an open-source platform for monitoring and observability. Grafana allows you to query, visualize, alert on, and understand your metrics no matter where they are stored.
Setup:
- Install Prometheus on a central server.
- Configure Prometheus to scrape metrics from exporters running on each node.
- Use node exporters to expose the CPU usage metrics on each node.
- Install and configure Grafana to use Prometheus as its data source.
- Create a dashboard in Grafana to visualize the top K nodes by CPU usage.
2.2. Nagios
Nagios is another robust monitoring tool capable of monitoring system metrics, network protocols, applications, services, servers, and network infrastructure. Alerts can be configured to react to particular conditions of CPU usage.
3. Cloud Services
Cloud providers like AWS, Azure, and Google Cloud Platform offer built-in tools to monitor and visualize CPU usage across varying instances.
- AWS CloudWatch: Monitors AWS resources and applications in real-time. You can use it to collect and track metrics, set alarms, and automatically react to changes in your AWS resources.
- Azure Monitor: Provides detailed and actionable insights to monitor applications, networks, and storage.
- Google Cloud Monitoring: Provides visibility into the performance, uptime, and overall health of cloud-powered applications.
Summary Table
| Tool / Method | Cloud / Local | Real-time Monitoring | Centralized Management | Details |
top / Custom Script | Local | Yes | No | Suitable for individual or a small set of nodes |
| Prometheus + Grafana | Both | Yes | Yes | Scalable for large deployments, provides visualization capabilities |
| Nagios | Both | Yes | Yes | Comprehensive monitoring with alerting mechanism |
| AWS CloudWatch | Cloud | Yes | Yes | Integrated with AWS ecosystem, provides automation |
| Azure Monitor | Cloud | Yes | Yes | Deep integration with Azure services |
| Google Cloud Monitor | Cloud | Yes | Yes | Works well with GCP services, offers seamless scalability |
Conclusion
Choosing the right method to monitor and display the top K nodes by CPU usage depends significantly on the specific requirements like the size of the deployment, whether the nodes are on a local network or distributed across cloud providers, and the level of detail and control needed. For larger and more dynamic environments, comprehensive tools like Prometheus combined with Grafana or specialized services from cloud providers offer significant advantages in scalability and depth of insight.
Related reading
- How to distribute tasks between servers where each task must be done by only one server?
- How to distribute the server Load
- How to divide Liquibase package structure for dev and prod environment in Spring Boot?
- How to download the latest build artifacts from Azure DevOps programmatically?
- How to divide a set into two sets such that the difference of the average is minimum?
- How to divide a set into two subsets such that difference between the sum of numbers in two sets is minimal?
- How to edit Docker container files from the host?
- How to edit files in stopped/not starting docker container

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.