forked from jedda/OSX-Monitoring-Tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_folder_size.sh
88 lines (71 loc) · 1.93 KB
/
check_folder_size.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/bin/bash
# Check Folder Size
# by Dan Barrett
# http://yesdevnull.net
# v1.1 - 28 October 2013
# Added OS X 10.9 Support and fixes a bug where folders with spaces in their name would fail with du.
# v1.0 - 9 August 2013
# Initial release.
# Checks to see how large the folder is and warns or crits if over a specified size.
# Defaults to MB
# Arguments:
# -f Path to folder
# -b Block size (i.e. data returned in MB, KB or GB - enter as m, k or g)
# -w Warning threshold for storage used
# -c Critical threshold for storage used
# Example:
# ./check_folder_size.sh -f /Library/Application\ Support/ -w 2048 -c 4096
# Supports:
# Untested but I'm sure it works fine on OS X 10.6 and 10.7
# * OS X 10.8.x
# * OS X 10.9
folderPath=""
blockSize="m"
blockSizeFriendly="MB"
warnThresh=""
critThresh=""
# Get the flags!
while getopts "f:b:w:c:" opt
do
case $opt in
f ) folderPath=$OPTARG;;
b ) blockSize=$OPTARG;;
w ) warnThresh=$OPTARG;;
c ) critThresh=$OPTARG;;
esac
done
if [ "$folderPath" == "" ]
then
printf "ERROR - You must provide a file path with -f!\n"
exit 2
fi
if [ "$warnThresh" == "" ]
then
printf "ERROR - You must provide a warning threshold with -w!\n"
exit 2
fi
if [ "$critThresh" == "" ]
then
printf "ERROR - You must provide a critical threshold with -c!\n"
exit 2
fi
if [ "$blockSize" == "k" ]
then
blockSizeFriendly="KB"
fi
if [ "$blockSize" == "g" ]
then
blockSizeFriendly="GB"
fi
folderSize=`du -s$blockSize "$folderPath" | grep -E -o "[0-9]+"`
if [ "$folderSize" -ge "$critThresh" ]
then
printf "CRITICAL - folder is $folderSize $blockSizeFriendly in size | folderSize=$folderSize;$warnThresh;$critThresh;\n"
exit 2
elif [ "$folderSize" -ge "$warnThresh" ]
then
printf "WARNING - folder is $folderSize $blockSizeFriendly in size | folderSize=$folderSize;$warnThresh;$critThresh;\n"
exit 1
fi
printf "OK - folder is $folderSize $blockSizeFriendly in size | folderSize=$folderSize;$warnThresh;$critThresh;\n"
exit 0