File check_webpage_sync of Package monitoring-plugins-webpage_sync
#!/bin/bash
#
# check_web_timestamp - nagios plugin
#
# Copyright (C) 2013, SUSE LCC
# 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.
#
PROGNAME=$(/bin/basename $0)
REVISION='0.3'
WGET=$(which wget)
TIMEOUT=10
VERBOSE='no'
OK=0
WARNING=1
CRITICAL=2
UNKNOWN=3
WARN=2
CRIT=5
OUTPUT='UNKOWN: script did not produce valuable output'
print_usage() {
echo "Usage: $(basename $0) -u http://my.url.test/file -c 5 -w 2"
echo " -u : url for remote file"
echo " -c : critical (in days - default $CRIT)"
echo " -w : warning (in days - default $WARN)"
echo " -t : timeout (default: $TIMEOUT)"
echo " -v : be verbose"
echo
echo "The script expects the first line of the downloaded file to contain something like:"
echo " 2020-03-20T00:14:41+0100 or 2020-03-20 00:14:41"
echo "Something that date \"+%Y-%m-%d %H:%M:%S\" prints out"
echo
}
print_help() {
print_revision
echo
print_usage
echo
}
print_revision() {
echo -e "$PROGNAME Version $REVISION\n"
echo -e "The nagios plugins come with ABSOLUTELY NO WARRANTY. You may redistribute\ncopies of the plugins under the terms of the GNU General Public License.\nFor more information about these matters, see the file named COPYING.\n"
}
cleanup_and_exit() {
local exitcode="$1"
rm "$URL_FILE" 2>/dev/null
echo "$OUTPUT"
exit $exitcode
}
if [ $# -lt 1 ]; then
print_usage
exit $UNKNOWN
fi
while getopts 'u:w:c:t:vV' OPTION ; do
case $OPTION in
V)
echo "Version: $REVISION"
exit $OK
;;
v)
VERBOSE='yes'
;;
h)
print_usage
exit $OK
;;
w)
WARN="$OPTARG"
;;
c)
CRIT="$OPTARG"
;;
u)
URL="$OPTARG"
;;
t)
TIMEOUT="$OPTARG"
;;
*)
print_usage
exit $UNKNOWN
;;
esac
done
shift $(( OPTIND - 1 ))
# check warning and critical value
if [ ${WARN} -ge ${CRIT} ]; then
echo "Warning level has the same size or is greater the the critical values."
echo "Please change and rerun ..."
exit $UNKNOWN
fi
if [ -z "$URL" ]; then
echo "No valid url for option -u: $URL" >&2
print_usage
exit $UNKNOWN
fi
trap 'echo' SIGHUP SIGINT SIGQUIT
trap 'cleanup_and_exit 2' SIGTRAP SIGBUS SIGKILL SIGPIPE SIGTERM
URL_FILE=$(mktemp /tmp/$(basename $0)-XXXXXX)
$WGET -q --timeout=$TIMEOUT --output-document="$URL_FILE" "$URL"
NOW=$(date +%s)
OLD=$(cat "$URL_FILE")
SYNCED=$(date +%s -d "$OLD" 2>/dev/null)
if grep -qi "down for maintenance" $URL_FILE ; then
OLD=$(echo "last year" ; cat "$URL_FILE")
SYNCED=$(date +%s -d "last year")
fi
let "WARN=$WARN*24"
let "CRIT=$CRIT*24"
let "DIFF=$NOW-$SYNCED"
if [ "$VERBOSE" == "yes" ]; then
echo "URL : $URL"
echo -n "Old : "
cat "$URL_FILE"
echo -n "Now : "
date "+%Y-%m-%d %H:%M:%S"
echo "WARN: $WARN"
echo "CRIT: $CRIT"
echo "DIFF: $DIFF"
fi
if [ "$DIFF" -lt "3600" ]; then
OUTPUT="OK: $URL was updated less than an hour ago"
cleanup_and_exit $OK
else
# Compute the approximate hour difference
hDIFF=$(scale=2; echo "$DIFF/3600" | bc)
if [ "$hDIFF" -lt "$WARN" ]; then
OUTPUT="OK: $URL was updated $hDIFF hour(s) ago"
cleanup_and_exit $OK
fi
if [ "$hDIFF" -lt "$CRIT" ]; then
OUTPUT="WARNING: $URL was last updated at $OLD"
cleanup_and_exit $WARNING
else
OUTPUT="CRITICAL: $URL was last updated at $OLD"
cleanup_and_exit $CRITICAL
fi
fi