#!/usr/bin/perl
# nagios: -epn

# $Id: check_whois,v 1.21 2015/10/12 16:44:13 wessels Exp $
#
# check_whois
#
# nagios plugin to check the expiration date of a domain.

# Copyright (c) 2008, The Measurement Factory, Inc. All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 
# Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# Neither the name of The Measurement Factory nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

# usage
#
# define command {
#   command_name    check-whois
#   command_line    /usr/local/libexec/nagios-local/check_whois $HOSTADDRESS$
# }
#
# define service {
#   name                   whois-service
#   check_command          check-whois
#   ...
# }
#
# define host {
#   use dns-zone
#   host_name zone.example.com
#   alias ZONE example.com
# }
#
# define service {
#   use whois-service
#   host_name zone.example.com
# }

# AUTHORS and CONTRIBUTORS:
#   Duane Wessels
#   Matt Christian
#   John Lines

use strict;
use warnings;

use Getopt::Std;
use Date::Manip;
use POSIX qw(strftime);
use Env '@PATH';

my %opts;
getopts('xds:', \%opts);

my $string_found = 0;

my $name = shift or die usage();
$name =~ s/^zone\.//;
my $whoiscmd = findwhois();
print STDERR "$0 version ". '$Id: check_whois,v 1.21 2015/10/12 16:44:13 wessels Exp $'. "\n" if $opts{d};
print STDERR "Whois command = $whoiscmd\n" if $opts{d};
grok($whoiscmd, $name);
exit 0;

sub findwhois {
	# List of whois commands to find (in order)
	my @whoiscmds = qw( jwhois whois );
	foreach my $wc (@whoiscmds) {
		if (grep { -x "$_/$wc" } @PATH) {
			$wc .= ' -n ' if ($wc eq 'jwhois');
			return $wc;
		}
	}
	die "Could not find a whois command!\n";
}

# 
# the ~ 2013-14-15 era New GTLDs have whois servers at whois.nic.$tld
# and they are slow to be configured in either whois clients or the
# whois-servers.net zone.
#
sub findnewgtldwhois($) {
	my $name = shift;
	my @x = split /\./, $name;
	my $tld = pop @x;
	my $whois = "whois.nic.$tld";
	return gethostbyname($whois) ? $whois : undef;
}

sub grok {
	my $whoiscmd = shift || die;
        my $name = shift || die;
	my $arg = $name;
	if ($name =~ /\.name$/) {
		# In order to get useful information out of
		# the .NAME whois server, the query must
		# be reformatted
		$arg = "'domain = $name'";
	}
        open (CMD, "$whoiscmd $arg|") || die;
        my $registrar = undef;
        my $whoisservice = undef;
        my $expires = undef;
        my $status = '';
	my $state = 0;
        print STDERR "checking $name\n" if $opts{d};
        while (<CMD>) {
                tr/A-Z/a-z/;
		print STDERR "# $_" if $opts{d};
                $registrar = $1 if (/registrar:\s*(\S.*)/);
                if (!defined($registrar)) {
                        $registrar = $1 if (/registrar id:\s*(\S.*)/);
                        $registrar = $1 if (/registrar ha                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          