File check_mirror.sh of Package monitoring-plugins-mirrorbrain_mirror

#!/bin/bash
#
# Copyright (C) 2015-2019, SUSE Linux GmbH
# Author: Lars Vogdt
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
#   list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
#   this list of conditions and the following disclaimer in the documentation
#   and/or other materials provided with the distribution.
#
# * Neither the name of the Novell nor the names of its contributors may be
#   used to endorse or promote products derived from this software without
#   specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
MB='/usr/bin/mb'
MB_CONFIG='/etc/mirrorbrain.conf'
UTILS='/usr/lib/nagios/plugins/utils.sh'

exit_nrpe(){
	echo "$MESSAGE"
	exit $STATUS
}

if [ ! -r "$UTILS" ]; then
	echo "Could not read $UTILS - please install monitoring-plugins-common package" 
    exit 2
else
	. "$UTILS"
fi

if [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
	echo "Usage: $(basename $0) <mirrorname>";
	echo
	echo "This check uses the mb tool to check if a mirror is enabled/disabled"
	echo "by admin and/or by the mirrorprobe script."
	echo "The assumption is that a given (monitored) mirror should always be"
	echo "enabled - so this script will exit with warn/crit messages if this"
	echo "is not the case."
	echo
	support;
	exit 0;
fi 

MIRROR="$1"
STATUS=$STATE_UNKNOWN
MESSAGE="State unknown for $MIRROR"

IS_ENABLED=$($MB --config $MB_CONFIG show $MIRROR | grep ^enabled | tr [[:upper:]] [[:lower:]] | awk '":" { print $3 }')
BASEURL=$($MB --config $MB_CONFIG show $MIRROR | grep ^statusBaseurl | tr [[:upper:]] [[:lower:]] | awk '":" { print $3 }')

case $IS_ENABLED in 
	'true')
		STATUS=$STATE_OK
		MESSAGE="Mirror $MIRROR is enabled by admin"
	;;
	'false')
		STATUS=$STATE_CRITICAL
		MESSAGE="Mirror $MIRROR is disabled by admin"
		exit_nrpe
	;;
	*)
		STATUS=$STATE_UNKNOWN
		MESSAGE="Unknown 'enabled' state returned for $MIRROR"
	;;
esac

case $BASEURL in 
	'true')
		MESSAGE="Mirror $MIRROR is enabled and scanned"
		STATUS=$STATE_OK
	;;
	'false')
		if [ "$STATUS" == "$STATE_OK" ]; then
			MESSAGE="WARNING: Mirror $MIRROR is disabled by scanner, but enabled by admin"
			STATUS=$STATE_WARNING
		else
			MESSAGE="OK: $MIRROR is disabled by admin and by scanner - looks like this is wanted"
			STATUS=$STATE_OK
		fi
	;;
	*)
		STATUS=$STATE_UNKNOWN
		MESSAGE="Unknown 'statusBaseurl' ($BASEURL) state returned for $MIRROR"
	;;
esac

exit_nrpe
openSUSE Build Service is sponsored by