Building a Real-Time System Monitoring Dashboard with Bash

Pratik Nalawade - Aug 27 - - Dev Community

In today’s fast-paced IT environments, monitoring system resources in real time is crucial for maintaining the health and performance of servers. Whether you’re managing a proxy server or any other critical infrastructure, having a lightweight, customizable monitoring solution can make a big difference. In this blog post, I’ll walk you through how to create a real-time system monitoring dashboard using a Bash script. This script will allow you to keep an eye on CPU usage, network activity, disk space, and more—all from your terminal.

Why Bash?

Bash is a powerful and flexible tool that comes pre-installed on most Linux systems. While there are many sophisticated monitoring tools available, Bash offers a simple, no-frills approach that’s easy to customize and deploy. Plus, it’s a great way to deepen your understanding of system resources and shell scripting.

Key Features of the Script

The monitoring automation script we’ll build includes the following features:

  1. Top 10 Most Used Applications: Displays the top 10 processes consuming the most CPU and memory.
  2. Network Monitoring: Tracks the number of concurrent connections, packet drops, and data transfer rates.
  3. Disk Usage: Monitors disk space usage, highlighting partitions using more than 80% of their capacity.
  4. System Load: Shows the current load average and a detailed breakdown of CPU usage.
  5. Memory Usage: Provides an overview of total, used, and free memory, including swap usage.
  6. Process Monitoring: Lists the number of active processes and the top 5 resource-intensive ones.
  7. Service Monitoring: Checks the status of essential services like sshd, nginx/apache, and iptables.
  8. Custom Dashboard: Allows users to view specific sections of the dashboard using command-line switches.

Building the Script

Let’s dive into how we can build this script. Below is a breakdown of each section.

1. Monitoring Top Applications

To display the top 10 applications consuming the most CPU and memory, we use the ps command combined with sort:

ps aux --sort=-%cpu | head -n 11
ps aux --sort=-%mem | head -n 11
Enter fullscreen mode Exit fullscreen mode

This provides a quick overview of the most resource-hungry processes.

2. Network Monitoring

For network monitoring, we need to track concurrent connections, packet drops, and data transfer rates. We use netstat and ifconfig for this:

netstat -an | grep ESTABLISHED | wc -l
ifconfig eth0 | grep 'RX packets'
ifconfig eth0 | grep 'TX packets'
Enter fullscreen mode Exit fullscreen mode

These commands give us insights into the network activity in real-time.

3. Disk Usage

Monitoring disk usage is crucial for preventing storage issues. Here’s how we can highlight partitions using more than 80% of disk space:

df -h | awk '$5 > 80 {print $0}'
Enter fullscreen mode Exit fullscreen mode

This snippet checks the disk usage and flags any partitions that exceed the 80% threshold.

4. System Load

To monitor the system load, we use the uptime command:

uptime
Enter fullscreen mode Exit fullscreen mode

This command gives a snapshot of the load average, which indicates how many processes are demanding CPU time.

5. Memory Usage

Memory usage is tracked using free:

free -h
Enter fullscreen mode Exit fullscreen mode

This provides a detailed look at total, used, and free memory, including swap space.

6. Process Monitoring

We can easily monitor active processes and identify the top 5 resource-intensive ones:

ps -e | wc -l
ps aux --sort=-%cpu | head -n 6
Enter fullscreen mode Exit fullscreen mode

This combination helps in understanding the process load on the system.

7. Service Monitoring

Ensuring essential services are running is vital. We can check the status of critical services like sshd and iptables using systemctl:

systemctl is-active sshd
systemctl is-active iptables
Enter fullscreen mode Exit fullscreen mode

These checks ensure that critical services are operational and correctly configured.

8. Custom Dashboard

Finally, to make the script more flexible, we add command-line switches to allow users to view specific parts of the dashboard:

case "$1" in
    -cpu) monitor_cpu ;;
    -memory) monitor_memory ;;
    -network) monitor_network ;;
    -disk) monitor_disk ;;
    *) echo "Usage: $0 {-cpu|-memory|-network|-disk}" ;;
esac
Enter fullscreen mode Exit fullscreen mode

This modular approach makes it easy to focus on the metrics that matter most at any given time.

Lessons Learned

While building this script, I encountered a few challenges. For example, I initially forgot to install net-tools, which includes the netstat command necessary for network monitoring. This was quickly resolved by installing the package via sudo apt-get install net-tools. Additionally, ensuring that the script was executable required setting the correct permissions with chmod +x.

Conclusion

This Bash script provides a powerful, lightweight way to monitor system resources in real-time. It’s easy to customize and deploy, making it a valuable tool for anyone managing Linux servers. Whether you’re a seasoned sysadmin or just getting started with shell scripting, this project offers a hands-on way to deepen your understanding of system monitoring.

You can find the full script and documentation in my GitHub repository. Feel free to clone, modify, and use it as a starting point for your own monitoring solutions.

. . . . . . . . . . . . . .