-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathdisk_usage_monitor.sh
More file actions
executable file
·59 lines (46 loc) · 1.87 KB
/
disk_usage_monitor.sh
File metadata and controls
executable file
·59 lines (46 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/bin/bash
# =============================================
# ezEML Disk Usage Monitor with 90% Alert
# =============================================
TARGET_DIR="/home/pasta/ezeml/user-data"
LOG_FILE="${TARGET_DIR}/disk_usage.log"
ALERT_EMAIL="support@edirepository.org"
THRESHOLD=90
# Ensure log file exists
touch "$LOG_FILE"
TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S')
echo "==================================================" >> "$LOG_FILE"
echo "Disk Usage Report - $TIMESTAMP" >> "$LOG_FILE"
echo "==================================================" >> "$LOG_FILE"
echo "" >> "$LOG_FILE"
log_command() {
echo ">>> $1" >> "$LOG_FILE"
echo "----------------------------------------" >> "$LOG_FILE"
eval "$1" >> "$LOG_FILE" 2>&1
echo "" >> "$LOG_FILE"
}
# Run the monitoring commands
log_command "df -h \"$TARGET_DIR\""
log_command "du -sh \"$TARGET_DIR\""
log_command "du -sh \"$TARGET_DIR\"/* | sort -hr | head -n 10"
echo "Disk usage check completed at $TIMESTAMP" >> "$LOG_FILE"
echo "" >> "$LOG_FILE"
# === Check disk usage percentage and send alert if over threshold ===
USAGE_PERCENT=$(df "$TARGET_DIR" | awk 'NR==2 {print $5}' | tr -d '%')
if [ "$USAGE_PERCENT" -ge "$THRESHOLD" ]; then
SUBJECT="⚠️ ezEML Disk Usage Alert: ${USAGE_PERCENT}% on $(hostname)"
{
echo "WARNING: ezEML user-data directory disk usage has exceeded ${THRESHOLD}%!"
echo ""
echo "Current usage: ${USAGE_PERCENT}%"
echo "Directory: ${TARGET_DIR}"
echo "Hostname: $(hostname)"
echo "Time: ${TIMESTAMP}"
echo ""
echo "=== Full Disk Usage Report ==="
echo ""
cat "$LOG_FILE" | tail -n 100 # Last 100 lines (recent report)
} | mail -s "$SUBJECT" "$ALERT_EMAIL"
echo "🚨 ALERT: Usage at ${USAGE_PERCENT}% — Email sent to ${ALERT_EMAIL}" >> "$LOG_FILE"
fi
echo "✅ Disk usage check completed." >> "$LOG_FILE"