Idiomes

Comprovar la data d'expiració d'un domini: check_domain

No ens agradaria que el nostre domini caduqués i vingués un ciberespeculador (també mal anomenats "ciberokupes") i ens demanés 1.000$ per ell, quan en realitat val 20 (ho he viscut amb un domini personal que tenia).

No és un greu problema, al cap i a la fi els registrars sempre avisen amb temps d'antelació per donar-te totes les facilitats de renovació (que és el que a ells els interessa). Però... i si l'adreça de correu que vau configurar el seu dia, ja no està activa? I si la nova secretaria del jefe ho confon amb spam i ho ignora (cas real)? I si l'empresa és tan gran que ningú no sap qui llegeix aquella adreça de correu?

Per assegurar-nos que estem al dia dels nostres dominis, he creat un plugin pel nagios que es diu "check_domain". És força senzill (si ens hi fixem veurem que hi ha més tros parsejant paràmetres que no pas fent coses ), però cobreix la necessitat i t'avisa quan el domini està a prop de caducar.

A l'article complet ("llegir més") hi apareix el codi i l'arxiu descarregable.

#!/bin/bash

PROGPATH=`echo $0 | /bin/sed -e 's,[\\/][^\\/][^\\/]*$,,'`

. $PROGPATH/utils.sh

# Default values (days):
critical=7
warning=30

# Parse arguments
args=`getopt -o hd:w:c:P: --long help,domain:,warning:,critical:,path: -u -n $0 -- "$@"`
[ $? != 0 ] && echo "$0: Could not parse arguments" && echo "Usage: $0 -h | -d <domain> [-c <critical>] [-w <warning>]" && exit
set -- $args

while true ; 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)     echo "check_domain - v1.01"
                               echo "Copyright (c) 2005 Tom�s N��ez Lirola <tnunez@criptos.com> under GPL License"
                               echo "This plugin checks the expiration date of a domain name."
                               echo ""
                               echo "Usage: $0 -h | -d <domain> [-c <critical>] [-w <warning>]"
                               echo "NOTE: -d must be specified"
                               echo ""
                               echo "Options:"
                               echo "-h"
                               echo "     Print detailed help"
                               echo "-d"
                               echo "     Domain name to check"
                               echo "-w"
                               echo "     Response time to result in warning status (days)"
                               echo "-c"
                               echo "     Response time to result in critical status (days)"
                               echo ""
                               echo "This plugin will use whois service to get the expiration date for the domain name. "
                               echo "Example:"
                               echo "     $0 -d domain.tld -w 30 -c 10"
                               echo ""
                               exit;;
             --) shift; break;;
                *)  echo "Internal error!" ; exit 1 ;;
        esac
done

[ -z $domain ] && echo "UNKNOWN - There is no domain name to check" && exit $STATE_UNKNOWN

# Looking for whois binary
if [ -z $whoispath ]; then
      type whois &> /dev/null || error="yes"
      [ ! -z $error ] && echo "UNKNOWN - Unable to find whois binary in your path. Is it installed? Please specify path." && exit $STATE_UNKNOWN
else
      [ ! -x "$whoispath/whois" ] && echo "UNKNOWN - Unable to find whois binary, you specified an incorrect path" && exit $STATE_UNKNOWN
fi

# Calculate days until expiration
expiration=`whois $domain |grep "Expiration Date:"| awk -F"Date:" '{print $2}'|cut -f 1`
expseconds=`date +%s --date="$expiration"`
nowseconds=`date +%s`
((diffseconds=expseconds-nowseconds))
expdays=$((diffseconds/86400))

# Trigger alarms if applicable
[ -z "$expiration" ] && echo "UNKNOWN - Domain doesn't exist or no WHOIS server available." && exit $STATE_UNKNOWN
[ $expdays -lt 0 ] && echo "CRITICAL - Domain expired on $expiration" && exit $STATE_CRITICAL
[ $expdays -lt $critical ] && echo "CRITICAL - Domain will expire in $expdays days" && exit $STATE_CRITICAL
[ $expdays -lt $warning ]&& echo "WARNING - Domain will expire in $expdays days" && exit $STATE_WARNING

# No alarms? Ok, everything is right.
echo "OK - Domain will expire in $expdays days"
exit $STATE_OK

AdjuntMida
check_domain.3.35 KB