Skip to content
This repository has been archived by the owner on Mar 8, 2023. It is now read-only.

fix shellcheck warnings and errors (https://www.shellcheck.net/) #55

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 71 additions & 71 deletions check_mountpoints.sh
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@
# --------------------------------------------------------------------
# configuration
# --------------------------------------------------------------------
PROGNAME=$(basename $0)
PROGNAME=$(basename "$0")
ERR_MESG=()
LOGGER="`which logger` -i -p kern.warn -t"
LOGGER="$(which logger) -i -p kern.warn -t"

AUTO=0
AUTOIGNORE=0
Expand All @@ -120,15 +120,16 @@ EXCLUDE=none
export PATH="/bin:/usr/local/bin:/sbin:/usr/bin:/usr/sbin:/usr/sfw/bin"
LIBEXEC="/opt/nagios/libexec /usr/lib64/nagios/plugins /usr/lib/nagios/plugins /usr/lib/monitoring-plugins /usr/local/nagios/libexec /usr/local/icinga/libexec /usr/local/libexec /opt/csw/libexec/nagios-plugins /opt/plugins /usr/local/libexec/nagios/"
for i in ${LIBEXEC} ; do
[ -r ${i}/utils.sh ] && . ${i}/utils.sh
# shellcheck source=/dev/null
[ -r "${i}"/utils.sh ] && . "${i}"/utils.sh
done

if [ -z "$STATE_OK" ]; then
echo "nagios utils.sh not found" &>/dev/stderr
exit 1
fi

KERNEL=`uname -s`
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
Expand All @@ -137,35 +138,35 @@ case $KERNEL in
NOAUTOSTR=no
FSTAB=/etc/vfstab
MTAB=/etc/mnttab
GREP=ggrep
STAT=stat
GREP="ggrep"
STAT="stat"
;;
HP-UX) FSF=3
MF=2
OF=4
NOAUTOSTR=noauto
FSTAB=/etc/fstab
MTAB=/dev/mnttab
GREP=grep
STAT=stat
GREP="grep"
STAT="stat"
;;
FreeBSD) FSF=3
MF=2
OF=4
NOAUTOSTR=noauto
FSTAB=/etc/fstab
MTAB=none
GREP=grep
STAT=stat
MTAB="none"
GREP="grep"
STAT="stat"
;;
*) FSF=3
MF=2
OF=4
NOAUTOSTR=noauto
FSTAB=/etc/fstab
MTAB=/proc/mounts
GREP=grep
STAT=stat
GREP="grep"
STAT="stat"
;;
esac

Expand All @@ -180,7 +181,7 @@ TIME_TILL_STALE=3
# functions
# --------------------------------------------------------------------
function log() {
$LOGGER ${PROGNAME} "$@";
$LOGGER "${PROGNAME}" "$@";
}

function usage() {
Expand Down Expand Up @@ -222,11 +223,11 @@ function print_help() {
# 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
mount > "$mtab"
sed -i '' 's/ on / /' "$mtab"
sed -i '' 's/ (/ /' "$mtab"
sed -i '' 's/,.*/ /' "$mtab"
echo "$mtab"
}


Expand All @@ -236,89 +237,89 @@ function make_mtab() {

if [ $# -eq 0 ]; then
usage
exit $STATE_CRITICAL
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;;
-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;;
--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;;
-t) FSTYPE=$2; shift 2;;
-e) DFARGS="$2"; shift 2;;
-t) FSTYPE="$2"; shift 2;;
/*) MPS="${MPS} $1"; shift;;
*) usage; exit $STATE_UNKNOWN;;
*) usage; exit "$STATE_UNKNOWN";;
esac
done

# ZFS file system have no fstab. Make on

if [ -x '/sbin/zfs' ]; then
TMPTAB=$(mktemp)
cat ${FSTAB} > ${TMPTAB}
cat "${FSTAB}" > "${TMPTAB}"
for DS in $(zfs list -H -o name -t filesystem); do
MP=$(zfs get -H mountpoint ${DS} |awk '{print $3}')
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
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
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
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"}')
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}
echo -e "$DS\t$MP\tzfs\t$RO\t0\t0" >> "${TMPTAB}"
done
FSTAB=${TMPTAB}
FSTAB="${TMPTAB}"
fi

if [ ${AUTO} -eq 1 ]; then
if [ ${NOAUTOIGNORE} -eq 1 ]; then
NOAUTOCOND='!index($'${OF}',"'${NOAUTOSTR}'")'
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' ' '`
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' ' '`
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
exit "$STATE_OK"
elif [ -z "${MPS}" ]; then
log "ERROR: no mountpoints given!"
echo "ERROR: no mountpoints given!"
usage
exit $STATE_UNKNOWN
exit "$STATE_UNKNOWN"
fi

if [ ! -f /proc/mounts -a "${MTAB}" == "/proc/mounts" ]; then
if [ ! -f /proc/mounts ] && [ "${MTAB}" == "/proc/mounts" ]; then
log "CRIT: /proc wasn't mounted!"
mount -t proc proc /proc
ERR_MESG[${#ERR_MESG[*]}]="CRIT: mounted /proc $?"
Expand All @@ -331,13 +332,13 @@ fi
if [ ! -e "${MTAB}" ]; then
log "CRIT: ${MTAB} doesn't exist!"
echo "CRIT: ${MTAB} doesn't exist!"
exit $STATE_CRITICAL
exit "$STATE_CRITICAL"
fi

if [ -n "${FSTYPE}" ]; then
# split on commas
oIFS=$IFS
IFS=, read -a fstypes <<<"${FSTYPE}"
IFS=, read -ra fstypes <<<"${FSTYPE}"
IFS=$oIFS
fi

Expand All @@ -353,40 +354,40 @@ mpidx=0
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
if [ ! -f /proc/vz/veinfo ] && [ ${AUTO} -ne 1 ] && [ ${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 [ -z "$( awk '$'"${MF}"' == "'"${MP}"'" {print $'"${MF}"'}' "${MTAB}" )" ]; then
## if a softlink is not an adequate replacement
if [ -z "$LINKOK" -o ! -L ${MP} ]; then
if [ -z "$LINKOK" ] || [ ! -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 &
df -k "${DFARGS}" "${MP}" &>/dev/null &
DFPID=$!
disown
for (( i=1 ; i<$TIME_TILL_STALE ; i++ )) ; do
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)
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
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
Expand All @@ -395,7 +396,7 @@ for MP in ${MPS} ; do
## 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
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."
Expand All @@ -405,25 +406,25 @@ for MP in ${MPS} ; do
fi
if [ ${ISRW} -eq 1 ]; then
TOUCHFILE=${MP}/.mount_test_from_$(hostname)_$(date +%Y-%m-%d--%H-%M-%S).$RANDOM.$$
touch ${TOUCHFILE} &>/dev/null &
touch "${TOUCHFILE}" &>/dev/null &
TOUCHPID=$!
for (( i=1 ; i<$TIME_TILL_STALE ; i++ )) ; do
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)
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
if [ ! -f "${TOUCHFILE}" ]; then
log "CRIT: ${TOUCHFILE} is not writable."
ERR_MESG[${#ERR_MESG[*]}]="Could not write in ${MP}."
else
rm ${TOUCHFILE} &>/dev/null
rm "${TOUCHFILE}" &>/dev/null
fi
fi
fi
Expand All @@ -438,8 +439,7 @@ for MP in ${MPS} ; do
continue
fi

rfstype=$(${STAT} -f --printf='%T' "${MP}")
if [ $? -ne 0 ]
if ! rfstype="$(${STAT} -f --printf='%T' "${MP}")";
then
log "CRIT: Fail to fetch FS type for ${MP}"
ERR_MESG[${#ERR_MESG[*]}]="Fail to fetch FS type for ${MP}"
Expand All @@ -456,20 +456,20 @@ done

# Remove temporary files
if [[ "${MTAB}" =~ "/tmp" ]]; then
rm -f ${MTAB}
rm -f "${MTAB}"
fi
if [[ "${FSTAB}" =~ "/tmp" ]]; then
rm -f ${FSTAB}
rm -f "${FSTAB}"
fi

if [ ${#ERR_MESG[*]} -ne 0 ]; then
echo -n "CRITICAL: "
for element in "${ERR_MESG[@]}"; do
echo -n ${element}" ; "
echo -n "${element}"" ; "
done
echo
exit $STATE_CRITICAL
exit "$STATE_CRITICAL"
fi

echo "OK: all mounts were found (${MPS})"
exit $STATE_OK
exit "$STATE_OK"