-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisk-alert.sh
More file actions
105 lines (91 loc) · 3.33 KB
/
disk-alert.sh
File metadata and controls
105 lines (91 loc) · 3.33 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
105
#!/usr/bin/env bash
set -euo pipefail
THRESHOLD="${THRESHOLD:-80}"
TELEGRAM_BOT_TOKEN="${TELEGRAM_BOT_TOKEN:-}"
TELEGRAM_CHAT_ID="${TELEGRAM_CHAT_ID:-}"
MOUNT_FILTER="${MOUNT_FILTER:-}"
DRY_RUN=false
RED='\033[0;31m'
YELLOW='\033[1;33m'
GREEN='\033[0;32m'
NC='\033[0m'
_notify() {
[[ -z "$TELEGRAM_BOT_TOKEN" || -z "$TELEGRAM_CHAT_ID" ]] && return 0
curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" \
-d chat_id="$TELEGRAM_CHAT_ID" -d text="$1" -d parse_mode="Markdown" > /dev/null 2>&1 || true
}
_parse_args() {
while [[ $# -gt 0 ]]; do
case "$1" in
--threshold) THRESHOLD="${2:?--threshold requires a value}"; shift 2 ;;
--mount) MOUNT_FILTER="${2:?--mount requires a value}"; shift 2 ;;
--dry-run) DRY_RUN=true; shift ;;
--help|-h) show_help; exit 0 ;;
*) echo "Unknown option: $1" >&2; show_help; exit 1 ;;
esac
done
}
_get_partitions() {
df -P | awk 'NR>1 && /^\// {
gsub(/%/, "", $5)
print $5, $4, $6
}'
}
_check_disk() {
local alerts=0
local hostname
hostname=$(hostname)
while read -r usage _avail mount; do
[[ -z "$usage" ]] && continue
if [[ -n "$MOUNT_FILTER" ]] && [[ "$mount" != "$MOUNT_FILTER" ]]; then
continue
fi
if (( usage > THRESHOLD )); then
alerts=$((alerts + 1))
local avail_h
avail_h=$(df -h "$mount" 2>/dev/null | awk 'NR==2{print $4}')
if [[ "$DRY_RUN" == true ]]; then
echo -e "${YELLOW}[DRY-RUN]${NC} would alert: ${mount} at ${usage}% (${avail_h} free)"
else
echo -e "${RED}[ALERT]${NC} ${mount} — ${usage}% used (${avail_h} free, threshold: ${THRESHOLD}%)"
_notify "🚨 *Disk alert* on \`${hostname}\`: \`${mount}\` at *${usage}%* (${avail_h} free, threshold: ${THRESHOLD}%)"
fi
else
if [[ "$DRY_RUN" == true ]]; then
echo -e "${GREEN}[OK]${NC} ${mount} at ${usage}% — below threshold"
fi
fi
done < <(_get_partitions)
if [[ "$DRY_RUN" == true ]] && (( alerts == 0 )); then
echo -e "${GREEN}No partitions exceed ${THRESHOLD}% threshold${NC}"
fi
return 0
}
show_help() {
echo "disk-alert.sh — disk usage alert with Telegram notifications"
echo ""
echo "Usage:"
echo " ./disk-alert.sh [options]"
echo ""
echo "Options:"
echo " --threshold N Alert when usage exceeds N% (default: 80)"
echo " --mount /path Only check a specific mount point"
echo " --dry-run Preview alerts without sending notifications"
echo " --help, -h Show this help"
echo ""
echo "Environment variables:"
echo " THRESHOLD Same as --threshold"
echo " MOUNT_FILTER Same as --mount"
echo " TELEGRAM_BOT_TOKEN Bot token for Telegram alerts"
echo " TELEGRAM_CHAT_ID Chat ID for Telegram alerts"
echo ""
echo "Examples:"
echo " ./disk-alert.sh"
echo " ./disk-alert.sh --threshold 90 --mount /"
echo " ./disk-alert.sh --dry-run"
echo " THRESHOLD=75 ./disk-alert.sh"
echo " # Cron every hour:"
echo " 0 * * * * TELEGRAM_BOT_TOKEN=xxx TELEGRAM_CHAT_ID=yyy /path/to/disk-alert.sh"
}
_parse_args "$@"
_check_disk