Day 4 - Part II: Monitor Your System Metrics with a Bash Script!

Day 4 - Part II: Monitor Your System Metrics with a Bash Script!

ยท

4 min read

๐Ÿ“ Introduction:

Are you looking for a simple and efficient way to keep track of your system's performance? Look no further! In this blog post, we'll walk you through the creation of a powerful Bash script that monitors essential system metrics, including CPU usage, memory usage, disk space, and even specific services like Nginx. Our script comes with a user-friendly interface and allows continuous monitoring with a customizable sleep interval.

โญ๏ธ Why Monitor System Metrics? โญ๏ธ

Monitoring system metrics is crucial for several reasons:

  • It helps you identify performance bottlenecks and potential issues before they escalate.

  • You can make informed decisions about system resource allocation and optimization.

  • By keeping an eye on specific services like Nginx, you ensure critical components are up and running as expected.

๐Ÿ“œ The Tasks: Implementing the Perfect Monitoring Script ๐Ÿ“œ

  1. Implementing Basic Metrics Monitoring ๐Ÿ“Š We start by fetching essential metrics using the top, free, and df commands. These commands provide real-time data on CPU usage, memory usage, and disk space, respectively.

  2. User-Friendly Interface ๐Ÿค A user-friendly interface enhances the user experience. Our script will display a clear menu with options to view metrics and an exit option.

  3. Continuous Monitoring with Sleep โฐ Continuous monitoring is a key feature. We'll introduce a loop to keep fetching metrics until the user decides to exit. We'll also add a "sleep" mechanism to pause monitoring at a specified interval.

  4. Monitoring Nginx Service ๐ŸŒ Extending the script's capabilities, we'll check if the Nginx service is running and display its status. If it's not running, the user can start it using the script.

  5. Monitoring a Different Service ๐Ÿ› ๏ธ We'll take it a step further by allowing users to monitor any service of their choice. The script will prompt the user to enter the service name and display its status accordingly.

  6. Error Handling โŒ To make the script robust, we'll implement error handling. Meaningful error messages will guide users on troubleshooting and fixing issues.

๐Ÿ“ Let's Dive into the Script! ๐Ÿ“

Below is the shell script, structured with bullet points, emojis, and professional formatting:

#!/bin/bash

# Function to display menu
show_menu() {
    echo "===== System Metrics Monitoring ====="
    echo "1. View CPU Usage"
    echo "2. View Memory Usage"
    echo "3. View Disk Space"
    echo "4. Monitor Nginx Service"
    echo "5. Monitor a Different Service"
    echo "6. Exit"
    echo "====================================="
    echo
}

# Function to display CPU usage
show_cpu_usage() {
    top -n 1
}

# Function to display memory usage
show_memory_usage() {
    free -h
}

# Function to display disk space
show_disk_space() {
    df -h
}

# Function to check and start Nginx service
monitor_nginx_service() {
    # Check if Nginx is running
    if systemctl is-active --quiet nginx; then
        echo "Nginx service is running."
    else
        echo "Nginx service is not running."
        read -p "Do you want to start Nginx? (y/n): " choice
        if [[ $choice == "y" ]]; then
            sudo systemctl start nginx
            echo "Nginx service has been started."
        fi
    fi
}

# Function to monitor a specific service
monitor_specific_service() {
    read -p "Enter the name of the service to monitor: " service_name
    if systemctl is-active --quiet "$service_name"; then
        echo "$service_name service is running."
    else
        echo "$service_name service is not running."
    fi
}

# Function for error handling
handle_error() {
    echo "Error: $1"
    exit 1
}

# Clear the screen before the loop starts
clear

# Main script
while true; do
    show_menu
    read -p "Enter your choice: " choice
    echo

    case $choice in
        1) show_cpu_usage ;;
        2) show_memory_usage ;;
        3) show_disk_space ;;
        4) monitor_nginx_service ;;
        5) monitor_specific_service ;;
        6) exit 0 ;;
        *) handle_error "Invalid choice. Please enter a valid option." ;;
    esac

    echo
    read -p "Press Enter to continue..." -t 3
    echo
done

๐Ÿ” Running the Script ๐Ÿ”

Once you've saved the script to a file (e.g., monitor_metrics.sh) and granted it execute permission, you can run it with the following command:

./monitor_metrics.sh

๐ŸŽ‰ Monitor Your System Metrics Effortlessly ๐ŸŽ‰

With this powerful and user-friendly Bash script, you can now effortlessly monitor your system's performance and crucial services. Use it to stay ahead of potential issues, optimize resource allocation, and keep your system running smoothly.

๐Ÿ” Checkout GitHub Repository for projects:

๐Ÿ”— github.com/sumanprasad007

๐Ÿ” Check out my YouTube channel - Prasad Suman Mohan:

๐Ÿ”— youtube.com/@sumanprasad007

Did you find this article valuable?

Support Prasad Suman Mohan by becoming a sponsor. Any amount is appreciated!

ย