LoginSignup
21
20

More than 5 years have passed since last update.

Zabbixでドメインの有効期限を監視する

Last updated at Posted at 2013-12-02

ドメインの有効期限を忘れていて、うっかり失効するという事態を防ぐ為に、Zabbixでドメインの有効期限を監視できるようにしてみました

監視方法は、

1. ドメインの残有効日数を取得するスクリプトをzabbix_agentが起動しているサーバに設置

2. Zabbixサーバに、1で設置したスクリプトを起動するアイテムを登録

3. アイテムに対するトリガーを設定

といった感じになります。

使用するスクリプトはドメインの有効期限を監視するシェルスクリプトをお借りしました。(+監視できるドメインを追加しました)
 


まず、下記のスクリプトをzabbixエージェントが起動しているサーバに設置します。

get-domain-expire.sh
#!/bin/bash

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

#. $PROGPATH/utils.sh

# Default values (days):
critical=30
warning=60
whois="/usr/bin/whois"
host=""

usage() {
        echo "check_domain - v1.01p1"
        echo "Copyright (c) 2005 Tom〓s N〓〓ez Lirola <tnunez@criptos.com> under GPL License"
        echo "Modified by MATSUU Takuto <matsuu@gentoo.org>"
        echo "Modified by Tateoka Mamoru <qphoney@gmail.com>"
        echo ""
        echo "This plugin checks the expiration date of a domain name." 
        echo ""
        echo "Usage: $0 -h | -d <domain> [-W <command>] [-H <host>] [-c <critical>] [-w <warning>]"
        echo "NOTE: -d must be specified"
        echo ""
        echo "Options:"
        echo "-h"
        echo "  Print detailed help"
        echo "-d DOMAIN"
        echo "  Domain name to check"
        echo "-H HOST"
        echo "  Connect to server HOST"
        echo "-W COMMAND"
        echo "  Use COMMAND instead of whois"
        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 example.org -w 30 -c 10"
        echo "  $0 -d example.jp/e -H whois.jprs.jp -w 30 -c 10"
        echo "  $0 -d example.jp -W /usr/bin/jwhois -w 30 -c 10"
        echo ""
}

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

while true ; do
        case "$1" in
                -h|--help)      usage;exit;;
                -d|--domain)    domain=$2;shift 2;;
                -w|--warning)   warning=$2;shift 2;;
                -c|--critical)  critical=$2;shift 2;;
                -W|--whois)     whois=$2;shift 2;;
                -H|--host)      host="-h $2";shift 2;;
                --)             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 [ ! -x $whois ]; then
        echo "UNKNOWN - Unable to find whois binary in your path. Is it installed? Please specify path."
        exit $STATE_UNKNOWN
fi

# Calculate days until expiration
TLDTYPE=`echo ${domain##*.} | tr '[A-Z]' '[a-z]'`
#echo "host:$domain"
if [ "${TLDTYPE}" == "in" -o "${TLDTYPE}" == "info" -o "${TLDTYPE}" == "org" ]; then
        expiration=`$whois $host $domain | awk '/Expiration Date:/ { print $2 }' |cut -d':' -f2`
elif [ "${TLDTYPE}" == "net" ]; then
        expiration=`$whois $host $domain | awk '/Expiration Date/ {print $5,$6}'`
elif [ "${TLDTYPE}" == "cc" ]; then
        expiration=`$whois $host $domain | awk '/Expiration Date:/ {print $5}' |cut -d'T' -f1`
elif [ "${TLDTYPE}" == "com" ]; then
        expiration=`$whois $host $domain | awk '/Expiration Date:/ {print $5}'`
elif [ "${TLDTYPE}" == "net" ]; then
        expiration=`$whois $host $domain | awk '/Domain Expiration Date:/ { print $6"-"$5"-"$9 }'`
elif [ "${TLDTYPE}" == "sc" ]; then
        expiration=`$whois -h whois2.afilias-grs.net $host $domain | awk '/Expiration Date:/ { print $2 }' | awk -F : '{ print $2 }'`
elif [ "${TLDTYPE}" == "jp" -o "${TLDTYPE}" == "jp/e" -o "${TLDTYPE}" == "am"  ]; then
        expiration=`$whois $host $domain | awk '/Expires/ { print $NF }'`
    if [ -z $expiration ]; then
            expiration=`$whois $host $domain | awk '/State/ { print $NF }' | tr -d \(\)`
    fi
else
        expiration=`$whois $host $domain | awk '/Expiration/ { print $NF }'`
fi
#echo $expiration
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
#[ $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

echo $expdays
#echo 30
exit $STATE_OK

スクリプトはget-domain-expire.sh -d [ドメイン]で使用できます(get-domain-expire.sh -d yahoo.co.jp)
スクリプトを実行すると、ドメインの有効日数が出力されます。
 


上記のスクリプトをアイテムに登録して、起動できるようにする為に、zabbix_agentd.confにUserParameterを追加します

zabbix_agentd.conf
UserParameter=get_domain_expire[*],/path/to/get-domain-expire.sh -d $1

zabbix_agentを再起動してください(実行権限の設定をお忘れなく)
 
で、アイテムを登録します

zabbix1.png

 
yahoo.co.jpとなっている部分はマクロ({HOST.HOST})とかが使えるのかな?
 


最後に、トリガーを設定します。
zabbix2.png


 
以上、ドメインの有効期限の監視ができるようになると思います。
 
 
 
 


ホスト名=ドメイン名でサーバを構築していれば、テンプレート化できると思います。(じゃないと設定が大変かも・・・)
大人の都合でサーバ名がドメイン名でなかったり、zabbix_agentがインストールされていない場合は、どこか1カ所にスクリプトを設置して、ひたすら手動でアイテムとトリガーを追加するとよいと思います。
 
↓1つのホストにこんなアイテムとトリガーをひたすら追加・・・
get_domain_expire[abc.jp]
get_domain_expire[zzz.jp]
get_domain_expire[zxy.jp]
get_domain_expire[hoge.jp]

{127.0.0.1:get_domain_expire[abc.jp].last()}<31
{127.0.0.1:get_domain_expire[zzz.jp].last()}<31
{127.0.0.1:get_domain_expire[zxy.jp].last()}<31
{127.0.0.1:get_domain_expire[hoge.jp].last()}<31


 

21
20
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
21
20