-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdisk_usage_monitor.sh
More file actions
53 lines (38 loc) · 1.67 KB
/
disk_usage_monitor.sh
File metadata and controls
53 lines (38 loc) · 1.67 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
#!/bin/bash
# =====================================================
# Script Name : disk_usage_monitor.sh
# Purpose : Monitor disk usage and alert if threshold exceeded
# Usage : ./disk_usage_monitor.sh
# =====================================================
# Threshold value (in percentage)
THRESHOLD=80
# Log file location
LOG_FILE="/var/log/disk_usage_monitor.log"
# Date and hostname for logging
DATE=$(date +"%Y-%m-%d %H:%M:%S")
HOST=$(hostname)
# Fetch disk usage details (excluding tmpfs and overlay)
DISK_USAGE=$(df -h --output=pcent,target -x tmpfs -x overlay | tail -n +2)
echo "[$DATE] Checking disk usage on $HOST..." >> $LOG_FILE
while read -r usage mountpoint; do
# Remove '%' symbol
usage=${usage%\%}
if [ "$usage" -ge "$THRESHOLD" ]; then
echo "[$DATE] ALERT: Disk usage on $mountpoint is at ${usage}%!" | tee -a $LOG_FILE
# Optionally send an email alert (requires MTA configured)
# echo "Disk usage on $mountpoint has reached ${usage}% on $HOST" | mail -s "Disk Alert: $HOST" [email protected]
fi
done <<< "$DISK_USAGE"
echo "[$DATE] Disk usage check completed." >> $LOG_FILE
# ⚙️ How It Works
# Step Description
# df -h --output=pcent,target Gets disk usage % and mount points
# tail -n +2 Skips header line
# ${usage%\%} Removes % symbol
# if [ "$usage" -ge "$THRESHOLD" ]; then Compares against threshold
# tee -a $LOG_FILE Logs and prints alert simultaneously
# Optional mail command Sends alert email (if configured)
# 💡 Example Output
# [2025-10-30 12:10:45] Checking disk usage on ip-172-31-25-181...
# [2025-10-30 12:10:45] ALERT: Disk usage on / is at 85%!
# [2025-10-30 12:10:45] Disk usage check completed.