#!/bin/bash ################################################################################ # # # This program is free software; you can redistribute it and/or modify # # it under the terms of the GNU General Public License as published by # # the Free Software Foundation; either version 2 of the License, or # # (at your option) any later version. # # # # This program is distributed in the hope that it will be useful, # # but WITHOUT ANY WARRANTY; without even the implied warranty of # # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # # GNU General Public License for more details. # # # # You should have received a copy of the GNU General Public License # # along with this program; if not, write to the Free Software # # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # # # ################################################################################ ############################################################################### # The latest version of check_domain can be found at: # # https://github.com/Elyrith/check_domain # ############################################################################### # VERSION: Version 0.96 # AUTHOR: See README. This version updated by Ryan Loudfoot (ryan@delotha.com) PROGRAM=${0##*/} PROGPATH=${0%/*} # Ryan's note: utils.sh is installed with nagios-plugins in with the plugins # Check if utils.sh exists. This lets you use check_domain in a testing environment # or outside of Nagios. if [ -e "$PROGPATH/utils.sh" ]; then . "$PROGPATH/utils.sh" else STATE_OK=0 STATE_WARNING=1 STATE_CRITICAL=2 STATE_UNKNOWN=3 # STATE_DEPENDENT=4 (Commented because it's unused.) fi # Default values (days): critical=7 warning=30 # Array for converting 3-letter month into number (currently unused) #MONTH=("jan" "feb" "mar" "apr" "may" "jun" "jul" "aug" "sep" "oct" "nov" "dec") # Parse arguments args=$(getopt -o hd:w:c:P: --long help,domain:,warning:,critical:,path: -s bash -n "$PROGRAM" -- "$@") if [ $? != 0 ]; then echo >&2 "$PROGRAM: Could not parse arguments" echo "Usage: $PROGRAM -h | -d [-c ] [-w ]" exit 1 fi eval "set -- $args" die() { local rc=$1 local msg=$2 echo "$msg" exit "$rc" } fullusage() { cat < [-c ] [-w ] 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 Currently supports .com, .ca, .tv, .ee, .ru 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 # Use GoDaddy if domain is a .ca case "$domain" in *.ca) out=$("$whois" -h whois.godaddy.com "$domain") ;; *) out=$("$whois" "$domain") ;; esac # Calculate days until expiration case "$domain" in *.com) # Old way, which didn't work for some .com domains, but kept in case the new way doesn't work for some # expiration=$(echo "$out" | awk -F: '/Registrar Expiration Date:/{print substr($0, length($1) + 2)}') # New way (2013-09-06) expiration=$(echo "$out" | awk -F: '/Expiration Date:/{print substr($0, length($1) + 2);exit;}') ;; *.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)}') ;; *.ca) expiration=$(echo "$out" | awk '/Expiry date:/{split($3, a, "/"); printf("%s/%s/%s\n", a[2], a[3], a[1])}') ;; *.se) expiration=$(echo "$out" | awk '/expires:/{split($2, a, "-"); printf("%s-%s-%s\n", a[1], a[2], a[3])}') ;; *.net) expiration=$(echo "$out" | awk -F: '/ Expiration Date:/{print substr($0, length($1) + 3, 11)}') ;; *.nu) expiration=$(echo "$out" | grep expires | awk '{print $2}') ;; #*.nu) # I'll leave this in just in case /Jack-Benny # This doesn't work, yet. Need to convert 3-letter month into number. -Ryan, Aug 21, 2013 # expmonth=$(echo "$out" | awk -F '/Record expires on/{print substr($4, length($1) + 2)}'} # echo $expmonth # expiration=$(echo "$out" | awk '/Record expires on/{split($4, a, "-"); printf("%s-%s-%s\n", a[2], a[3], a[1]);}') # ;; *) expiration=$(echo "$out" | awk -F: '/Expiration Date:/{print substr($0, length($1) + 2)}') ;; esac # For debugging only: #echo $expiration [ -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. | Warning: $warning, Critical: $critical" [ $expdays -lt "$critical" ] && die $STATE_CRITICAL "CRITICAL - Domain will expire in $expdays days ($expdate). | Warning: $warning, Critical: $critical" [ $expdays -lt "$warning" ] && die $STATE_WARNING "WARNING - Domain will expire in $expdays days ($expdate). | Warning: $warning, Critical: $critical" # No alarms? Ok, everything is right. echo "OK - Domain will expire in $expdays days ($expdate). | Warning: $warning, Critical: $critical" exit $STATE_OK