07 ‐ Disk Usage Monitoring Script Documentation - SanjeevOCI/Ocidocs GitHub Wiki
Overview This script is designed to monitor the disk usage of all root mount points on a Unix-based system. It retrieves the current disk usage statistics and highlights any mount points that have a usage percentage above a specified threshold (85% in this case).
Features Retrieves disk usage information using the df -h command. Prints the header of the disk usage information for reference. Iterates over each line of the disk usage output to check the usage percentage. Highlights lines where the usage percentage exceeds 85% in red for easy identification. Prints lines with usage below the threshold normally.
Usage Save the script to a file, for example, check_disk_usage.sh. Make the script executable by running the following command:
chmod +x check_disk_usage.sh
Execute the script:
./check_disk_usage.sh
Script Details Header Printing: The script prints the header of the df -h output to provide context for the disk usage information. Usage Extraction: The script extracts the usage percentage from each line of the df -h output using awk and sed. Threshold Check: The script checks if the extracted usage percentage is greater than 85%. Highlighting: If the usage percentage exceeds 85%, the line is highlighted in red using ANSI escape codes. Otherwise, the line is printed normally.
Example Output The script will produce an output similar to the following:
Filesystem Size Used Avail Use% Mounted on /dev/mapper/ocivolume-root 36G 8.0G 28G 23% / /dev/sda2 1014M 329M 686M 33% /boot /dev/mapper/ocivolume-oled 10G 400M 9.7G 4% /var/oled ...
Lines with usage above 85% will be highlighted in red.
Script Here is the script for reference:
#!/bin/bash
Get the disk usage information
df_output=$(df -h)
Print the header
echo "$df_output" | head -n 1
Iterate over each line of the df output
echo "$df_output" | tail -n +2 | while read -r line; do
Extract the usage percentage
usage=$(echo $line | awk '{print $5}' | sed 's/%//')
Check if the usage is above 85%
if [ "$usage" -gt 85 ]; then # Highlight the line echo -e "\e[31m$line\e[0m" else # Print the line normally echo "$line" fi done