scripts/domain-check
changeset 22 4bccc75a8a1c
parent 21 14a903cf9505
child 23 5d95402f2a3a
equal deleted inserted replaced
21:14a903cf9505 22:4bccc75a8a1c
     6 # Author: Matty < matty91 at gmail dot com >
     6 # Author: Matty < matty91 at gmail dot com >
     7 # 
     7 # 
     8 # Current Version: 1.95
     8 # Current Version: 1.95
     9 #
     9 #
    10 # Revision History:
    10 # Revision History:
       
    11 #  Version 1.96
       
    12 #    Rename variables so system variables aren't potentially changed. -- Eric Renfro <psi-jack@linux-help.org>
       
    13 #    Code optimization and per-whois delay capability is possible. -- Eric Renfro <psi-jack@linux-help.org>
       
    14 #    Output table is now fully string size padded for results. -- Eric Renfro <psi-jack@linux-help.org>
       
    15 
    11 #  Version 1.95
    16 #  Version 1.95
    12 #    Added support for .ie and .us domain names
    17 #    Added support for .ie and .us domain names
    13 #    Cleaned up some areas where awk and cut were being called
    18 #    Cleaned up some areas where awk and cut were being called
    14 #    directly rather than from the variables they were asigned to. -- Chris Jones <chris@sysadminchris.com>
    19 #    directly rather than from the variables they were asigned to. -- Chris Jones <chris@sysadminchris.com>
    15 #  Version 1.9
    20 #  Version 1.9
    16 #    Bug fix and enhancement for .uk and .co.uk -- Vivek Gite <vivek@nixcraft.com>
    21 #    Bug fix and enhancement for .uk and .co.uk -- Vivek Gite <vivek@nixcraft.com>
    17 #
    22 #
    18 #  Version 1.8
    23 #  Version 1.8
    19 #    Bug fix added $MAIL -- Vivek Gite <vivek@nixcraft.com>
    24 #    Bug fix added $mailContact -- Vivek Gite <vivek@nixcraft.com>
    20 #
    25 #
    21 #  Version 1.7
    26 #  Version 1.7
    22 #    Added support for .jp domain names  -- Vivek Gite <vivek@nixcraft.com>
    27 #    Added support for .jp domain names  -- Vivek Gite <vivek@nixcraft.com>
    23 #
    28 #
    24 #  Version 1.6
    29 #  Version 1.6
   108 #
   113 #
   109 
   114 
   110 PATH=/bin:/usr/bin:/usr/local/bin:/usr/local/ssl/bin:/usr/sfw/bin ; export PATH
   115 PATH=/bin:/usr/bin:/usr/local/bin:/usr/local/ssl/bin:/usr/sfw/bin ; export PATH
   111 
   116 
   112 # Who to page when an expired domain is detected (cmdline: -e)
   117 # Who to page when an expired domain is detected (cmdline: -e)
   113 ADMIN="sysadmin@mydomain.com"
   118 admin="sysadmin@mydomain.com"
       
   119 
       
   120 # Hostname to report to Zabbix Servers (cmdline: -i)
       
   121 hostname="-"
   114 
   122 
   115 # Number of days in the warning threshhold  (cmdline: -x)
   123 # Number of days in the warning threshhold  (cmdline: -x)
   116 WARNDAYS=30
   124 warnDays=30
   117 
   125 
   118 # If QUIET is set to TRUE, don't print anything on the console (cmdline: -q)
   126 # If quiet is set to TRUE, don't print anything on the console (cmdline: -q)
   119 QUIET="FALSE"
   127 quiet=false
       
   128 
       
   129 # Human readable output default to off for Zabbix server processing (cmdline: -r)
       
   130 human=false
   120 
   131 
   121 # Don't send emails by default (cmdline: -a)
   132 # Don't send emails by default (cmdline: -a)
   122 ALARM="FALSE"
   133 alarm=false
       
   134 
       
   135 # JSON Discovery mode off by default (cmdline: -j)
       
   136 json=false
   123 
   137 
   124 # Whois server to use (cmdline: -s)
   138 # Whois server to use (cmdline: -s)
   125 WHOIS_SERVER="whois.internic.org"
   139 whoisServer="whois.internic.org"
   126 
   140 
   127 # Location of system binaries
   141 # Location of system binaries
   128 AWK="/usr/bin/awk"
   142 awk="/usr/bin/awk"
   129 WHOIS="/usr/bin/whois"
   143 whois="/usr/bin/whois"
   130 DATE="/bin/date"
   144 date="/bin/date"
   131 CUT="/usr/bin/cut"
   145 cut="/usr/bin/cut"
   132 MAIL="/bin/mail"
   146 mailContact="/bin/mail"
   133 # Place to stash temporary files
   147 
   134 WHOIS_TMP="/var/tmp/whois.$$"
   148 # Track tld types for query limiting:
       
   149 declare -A domainPoll
   135 
   150 
   136 #############################################################################
   151 #############################################################################
   137 # Purpose: Convert a date from MONTH-DAY-YEAR to Julian format
   152 # Purpose: Convert a date from month-DAY-YEAR to Julian format
   138 # Acknowledgements: Code was adapted from examples in the book
   153 # Acknowledgements: Code was adapted from examples in the book
   139 #                   "Shell Scripting Recipes: A Problem-Solution Approach"
   154 #                   "Shell Scripting Recipes: A Problem-Solution Approach"
   140 #                   ( ISBN 1590594711 )
   155 #                   ( ISBN 1590594711 )
   141 # Arguments:
   156 # Arguments:
   142 #   $1 -> Month (e.g., 06)
   157 #   $1 -> Month (e.g., 06)
   143 #   $2 -> Day   (e.g., 08)
   158 #   $2 -> Day   (e.g., 08)
   144 #   $3 -> Year  (e.g., 2006)
   159 #   $3 -> Year  (e.g., 2006)
   145 #############################################################################
   160 #############################################################################
   146 date2julian() 
   161 date2julian() 
   147 {
   162 {
   148     if [ "${1} != "" ] && [ "${2} != ""  ] && [ "${3}" != "" ]
   163     if [[ "${1} != "" && "${2} != "" && "${3}" != "" ]]
   149     then
   164     then
   150          ## Since leap years add aday at the end of February, 
   165          ## Since leap years add aday at the end of February, 
   151          ## calculations are done from 1 March 0000 (a fictional year)
   166          ## calculations are done from 1 March 0000 (a fictional year)
   152          d2j_tmpmonth=$((12 * ${3} + ${1} - 3))
   167          d2j_tmpmonth=$((12 * ${3} + ${1} - 3))
   153         
   168         
   168 # Arguments:
   183 # Arguments:
   169 #   $1 -> Month name (e.g., Sep)
   184 #   $1 -> Month name (e.g., Sep)
   170 #############################################################################
   185 #############################################################################
   171 getmonth() 
   186 getmonth() 
   172 {
   187 {
   173        LOWER=`tolower $1`
   188        case ${1,,} in
   174               
       
   175        case ${LOWER} in
       
   176              jan) echo 1 ;;
   189              jan) echo 1 ;;
   177              feb) echo 2 ;;
   190              feb) echo 2 ;;
   178              mar) echo 3 ;;
   191              mar) echo 3 ;;
   179              apr) echo 4 ;;
   192              apr) echo 4 ;;
   180              may) echo 5 ;;
   193              may) echo 5 ;;
   183              aug) echo 8 ;;
   196              aug) echo 8 ;;
   184              sep) echo 9 ;;
   197              sep) echo 9 ;;
   185              oct) echo 10 ;;
   198              oct) echo 10 ;;
   186              nov) echo 11 ;;
   199              nov) echo 11 ;;
   187              dec) echo 12 ;;
   200              dec) echo 12 ;;
   188                *) echo  0 ;;
   201                *) echo 0 ;;
   189        esac
   202        esac
   190 }
   203 }
   191 
   204 
   192 #############################################################################
   205 #############################################################################
   193 # Purpose: Calculate the number of seconds between two dates
   206 # Purpose: Calculate the number of seconds between two dates
   195 #   $1 -> Date #1
   208 #   $1 -> Date #1
   196 #   $2 -> Date #2
   209 #   $2 -> Date #2
   197 #############################################################################
   210 #############################################################################
   198 date_diff() 
   211 date_diff() 
   199 {
   212 {
   200         if [ "${1}" != "" ] &&  [ "${2}" != "" ]
   213         if [[ "${1}" != "" && "${2}" != "" ]]
   201         then
   214         then
   202                 echo $(expr ${2} - ${1})
   215                 echo $(expr ${2} - ${1})
   203         else
   216         else
   204                 echo 0
   217                 echo 0
   205         fi
   218         fi
   206 }
   219 }
   207 
   220 
   208 ##################################################################
   221 ##################################################################
   209 # Purpose: Converts a string to lower case
       
   210 # Arguments:
       
   211 #   $1 -> String to convert to lower case
       
   212 ##################################################################
       
   213 tolower() 
       
   214 {
       
   215      LOWER=`echo ${1} | tr [A-Z] [a-z]`
       
   216      echo $LOWER
       
   217 }
       
   218 
       
   219 ##################################################################
       
   220 # Purpose: Access whois data to grab the registrar and expiration date
   222 # Purpose: Access whois data to grab the registrar and expiration date
   221 # Arguments:
   223 # Arguments:
   222 #   $1 -> Domain to check
   224 #   $1 -> Domain to check
   223 ##################################################################
   225 ##################################################################
   224 check_domain_status() 
   226 check_domain_status() 
   225 {
   227 {
   226     local REGISTRAR=""
   228     local registrar=""
   227     # Avoid WHOIS LIMIT EXCEEDED - slowdown our whois client by adding 3 sec 
   229     # Avoid whois LIMIT EXCEEDED - slowdown our whois client by adding 3 sec 
   228     sleep 3
   230     #sleep 3
   229     # Save the domain since set will trip up the ordering
   231     # Save the domain since set will trip up the ordering
   230     DOMAIN=${1}
   232     domain=${1}
   231     TLDTYPE="`echo ${DOMAIN} | ${CUT} -d '.' -f3 | tr '[A-Z]' '[a-z]'`" 
   233     tldType="$(echo ${domain} | ${cut} -d '.' -f3 | tr '[A-Z]' '[a-z]')" 
   232     if [ "${TLDTYPE}"  == "" ];
   234     if [[ "${tldType}"  = "" ]];
   233     then
   235     then
   234 	    TLDTYPE="`echo ${DOMAIN} | ${CUT} -d '.' -f2 | tr '[A-Z]' '[a-z]'`" 
   236 	    tldType="$(echo ${domain} | ${cut} -d '.' -f2 | tr '[A-Z]' '[a-z]')" 
   235     fi
   237     fi
   236 
   238 
   237     # Invoke whois to find the domain registrar and expiration date
   239     # Invoke whois to find the domain registrar and expiration date
   238     #${WHOIS} -h ${WHOIS_SERVER} "=${1}" > ${WHOIS_TMP}
   240     #whoisVal=$(${whois} -h ${whoisServer} "=${1}")
   239     # Let whois select server 
   241     # Let whois select server 
   240     if [ "${TLDTYPE}"  == "org" ];
   242    
   241     then
   243     # The whois Expiration data should resemble the following: "Expiration Date: 09-may-2008"
   242         ${WHOIS} -h "whois.pir.org" "${1}" > ${WHOIS_TMP}
   244     case $tldType in
   243     elif [ "${TLDTYPE}"  == "in" ]; # India
   245         "org")  [[ -n ${domainPoll[$tldType]} ]] && sleep 15
   244     then
   246                 whoisVal=$(${whois} -h "whois.pir.org" "${1}")
   245         ${WHOIS} -h "whois.registry.in" "${1}" > ${WHOIS_TMP}
   247                 registrar=$(echo "$whoisVal" | grep "Sponsoring Registrar:" | cut -d':' -f2)
   246     elif [ "${TLDTYPE}"  == "uk" ]; # United Kingdom  
   248                 domainDate=$(echo "${whoisVal}" | ${awk} '/Expiration Date:/ { print $2 }' | ${cut} -d':' -f2)
   247     then
   249                 ;;
   248         ${WHOIS} -h "whois.nic.uk" "${1}" > ${WHOIS_TMP}
   250         "in")   whoisVal=$(${whois} -h "whois.registry.in" "${1}")
   249 
   251                 registrar=$(echo "$whoisVal" | grep "Sponsoring Registrar:" | cut -d':' -f2)
   250     elif [ "${TLDTYPE}"  == "biz" ];
   252                 domainDate=$(echo "${whoisVal}" | ${awk} '/Expiration Date:/ { print $2 }' | ${cut} -d':' -f2)
   251     then
   253                 ;;
   252         ${WHOIS} -h "whois.neulevel.biz" "${1}" > ${WHOIS_TMP}
   254         "uk")   whoisVal=$(${whois} -h "whois.nic.uk" "${1}")
   253     elif [ "${TLDTYPE}"  == "ie" ]; # Ireland
   255                 registrar=$(echo "$whoisVal" | grep "Sponsoring Registrar:" | cut -d':' -f2)
   254     then
   256                 domainDate=$(echo "${whoisVal}" | ${awk} '/Renewal date:/ || /Expiry date:/ { print $3 }')
   255         ${WHOIS} -h "whois.domainregistry.ie" "${1}" > ${WHOIS_TMP}
   257                 ;;
   256     elif [ "${TLDTYPE}"  == "info" ];
   258         "biz")  whoisVal=$(${whois} -h "whois.neulevel.biz" "${1}")
   257     then
   259                 registrar=$(echo "$whoisVal" | grep "Sponsoring Registrar:" | cut -d':' -f2)
   258         ${WHOIS} -h "whois.afilias.info" "${1}" > ${WHOIS_TMP}
   260                 domainDate=$(echo "${whoisVal}" | ${awk} '/Domain Expiration Date:/ { print $6"-"$5"-"$9 }')
   259     elif [ "${TLDTYPE}"  == "jp" ]; # Japan
   261                 ;;
   260     then
   262         "info") whoisVal=$(${whois} -h "whois.afilias.info" "${1}")
   261         ${WHOIS} -h "whois.jprs.jp" "${1}" > ${WHOIS_TMP}
   263                 registrar=$(echo "$whoisVal" | grep "Sponsoring Registrar:" | cut -d':' -f2)
   262     elif [ "${TLDTYPE}"  == "us" ];
   264                 domainDate=$(echo "${whoisVal}" | ${awk} '/Expiration Date:/ { print $2 }' | ${cut} -d':' -f2)
   263     then
   265                 ;;
   264         ${WHOIS} -h "whois.nic.us" "${1}" > ${WHOIS_TMP}
   266         "us")   whoisVal=$(${whois} -h "whois.nic.us" "${1}")
   265 
   267                 registrar=$(echo "$whoisVal" | grep "Sponsoring Registrar:" | cut -d':' -f2)
   266     elif [ "${TLDTYPE}"  == "com" -o "${TLDTYPE}"  == "net" -o "${TLDTYPE}"  == "edu" ];
   268                 domainDate=$(echo "${whoisVal}" | ${awk} '/Domain Expiration Date:/ { print $6"-"$5"-"$9 }')
   267     then
   269                 ;;
   268 	${WHOIS} -h ${WHOIS_SERVER} "=${1}" > ${WHOIS_TMP}
   270         "com" | "net" | "edu")
       
   271                 whoisVal=$(${whois} -h "whois.internic.org" "${1}")
       
   272                 registrar=$(echo "$whoisVal" | grep "Registrar:" | sed -e 's/^[ \t]*//g' -e 's/[ \t]*$//g' | cut -d':' -f2 | sed -e 's/^[ \t]*//g')
       
   273                 domainDate=$(echo "${whoisVal}" | ${awk} '/Expiration/ { print $NF }')
       
   274 
       
   275                 #echo "Registrar = $registrar"
       
   276                 #exit 0
       
   277                 ;;
       
   278         "ie")   whoisVal=$(${whois} -h "whois.domainregistry.ie" "${1}")
       
   279                 registrar=$(echo "$whoisVal" | grep "Sponsoring Registrar:" | cut -d':' -f2)
       
   280                 tdomdate=$(echo "${whoisVal}" | ${awk} '/renewal/ { print $2 }')
       
   281                 tyear=$(echo ${tdomdate} | ${cut} -d'-' -f3)
       
   282                 tmon=$(echo ${tdomdate} | ${cut} -d'-' -f2)
       
   283     	        case ${tmon} in
       
   284     	            "January")      tmonth=jan ;;
       
   285     	            "February")     tmonth=feb ;;
       
   286     	            "March")        tmonth=mar ;;
       
   287     	            "April")        tmonth=apr ;;
       
   288     	            "May")          tmonth=may ;;
       
   289     	            "June")         tmonth=jun ;;
       
   290     	            "July")         tmonth=jul ;;
       
   291     	            "August")       tmonth=aug ;;
       
   292     	            "September")    tmonth=sep ;;
       
   293     	            "October")      tmonth=oct ;;
       
   294     	            "November")     tmonth=nov ;;
       
   295     	            "December")     tmonth=dec ;;
       
   296                    	*)              tmonth=0   ;;
       
   297     		    esac
       
   298                 tday=$(echo ${tdomdate} | ${cut} -d'-' -f1)
       
   299     	        domainDate=$(echo $tday-$tmonth-$tyear)
       
   300                 ;;
       
   301         "jp")   whoisVal=$(${whois} -h "whois.jprs.jp" "${1}")
       
   302                 registrar=$(echo "$whoisVal" | grep "Sponsoring Registrar:" | cut -d':' -f2)
       
   303         	    tdomdate=$(echo "${whoisVal}" | ${awk} '/Expires on/ { print $3 }')
       
   304                 tyear=$(echo ${tdomdate} | ${cut} -d'/' -f1)
       
   305                 tmon=$(echo ${tdomdate} | ${cut} -d'/' -f2)
       
   306     	        case ${tmon} in
       
   307     	             1|01)  tmonth=jan ;;
       
   308     	             2|02)  tmonth=feb ;;
       
   309     	             3|03)  tmonth=mar ;;
       
   310     	             4|04)  tmonth=apr ;;
       
   311     	             5|05)  tmonth=may ;;
       
   312     	             6|06)  tmonth=jun ;;
       
   313     	             7|07)  tmonth=jul ;;
       
   314     	             8|08)  tmonth=aug ;;
       
   315     	             9|09)  tmonth=sep ;;
       
   316     	             10)    tmonth=oct ;;
       
   317     	             11)    tmonth=nov ;;
       
   318     	             12)    tmonth=dec ;;
       
   319                    	 *)     tmonth=0   ;;
       
   320     		    esac
       
   321                 tday=$(echo ${tdomdate} | ${cut} -d'/' -f3)
       
   322     	        domainDate=$(echo $tday-$tmonth-$tyear)
       
   323                 ;;
       
   324         "cz")   whoisVal=$(${whois} -h "${whoisServer}" "${1}")
       
   325                 registrar=$(echo "$whoisVal" | grep "Sponsoring Registrar:" | cut -d':' -f2)
       
   326         	    tdomdate=$(echo "${whoisVal}" | awk '/expire:/ { print $2 }')
       
   327                 tyear=$(echo ${tdomdate} | cut -d'.' -f3)
       
   328                 tmon=$(echo ${tdomdate} | cut -d'.' -f2)
       
   329                 tday=$(echo ${tdomdate} | cut -d'.' -f1)
       
   330                 case ${tmon} in
       
   331                     1|01) tmonth=jan ;;
       
   332                     2|02) tmonth=feb ;;
       
   333                     3|03) tmonth=mar ;;
       
   334                     4|04) tmonth=apr ;;
       
   335                     5|05) tmonth=may ;;
       
   336                     6|06) tmonth=jun ;;
       
   337                     7|07) tmonth=jul ;;
       
   338                     8|08) tmonth=aug ;;
       
   339                     9|09) tmonth=sep ;;
       
   340                     10)   tmonth=oct ;;
       
   341                     11)   tmonth=nov ;;
       
   342                     12)   tmonth=dec ;;
       
   343                     *)    tmonth=0   ;;
       
   344                 esac
       
   345     	        domainDate=$(echo $tday-$tmonth-$tyear)
       
   346                 ;;
       
   347         *)      whoisVal=$(${whois} -h "${whoisServer}" "${1}")
       
   348                 registrar=$(echo "$whoisVal" | grep "Sponsoring Registrar:" | cut -d':' -f2)
       
   349                 domainDate=$(echo "${whoisVal}" | ${awk} '/Expiration/ { print $NF }')
       
   350                 ;;
       
   351     esac
       
   352 
       
   353     if [[ -z ${domainPoll[$tldType]} ]]; then
       
   354         domainPoll[$tldType]=1
   269     else
   355     else
   270 	${WHOIS} "${1}" > ${WHOIS_TMP}
   356         let domainPoll[$tldType]++
   271     fi
       
   272 
       
   273     # Parse out the expiration date and registrar -- uses the last registrar it finds
       
   274     REGISTRAR=`cat ${WHOIS_TMP} | ${AWK} -F: '/Registrar/ && $2 != ""  { REGISTRAR=substr($2,2,17) } END { print REGISTRAR }'`
       
   275 
       
   276     if [ "${TLDTYPE}" == "uk" ]; # for .uk domain
       
   277     then
       
   278 	REGISTRAR=`cat ${WHOIS_TMP} | ${AWK} -F: '/Registrar:/ && $0 != ""  { getline; REGISTRAR=substr($0,2,17) } END { print REGISTRAR }'`
       
   279     elif [ "${TLDTYPE}" == "jp" ];
       
   280     then
       
   281         REGISTRAR=`cat ${WHOIS_TMP} | ${AWK} '/Registrant/ && $2 != ""  { REGISTRAR=substr($2,1,17) } END { print REGISTRAR }'`
       
   282     elif [ "${TLDTYPE}" == "cz" ];
       
   283     then
       
   284         REGISTRAR=`cat ${WHOIS_TMP} | ${AWK} '/registrar:/ && $2 != ""  { REGISTRAR=substr($2,1,17) } END { print REGISTRAR }'`
       
   285     elif [ "${TLDTYPE}" == "ie" ]; # for .ie domain
       
   286     then
       
   287         REGISTRAR=`cat ${WHOIS_TMP} | ${AWK} '/source/ && $2 != ""  { REGISTRAR=substr($2,1,17) } END { print REGISTRAR }'`
       
   288     elif [ "${TLDTYPE}" == "us" ];
       
   289     then
       
   290         REGISTRAR=`cat ${WHOIS_TMP} | ${AWK} -F: '/Last Updated by Registrar:/ && $2 != ""  { REGISTRAR=substr($2,20,17) } END { print REGISTRAR }'`
       
   291     fi
   357     fi
   292 
   358 
   293     # If the Registrar is NULL, then we didn't get any data
   359     # If the Registrar is NULL, then we didn't get any data
   294     if [ "${REGISTRAR}" = "" ]
   360     #if [[ "${registrar}" = "" ]]
   295     then
   361     #then
   296         prints "$DOMAIN" "Unknown" "Unknown" "Unknown" "Unknown"
   362     #    prints "$domain" "Unknown" "Unknown" "Unknown" "Unknown"
   297         return
   363     #    return
   298     fi
   364     #fi
   299 
   365 
   300     # The whois Expiration data should resemble the following: "Expiration Date: 09-may-2008"
   366     #echo $domainDate # debug 
   301 
       
   302     # for .in, .info, .org domains
       
   303     if [ "${TLDTYPE}" == "in" -o "${TLDTYPE}" == "info" -o "${TLDTYPE}" == "org" ];
       
   304     then
       
   305 	    DOMAINDATE=`cat ${WHOIS_TMP} | ${AWK} '/Expiration Date:/ { print $2 }' | ${CUT} -d':' -f2`
       
   306     elif [ "${TLDTYPE}" == "biz" ]; # for .biz domain
       
   307     then
       
   308             DOMAINDATE=`cat ${WHOIS_TMP} | ${AWK} '/Domain Expiration Date:/ { print $6"-"$5"-"$9 }'`
       
   309     elif [ "${TLDTYPE}" == "ie" ]; # for .ie domain
       
   310     then
       
   311             tdomdate=`cat ${WHOIS_TMP} | ${AWK} '/renewal/ { print $2 }'`
       
   312             tyear=`echo ${tdomdate} | ${CUT} -d'-' -f3`
       
   313             tmon=`echo ${tdomdate} | ${CUT} -d'-' -f2`
       
   314 	       case ${tmon} in
       
   315 	             "January") tmonth=jan ;;
       
   316 	             "February") tmonth=feb ;;
       
   317 	             "March") tmonth=mar ;;
       
   318 	             "April") tmonth=apr ;;
       
   319 	             "May") tmonth=may ;;
       
   320 	             "June") tmonth=jun ;;
       
   321 	             "July") tmonth=jul ;;
       
   322 	             "August") tmonth=aug ;;
       
   323 	             "September") tmonth=sep ;;
       
   324 	             "October")tmonth=oct ;;
       
   325 	             "November") tmonth=nov ;;
       
   326 	             "December") tmonth=dec ;;
       
   327                	      *) tmonth=0 ;;
       
   328 		esac
       
   329             tday=`echo ${tdomdate} | ${CUT} -d'-' -f1`
       
   330 	    DOMAINDATE=`echo $tday-$tmonth-$tyear`
       
   331     elif [ "${TLDTYPE}" == "uk" ]; # for .uk domain
       
   332     then
       
   333             DOMAINDATE=`cat ${WHOIS_TMP} | ${AWK} '/Renewal date:/ || /Expiry date:/ { print $3 }'`
       
   334     elif [ "${TLDTYPE}" == "us" ]; # for .us domain
       
   335     then
       
   336             DOMAINDATE=`cat ${WHOIS_TMP} | ${AWK} '/Domain Expiration Date:/ { print $6"-"$5"-"$9 }'`
       
   337     elif [ "${TLDTYPE}" == "jp" ]; # for .jp 2010/04/30
       
   338     then
       
   339 	    tdomdate=`cat ${WHOIS_TMP} | ${AWK} '/Expires on/ { print $3 }'`
       
   340             tyear=`echo ${tdomdate} | ${CUT} -d'/' -f1`
       
   341             tmon=`echo ${tdomdate} | ${CUT} -d'/' -f2`
       
   342 	       case ${tmon} in
       
   343 	             1|01) tmonth=jan ;;
       
   344 	             2|02) tmonth=feb ;;
       
   345 	             3|03) tmonth=mar ;;
       
   346 	             4|04) tmonth=apr ;;
       
   347 	             5|05) tmonth=may ;;
       
   348 	             6|06) tmonth=jun ;;
       
   349 	             7|07) tmonth=jul ;;
       
   350 	             8|08) tmonth=aug ;;
       
   351 	             9|09) tmonth=sep ;;
       
   352 	             10)tmonth=oct ;;
       
   353 	             11) tmonth=nov ;;
       
   354 	             12) tmonth=dec ;;
       
   355                	      *) tmonth=0 ;;
       
   356 		esac
       
   357             tday=`echo ${tdomdate} | ${CUT} -d'/' -f3`
       
   358 	    DOMAINDATE=`echo $tday-$tmonth-$tyear`
       
   359     elif [ "${TLDTYPE}" == "cz" ]; # for .cz 08.02.2013 dd.mm.yyyy
       
   360     then
       
   361 	    tdomdate=`cat ${WHOIS_TMP} | awk '/expire:/ { print $2 }'`
       
   362         tyear=`echo ${tdomdate} | cut -d'.' -f3`
       
   363         tmon=`echo ${tdomdate} | cut -d'.' -f2`
       
   364         tday=`echo ${tdomdate} | cut -d'.' -f1`
       
   365         case ${tmon} in
       
   366              1|01) tmonth=jan ;;
       
   367              2|02) tmonth=feb ;;
       
   368              3|03) tmonth=mar ;;
       
   369              4|04) tmonth=apr ;;
       
   370              5|05) tmonth=may ;;
       
   371              6|06) tmonth=jun ;;
       
   372              7|07) tmonth=jul ;;
       
   373              8|08) tmonth=aug ;;
       
   374              9|09) tmonth=sep ;;
       
   375              10)   tmonth=oct ;;
       
   376              11)   tmonth=nov ;;
       
   377              12)   tmonth=dec ;;
       
   378               *)   tmonth=0 ;;
       
   379         esac
       
   380 	    DOMAINDATE=`echo $tday-$tmonth-$tyear`
       
   381     else # .com, .edu, .net and may work with others	 
       
   382 	    DOMAINDATE=`cat ${WHOIS_TMP} | ${AWK} '/Expiration/ { print $NF }'`	
       
   383     fi
       
   384 
       
   385     #echo $DOMAINDATE # debug 
       
   386     # Whois data should be in the following format: "13-feb-2006"
   367     # Whois data should be in the following format: "13-feb-2006"
   387     IFS="-"
   368     IFS="-"
   388     set -- ${DOMAINDATE}
   369     set -- ${domainDate}
   389     MONTH=$(getmonth ${2})
   370     month=$(getmonth ${2})
   390     IFS=""
   371     IFS=""
   391 
   372 
   392     # Convert the date to seconds, and get the diff between NOW and the expiration date
   373     # Convert the date to seconds, and get the diff between NOW and the expiration date
   393     DOMAINJULIAN=$(date2julian ${MONTH} ${1#0} ${3})
   374     domainJulian=$(date2julian ${month} ${1#0} ${3})
   394     DOMAINDIFF=$(date_diff ${NOWJULIAN} ${DOMAINJULIAN})
   375     domainDiff=$(date_diff ${nowJulian} ${domainJulian})
   395 
   376 
   396     if [ ${DOMAINDIFF} -lt 0 ]
   377     if ! $human
   397     then
   378     then
   398           if [ "${ALARM}" = "TRUE" ]
   379         echo "$hostname domain.daysleft.${domain} ${domainDiff}"
   399           then
   380     fi
   400                 echo "The domain ${DOMAIN} has expired!" \
   381 
   401                 | ${MAIL} -s "Domain ${DOMAIN} has expired!" ${ADMIN}
   382     if [[ ${domainDiff} -lt 0 ]]
   402            fi
   383     then
   403 
   384         if $alarm
   404            prints ${DOMAIN} "Expired" "${DOMAINDATE}" "${DOMAINDIFF}" ${REGISTRAR}
   385         then
   405 
   386             echo "The domain ${domain} has expired!" \
   406     elif [ ${DOMAINDIFF} -lt ${WARNDAYS} ]
   387                 | ${mailContact} -s "Domain ${domain} has expired!" ${admin}
   407     then
   388         fi
   408            if [ "${ALARM}" = "TRUE" ]
   389         prints ${domain} "Expired" "${domainDate}" "${domainDiff}" ${registrar}
   409            then
   390     elif [[ ${domainDiff} -lt ${warnDays} ]]
   410                     echo "The domain ${DOMAIN} will expire on ${DOMAINDATE}" \
   391     then
   411                     | ${MAIL} -s "Domain ${DOMAIN} will expire in ${WARNDAYS}-days or less" ${ADMIN}
   392         if $alarm
   412             fi
   393         then
   413             prints ${DOMAIN} "Expiring" "${DOMAINDATE}" "${DOMAINDIFF}" "${REGISTRAR}"
   394             echo "The domain ${domain} will expire on ${domainDate}" \
   414      else
   395                 | ${mailContact} -s "Domain ${domain} will expire in ${warnDays}-days or less" ${admin}
   415             prints ${DOMAIN} "Valid" "${DOMAINDATE}"  "${DOMAINDIFF}" "${REGISTRAR}"
   396         fi
   416      fi
   397         prints ${domain} "Expiring" "${domainDate}"  "${registrar}"
       
   398     else
       
   399         prints ${domain} "Valid" "${domainDate}"  "${domainDiff}" "${registrar}"
       
   400     fi
   417 }
   401 }
   418 
   402 
   419 ####################################################
   403 ####################################################
   420 # Purpose: Print a heading with the relevant columns
   404 # Purpose: Print a heading with the relevant columns
   421 # Arguments:
   405 # Arguments:
   422 #   None
   406 #   None
   423 ####################################################
   407 ####################################################
   424 print_heading()
   408 print_heading()
   425 {
   409 {
   426         if [ "${QUIET}" != "TRUE" ]
   410     if $human
       
   411     then
       
   412         if ! $quiet
   427         then
   413         then
   428                 printf "\n%-35s %-17s %-8s %-11s %-5s\n" "Domain" "Registrar" "Status" "Expires" "Days Left"
   414             printf "\n%-35s %-17s %-8s %-11s %-5s\n" "Domain" "Registrar" "Status" "Expires" "Days Left"
   429                 echo "----------------------------------- ----------------- -------- ----------- ---------"
   415             echo "----------------------------------- ----------------- -------- ----------- ---------"
   430         fi
   416         fi
       
   417     fi
   431 }
   418 }
   432 
   419 
   433 #####################################################################
   420 #####################################################################
   434 # Purpose: Print a line with the expiraton interval
   421 # Purpose: Print a line with the expiraton interval
   435 # Arguments:
   422 # Arguments:
   439 #   $4 -> Days left until the domain will expire
   426 #   $4 -> Days left until the domain will expire
   440 #   $5 -> Domain registrar
   427 #   $5 -> Domain registrar
   441 #####################################################################
   428 #####################################################################
   442 prints()
   429 prints()
   443 {
   430 {
   444     if [ "${QUIET}" != "TRUE" ]
   431     if $human
   445     then
   432     then
   446             MIN_DATE=$(echo $3 | ${AWK} '{ print $1, $2, $4 }')
   433         if ! $quiet
   447             printf "%-35s %-17s %-8s %-11s %-5s\n" "$1" "$5" "$2" "$MIN_DATE" "$4"
   434         then
       
   435             minDate=$(echo $3 | ${awk} '{ print $1, $2, $4 }')
       
   436             printf "%-35s %-17s %-8s %-11s %-5s\n" "${1:0:35}" "${5:0:17}" "${2:0:8}" "${minDate:0:11}" "${4:0:5}"
       
   437         fi
   448     fi
   438     fi
   449 }
   439 }
   450 
   440 
   451 ##########################################
   441 ##########################################
   452 # Purpose: Describe how the script works
   442 # Purpose: Describe how the script works
   453 # Arguments:
   443 # Arguments:
   454 #   None
   444 #   None
   455 ##########################################
   445 ##########################################
   456 usage()
   446 usage()
   457 {
   447 {
   458         echo "Usage: $0 [ -e email ] [ -x expir_days ] [ -q ] [ -a ] [ -h ]"
   448         #app="$(basename $0)"
   459         echo "          {[ -d domain_namee ]} || { -f domainfile}"
   449         echo "Usage:" >&2
   460         echo ""
   450         echo "Zabbix Agent Mode:" >&2
   461         echo "  -a               : Send a warning message through email "
   451         echo "  [-s whois_host] [-i hostname] <-d domain | -f file>" >&2
   462         echo "  -d domain        : Domain to analyze (interactive mode)"
   452         echo "Human Readable Mode:" >&2
   463         echo "  -e email address : Email address to send expiration notices"
   453         echo "  -r [-q][-a] [-e email] [-x days] [-s host] <-d domain|-f file>" >&2
   464         echo "  -f domain file   : File with a list of domains"
   454         echo "JSON Mode:" >&2
   465         echo "  -h               : Print this screen"
   455         echo "  -j -f domainfile" >&2
   466         echo "  -s whois server  : Whois sever to query for information"
   456         echo ""  >&2
   467         echo "  -q               : Don't print anything on the console"
   457         echo "  -a               : Send a warning message through email " >&2
   468         echo "  -x days          : Domain expiration interval (eg. if domain_date < days)"
   458         echo "  -d domain        : Domain to analyze (interactive mode)" >&2
   469         echo ""
   459         echo "  -e email address : Email address to send expiration notices" >&2
       
   460         echo "  -f domain file   : File with a list of domains" >&2
       
   461         echo "  -h               : Print this screen" >&2
       
   462         echo "  -i hostname      : Hostname for Zabbix Server" >&2
       
   463         echo "  -j               : Discovery JSON Object for Zabbix auto-discovery, (needs -f)" >&2
       
   464         echo "  -r               : Human Readable" >&2
       
   465         echo "  -s whois server  : Whois sever to query for information" >&2
       
   466         echo "  -q               : Don't print anything on the console" >&2
       
   467         echo "  -x days          : Domain expiration interval (eg. if domain_ date < days)" >&2
       
   468         echo "" >&2
   470 }
   469 }
   471 
   470 
   472 ### Evaluate the options passed on the command line
   471 ### Evaluate the options passed on the command line
   473 while getopts ae:f:hd:s:qx: option
   472 while getopts ae:f:i:jrhd:s:qx: option
   474 do
   473 do
   475         case "${option}"
   474         case "${option}"
   476         in
   475         in
   477                 a) ALARM="TRUE";;
   476                 a) alarm=true;;
   478                 e) ADMIN=${OPTARG};;
   477                 e) admin=${OPTARG};;
   479                 d) DOMAIN=${OPTARG};;
   478                 d) domain=${OPTARG};;
   480                 f) SERVERFILE=$OPTARG;;
   479                 f) serverFile=$OPTARG;;
   481                 s) WHOIS_SERVER=$OPTARG;;
   480                 i) hostname=$OPTARG;;
   482                 q) QUIET="TRUE";;
   481                 j) json=true;;
   483                 x) WARNDAYS=$OPTARG;;
   482                 r) human=true;;
       
   483                 s) whoisServer=$OPTARG;;
       
   484                 q) quiet=true;;
       
   485                 x) warnDays=$OPTARG;;
   484                 \?) usage
   486                 \?) usage
   485                     exit 1;;
   487                     exit 1;;
   486         esac
   488         esac
   487 done
   489 done
   488 
   490 
   489 ### Check to see if the whois binary exists
   491 ### Check to see if the whois binary exists
   490 if [ ! -f ${WHOIS} ]
   492 if [[ ! -f ${whois} ]]
   491 then
   493 then
   492         echo "ERROR: The whois binary does not exist in ${WHOIS} ."
   494         echo "ERROR: The whois binary does not exist in ${whois}." >&2
   493         echo "  FIX: Please modify the \$WHOIS variable in the program header."
   495         echo "  FIX: Please modify the \$whois variable in the program header." >&2
   494         exit 1
   496         exit 1
   495 fi
   497 fi
   496 
   498 
   497 ### Check to make sure a date utility is available
   499 ### Check to make sure a date utility is available
   498 if [ ! -f ${DATE} ]
   500 if [[ ! -f ${date} ]]
   499 then
   501 then
   500         echo "ERROR: The date binary does not exist in ${DATE} ."
   502         echo "ERROR: The date binary does not exist in ${date}." >&2
   501         echo "  FIX: Please modify the \$DATE variable in the program header."
   503         echo "  FIX: Please modify the \$date variable in the program header." >&2
   502         exit 1
   504         exit 1
   503 fi
   505 fi
   504 
   506 
   505 ### Baseline the dates so we have something to compare to
   507 ### Baseline the dates so we have something to compare to
   506 MONTH=$(${DATE} "+%m")
   508 month=$(${date} "+%m")
   507 DAY=$(${DATE} "+%d")
   509 DAY=$(${date} "+%d")
   508 YEAR=$(${DATE} "+%Y")
   510 YEAR=$(${date} "+%Y")
   509 NOWJULIAN=$(date2julian ${MONTH#0} ${DAY#0} ${YEAR})
   511 nowJulian=$(date2julian ${month#0} ${DAY#0} ${YEAR})
   510 
       
   511 ### Touch the files prior to using them
       
   512 touch ${WHOIS_TMP}
       
   513 
   512 
   514 ### If a HOST and PORT were passed on the cmdline, use those values
   513 ### If a HOST and PORT were passed on the cmdline, use those values
   515 if [ "${DOMAIN}" != "" ]
   514 if $json
   516 then
   515 then
   517         print_heading
   516     if [[ -f "${serverFile}" ]]
   518         check_domain_status "${DOMAIN}"
   517     then
   519 ### If a file and a "-a" are passed on the command line, check all
   518         echo "{"
   520 ### of the domains in the file to see if they are about to expire
   519         echo "  \"data\":["
   521 elif [ -f "${SERVERFILE}" ]
   520         echo
   522 then
   521         while read domain
   523         print_heading
       
   524         while read DOMAIN
       
   525         do
   522         do
   526                 sleep 30
   523             echo "  { \"{#DOMAIN}\":\"${domain}\" },"
   527                 check_domain_status "${DOMAIN}"
   524         done < ${serverFile}
   528 
   525         echo
   529         done < ${SERVERFILE}
   526         echo "  ]"
   530 
   527         echo "}"
   531 ### There was an error, so print a detailed usage message and exit
   528     else
   532 else
       
   533         usage
   529         usage
   534         exit 1
   530         exit 1
       
   531     fi
       
   532 else
       
   533     if [[ "${domain}" != "" ]]
       
   534     then
       
   535             print_heading
       
   536             check_domain_status "${domain}"
       
   537     ### If a file and a "-a" are passed on the command line, check all
       
   538     ### of the domains in the file to see if they are about to expire
       
   539     elif [[ -f "${serverFile}" ]]
       
   540     then
       
   541             print_heading
       
   542             while read domain
       
   543             do
       
   544                     #sleep 30
       
   545                     check_domain_status "${domain}"
       
   546 
       
   547             done < ${serverFile}
       
   548 
       
   549     ### There was an error, so print a detailed usage message and exit
       
   550     else
       
   551             usage
       
   552             exit 1
       
   553     fi
       
   554 
       
   555     # Add an extra newline
       
   556     if $human
       
   557     then
       
   558         echo
       
   559     fi
   535 fi
   560 fi
   536 
       
   537 # Add an extra newline
       
   538 echo
       
   539 
       
   540 ### Remove the temporary files
       
   541 rm -f ${WHOIS_TMP}
       
   542 
   561 
   543 ### Exit with a success indicator
   562 ### Exit with a success indicator
   544 exit 0
   563 exit 0
   545 
   564 
   546 # vim: et ts=4 sw=4
   565 # vim: et ts=4 sw=4