1
+ #! /bin/sh
2
+
3
+ # check_disk_space.sh
4
+ #
5
+ # Description:
6
+ # Checks for available disk space and sends warning (via email) if below
7
+ # urgent/critical thresholds. For use on outdated servers that weren't properly
8
+ # maintained by previous 'owners' that are running on legacy/end-of-life
9
+ # operating system(s). On a properly maintained system, there are far more
10
+ # elegant ways to monitor disk space. The author(s) recommend you use those
11
+ # other methods instead of this software if/where at all possible.
12
+ #
13
+ # Usage: Install to location of your choice and setup a cronjob as root.
14
+ #
15
+ # Example: 00 12 * * * /root/sbin/check_disk_space.sh
16
+ #
17
+ # Author : Omar Asfour <[email protected] > https://omar.asfour.ca
18
+ # License : CC0 1.0 Universal (CC0 1.0) - Public Domain Dedication
19
+ # License URL: http://creativecommons.org/publicdomain/zero/1.0/
20
+ #
21
+ # To the extent possible under law, the author(s) have dedicated all copyright
22
+ # and related and neighboring rights to this software to the public domain
23
+ # worldwide.
24
+ #
25
+ # This software is distributed without any warranty. You should have received a
26
+ # copy of the CC0 Public Domain Dedication along with this software.
27
+ # If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
28
+ #
29
+
30
+ # ## CONFIGURATION ###
31
+ urgent=2048 # urgent threshold (in MiB - Mebibytes available))
32
+ critical=1024 # critical threshold
33
+
34
+ mailhost=10.0.0.2
35
+ mailport=25
36
+ fromname=" Server"
37
+
38
+ toname=" recipient"
39
+
40
+
41
+ # ## SUPPORTING FUNCTIONS ###
42
+
43
+ # Checks last status line returned by server.
44
+ # By default, expects '250' status; but can be invoked
45
+ # to check for other status codes
46
+ # eg. checkStatus "${sts}" "${line}" 220
47
+ function checkStatus {
48
+ expect=250
49
+ if [ $# -eq 3 ] ; then
50
+ expect=" ${3} "
51
+ fi
52
+ if [ $1 -ne $expect ] ; then
53
+ echo " Error: ${2} "
54
+ exit
55
+ fi
56
+ }
57
+
58
+ # Establishes connection to server and sends message
59
+ # Parameters:
60
+ # $1 = status (string)
61
+ # $2 = space_available (integer)
62
+ # eg. sendWarning 'critical' 1234
63
+ function sendWarning {
64
+ msgdate=$( date +" %a, %d %b %Y %T %z" )
65
+ msgstatus=$( echo " $1 " | tr ' [:lower:]' ' [:upper:]' )
66
+ subject=" Server Disk Space $msgstatus "
67
+ message=" Disk space $1 : $2 MiB"
68
+
69
+ # Open TCP/UDP Socket using file-descriptor '3'
70
+ exec 3<> /dev/tcp/${mailhost} /${mailport}
71
+
72
+ # Connect to SMTP Server
73
+ read -u 3 sts line
74
+ checkStatus " ${sts} " " ${line} " 220
75
+ echo " HELO ${mailhost} " >&3
76
+
77
+ read -u 3 sts line
78
+ checkStatus " $sts " " $line "
79
+ echo " MAIL FROM: <${fromaddr} >" >&3
80
+
81
+ read -u 3 sts line
82
+ checkStatus " $sts " " $line "
83
+ echo " RCPT TO: <${toaddr} >" >&3
84
+
85
+ read -u 3 sts line
86
+ checkStatus " $sts " " $line "
87
+ echo " DATA" >&3
88
+
89
+ read -u 3 sts line
90
+ checkStatus " ${sts} " " ${line} " 354
91
+
92
+ # Send Payload (message)
93
+ echo " Date: $msgdate " >&3
94
+ echo " From: $fromname <$fromaddr >" >&3
95
+ echo " To: $toname <$toaddr >" >&3
96
+ echo " Subject: $subject " >&3
97
+ echo " $message " >&3
98
+ echo " ." >&3
99
+
100
+ # Confirm Success & Quit
101
+ read -u 3 sts line
102
+ checkStatus " $sts " " $line "
103
+ echo " QUIT" >&3
104
+
105
+ # Confirm Quit(ted) successfully -- Not really necessary
106
+ read -u 3 sts line
107
+ checkStatus " ${sts} " " ${line} " 221
108
+ }
109
+
110
+ # ## MAIN (script entry point) ###
111
+
112
+ # Get available MiB on / mount-point
113
+ available=` df -m | grep ' /$' | awk ' {print $3}' `
114
+
115
+ # Compare avaialble disk space against thresholds and
116
+ # send warning if below threshold
117
+ if [[ $available -lt $critical ]]; then
118
+ sendWarning ' critical' $available
119
+ elif [[ $available -lt $urgent ]]; then
120
+ sendWarning ' urgent' $available
121
+ fi
0 commit comments