#!/bin/sh
PROGRAM=${0##*/}
PROGPATH=${0%/*}
. $PROGPATH/utils.sh

# Default values (days):
critical=7
warning=30

# Parse arguments
args=$(getopt -o hd:w:c:P: --long help,domain:,warning:,critical:,path: -u -n $PROGRAM -- "$@")
if [ $? != 0 ]; then
	echo >&2 "$PROGRAM: Could not parse arguments"
	echo "Usage: $PROGRAM -h | -d <domain> [-c <critical>] [-w <warning>]"
	exit 1
fi
set -- $args

die() {
	local rc=$1
	local msg=$2
	echo $msg
	exit $rc
}

fullusage() {
	cat <<EOF
check_domain - v1.2.1
Copyright (c) 2005 Tomàs Núñez Lirola <tnunez@criptos.com>, 2009-2012 Elan Ruusamäe <glen@delfi.ee>
under GPL License

This plugin checks the expiration date of a domain name.

Usage: $PROGRAM -h | -d <domain> [-c <critical>] [-w <warning>]
NOTE: -d must be specified

Options:
-h
     Print detailed help
-d
     Domain name to check
-w
     Response time to result in warning status (days)
-c
     Response time to result in critical status (days)

This plugin will use whois service to get the expiration date for the domain name.
Example:
     $PROGRAM -d domain.tld -w 30 -c 10

EOF
}

while :; do
	case "$1" in
		-c|--critical) critical=$2; shift 2;;
		-w|--warning)  warning=$2; shift 2;;
		-d|--domain)   domain=$2; shift 2;;
		-P|--path)     whoispath=$2; shift 2;;
		-h|--help)     fullusage; exit;;
		--) shift; break;;
		*)  die $STATE_UNKNOWN "Internal error!";;
	esac
done

if [ -z $domain ]; then
	die $STATE_UNKNOWN "UNKNOWN - There is no domain name to check"
fi

# Looking for whois binary
if [ -z $whoispath ]; then
	type whois > /dev/null 2>&1 || die $STATE_UNKNOWN "UNKNOWN - Unable to find whois binary in your path. Is it installed? Please specify path."
	whois=whois
else
	[ -x "$whoispath/whois" ] || die $STATE_UNKNOWN "UNKNOWN - Unable to find whois binary, you specified an incorrect path"
	whois="$whoispath/whois"
fi

out=$($whois $domain)

# Calculate days until expiration
case "$domain" in
*.ru)
	expiration=$(echo "$out" | awk '/paid-till:/{split($2, a, "."); printf("%s-%s-%s\n", a[1], a[2], a[3])}')
	;;
*.ee)
	expiration=$(echo "$out" | awk '/expire:/{split($2, a, "."); printf("%s-%s-%s\n", a[3], a[2], a[1])}')
	;;
*.tv)
	expiration=$(echo "$out" | awk -F: '/Expiration Date:/{print substr($0, length($1) + 3, 10)}')
	;;
*)
	expiration=$(echo "$out" | awk -F: '/Expiration Date:/{print substr($0, length($1) + 2)}')
	;;
esac

[ -z "$expiration" ] && die $STATE_UNKNOWN "UNKNOWN - Domain doesn't exist or no WHOIS server available."

expseconds=$(date +%s --date="$expiration")
expdate=$(date +'%Y-%m-%d' --date="$expiration")
nowseconds=$(date +%s)
diffseconds=$((expseconds-nowseconds))
expdays=$((diffseconds/86400))

# Trigger alarms if applicable
[ $expdays -lt 0 ] && die $STATE_CRITICAL "CRITICAL - Domain expired on $expiration"
[ $expdays -lt $critical ] && die $STATE_CRITICAL "CRITICAL - Domain will expire in $expdays days ($expdate)."
[ $expdays -lt $warning ]&& die $STATE_WARNING "WARNING - Domain will expire in $expdays days ($expdate)."

# No alarms? Ok, everything is right.
echo "OK - Domain will expire in $expdays days ($expdate)."
exit $STATE_OK
