2016-06-27 16:51:12 +03:00
#!/usr/bin/env python
""" Active Nagios chheck for whois database for matching DNS servers. """
2015-02-01 13:29:22 +02:00
import pythonwhois
import argparse
import sys
2015-02-01 17:51:11 +02:00
import itertools
2015-02-01 13:29:22 +02:00
parser = argparse . ArgumentParser ( description = ' Check whois and dns information. ' )
parser . add_argument ( ' --domain ' , nargs = 1 , required = True , help = ' the domain name ' )
2015-02-01 17:51:11 +02:00
parser . add_argument ( ' --nameserver ' , nargs = ' + ' , help = ' a space delimited list of nameservers ' )
2015-02-01 13:29:22 +02:00
args = parser . parse_args ( )
2015-02-01 17:51:11 +02:00
nsargs = args . nameserver
# how many ns servers should we expect?
nsargscount = len ( nsargs )
2016-06-27 16:51:12 +03:00
# whoiscount = []
2015-02-01 17:51:11 +02:00
2015-02-01 13:29:22 +02:00
try :
2015-02-01 17:51:11 +02:00
whois = pythonwhois . get_whois ( args . domain [ 0 ] )
whoiscount = [ ]
2015-02-04 17:28:07 +02:00
2015-02-01 17:51:11 +02:00
for key , value in whois . iteritems ( ) :
if key == " nameservers " :
whoiscount . append ( value )
2016-06-27 16:51:12 +03:00
# I know this is bad W0702; used as a catch-all
# Ill get to it someday
2015-02-01 13:29:22 +02:00
except :
#something happened, raise an erro
2015-02-01 17:51:11 +02:00
print " 3 Unexpected_error: " , sys . exc_info ( ) [ 0 ]
# how many nameservers did whois return?
#whoiscount = reduce(lambda x,y: x.extend(y),whoiscount)
2015-02-04 17:28:07 +02:00
#print len(whoiscount)
#print nsargscount
2015-02-01 17:51:11 +02:00
nscount = list ( itertools . chain . from_iterable ( whoiscount ) )
2015-02-04 17:28:07 +02:00
#print len(nscount)
2015-02-01 17:51:11 +02:00
if len ( nscount ) < nsargscount :
2016-06-27 17:39:03 +03:00
print " Extended_DNS_Check nscount= " + str ( nsargscount ) + " |whoiscount= " + str ( len ( nscount ) ) + " CRITICAL - WHOIS DNS servers do not match provided DNS nameservers. "
2016-06-27 16:51:12 +03:00
sys . exit ( 2 )
2015-02-01 17:51:11 +02:00
elif len ( nscount ) > nsargscount :
2016-06-27 17:39:03 +03:00
print " Extended_DNS_Check nscount= " + str ( nsargscount ) + " |whoiscount= " + str ( len ( nscount ) ) + " WARNING - WHOIS DNS servers do not match. \
2016-06-27 16:51:12 +03:00
There are more DNS servers from the WHOIS datbase than in your query . "
sys . exit ( 1 )
2015-02-01 17:51:11 +02:00
else :
2016-06-27 17:39:03 +03:00
print ( " 0 Extended_DNS_Check nscount= " + str ( nsargscount ) + " |whoiscount= " + str ( len ( nscount ) ) + " OK - all DNS server match " )
2016-06-27 16:51:12 +03:00
sys . exit ( 0 )