-
Notifications
You must be signed in to change notification settings - Fork 1
/
Check_mountpoints.sh
318 lines (290 loc) · 10.7 KB
/
Check_mountpoints.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#!/usr/bin/env bash
# --------------------------------------------------------------------
# configuration
# --------------------------------------------------------------------
PROGNAME=$(basename $0)
ERR_MESG=()
LOGGER="`which logger` -i -p kern.warn -t"
AUTO=0
AUTOIGNORE=0
IGNOREFSTAB=0
WRITETEST=0
NOAUTOCOND=1
NOAUTOIGNORE=0
DFARGS=''
EXCLUDE=none
KERNEL=`uname -s`
case $KERNEL in
# For solaris FSF=4 MF=3 FSTAB=/etc/vfstab MTAB=/etc/mnttab gnu grep and bash required
SunOS) FSF=4
MF=3
OF=6
NOAUTOSTR=no
FSTAB=/etc/vfstab
MTAB=/etc/mnttab
GREP=ggrep
;;
HP-UX) FSF=3
MF=2
OF=4
NOAUTOSTR=noauto
FSTAB=/etc/fstab
MTAB=/dev/mnttab
GREP=grep
;;
FreeBSD) FSF=3
MF=2
OF=4
NOAUTOSTR=noauto
FSTAB=/etc/fstab
MTAB=none
GREP=grep
;;
*) FSF=3
MF=2
OF=4
NOAUTOSTR=noauto
FSTAB=/etc/fstab
MTAB=/proc/mounts
GREP=grep
;;
esac
# Time in seconds after which the check assumes that an NFS mount is staled, if
# it does not respond. (default: 3)
TIME_TILL_STALE=3
# --------------------------------------------------------------------
# --------------------------------------------------------------------
# functions
# --------------------------------------------------------------------
function log() {
$LOGGER ${PROGNAME} "$@";
}
function usage() {
echo "Usage: $PROGNAME [-m FILE] \$mountpoint [\$mountpoint2 ...]"
echo "Usage: $PROGNAME -h,--help"
echo "Options:"
echo " -m FILE Use this mtab instead (default: ${MTAB})"
echo " -f FILE Use this fstab instead (default: ${FSTAB})"
echo " -N NUMBER FS Field number in fstab (default: ${FSF})"
echo " -M NUMBER Mount Field number in fstab (default: ${MF})"
echo " -O NUMBER Option Field number in fstab (default: ${OF})"
echo " -T SECONDS Responsetime at which an NFS is declared as staled (default: ${TIME_TILL_STALE})"
echo " -L Allow softlinks to be accepted instead of mount points"
echo " -i Ignore fstab. Do not fail just because mount is not in fstab. (default: unset)"
echo " -a Autoselect mounts from fstab (default: unset)"
echo " -A Autoselect from fstab. Return OK if no mounts found. (default: unset)"
echo " -E PATH Use with -a or -A to exclude a path from fstab. Use '\|' between paths for multiple. (default: unset)"
echo " -o When autoselecting mounts from fstab, ignore mounts having noauto flag. (default: unset)"
echo " -w Writetest. Touch file \$mountpoint/.mount_test_from_\$(hostname) (default: unset)"
echo " -e ARGS Extra arguments for df (default: unset)"
echo " MOUNTPOINTS list of mountpoints to check. Ignored when -a is given"
}
function print_help() {
echo ""
usage
}
# Create a temporary mtab systems that don't have such a file
# Format is dev mountpoint filesystem
function make_mtab() {
mtab=$(mktemp)
mount > $mtab
sed -i '' 's/ on / /' $mtab
sed -i '' 's/ (/ /' $mtab
sed -i '' 's/,.*/ /' $mtab
echo $mtab
}
# --------------------------------------------------------------------
# startup checks
# --------------------------------------------------------------------
if [ $# -eq 0 ]; then
usage
exit $STATE_CRITICAL
fi
while [ "$1" != "" ]
do
case "$1" in
-a) AUTO=1; shift;;
-A) AUTO=1; AUTOIGNORE=1; shift;;
-E) EXCLUDE=$2; shift 2;;
-o) NOAUTOIGNORE=1; shift;;
--help) print_help; exit $STATE_OK;;
-h) print_help; exit $STATE_OK;;
-m) MTAB=$2; shift 2;;
-f) FSTAB=$2; shift 2;;
-N) FSF=$2; shift 2;;
-M) MF=$2; shift 2;;
-O) OF=$2; shift 2;;
-T) TIME_TILL_STALE=$2; shift 2;;
-i) IGNOREFSTAB=1; shift;;
-w) WRITETEST=1; shift;;
-L) LINKOK=1; shift;;
-e) DFARGS=$2; shift 2;;
/*) MPS="${MPS} $1"; shift;;
*) usage; exit $STATE_UNKNOWN;;
esac
done
# ZFS file system have no fstab. Make on
if [ -x '/sbin/zfs' ]; then
TMPTAB=$(mktemp)
cat ${FSTAB} > ${TMPTAB}
for DS in $(zfs list -H -o name -t filesystem); do
MP=$(zfs get -H mountpoint ${DS} |awk '{print $3}')
# mountpoint ~ "none|legacy|-"
if [ ! -d "$MP" ]; then
continue
fi
if [ $(zfs get -H canmount ${DS} |awk '{print $3}') == 'off' ]; then
continue
fi
case $KERNEL in
SunOS)
if [ $(zfs get -H zoned ${DS} |awk '{print $3}') == 'on' ]; then
continue
fi
;;
FreeBSD)
if [ $(zfs get -H jailed ${DS} |awk '{print $3}') == 'on' ]; then
continue
fi
;;
esac
RO=$(zfs get -H readonly ${DS} |awk '($3 == "on"){print "ro"}')
[ -z "$RO" ] && RO='rw'
echo -e "$DS\t$MP\tzfs\t$RO\t0\t0" >> ${TMPTAB}
done
FSTAB=${TMPTAB}
fi
if [ ${AUTO} -eq 1 ]; then
if [ ${NOAUTOIGNORE} -eq 1 ]; then
NOAUTOCOND='!index($'${OF}',"'${NOAUTOSTR}'")'
fi
if [ "${EXCLUDE}" == "none" ]; then
MPS=`${GREP} -v '^#' ${FSTAB} | awk '{if ('${NOAUTOCOND}'&&($'${FSF}'=="ext2" || $'${FSF}'=="ext3" || $'${FSF}'=="xfs" || $'${FSF}'=="auto" || $'${FSF}'=="ext4" || $'${FSF}'=="nfs" || $'${FSF}'=="nfs4" || $'${FSF}'=="davfs" || $'${FSF}'=="cifs" || $'${FSF}'=="fuse" || $'${FSF}'=="glusterfs" || $'${FSF}'=="ocfs2" || $'${FSF}'=="lustre" || $'${FSF}'=="ufs" || $'${FSF}'=="zfs" || $'${FSF}'=="ceph" || $'${FSF}'=="btrfs" || $'${FSF}'=="yas3fs"))print $'${MF}'}' | sed -e 's/\/$//i' | tr '\n' ' '`
else
MPS=`${GREP} -v '^#' ${FSTAB} | ${GREP} -v ${EXCLUDE} | awk '{if ('${NOAUTOCOND}'&&($'${FSF}'=="ext2" || $'${FSF}'=="ext3" || $'${FSF}'=="xfs" || $'${FSF}'=="auto" || $'${FSF}'=="ext4" || $'${FSF}'=="nfs" || $'${FSF}'=="nfs4" || $'${FSF}'=="davfs" || $'${FSF}'=="cifs" || $'${FSF}'=="fuse" || $'${FSF}'=="glusterfs" || $'${FSF}'=="ocfs2" || $'${FSF}'=="lustre" || $'${FSF}'=="ufs" || $'${FSF}'=="zfs" || $'${FSF}'=="ceph" || $'${FSF}'=="btrfs" || $'${FSF}'=="yas3fs"))print $'${MF}'}' | sed -e 's/\/$//i' | tr '\n' ' '`
fi
fi
if [ -z "${MPS}" ] && [ ${AUTOIGNORE} -eq 1 ] ; then
echo "OK: no external mounts were found in ${FSTAB}"
exit $STATE_OK
elif [ -z "${MPS}" ]; then
log "ERROR: no mountpoints given!"
echo "ERROR: no mountpoints given!"
usage
exit $STATE_UNKNOWN
fi
if [ ! -f /proc/mounts -a "${MTAB}" == "/proc/mounts" ]; then
log "CRIT: /proc wasn't mounted!"
mount -t proc proc /proc
ERR_MESG[${#ERR_MESG[*]}]="CRIT: mounted /proc $?"
fi
if [ "${MTAB}" == "none" ]; then
MTAB=$(make_mtab)
fi
if [ ! -e "${MTAB}" ]; then
log "CRIT: ${MTAB} doesn't exist!"
echo "CRIT: ${MTAB} doesn't exist!"
exit $STATE_CRITICAL
fi
# --------------------------------------------------------------------
# now we check if the given parameters ...
# 1) ... exist in the /etc/fstab
# 2) ... are mounted
# 3) ... df -k gives no stale
# 4) ... exist on the filesystem
# 5) ... is writable (optional)
# --------------------------------------------------------------------
for MP in ${MPS} ; do
## If its an OpenVZ Container or -a Mode is selected skip fstab check.
## -a Mode takes mounts from fstab, we do not have to check if they exist in fstab ;)
if [ ! -f /proc/vz/veinfo -a ${AUTO} -ne 1 -a ${IGNOREFSTAB} -ne 1 ]; then
if [ -z "$( "${GREP}" -v '^#' "${FSTAB}" | awk '$'${MF}' == "'${MP}'" {print $'${MF}'}' )" ]; then
log "CRIT: ${MP} doesn't exist in /etc/fstab"
ERR_MESG[${#ERR_MESG[*]}]="${MP} doesn't exist in fstab ${FSTAB}"
fi
fi
## check kernel mounts
if [ -z "$( awk '$'${MF}' == "'${MP}'" {print $'${MF}'}' "${MTAB}" )" ]; then
## if a softlink is not an adequate replacement
if [ -z "$LINKOK" -o ! -L ${MP} ]; then
log "CRIT: ${MP} is not mounted"
ERR_MESG[${#ERR_MESG[*]}]="${MP} is not mounted"
fi
fi
## check if it stales
df -k ${DFARGS} ${MP} &>/dev/null &
DFPID=$!
disown
for (( i=1 ; i<$TIME_TILL_STALE ; i++ )) ; do
if ps -p $DFPID > /dev/null ; then
sleep 1
else
break
fi
done
if ps -p $DFPID > /dev/null ; then
$(kill -s SIGTERM $DFPID &>/dev/null)
ERR_MESG[${#ERR_MESG[*]}]="${MP} did not respond in $TIME_TILL_STALE sec. Seems to be stale."
else
## if it not stales, check if it is a directory
ISRW=0
if [ ! -d ${MP} ]; then
log "CRIT: ${MP} doesn't exist on filesystem"
ERR_MESG[${#ERR_MESG[*]}]="${MP} doesn't exist on filesystem"
## if wanted, check if it is writable
elif [ ${WRITETEST} -eq 1 ]; then
ISRW=1
## in auto mode first check if it's readonly
elif [ ${WRITETEST} -eq 1 ] && [ ${AUTO} -eq 1 ]; then
ISRW=1
for OPT in $(${GREP} -w ${MP} ${FSTAB} |awk '{print $4}'| sed -e 's/,/ /g'); do
if [ "$OPT" == 'ro' ]; then
ISRW=0
log "CRIT: ${TOUCHFILE} is not mounted as writable."
ERR_MESG[${#ERR_MESG[*]}]="Could not write in ${MP} filesystem was mounted RO."
fi
done
fi
if [ ${ISRW} -eq 1 ]; then
TOUCHFILE=${MP}/.mount_test_from_$(hostname)_$(date +%Y-%m-%d--%H-%M-%S).$RANDOM.$$
touch ${TOUCHFILE} &>/dev/null &
TOUCHPID=$!
for (( i=1 ; i<$TIME_TILL_STALE ; i++ )) ; do
if ps -p $TOUCHPID > /dev/null ; then
sleep 1
else
break
fi
done
if ps -p $TOUCHPID > /dev/null ; then
$(kill -s SIGTERM $TOUCHPID &>/dev/null)
log "CRIT: ${TOUCHFILE} is not writable."
ERR_MESG[${#ERR_MESG[*]}]="Could not write in ${MP} in $TIME_TILL_STALE sec. Seems to be stale."
else
if [ ! -f ${TOUCHFILE} ]; then
log "CRIT: ${TOUCHFILE} is not writable."
ERR_MESG[${#ERR_MESG[*]}]="Could not write in ${MP}."
else
rm ${TOUCHFILE} &>/dev/null
fi
fi
fi
fi
done
# Remove temporary files
if [[ "${MTAB}" =~ "/tmp" ]]; then
rm -f ${MTAB}
fi
if [[ "${FSTAB}" =~ "/tmp" ]]; then
rm -f ${FSTAB}
fi
if [ ${#ERR_MESG[*]} -ne 0 ]; then
echo -n "CRITICAL: "
for element in "${ERR_MESG[@]}"; do
echo -n ${element}" ; "
done
echo
exit $STATE_CRITICAL
fi
echo "OK: all mounts were found (${MPS})"
exit $STATE_OK