Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Simple script to download the `last-modified` file from PyPI mirror and check if it's up-to-date.
#!/bin/bash
# Thomas Merkel <tm@core.io>
# Check PyPI mirror with nagios
PROGPATH=$(echo ${0} | sed -e 's,[\\/][^\\/][^\\/]*$,,')
REVISION="1.1"
source ${PROGPATH}/utils.sh
# Function to print help
function help() {
print_revision ${0} ${REVISION}
echo
echo "${0} -u <url/last-modified> -w <warning seconds> -c <critical seconds>"
echo
echo "OPTIONS:"
echo " -u <url/last-modified>: URL to the last-modified file"
echo " -w <warning seconds>: Difference in seconds for warning (1800)"
echo " -c <critical seconds>: Difference in seconds for criticial (3600)"
exit ${STATE_UNKNOWN}
}
# Parse all option
WARN=1800
CRIT=3600
while getopts "h?u:w:c:" opt; do
case "$opt" in
h|\?)
help
;;
u)
URL=${OPTARG}
;;
w)
WARN=${OPTARG}
;;
c)
CRIT=${OPTARG}
;;
esac
done
if [ $# -eq 0 ]; then
help
fi
shift $((OPTIND-1))
# Download and check curl return date
l_date=$(date -u)
r_curl=$(curl -sq ${URL})
if [ ${?} -ne 0 ]; then
echo "CRIT: failed to download last-modified file"
exit ${STATE_CRITICAL}
fi
# Convert remote date to utc timestamp, because the file contains
# another date format we need to convert it
r_date=$(echo "${r_curl} UTC" | sed "s:T: :")
# Convert to unix timestamp
l_unixtime=$(date -d "${l_date}" +%s)
r_unixtime=$(date -d "${r_date}" +%s)
# Check difference
if [[ ! $((${r_unixtime}+${CRIT})) -lt ${l_unixtime} || \
! $((${r_unixtime}+${WARN})) -lt ${l_unixtime} ]]; then
echo "OK: mirror is up-to-date [remote ${r_date}]"
exit ${STATE_OK}
fi
if [ $((${r_unixtime}+${CRIT})) -lt ${l_unixtime} ]; then
echo "CRIT: mirror out of sync [remote ${r_date}]"
exit ${STATE_CRITICAL}
fi
if [ $((${r_unixtime}+${WARN})) -lt ${l_unixtime} ]; then
echo "WARN: mirror out of sync [remote ${r_date}]"
exit ${STATE_WARNING}
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment