Is server alive?

サーバが稼動しているか、Webサーバが稼動しているか、をチェックするスクリプト。ping, wget, mailコマンドを組み合わせて、動作していないようならメールで通知するようにしている。

しかし、落ちている間中メールを何度も送信してくるのは勘弁してもらいたいので、フラグファイルを用意することにした。もっといい方法があるなら書き変えたいところだ。後の問題はどこで動かしておくかだなぁ。

#!/bin/sh
# Is server alive ?
#
mailaddress=name@example.com
ip=$1
if LANG=C ping -c 1 $ip | grep ‘0 received’ > /dev/null; then
echo “$ip is not alive.”;
if [ -f $ip.alive ]; then
echo $ip | mail -s “$ip is not alive.” $mailaddress
rm $ip.alive
fi
else
if [ -f $ip.alive ]; then
echo “$ip is alive.”;
else
echo $ip > $ip.alive
fi
fi

retval=`LANG=C wget –spider http://$ip 2>&1 tee | grep ‘200 OK’`
if [ ! -z “$retval” ]; then
#if LANG=C wget –spider http://$ip 2>&1 grep ‘200 OK’ > /dev/null; then
if [ -f $ip.web.alive ]; then
echo “$ip’s web service is alive.”;
else
echo $ip > $ip.web.alive
fi
else
echo “$ip’s web service is not alive.”;
if [ -f $ip.web.alive ]; then
echo $ip | mail -s “$ip’s web service is not alive.” $mailaddress
rm $ip.web.alive
fi
fi

同じカテゴリの記事: Linux