-
Notifications
You must be signed in to change notification settings - Fork 0
/
DiskUsage.sh
executable file
·73 lines (53 loc) · 2.53 KB
/
DiskUsage.sh
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
#!/bin/bash
# Author: Md Afzal Hassan Ehsaani
# Function to display a disk usage-themed banner with ASCII art
show_banner() {
clear # Clear the terminal screen for a clean output
echo "******************************************"
echo "| ██╗ ██╗███████╗███████╗ |"
echo "| ██║ ██║██╔════╝██╔════╝ |"
echo "| ██║ ██║█████╗ █████╗ |"
echo "| ██║ ██║██╔══╝ ██╔══╝ |"
echo "| ╚██████╔╝███████╗███████╗ |"
echo "| ╚═════╝ ╚══════╝╚══════╝ |"
echo "******************************************"
echo "| D I S K U S A G E M O N I T O R |"
echo "******************************************"
echo "| Author: Md Afzal Hassan Ehsaani |"
echo "|----------------------------------------|"
echo "| Checking disk usage and alerting |"
echo "| when usage exceeds the set threshold |"
echo "******************************************"
}
# Display the banner
show_banner
sleep 3 # Pause for 3 seconds for effect
THRESHOLD=80
# Log file to store output
LOG_FILE="/var/log/disk_usage_monitor.log"
# Function to log messages with a timestamp
log_message() {
local message="$1"
echo "$(date +'%Y-%m-%d %H:%M:%S') - $message" >> "$LOG_FILE"
}
# Check disk usage and print a warning if usage is above the threshold
df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }' | while read -r output; do
usage=$(echo "$output" | awk '{ print $1}' | cut -d'%' -f1)
partition=$(echo "$output" | awk '{ print $2 }')
if [ "$usage" -ge "$THRESHOLD" ]; then
echo ""
echo "******************************************"
echo "| ALERT: High Disk Usage! |"
echo "| Partition: $partition |"
echo "| Usage: ${usage}% |"
echo "| Take action: Scale or clean space |"
echo "******************************************"
echo ""
sleep 2 # Pause for readability
log_message "Warning: Disk usage on $partition is at ${usage}%"
else
echo "Disk usage on $partition is healthy at: ${usage}%"
fi
done
# Optionally: Send a summary report of the disk usage
log_message "Disk usage check completed."