#!/bin/bash # This script monitors disk space and sends an alert if usage exceeds a specified threshold. # Usage: ./storage-monitor.sh [mount-point] [threshold] [url_to_call] # Check if correct number of arguments provided if [ $# -lt 2 ]; then echo "Usage: $0 [mount-point] [threshold] [url_to_call]" echo "Example: $0 / 80 https://example.com/alert" echo "Threshold should be a percentage (0-100)" exit 1 fi # Assign arguments to variables MOUNT_POINT="$1" THRESHOLD="$2" URL_TO_CALL="$3" # Validate that mount point exists if [ ! -d "$MOUNT_POINT" ]; then echo "Error: Mount point '$MOUNT_POINT' does not exist" exit 1 fi # Validate threshold is a number between 0 and 100 if ! [[ "$THRESHOLD" =~ ^[0-9]+$ ]] || [ "$THRESHOLD" -lt 0 ] || [ "$THRESHOLD" -gt 100 ]; then echo "Error: Threshold must be a number between 0 and 100" exit 1 fi # Get disk usage percentage (remove % sign) USAGE=$(df "$MOUNT_POINT" | awk 'NR==2 {print $5}' | sed 's/%//') # Check if we got a valid usage value if ! [[ "$USAGE" =~ ^[0-9]+$ ]]; then echo "Error: Could not determine disk usage for '$MOUNT_POINT'" exit 1 fi # Display current usage echo "Current disk usage for $MOUNT_POINT: $USAGE%" # Check if usage exceeds threshold if [ "$USAGE" -gt "$THRESHOLD" ]; then echo "ALERT: Disk usage ($USAGE%) exceeds threshold ($THRESHOLD%)" # Prepare alert message MESSAGE="ALERT: Disk usage on $MOUNT_POINT is at $USAGE%, exceeding threshold of $THRESHOLD%" # If URL is provided, send HTTP request if [ -n "$URL_TO_CALL" ]; then echo "Sending alert to: $URL_TO_CALL" # Try to send alert using curl (most common) if command -v curl &> /dev/null; then curl -X POST -d "message=$MESSAGE" "$URL_TO_CALL" 2>/dev/null # Fallback to wget if curl is not available elif command -v wget &> /dev/null; then wget --post-data="message=$MESSAGE" "$URL_TO_CALL" -O /dev/null 2>/dev/null else echo "Warning: Neither curl nor wget is available to send HTTP request" fi fi # Exit with error code to indicate alert condition exit 2 else echo "Disk usage ($USAGE%) is within acceptable limits (threshold: $THRESHOLD%)" exit 0 fi # Usage: ./storage-monitor.sh [mount-point] [threshold] [url_to_call]