File: //scripts/storage_alert.sh
#!/bin/bash
# ----------------------------
# Configuration
# ----------------------------
THRESHOLD=99 # Alert when usage >= 99%
PHONE="918200010737" # WhatsApp number
SERVER_NAME="5.9.111.147 Shared Server"
LOG_FILE="/scripts/storage_alert.log"
LAST_USAGE_FILE="/tmp/last_storage_usage.txt"
# ----------------------------
# Ensure log directory exists
# ----------------------------
mkdir -p "$(dirname "$LOG_FILE")"
# ----------------------------
# Get root (/) usage
# ----------------------------
USAGE=$(df -P / | awk 'NR==2 {gsub("%",""); print $5}')
# ----------------------------
# Log current usage
# ----------------------------
echo "$(date '+%Y-%m-%d %H:%M:%S') - Current usage: ${USAGE}%" >> "$LOG_FILE"
# ----------------------------
# Read last sent usage
# ----------------------------
if [ -f "$LAST_USAGE_FILE" ]; then
LAST_SENT=$(cat "$LAST_USAGE_FILE")
else
LAST_SENT=0
fi
# ----------------------------
# Send alert if usage >= threshold AND alert not sent yet
# ----------------------------
if [ "$USAGE" -ge "$THRESHOLD" ] && [ "$LAST_SENT" -lt "$THRESHOLD" ]; then
JSON=$(cat <<EOF
{
"phone": "${PHONE}",
"message": "đ¨ *High Storage Alert* đ¨\n\nđ *Current Usage:* ${USAGE}%\n\nâ ď¸ *Warning:* Storage space is critically low!\n\nđ ď¸ *Action Required:* Free up space on *${SERVER_NAME}*"
}
EOF
)
echo "$(date '+%Y-%m-%d %H:%M:%S') - Sending WhatsApp alert..." >> "$LOG_FILE"
curl --location --request POST 'https://marketing.wp.karmanye.in/api/Karmanye/send-message' \
--header 'Content-Type: application/json; charset=utf-8' \
--header 'Accept: application/json' \
--header 'Authorization: Bearer $2b$10$f7yAQHm3Y1eOxs92aiEJ9eohJ0AdUQ2YwXxUARzk3sVLgKIVw0k2q' \
--data "$JSON" >> "$LOG_FILE" 2>&1
echo "$(date '+%Y-%m-%d %H:%M:%S') - Alert sent for ${USAGE}% usage." >> "$LOG_FILE"
# Update last sent usage
echo "$USAGE" > "$LAST_USAGE_FILE"
# ----------------------------
# Clear last usage if usage drops below threshold
# ----------------------------
elif [ "$USAGE" -lt "$THRESHOLD" ] && [ -f "$LAST_USAGE_FILE" ]; then
rm -f "$LAST_USAGE_FILE"
echo "$(date '+%Y-%m-%d %H:%M:%S') - Usage back to normal. Alert reset." >> "$LOG_FILE"
else
echo "$(date '+%Y-%m-%d %H:%M:%S') - No alert needed." >> "$LOG_FILE"
fi