#!/bin/bash

# Nagios check
# Check the number of days before an internet domain expire
# 2014-09-06  Patrice Nadeau  <pnadeau@patricenadeau.com>

# TODO Should use the arguments (short and long) -w -c

# Version
declare -r VERSION="0.0.02"

# Copyright
declare -r COPYRIGHT="Copyright 2014 Patrice Nadeau  <pnadeau@patricenadeau.com>"

# OK
declare -r RETURN_OK=0

# Warning
declare -r RETURN_WARNING=1

# Critical
declare -r RETURN_CRITICAL=2

# Unknow
declare -r RETURN_UNKNOW=3

# State of the check, undefined when starting
declare STATE=$RETURN_UNKNOW

# Expiration date
declare EXPIRATION

function print_revision {
	echo "${0##*/} version $VERSION"
	echo "$COPYRIGHT"
}

function print_help {
# use cat << EOF
	print_revision
	cat << EOF
This plugin check the expiration date for an internet domain
	
Usage: ${0##*/} DOMAIN WARNINGDAYS CRITICALDAYS
	DOMAIN			domain name to be verified
	WARNINGDAYS		days before a warning message is issued
	CRITICALDAYS	days before a critical messages is issued

	The order of the arguments MUST be in that order
EOF
}

# Check if help as been invoked
if [[ $# -eq 1 ]]
then
	case $1 in
		"-h" | "--help")
			print_help
			;;
		"-V" | "--version")
			print_revision
			;;
		*)
			echo "CRITICAL: Unknown option"
			echo "Try ${0##*/} -h"
			;;
	esac
	exit $RETURN_CRITICAL
fi

# Check if whois is installed
which whois &>> /dev/null
if [[ $? -gt 0 ]]
then
	echo "whois is not installed !"
	exit $RETURN_UNKNOW
fi

# Check if all 3 argments (-h -w -c) have been given
if [ $# -lt 3 ]
then
	echo "CRITICAL: Missing arguments !"
	exit $RETURN_CRITICAL
fi

# Get the top level domain
TLD=`echo $1 | awk -F"." '{print $NF}'`

# Find the expiration date depending on the top level domain
case $TLD in
	ca)
		# cira.ca
		EXPIRATION=`whois -H $1 | grep "Expiry date" | awk '{print $3}'`
		;;
	com | net)
		EXPIRATION=`whois -H $1 | grep "Expiration Date" | head -n 1 | awk '{print $3}'`
		;;
	edu)
		EXPIRATION=`whois -H $1 | grep "Domain expires" | awk '{print $3}'`
		;;
	gov)
		# rs.internic.net
		echo "CRITICAL: No date is returned for this TLD"
		exit $RETURN_CRITICAL
		;;
	us | biz | co)
		# The date contains space (Fri Apr 17 23:59:59 GMT 2015)
		EXPIRATION=`whois -H $1 | grep "Domain Expiration Date" | awk '{print $5, $6, $9}'`
		;;
	info | xxx | org)
		# The date contain the time
		EXPIRATION=`whois -H $1 | grep "Registry Expiry Date" | awk '{print $4}'`
		;;
	me)
		# There's no space between the description and the date
		EXPIRATION=`whois -H $1 | grep "Domain Expiration Date" | awk -F":" '{print $2}'`
		;;
	mobi)
		# There's no space between the description and the date
		EXPIRATION=`whois -H $1 | grep "Expiration Date" | awk -F":" '{print $2}'`
		;;
	tv)
		# Date is in format 2015-03-18T17:16:19.0Z
		EXPIRATION=`whois -H $1 | grep "Registrar Registration Expiration Date" | awk '{print $5}'`
		echo $EXPIRATION
		;;
	*)
		echo "CRITICAL: This TLD is not supported yet"
		exit $RETURN_CRITICAL
		;;
esac

# Check if the domain exist 
if [[ -z $EXPIRATION ]]
then
	echo "CRITICAL: Domain not found"
	exit $RETURN_CRITICAL
fi

# Calculate the number of days between the expiration and now
DAYS=$(( ($(date --date="$EXPIRATION" +%s) - $(date +%s) )/(60*60*24) ))

# Check if the number of days is OK, Warning or Critical range
if [[ $DAYS -gt $2 ]]
then
	echo -ne "OK: "
	STATE=$RETURN_OK
else 
	if [[ $DAYS -gt $3 ]]
	then
		echo -ne "WARNING: "
		STATE=$RETURN_WARNING
	else 
		echo -ne "CRITICAL: "
		STATE=$RETURN_CRITICAL
	fi
fi
		
echo -ne "$DAYS"

if [[ $DAYS -ge 0 ]]
then
	echo " days before expiration"
else
	echo " days since expiration"
fi

exit $STATE

