+

Last commit for check_lock_file: a35c5fc86847dc88017542c3ae7abdcafa288204

Add README file and improve usage message

Benjamin Renard [2014-12-08 13:03:37]
Add README file and improve usage message
#!/bin/bash
# Lock file path
LOCK_FILE=""
# Lock file valid owner
VALID_OWNER=""
# WARNING time in hours
WARN=12
# Critical time in hours
CRIT=24
# Debug mode
DEBUG=0

function usage() {
	echo "Usage : $0 -l /path/to/lock.file [-o uid] [-w W] [-c C] [-d] [-h]"
	echo
	echo "Check lock file status"
	echo
	echo "Parameters :"
	echo "  -l /path/to/lock.file   Path to lock file"
	echo "  -o uid                  Valid lock file owner (optional)"
	echo "  -w W                    Exit with WARNING status if lock file"
	echo "                          has been created for more than W hours"
	echo "  -c C                    Exit with CRITICAL status if lock file"
	echo "                          has been created for more than C hours"
	echo "  -d                      Enable debug mode"
	echo "  -h                      Show this message"
}

while getopts :l:o:w:c:dh FLAG
do
	[ $DEBUG -eq 1 ] && echo "Parameter $FLAG present. Value : '$OPTARG'"
	case $FLAG in
		l)
			LOCK_FILE=$OPTARG
		;;
		o)
			VALID_OWNER=$OPTARG
		;;
		w)
			[ $( echo "$OPTARG"|grep -c -e "^[0-9][0-9]*$" ) -ne 1 ] && echo "Invalid WARNING parameter (-w). Must be an integer" && exit 1
			WARN=$OPTARG
		;;
		c)
			[ $( echo "$OPTARG"|grep -c -e "^[0-9][0-9]*$" ) -ne 1 ] && echo "Invalid CRITICAL parameter (-c). Must be an integer" && exit 1
			CRIT=$OPTARG
		;;
		d)
			DEBUG=1
		;;
		h)
			usage
			exit 0
		;;
	esac
done

[ -z "$LOCK_FILE" ] && echo "You must specify the lock file path (-l parameter)." && usage && exit 1

if [ ! -d "$( dirname $LOCK_FILE )" ]
then
	echo "UNKNWON - Lock file's parent directory does not exists."
	exit 3
fi
[ $DEBUG -eq 1 ] && ls -ld "$( dirname $LOCK_FILE )"

if [ -f $LOCK_FILE ]
then
	# Check owner
	if [ ! -z "$VALID_OWNER" -a $( ls -al $LOCK_FILE|awk '{print $3}' ) != "$VALID_OWNER" ]
	then
		echo "CRITICAL - Lock file present but not owned by $VALID_OWNER"
		exit 2
	fi

	T=$( ls -al --time-style=+%s $LOCK_FILE|awk '{print $6}' )
	CUR=$( date +%s )
	let diff=CUR-T

	let WARN_TIME=WARN*3600
	let CRIT_TIME=CRIT*3600

	# CRITICAL if diff > $CRIT
	if [ $diff -gt $CRIT_TIME ]
	then
		echo "CRITICAL - Parser lock file present since more than ${CRIT}h"
		exit 2
	# WARNING if diff > $WARN
	elif [ $diff -gt $WARN_TIME ]
	then
		echo "WARNING - Parser lock file present since more than ${WARN}h"
		exit 1
	else
		echo "OK - Lock file present since less than ${WARN}h"
		exit 0
	fi
else
	echo 'OK - Lock file not present'
	exit 0
fi
ViewGit