2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

SNMPを利用して時刻監視をするNagiosプラグイン(シェル版)

Posted at

SNMPを利用して対象サーバの時刻(監視サーバとの時差)を監視するプラグインを作成してみました。

標準のプラグインとしてcheck_ntpがありますが、対象サーバがNTPを起動していない場合(VMとか)や別サーバからNTP接続を許可していない場合などに設定を入れてもらう必要がありやや面倒です。

というわけで、それならsnmpで時刻を取得して監視すればいいじゃないかとシェルでプラグインを作成してみました。 閾値は「」で、監視サーバと指定した秒数以上の開きがあれば警告を返します。

ちなみに方法ですが、snmpwalkコマンドで HOST-RESOURCES-MIB::hrSystemDate を取得しますが、 2014-8-6,15:43:9.0,+9:0 といった結果が返ってきますのでそれを解析して閾値と比較しています。

check_datetime.sh
# !/bin/sh
########################################################################
#    Nagios Plugin for Datetime-diff for SNMP.
##
###    Argument    Community / HostIP / Warning / Critical
####   Create      2014.8.6
#####
#######################################################################


# 引数チェック
if [ $# -ne 4 ] ; then
        echo "Argument is not enough.($#)"
        exit 3
fi


# 絶対値を返す関数
function abs()
{
        if [ $1 -lt 0 ] ; then
                res=`expr $1 \* -1`
                echo $res
        else
                echo $1
        fi
}


# snmpwalkで対象ホストの時刻を取得
RESULT=`/usr/bin/snmpwalk -v 1 -t 0.1 -c $1 $2 HOST-RESOURCES-MIB::hrSystemDate -O v 2>&1`
if [ $? != 0 ] ; then
        echo $RESULT
        exit 1
fi


# 取得したsnmpの結果文字列から日付と時間を取得
TEMP=`echo $RESULT | cut -d ' ' -f2-`
DATE=`echo $TEMP | cut -d ',' -f1`
TEMP2=`echo $TEMP | cut -d ',' -f2`
TIME=`echo $TEMP2 | cut -d '.' -f1`
UNIXTIME_SERVER=`date -d "${DATE} ${TIME}" +%s`


# 現在の時刻と取得したサーバ時刻との差異(秒)を取得
UNIXTIME_HOST=`date +%s`
UNIXTIME_DIFF=`expr ${UNIXTIME_HOST} - ${UNIXTIME_SERVER}`


# 時刻の差異を閾値で比較して結果を返す
UNIXTIME_DIFF_ABS=`abs $UNIXTIME_DIFF`
echo "Difftime (${UNIXTIME_DIFF_ABS} sec) | difftime=${UNIXTIME_DIFF_ABS}"
if [ $UNIXTIME_DIFF_ABS -ge $4  ] ; then
        # Critical
        exit 2
elif [ $UNIXTIME_DIFF_ABS -ge $3 ] ; then
        # Warning
        exit 1
else
        # OK
        exit 0
fi

こんな感じです。

2
3
0

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
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?