-
Notifications
You must be signed in to change notification settings - Fork 8
/
disk_monitor.sh
44 lines (36 loc) · 1.39 KB
/
disk_monitor.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
#!/bin/bash
# Javier Ferrándiz Fernández - 27/07/2023 - https://github.com/javisys
# Disk space threshold (in percent)
threshold=80
# Function to send a notification by mail
send_notification() {
local subject="Alert: Disk space critically low"
local message="Disk space reached the threshold of $threshold%.\n\n"
local total_space=$(df -BG --output=avail / | awk 'NR==2 {print $1}')
local used_space=$(df -BG --output=pcent / | awk 'NR==2 {print $1}')
message+="Total space available: ${total_space}GB\n"
message+="Space used: ${used_space}%\n"
echo -e "$message" | mail -s "$subject" your_mail@example.com
}
# Function to check disk space and send a notification if necessary
check_disk_space() {
local total_space=$(df -BG --output=avail / | awk 'NR==2 {print $1}' | sed 's/G//')
local used_space=$(df -BG --output=pcent / | awk 'NR==2 {print $1}' | sed 's/%//')
if [ "$used_space" -ge "$thresold" ];
then
echo "Warning! Disk space reached the threshold of $thresold%"
echo "Total space available: ${total_space}GB"
echo "Space used: ${used_space}%"
send_notification
else
echo "Disk space: ${used_space}%. All in order."
fi
}
# Time interval for checking disk space (in seconds)
check_interval=3600
# Infinite loop to monitor disk space periodically
while true;
do
check_disk_space
sleep "check_interval"
done