HEX
Server: LiteSpeed
System: Linux CentOS-79-64-minimal 3.10.0-1160.119.1.el7.x86_64 #1 SMP Tue Jun 4 14:43:51 UTC 2024 x86_64
User: vishn3436 (5293)
PHP: 8.0.15
Disabled: NONE
Upload Files
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