#!/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 "$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
}

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)     echo "check_domain - v1.2"
					   echo "Copyright (c) 2005 Tomàs Núñez Lirola <tnunez@criptos.com>, 2009 Elan Ruusamäe <glen@delfi.ee>"
					   echo "under GPL License"
					   echo ""
					   echo "This plugin checks the expiration date of a domain name."
					   echo ""
					   echo "Usage: $PROGRAM -h | -d <domain> [-c <critical>] [-w <warning>]"
					   echo "NOTE: -d must be specified"
					   echo ""
					   echo "Options:"
					   echo "-h"
					   echo "     Print detailed help"
					   echo "-d"
					   echo "     Domain name to check"
					   echo "-w"
					   echo "     Response time to result in warning status (days)"
					   echo "-c"
					   echo "     Response time to result in critical status (days)"
					   echo ""
					   echo "This plugin will use whois service to get the expiration date for the domain name. "
					   echo "Example:"
					   echo "     $PROGRAM -d domain.tld -w 30 -c 10"
					   echo ""
					   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."
else
	[ -x "$whoispath/whois" ] || die $STATE_UNKNOWN "UNKNOWN - Unable to find whois binary, you specified an incorrect path"
fi

# Calculate days until expiration
case "$domain" in
*.ru)
	expiration=$(whois $domain | awk '/paid-till:/{split($2, a, "."); printf("%s-%s-%s\n", a[1], a[2], a[3])}')
	;;
*.ee)
	expiration=$(whois $domain | awk '/expire:/{split($2, a, "."); printf("%s-%s-%s\n", a[3], a[2], a[1])}')
	;;
*)
	expiration=$(whois $domain |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
