-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcheck_dir_files.sh
171 lines (153 loc) · 3.72 KB
/
check_dir_files.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/bin/sh
#
# ## Plugin for Nagios to monitor directory size
# ## Written by Gerd Stammwitz (http://www.enbiz.de/)
# ##
# ## - 20040727 coded and tested for Solaris and Linux
# ## - 20041216 published on NagiosExchange
# ## - 20070710 modified by Jose Vicente Mondejar to add perfdata option
# ## - 20130707 modified by Felipe Ferreira to count number of files in directory
#
#
# ## You are free to use this script under the terms of the Gnu Public License.
# ## No guarantee - use at your own risc.
#
#
# Usage: ./check_dirsize -d <path> -w <warn> -c <crit>
#
# ## Description:
#
# This plugin determines the number of files inside a a directory (including sub dirs)
# and compares it with the supplied thresholds.
#PATH=""
LS="/bin/ls"
CUT="/usr/bin/cut"
WC="/usr/bin/wc"
GREP="/bin/grep"
FIND="/usr/bin/find"
PROGNAME=$0
PROGPATH=$(echo $0 | /bin/sed -e 's,[\\/][^\\/][^\\/]*$,,')
REVISION="Revision 1.1"
AUTHOR="(c) 2004,2007 Gerd Stammwitz (http://www.enbiz.de/) - Edited by Felipe Ferreira(2013)"
# Exit codes
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
STATE_DEPENDENT=4
print_revision() {
echo "$REVISION $AUTHOR"
}
print_usage() {
echo "Usage: $PROGNAME -d|--dirname <path> [-w|--warning <warn>] [-c|--critical <crit>] [-f|--perfdata]"
echo "Usage: $PROGNAME -h|--help"
echo "Usage: $PROGNAME -V|--version"
echo ""
echo "<warn> and <crit> must be expressed in KB"
}
print_help() {
print_revision $PROGNAME $REVISION
echo ""
echo "Directory size monitor plugin for Nagios"
echo ""
print_usage
echo ""
}
# Make sure the correct number of command line
# arguments have been supplied
if [ $# -lt 1 ]; then
print_usage
exit $STATE_UNKNOWN
fi
# Grab the command line arguments
thresh_warn=""
thresh_crit=""
perfdata=0
exitstatus=$STATE_WARNING #default
while test -n "$1"; do
case "$1" in
--help)
print_help
exit $STATE_OK
;;
-h)
print_help
exit $STATE_OK
;;
--version)
print_revision $PROGNAME $VERSION
exit $STATE_OK
;;
-V)
print_revision $PROGNAME $VERSION
exit $STATE_OK
;;
--dirname)
dirpath=$2
shift
;;
-d)
dirpath=$2
shift
;;
--warning)
thresh_warn=$2
shift
;;
-w)
thresh_warn=$2
shift
;;
--critical)
thresh_crit=$2
shift
;;
-c)
thresh_crit=$2
shift
;;
-f)
perfdata=1
;;
-perfdata)
perfdata=1
;;
*)
echo "Unknown argument: $1"
print_usage
exit $STATE_UNKNOWN
;;
esac
shift
done
##### Get size of specified directory
error=""
duresult=`$FIND $dirpath -type f |$WC -l || error="Error"`
if [ ! "$error" = "" ]; then
errtext=`echo $duresult`
echo "$error:$errtext"
exit $STATE_UNKNOWN
fi
dirsize=`echo $duresult`
result="OK"
exitstatus=$STATE_OK
##### Compare with thresholds
if [ "$thresh_warn" != "" ]; then
if [ $dirsize -ge $thresh_warn ]; then
result="WARNING"
exitstatus=$STATE_WARNING
fi
fi
if [ "$thresh_crit" != "" ]; then
if [ $dirsize -ge $thresh_crit ]; then
result="CRITICAL"
exitstatus=$STATE_CRITICAL
fi
fi
if [ $perfdata -eq 1 ]; then
result="$result - There are $dirsize files in dir $dirpath|'size'=${dirsize};${thresh_warn};${thresh_crit}"
echo $result
exit $exitstatus
fi
echo "$result - There are $dirsize files in dir $dirpath"
exit $exitstatus