avatarKarthick Dkk

Summary

The web content introduces Bash scripting for automation in Linux environments, focusing on disk space monitoring, service health checking, and system health monitoring.

Abstract

The article is part of a "DevOps in 90 Days" series and emphasizes the importance of Bash scripting for automating repetitive tasks in Linux. It provides practical examples of scripts to monitor disk space usage and alert administrators when thresholds are exceeded, as well as scripts to automatically restart services that have stopped and monitor overall system health, including memory, disk, and CPU usage. The article also suggests logging data to files for later analysis and setting up alert systems for critical resource usage. It concludes by encouraging readers to explore further automation possibilities and to connect with the author for more insights and updates.

Opinions

  • The author, Karthick DKK, positions Bash scripts as essential tools for DevOps and SysAdmin roles, highlighting their versatility and necessity for system maintenance and complex workflows.
  • The author expresses enthusiasm about the power of automation, suggesting that it can make system administrators feel like "superheroes" when tasks run on autopilot.
  • There is an emphasis on proactive system management, with scripts designed to alert administrators before issues like full disk partitions or downed services become critical.
  • The author encourages customization of the provided scripts to suit individual needs and to automate other services beyond those explicitly mentioned, such as nginx or mysql.
  • By providing a step-by-step approach and encouraging the reader to follow along, the author conveys a teaching and mentoring attitude, aiming to empower readers to become proficient in Bash scripting and Linux automation.
  • The article concludes with a call to action, inviting readers to engage with the author on LinkedIn and to subscribe to the Medium account for more updates and valuable information on DevOps practices.

Day 5: Automating Tasks with Bash Scripts: Part 1

Hey there, fellow Linux explorers! 👋 Welcome back to Day 5 of our DevOps in 90 Days series, where we dive into the world of automation using the magic of Bash scripting. If you’re tired of doing repetitive tasks manually and want to unleash the full potential of automation, buckle up. It’s time to write some scripts that not only save time but also make you feel like a superhero 🦸‍♂️ when your system runs on autopilot!

Why Bash Scripts?

Bash scripts are the Swiss Army knives of Linux. They can automate pretty much anything, from system maintenance to complex workflows. If you’re working in a DevOps or SysAdmin role (like me 🙋‍♂️), automating routine tasks is essential. Today, we’ll start by tackling disk space monitoring and service health checking — two vital tasks for keeping your system in check. 🚀

Let’s dive right into two awesome scripts!

Script 1: Disk Usage Monitoring and Alerts 🚨

Ever faced the horror of your / partition filling up unexpectedly? 😱 Or maybe your /home directory is hogging space like it’s preparing for the apocalypse. Well, no more sleepless nights! Here’s a simple script that checks if your root partition (/) is 90% full or if the /home directory exceeds 2 GB. If either condition is met, it’ll shoot an email to the admin to sound the alarm! 📧

The Script:

#!/bin/bash
# Admin email address
ADMIN="[email protected]"
# Get the percentage of used space on /
ROOT_USAGE=$(df / | grep / | awk '{print $5}' | sed 's/%//g')
# Check if the / partition usage is 90% or more
if [ "$ROOT_USAGE" -ge 90 ]; then
    echo "Warning: / partition usage is at ${ROOT_USAGE}%!" | mail -s "/ Partition Warning" $ADMIN
fi
# Check the size of the /home directory (in GB)
HOME_SIZE=$(du -sh /home | awk '{print $1}' | sed 's/G//g')
# Check if /home size exceeds 2GB
if (( $(echo "$HOME_SIZE > 2" | bc -l) )); then
    echo "Warning: /home size is ${HOME_SIZE}GB, exceeding 2GB!" | mail -s "/home Directory Warning" $ADMIN
fi

How it works:

  • Disk usage check: The script uses the df command to monitor the usage of the / partition. If it’s over 90%, it sends a warning email.
  • Directory size check: The script is used du to calculate the size of the /home directory. If it’s over 2 GB, it sends another email.
  • Email notifications: The mail command sends an alert to the system admin, allowing them to act before things get out of hand.

Setting Up the Script:

  1. Save the script as disk_monitor.sh.
  2. Make it executable:
chmod +x disk_monitor.sh

Schedule it to run periodically using cron. For example, to run the script every hour:

crontab -e

Add this line:

0 * * * * /path/to/disk_monitor.sh

That’s it! You’ll now receive an email whenever your disk usage crosses critical levels, saving your system from running out of space.

Script 2: Monitoring and Restarting Services 🛠️

Imagine this: You’re working on a project, and suddenly, the web server goes down. 😱 What if you could have a script that automatically checks whether a service is running and, if it isn’t, restarts it for you? Say goodbye to manual restarts!

The Script:

#!/bin/bash
# Define the service you want to monitor
SERVICE="apache2"
# Check if the service is running
if ! systemctl is-active --quiet $SERVICE; then
    # If not running, start the service
    systemctl start $SERVICE
    # Send an email notification to the admin
    echo "The $SERVICE service was down and has been restarted." | mail -s "$SERVICE Service Restarted" [email protected]
fi

How it works:

  • Service check: The script checks if the defined service (in this case, apache2) is running using systemctl is-active. If it’s not running, it starts the service.
  • Auto-restart: If the service is found to be down, the script restarts it with systemctl start.
  • Notification: Once the service is restarted, an email is sent to the admin, letting them know about the downtime and the restart action.

Setting Up the Script:

  1. Save the script as service_monitor.sh.
  2. Make it executable:
chmod +x service_monitor.sh
  1. Schedule it to run every minute using cron:
crontab -e
  1. Add this line to check the service status every minute:
* * * * * /path/to/service_monitor.sh

Now, your system will automatically monitor the service and restart it whenever necessary. You can easily customize this script for any other services you rely on, such as nginx, mysql, or even custom services.

Script 3:Monitor System Health ( Disk.Memory,CPU)

#! /bin/bash
TIME=$(date "+%Y-%m-%d %H:%M:%S")
echo -e "Time\tMemory\t\tDisk /root\tCPU"
seconds="3600"
end=$((SECONDS+seconds))
while [ $SECONDS -lt $end ]; do
MEMORY=$(free -m | awk 'NR==2{printf "%.f%%\t\t", $3*100/$2 }')
DISK=$(df -h | awk '$NF=="/"{printf "%s\t\t", $5}')
CPU=$(top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{printf("%.f\n", 100 - $1)}')
echo -e "$TIME\t$MEMORY$DISK$CPU"
sleep 3
done

This script is designed to monitor system resource usage (Memory, Disk, and CPU) for a period of one hour (3600 seconds). Here’s how the script works, and I’ll explain each part in detail:

Script Explanation:

#!/bin/bash
  • Shebang: This tells the system to use Bash to run the script.
TIME=$(date "+%Y-%m-%d %H:%M:%S")
  • TIME Variable: Captures the current date and time in the format YYYY-MM-DD HH:MM:SS. This value is displayed with each iteration of the resource check.
echo -e "Time\t\t\tMemory\t\tDisk /root\tCPU"
  • Header Output: Prints a table header for the output: Time, Memory, Disk usage of /root, and CPU usage.
seconds="3600"
end=$((SECONDS+seconds))
  • Timer Setup: This sets the script to run for 3600 seconds (one hour). The SECONDS variable is a special Bash variable that tracks how long the script has been running.
while [ $SECONDS -lt $end ]; do
  • While Loop: This loop will keep running until the current SECONDS value exceeds the total time set by end.

Resource Monitoring:

Memory Usage:

MEMORY=$(free -m | awk 'NR=2{printf "%.f%%\t\t", $3*100/$2 }')
  • Command: free -m shows memory usage in megabytes.
  • Explanation: The awk command selects the second line (main memory line) and calculates the memory usage percentage by dividing used memory ($3) by total memory ($2), then prints it with %.f for no decimal points.

Disk Usage:

DISK=$(df -h | awk '$NF=="/"{printf "%s\t\t", $5}')
  • Command: df -h shows disk space usage.
  • Explanation: The awk command looks for the line where the mount point is /, and extracts the disk usage percentage ($5).

CPU Usage:

CPU=$(top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{printf("%.f\n", 100 - $1)}')
  • Command: top -bn1 gets real-time CPU usage data.
  • Explanation: The grep and sed commands extract the idle CPU percentage, then the script subtracts that value from 100 to get the CPU usage percentage. The result is formatted to show whole numbers (%.f).

Output:

echo -e "$TIME\t$MEMORY$DISK$CPU"
  • Display: This line prints the current time, memory usage, disk usage, and CPU usage in one line, aligned with the header.

Wait and Repeat:

sleep 3
done
  • Sleep: The script waits for 3 seconds between checks. After this pause, it repeats the loop until the total time of 3600 seconds has passed.

Example Output:

Time                Memory         Disk /root      CPU
2024-10-05 14:10:00 67%            40%             23
2024-10-05 14:10:03 68%            40%             25
2024-10-05 14:10:06 67%            40%             21

Improvements You Can Make:

  1. Log to a File: You can direct the output to a log file for later analysis.
echo -e "$TIME\t$MEMORY$DISK$CPU" >> /path/to/logfile.txt

Alert System: You could add a check for high CPU or disk usage, and send an alert (email or notification) when a threshold is exceeded.

Let me know if you want to modify or expand this script!

Need more fun ! Checkout below scripts 👇

Wrapping Up✌️

And that’s a wrap for Part 1 of Automating Tasks with Bash Scripts! With just a few lines of Bash code, you’ve automated two critical tasks:

  • Monitoring disk space and alerting you before it’s too late.
  • Keeping services alive by automatically restarting them if they crash.

These scripts save you the headache of manual monitoring and provide peace of mind knowing that your system will keep running smoothly. In Part 2, we’ll dive deeper into more complex automation, covering topics like log rotation, backup scheduling, and automated security audits.

Until next time, happy scripting! 🤓

Want to Learn More?

If you enjoyed this post and want more hands-on scripts, feel free to check out my other articles and join me in exploring the endless possibilities of Linux automation. Let’s turn you into a Bash scripting ninja! 🐱‍👤

Stay tuned for more DevOps awesomeness!

✅✅feel free to connect with us.

LinkedIn: https://www.linkedin.com/in/karthick-dkk/

Follow my Medium Account (To get valuable information)

For more updates: subscribe to this medium account.

Follow for more: ✌️

LinkedIn: https://www.linkedin.com/in/karthick-dkk/

Linux
DevOps
AWS
Shell Script
Devopsin90days
Recommended from ReadMedium