---
title: Nagiosでドメイン有効期限を監視する
date: 2013-01-27T15:04:05Z
author: kazu634
images:
- "ogp/2013-01-27-check_domain_expiration_by_nagios.webp"
categories:
- インフラ
- 監視
tags:
- nagios
---
「ドメインの残り有効期限を取得するシェルスクリプト」で紹介したスクリプトを Nagios から使用できるようにしてみました。
## これは何をするもの?
Nagios からドメインの有効期限を監視します。今のところ .com にしか対応していません。作業完了後のイメージはこんな感じです:
## Nagiosプラグインの仕様
ここでNagiosプラグインの仕様がまとめられています。簡単にまとめると次のようになります:
* リターンコード 0: OK
* リターンコード 1: Warning
* リターンコード 2: Critical
* リターンコード 3: Unknown
* 最低1行は標準出力に何か出力すること
## スクリプト
「ドメインの残り有効期限を取得するシェルスクリプト」で作成したスクリプトを修正して、適切なリターンコードを返すように修正しました:
#!/bin/bash
########################################
# Name: Kazuhiro MUSASHI
#
# about:
#
# Usage:
#
# Author:
# Date:
########################################
set -e
# Constants
WHOIS='/usr/bin/whois'
# Thresholds
WARNINGS=60
CRITICAL=30
# Return Codes
OK=0
WARN=1
CRIT=2
UNKNOWN=3
# Check the number of the arguments
if [ $# -ne 1 ]; then
exit ${UNKNOWN}
fi
DOMAIN=$1
# Check the specified domain name
if [ ! ${DOMAIN##*.} == "com" ]; then
echo "Specify the .com domain name."
exit ${UNKNOWN}
fi
# Check whether the whois command exists or not
if [ ! -x ${WHOIS} ]; then
echo "${WHOIS} command does not exist."
exit ${UNKNOWN}
fi
# Execute the whois command
EXPIRE=`${WHOIS} ${DOMAIN} | grep Expiration | tail -n 1 | cut -f 3 -d " "`
# Convert the expiration date into seconds
EXPIRE_SECS=`date +%s --date=${EXPIRE}`
# Acquire the now seconds
CURRENTDATE_SEC=`date +%s`
# Calculate the remaining days
((DIFF_SEC=EXPIRE_SECS-CURRENTDATE_SEC))
REMAIN_DAYS=$((DIFF_SEC/86400))
# OK case
if [ ${REMAIN_DAYS} -ge ${WARNINGS} ]; then
echo "The domain will be expired in ${REMAIN_DAYS} days."
exit ${OK}
# Warning case
elif [ ${REMAIN_DAYS} -ge ${CRITICAL} ]; then
echo "The domain will be expired in ${REMAIN_DAYS} days."
exit ${WARN}
# Critical case
elif [ ${REMAIN_DAYS} -lt ${CRITICAL} ]; then
echo "The domain will be expired in ${REMAIN_DAYS} days."
exit ${CRIT}
fi
echo "The script should not end at this point."
exit ${UNKNOWN}
私は作成したスクリプトを /usr/loca/bin/ 配下に配置しました。
## commnds.cfgの編集
Nagiosのcommands.cfgに以下の行を追加します:
+# 'check_domain' command definition
+define command{
+ command_name check_domain
+ command_line /usr/local/bin/check_domain $ARG1$
+ }
## 監視サービスの追加
監視設定を記述している設定ファイルに例えば以下の行を追加します:
+define service{
+ use generic-service
+ host_name sakura-vps
+ service_description domain
+ check_command check_domain!kazu634.com
+ check_interval 1440
+ }
後は「sudo service nagios reload」などのコマンドを実行し、Nagios設定ファイルをリロードしてください。
## 参考
* Nagiosプラグイン開発ガイドライン
* Nagiosプラグイン自作方法の紹介