Repository navigation
Expand file tree
/
Copy pathcheck-disk-space
More file actions
executable file
·104 lines (84 loc) · 2.8 KB
/
Copy pathcheck-disk-space
File metadata and controls
executable file
·104 lines (84 loc) · 2.8 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/bin/bash
# Disk Space Monitor
# Checks disk usage and sends notifications when thresholds are exceeded
# Configuration
WARNING_THRESHOLD=85 # Warning at 85%
CRITICAL_THRESHOLD=90 # Critical at 90%
LOG_FILE="$HOME/.notes/disk-monitor.log"
# Ensure log directory exists
mkdir -p "$(dirname "$LOG_FILE")"
# Function to send macOS notification
send_notification() {
local title="$1"
local message="$2"
local sound="$3"
osascript -e "display notification \"$message\" with title \"$title\" sound name \"$sound\""
}
# Function to log message
log_message() {
echo "$(date '+%Y-%m-%d %H:%M:%S'): $1" >> "$LOG_FILE"
}
# Function to get disk usage percentage for a filesystem
get_disk_usage() {
local filesystem="$1"
df -h "$filesystem" | awk 'NR==2 {gsub(/%/, "", $5); print $5}'
}
# Function to get available space
get_available_space() {
local filesystem="$1"
df -h "$filesystem" | awk 'NR==2 {print $4}'
}
# Check main filesystem (/)
main_usage=$(get_disk_usage "/")
main_available=$(get_available_space "/")
# Check data filesystem (/System/Volumes/Data)
data_usage=$(get_disk_usage "/System/Volumes/Data")
data_available=$(get_available_space "/System/Volumes/Data")
# log_message "Main: ${main_usage}% used (${main_available} free), Data: ${data_usage}% used (${data_available} free)"
# Check thresholds and send notifications
check_filesystem() {
local name="$1"
local usage="$2"
local available="$3"
if (( usage >= CRITICAL_THRESHOLD )); then
send_notification "🚨 CRITICAL: $name Disk Almost Full!" \
"$name filesystem is ${usage}% full (${available} remaining). Immediate action required!" \
"Basso"
log_message "CRITICAL: $name filesystem at ${usage}%"
return 2
elif (( usage >= WARNING_THRESHOLD )); then
send_notification "⚠️ WARNING: $name Disk Getting Full" \
"$name filesystem is ${usage}% full (${available} remaining). Consider cleaning up soon." \
"Purr"
log_message "WARNING: $name filesystem at ${usage}%"
return 1
fi
return 0
}
# Check both filesystems
exit_code=0
check_filesystem "Main" "$main_usage" "$main_available"
main_result=$?
check_filesystem "Data" "$data_usage" "$data_available"
data_result=$?
# Use highest exit code
if (( main_result > exit_code )); then
exit_code=$main_result
fi
if (( data_result > exit_code )); then
exit_code=$data_result
fi
# If critical, also suggest cleanup commands
if (( exit_code == 2 )); then
log_message "Suggested cleanup commands logged"
cat >> "$LOG_FILE" << 'EOF'
Suggested cleanup commands:
- brew cleanup --prune=all
- yarn cache clean
- pnpm store prune
- docker system prune -a -f --volumes
- rm -rf ~/.cache/uv ~/.cache/puppeteer
- xcrun simctl delete unavailable
EOF
fi
exit $exit_code