Build a Linux System Monitor Using Bash

LinuxLinuxBeginner
Practice Now

Introduction

In this project, you will learn how to create a Linux system monitor using a shell script. The system monitor will continuously monitor the CPU, memory, and disk usage of your Linux system and display the usage percentages. If any of the thresholds for CPU, memory, or disk usage are exceeded, an alert will be sent. You can customize the threshold values as per your requirements and add your own logic to send alerts.

Linux System Monitor

ðŸŽŊ Tasks

In this project, you will learn:

  • How to create a shell script to monitor system resources
  • How to set threshold values for CPU, memory, and disk usage
  • How to define a function to send alerts when thresholds are exceeded

🏆 Achievements

After completing this project, you will be able to:

  • Create a Linux system monitor using a shell script
  • Monitor the CPU, memory, and disk usage of your Linux system
  • Customize the threshold values and add your own logic to send alerts

Create the project files

  1. Open a text editor of your choice.
  2. Create a new file and name it system_monitor.sh.
cd ~/project
touch system_monitor.sh
  1. Copy the following code into the system_monitor.sh file:
#!/bin/bash

## Define the threshold values for CPU, memory, and disk usage (in percentage)
CPU_THRESHOLD=80
MEMORY_THRESHOLD=80
DISK_THRESHOLD=80

In this step, we are setting the threshold values for CPU, memory, and disk usage. You can modify these values as per your requirements. The values are set in percentage.

âœĻ Check Solution and Practice

Define the send_alert function

## Function to send an alert
send_alert() {
  echo "$(tput setaf 1)ALERT: $1 exceeded threshold! Current value: $2%$(tput sgr0)"
  ## You can add your own logic here to send an email or notification to the user
}

This function is responsible for sending an alert when any of the threshold values are exceeded. In this code, an alert message is printed to the console using echo. You can customize this function to send alerts via email or notifications to the user.

âœĻ Check Solution and Practice

Define the monitor_system function

## Main function to monitor system resources
monitor_system() {
  while true; do
    ## Get CPU usage percentage
    cpu_usage=$(top -bn1 | grep "Cpu(s)" | awk '{print $2 + $4}')
    cpu_usage=${cpu_usage%.*} ## Convert to integer

    ## Get memory usage percentage
    memory_usage=$(free | awk '/Mem/ {printf("%3.1f", ($3/$2) * 100)}')

    ## Get disk usage percentage of the root directory
    disk_usage=$(df -h / | awk '/\// {print $(NF-1)}')
    disk_usage=${disk_usage%?} ## Remove the % sign

    ## Check if any threshold is exceeded
    if ((cpu_usage >= CPU_THRESHOLD)); then
      send_alert "CPU" "$cpu_usage"
    fi

    if ((memory_usage >= MEMORY_THRESHOLD)); then
      send_alert "Memory" "$memory_usage"
    fi

    if ((disk_usage >= DISK_THRESHOLD)); then
      send_alert "Disk Usage" "$disk_usage"
    fi

    ## Format and display the output
    clear
    printf "%-15s %s\n" "Resource" "Usage (%)"
    echo "----------------------"
    printf "%-15s %s\n" "CPU Usage" "$cpu_usage"
    printf "%-15s %s\n" "Memory Usage" "$memory_usage"
    printf "%-15s %s\n" "Disk Usage" "$disk_usage"

    sleep 1 ## Adjust the interval as per your preference
  done
}

This function is the main part of the system monitor. It runs an infinite loop to continuously monitor the system resources. Inside the loop, it retrieves the CPU, memory, and disk usage percentages. Then, it checks if any of the thresholds are exceeded and calls the send_alert function if necessary. Finally, it displays the resource usage on the console using printf and echo statements.

âœĻ Check Solution and Practice

Start monitoring the system

## Start monitoring the system
monitor_system

This line of code calls the monitor_system function, which initiates the system monitoring process. Once this line is executed, the system monitor will start running and continuously display the resource usage and check for threshold exceedances.

âœĻ Check Solution and Practice

Save the file

  1. Save the system_monitor.sh file in a location of your choice.
  2. Run the script using the following command:
bash system_monitor.sh
Linux System Monitor
âœĻ Check Solution and Practice

Summary

Congratulations! You have successfully created a Linux system monitor using a bash script. The script monitors the CPU, memory, and disk usage of your Linux system and displays an alert if any resources exceed a set threshold. You can use this script to keep track of resource usage and take necessary actions if required. Remember to adjust the threshold values and the monitoring interval according to your needs.

Other Linux Tutorials you may like