2008年5月アーカイブ
リアル引越しを機に、nullpopopoのサーバを自宅からVPSへ移行したのはいいのですが、外部からの監視をどうしようかとずっと思案してたのです。珠玉のエロ画像を保存しているsambaサーバには、すでにmuninがインストールされているので、こいつを使って監視できないかなー・・・と。
VPSのほうは、当然のことながらIPアドレスは固定なので、監視サーバ側のmunin.confの記述は非常に楽です。問題はVPSのサーバに munin-nodeをインストールして、munin-node.confにどうやって監視サーバのIPアドレスを自動的に書いてやるか・・・です。そこでピコーンと閃いたのは、
「そうだ、監視サーバからsshでログインして、それを契機にIPアドレスを取得しよう!」
という案です。おまけに、ログインした後はIPアドレスの通知スクリプトだけ動かすようにしてやれば、VPSサーバのほうでcronの設定などしなくていいし、いいことづくめかも。なお、TCPの4949番ポートは、あらかじめiptablesですべてのIPアドレスに対して穴をあけておきます。
それでは作業手順です。
■■ VPSサーバにmunin-nodeインストール
[root@nullpopopo ~]# yum --enablerepo=rpmforge install munin-node
(中略)
Dependencies Resolved
=============================================================================
Package Arch Version Repository Size
=============================================================================
Installing:
munin-node noarch 1.2.5-1.el4.rf rpmforge 142 k
Installing for dependencies:
perl-Crypt-DES x86_64 2.05-3.2.el4.rf rpmforge 19 k
perl-Net-SNMP noarch 5.2.0-1.2.el4.rf rpmforge 95 k
perl-Net-Server noarch 0.97-1.el4.rf rpmforge 154 k
Transaction Summary
=============================================================================
Install 4 Package(s)
Update 0 Package(s)
Remove 0 Package(s)
Total download size: 410 k
Is this ok [y/N]: y
■■ 基本設定
[root@nullpopopo ~]# cd /etc/munin/
[root@nullpopopo munin]# cp -p munin-node.conf munin-node.conf.orig
[root@nullpopopo munin]# vi munin-node.conf
最終行のここを
allow ^127\.0\.0\.1$
こうする
allow ^127\.0\.0\.1$
##### 以下は実運用後の手動編集禁止
allow ^123\.456\.789\.012$ ← まずは、今の自宅のグローバルIPアドレスを記述する
※ 追記する2行は、configの最終行で、かつ、空行を付け加えないこと。
■■ プラグインの設定
■ 余計なプラグインの削除
[root@nullpopopo munin]# cd plugins/
[root@nullpopopo plugins]# rm -f df_inode entropy open_files open_inodes sendmail_mailqueue sendmail_mailstats sendmail_mailtraffic swap
※ nullpopopoのサーバはMTAがqmailなので、sendmailの項目は削除しました。あと、VPSにはswapがないので、swapの項目も削除します。
■ イーサネットインターフェイスの項目追加
[root@nullpopopo plugins]# ln -s /usr/share/munin/plugins/if_ if_venet0:0
※ インターフェイスの名前は、ifconfigコマンドで確認しましょう。
■ プロセス監視項目追加
・ drweb
[root@nullpopopo plugins]# ln -s /usr/share/munin/plugins/ps_ ps_drweb
・ httpsd
[root@nullpopopo plugins]# ln -s /usr/share/munin/plugins/ps_ ps_httpsd
・ httpd
[root@nullpopopo plugins]# ln -s /usr/share/munin/plugins/ps_ ps_httpd
・ couriertcpd
[root@nullpopopo plugins]# ln -s /usr/share/munin/plugins/ps_ ps_couriertcpd
・ qmail-send
[root@nullpopopo plugins]# ln -s /usr/share/munin/plugins/ps_ ps_qmail-send
・ mailman
[root@nullpopopo plugins]# ln -s /usr/share/munin/plugins/ps_ ps_mailman
・ sshd
[root@nullpopopo plugins]# ln -s /usr/share/munin/plugins/ps_ ps_sshd
・ monit
[root@nullpopopo plugins]# ln -s /usr/share/munin/plugins/ps_ ps_monit
・ named
[root@nullpopopo plugins]# ln -s /usr/share/munin/plugins/ps_ ps_named
・ xinetd
[root@nullpopopo plugins]# ln -s /usr/share/munin/plugins/ps_ ps_xinetd
・ mysqld
[root@nullpopopo plugins]# ln -s /usr/share/munin/plugins/ps_ ps_mysqld
・ mysql_bytes
[root@nullpopopo plugins]# ln -s /usr/share/munin/plugins/mysql_bytes mysql_bytes
・ mysql_queries
[root@nullpopopo plugins]# ln -s /usr/share/munin/plugins/mysql_queries mysql_queries
・ mysql_slowqueries
[root@nullpopopo plugins]# ln -s /usr/share/munin/plugins/mysql_slowqueries mysql_slowqueries
・ mysql_threads
[root@nullpopopo plugins]# ln -s /usr/share/munin/plugins/mysql_threads mysql_threads
■■ MySQLの環境変数追加
munin-nodeがmysqladminを実行できるよう、ユーザ名とパスワード、そしてmysqladminのパスを記述します。
[root@nullpopopo plugins]# vi ../plugin-conf.d/munin-node
ここを
[mysql*]
#env.mysqlopts -u someuser
こうする
[mysql*]
#env.mysqlopts -u someuser
env.mysqlopts -u admin -p[パスワード]
env.mysqladmin /usr/bin/mysqladmin
※ -pとパスワードとの間には、スペースをあけません。
■■ 監視サーバ側設定
[oresama@intra-server munin]$ sudo cp -p munin.conf munin.conf.20080529
[oresama@intra-server munin]$ sudo vi munin.conf
以下を追記
[nullpopopo.bizcube.info]
address 121.50.42.188 ← VPSサーバのIPアドレス
use_node_name yes
■■ munin-node起動
■ 自動起動設定
[root@nullpopopo plugins]# chkconfig munin-node --list
munin-node 0:off 1:off 2:off 3:off 4:off 5:off 6:off
[root@nullpopopo plugins]# chkconfig munin-node on
[root@nullpopopo plugins]# chkconfig munin-node --list
munin-node 0:off 1:off 2:on 3:on 4:on 5:on 6:off
■ 起動
[root@nullpopopo plugins]# /etc/init.d/munin-node start
Starting Munin Node: [ OK ]
■■ IPアドレス通知の環境を構築する
■ VPS側でサービス監視用アカウントを作成する
[root@nullpopopo ~]# useradd watcher0
[root@nullpopopo ~]# passwd watcher0
Changing password for user watcher0.
New UNIX password:
Retype new UNIX password:
passwd: all authentication tokens updated successfully.
■ 監視サーバで、パスフレーズなしの鍵を作成する
[oresama@intra-server ~]$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/oresama/.ssh/id_rsa): (空エンター)
Enter passphrase (empty for no passphrase): (空エンター)
Enter same passphrase again: (空エンター)
Your identification has been saved in /home/oresama/.ssh/id_rsa.
Your public key has been saved in /home/oresama/.ssh/id_rsa.pub.
The key fingerprint is:
34:87:79:4c:09:9a:a9:50:ed:aa:56:35:b1:26:aa:62 oresama@intra-server.intranet.local
■ VPS側で、公開鍵を登録する
[root@nullpopopo ~]# su - watcher0
[watcher0@nullpopopo ~]$ mkdir .ssh ; vi .ssh/authorized_keys
(監視サーバでの「cat ~/.ssh/id_rsa.pub」の文字列を貼り付ける)
■ 監視サーバからVPSサーバへ、鍵を使ってパスフレーズなしのログインができることを確認する
[oresama@intra-server ~]$ ssh -i .ssh/id_rsa watcher0@nullpopopo.bizcube.info
[watcher0@nullpopopo ~]$
パスフレーズなしでログインできたら、OKです。
■ VPSサーバでmuninグループにwatcher0を追加する
[root@nullpopopo ~]# vigr
ここを
munin:x:101:
こうする
munin:x:101:watcher0
■ munin-node.conf のグループに対する権限を変更する
munin-node.confに、muninグループへの書き込み権限を与えてあげます。
[root@nullpopopo ~]# cd /etc/munin/
[root@nullpopopo munin]# chgrp munin munin-node.conf
[root@nullpopopo munin]# chmod 664 munin-node.conf
[root@nullpopopo munin]# ls -l munin-node.conf
-rw-rw-r-- 1 root munin 818 5月 28 23:18 munin-node.conf
■ IPアドレス検知スクリプト作成
[watcher0@nullpopopo ~]$ mkdir bin ; cd bin
[watcher0@nullpopopo bin]$ touch munin-accept.sh
[watcher0@nullpopopo bin]$ chmod 700 munin-accept.sh
[watcher0@nullpopopo bin]$ vi munin-accept.sh
#!/bin/sh
LANG=C
ADMIN=【携帯のメールアドレス】
CONFDIR=/etc/munin/
cd $CONFDIR
# configファイルを3世代保存する
CF=munin-node.conf
BAK0=munin-node.conf.BAK0
BAK1=munin-node.conf.BAK1
# IPアドレスを検出する部分
NOW_IP_ADDR=`sudo egrep "^\w{3} [ :0-9]{11} [._[:alnum:]-]+ sshd\[[0-9]+\]: Accepted publickey for watcher0" /var/log/secure | tail -1 | awk '{print $11}'`
# IPアドレスをオクテット境界ごとに分割する
OA=`echo $NOW_IP_ADDR | awk 'BEGIN {FS="."}{print $1}'`
OB=`echo $NOW_IP_ADDR | awk 'BEGIN {FS="."}{print $2}'`
OC=`echo $NOW_IP_ADDR | awk 'BEGIN {FS="."}{print $3}'`
OD=`echo $NOW_IP_ADDR | awk 'BEGIN {FS="."}{print $4}'`
# IPアドレスの新旧比較のために、munin-node.confの最終行を置換する
ACCEPTED_IP=`tail -1 $CF | sed -e 's/allow\ \^//' | sed -e 's/\\\\//g' | sed -e 's/\\$//'`
# ログに書かれた最新のIPアドレスとmunin-node.confの最終行を比較して条件分岐する
if [ $NOW_IP_ADDR = $ACCEPTED_IP ];
# IPアドレスに変化がなければ何もしない
then
:
# IPアドレスに変化があったら、バックアップファイルを生成し、
# 接続許可対象のIPアドレスを置換し、munin-nodeを再起動し、メール通知する
else
# Change config
if [ -f $BAK1 ];
then
rm -f $BAK1
fi
if [ -f $BAK0 ];
then
mv $BAK0 $BAK1
fi
if [ -f $CF ];
then
cp -p $CF $BAK0
fi
sed -i '$s/^allow/#allow/' $CF
echo allow\ \^$OA\\.$OB\\.$OC\\.$OD\$ >> $CF
sudo /etc/init.d/munin-node restart
echo "`date` munin-node restarted" | mail -s "New munin-server is $NOW_IP_ADDR" $ADMIN
fi
■■ watcher0がsudoでmunin-nodeのスタートスクリプトを蹴っ飛ばせるようにする
[root@nullpopopo ~]# visudo
ここを
#
# Disable "ssh hostname sudo <cmd>", because it will show the password in clear.
# You have to run "ssh -t hostname sudo <cmd>".
#
Defaults requiretty
こうする
#
# Disable "ssh hostname sudo <cmd>", because it will show the password in clear.
# You have to run "ssh -t hostname sudo <cmd>".
#
#Defaults requiretty
ここを
## Allow root to run any commands anywhere
root ALL=(ALL) ALL
こうする
## Allow root to run any commands anywhere
root ALL=(ALL) ALL
watcher0 ALL=(root) NOPASSWD: /etc/init.d/munin-node *,/bin/egrep
NOPASSWD:を忘れると、自動実行でパスワードを聞かれてしまうので気をつけましょう。また、スタートスクリプトの後ろにアスタリスクをつけることで、引数つきで実行できるようにしてやることも忘れずに。2つのコマンドの区切りは、カンマです。これによって、watcher0はroot権限でmunin-nodeのスタートスクリプトを実行し、また、/var/log/secureのオーナーやパーミッションを変更することなくegrepすることができます。そして、必ず「Defaults requiretty」の部分をコメントアウトしてやらないと、監視サーバのcronからVPSサーバのsudoを実行することができません。
■■ vipwを使って、watcher0のシェルを/home/watcher0/bin/munin-accept.shにする
こうすることで、監視サーバからsshでログインした後、VPSサーバに作成したスクリプトを実行してログアウトします。
[root@nullpopopo cron.d]# vipw
ここを
watcher0:x:10017:2524::/home/watcher0:/bin/bash
こうする
watcher0:x:10017:2524::/home/watcher0:/home/watcher0/bin/munin-accept.sh
シャドウパスワードは編集しません。
■■ 監視サーバが定期的にVPSサーバへログインするようcron設定する
sh-3.00# cd /etc/cron.d
sh-3.00# vi munin-accept
*/5 * * * * oresama ssh -i /home/oresama/.ssh/id_rsa watcher0@nullpopopo.bizcube.info > /dev/null
■■ 試験
VPSサーバ側のmunin-node.confで、最終行の
allow ^123\.456\.789\.012$
の行を、わざと書き換えます。その後、監視サーバからcronを使ってwatcher0アカウントでログインし、munin-accept.shが実行され、前のIPアドレスがコメントアウトされて今のIPアドレスが書き込まれていることを確認します。さらにもう一度cronによるログインを待ち、munin-node.confが書き換わっていないこと、そして新たなバックアップファイルができていないことを確認します。
※ 最初、手動ログインで「できたできたー」とヌカ喜びしてたのはここだけの話 (;^ω^)
以上で、楽々監視システムのできあがりです。
VPSのほうは、当然のことながらIPアドレスは固定なので、監視サーバ側のmunin.confの記述は非常に楽です。問題はVPSのサーバに munin-nodeをインストールして、munin-node.confにどうやって監視サーバのIPアドレスを自動的に書いてやるか・・・です。そこでピコーンと閃いたのは、
「そうだ、監視サーバからsshでログインして、それを契機にIPアドレスを取得しよう!」
という案です。おまけに、ログインした後はIPアドレスの通知スクリプトだけ動かすようにしてやれば、VPSサーバのほうでcronの設定などしなくていいし、いいことづくめかも。なお、TCPの4949番ポートは、あらかじめiptablesですべてのIPアドレスに対して穴をあけておきます。
それでは作業手順です。
■■ VPSサーバにmunin-nodeインストール
[root@nullpopopo ~]# yum --enablerepo=rpmforge install munin-node
(中略)
Dependencies Resolved
=============================================================================
Package Arch Version Repository Size
=============================================================================
Installing:
munin-node noarch 1.2.5-1.el4.rf rpmforge 142 k
Installing for dependencies:
perl-Crypt-DES x86_64 2.05-3.2.el4.rf rpmforge 19 k
perl-Net-SNMP noarch 5.2.0-1.2.el4.rf rpmforge 95 k
perl-Net-Server noarch 0.97-1.el4.rf rpmforge 154 k
Transaction Summary
=============================================================================
Install 4 Package(s)
Update 0 Package(s)
Remove 0 Package(s)
Total download size: 410 k
Is this ok [y/N]: y
■■ 基本設定
[root@nullpopopo ~]# cd /etc/munin/
[root@nullpopopo munin]# cp -p munin-node.conf munin-node.conf.orig
[root@nullpopopo munin]# vi munin-node.conf
最終行のここを
allow ^127\.0\.0\.1$
こうする
allow ^127\.0\.0\.1$
##### 以下は実運用後の手動編集禁止
allow ^123\.456\.789\.012$ ← まずは、今の自宅のグローバルIPアドレスを記述する
※ 追記する2行は、configの最終行で、かつ、空行を付け加えないこと。
■■ プラグインの設定
■ 余計なプラグインの削除
[root@nullpopopo munin]# cd plugins/
[root@nullpopopo plugins]# rm -f df_inode entropy open_files open_inodes sendmail_mailqueue sendmail_mailstats sendmail_mailtraffic swap
※ nullpopopoのサーバはMTAがqmailなので、sendmailの項目は削除しました。あと、VPSにはswapがないので、swapの項目も削除します。
■ イーサネットインターフェイスの項目追加
[root@nullpopopo plugins]# ln -s /usr/share/munin/plugins/if_ if_venet0:0
※ インターフェイスの名前は、ifconfigコマンドで確認しましょう。
■ プロセス監視項目追加
・ drweb
[root@nullpopopo plugins]# ln -s /usr/share/munin/plugins/ps_ ps_drweb
・ httpsd
[root@nullpopopo plugins]# ln -s /usr/share/munin/plugins/ps_ ps_httpsd
・ httpd
[root@nullpopopo plugins]# ln -s /usr/share/munin/plugins/ps_ ps_httpd
・ couriertcpd
[root@nullpopopo plugins]# ln -s /usr/share/munin/plugins/ps_ ps_couriertcpd
・ qmail-send
[root@nullpopopo plugins]# ln -s /usr/share/munin/plugins/ps_ ps_qmail-send
・ mailman
[root@nullpopopo plugins]# ln -s /usr/share/munin/plugins/ps_ ps_mailman
・ sshd
[root@nullpopopo plugins]# ln -s /usr/share/munin/plugins/ps_ ps_sshd
・ monit
[root@nullpopopo plugins]# ln -s /usr/share/munin/plugins/ps_ ps_monit
・ named
[root@nullpopopo plugins]# ln -s /usr/share/munin/plugins/ps_ ps_named
・ xinetd
[root@nullpopopo plugins]# ln -s /usr/share/munin/plugins/ps_ ps_xinetd
・ mysqld
[root@nullpopopo plugins]# ln -s /usr/share/munin/plugins/ps_ ps_mysqld
・ mysql_bytes
[root@nullpopopo plugins]# ln -s /usr/share/munin/plugins/mysql_bytes mysql_bytes
・ mysql_queries
[root@nullpopopo plugins]# ln -s /usr/share/munin/plugins/mysql_queries mysql_queries
・ mysql_slowqueries
[root@nullpopopo plugins]# ln -s /usr/share/munin/plugins/mysql_slowqueries mysql_slowqueries
・ mysql_threads
[root@nullpopopo plugins]# ln -s /usr/share/munin/plugins/mysql_threads mysql_threads
■■ MySQLの環境変数追加
munin-nodeがmysqladminを実行できるよう、ユーザ名とパスワード、そしてmysqladminのパスを記述します。
[root@nullpopopo plugins]# vi ../plugin-conf.d/munin-node
ここを
[mysql*]
#env.mysqlopts -u someuser
こうする
[mysql*]
#env.mysqlopts -u someuser
env.mysqlopts -u admin -p[パスワード]
env.mysqladmin /usr/bin/mysqladmin
※ -pとパスワードとの間には、スペースをあけません。
■■ 監視サーバ側設定
[oresama@intra-server munin]$ sudo cp -p munin.conf munin.conf.20080529
[oresama@intra-server munin]$ sudo vi munin.conf
以下を追記
[nullpopopo.bizcube.info]
address 121.50.42.188 ← VPSサーバのIPアドレス
use_node_name yes
■■ munin-node起動
■ 自動起動設定
[root@nullpopopo plugins]# chkconfig munin-node --list
munin-node 0:off 1:off 2:off 3:off 4:off 5:off 6:off
[root@nullpopopo plugins]# chkconfig munin-node on
[root@nullpopopo plugins]# chkconfig munin-node --list
munin-node 0:off 1:off 2:on 3:on 4:on 5:on 6:off
■ 起動
[root@nullpopopo plugins]# /etc/init.d/munin-node start
Starting Munin Node: [ OK ]
■■ IPアドレス通知の環境を構築する
■ VPS側でサービス監視用アカウントを作成する
[root@nullpopopo ~]# useradd watcher0
[root@nullpopopo ~]# passwd watcher0
Changing password for user watcher0.
New UNIX password:
Retype new UNIX password:
passwd: all authentication tokens updated successfully.
■ 監視サーバで、パスフレーズなしの鍵を作成する
[oresama@intra-server ~]$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/oresama/.ssh/id_rsa): (空エンター)
Enter passphrase (empty for no passphrase): (空エンター)
Enter same passphrase again: (空エンター)
Your identification has been saved in /home/oresama/.ssh/id_rsa.
Your public key has been saved in /home/oresama/.ssh/id_rsa.pub.
The key fingerprint is:
34:87:79:4c:09:9a:a9:50:ed:aa:56:35:b1:26:aa:62 oresama@intra-server.intranet.local
■ VPS側で、公開鍵を登録する
[root@nullpopopo ~]# su - watcher0
[watcher0@nullpopopo ~]$ mkdir .ssh ; vi .ssh/authorized_keys
(監視サーバでの「cat ~/.ssh/id_rsa.pub」の文字列を貼り付ける)
■ 監視サーバからVPSサーバへ、鍵を使ってパスフレーズなしのログインができることを確認する
[oresama@intra-server ~]$ ssh -i .ssh/id_rsa watcher0@nullpopopo.bizcube.info
[watcher0@nullpopopo ~]$
パスフレーズなしでログインできたら、OKです。
■ VPSサーバでmuninグループにwatcher0を追加する
[root@nullpopopo ~]# vigr
ここを
munin:x:101:
こうする
munin:x:101:watcher0
■ munin-node.conf のグループに対する権限を変更する
munin-node.confに、muninグループへの書き込み権限を与えてあげます。
[root@nullpopopo ~]# cd /etc/munin/
[root@nullpopopo munin]# chgrp munin munin-node.conf
[root@nullpopopo munin]# chmod 664 munin-node.conf
[root@nullpopopo munin]# ls -l munin-node.conf
-rw-rw-r-- 1 root munin 818 5月 28 23:18 munin-node.conf
■ IPアドレス検知スクリプト作成
[watcher0@nullpopopo ~]$ mkdir bin ; cd bin
[watcher0@nullpopopo bin]$ touch munin-accept.sh
[watcher0@nullpopopo bin]$ chmod 700 munin-accept.sh
[watcher0@nullpopopo bin]$ vi munin-accept.sh
#!/bin/sh
LANG=C
ADMIN=【携帯のメールアドレス】
CONFDIR=/etc/munin/
cd $CONFDIR
# configファイルを3世代保存する
CF=munin-node.conf
BAK0=munin-node.conf.BAK0
BAK1=munin-node.conf.BAK1
# IPアドレスを検出する部分
NOW_IP_ADDR=`sudo egrep "^\w{3} [ :0-9]{11} [._[:alnum:]-]+ sshd\[[0-9]+\]: Accepted publickey for watcher0" /var/log/secure | tail -1 | awk '{print $11}'`
# IPアドレスをオクテット境界ごとに分割する
OA=`echo $NOW_IP_ADDR | awk 'BEGIN {FS="."}{print $1}'`
OB=`echo $NOW_IP_ADDR | awk 'BEGIN {FS="."}{print $2}'`
OC=`echo $NOW_IP_ADDR | awk 'BEGIN {FS="."}{print $3}'`
OD=`echo $NOW_IP_ADDR | awk 'BEGIN {FS="."}{print $4}'`
# IPアドレスの新旧比較のために、munin-node.confの最終行を置換する
ACCEPTED_IP=`tail -1 $CF | sed -e 's/allow\ \^//' | sed -e 's/\\\\//g' | sed -e 's/\\$//'`
# ログに書かれた最新のIPアドレスとmunin-node.confの最終行を比較して条件分岐する
if [ $NOW_IP_ADDR = $ACCEPTED_IP ];
# IPアドレスに変化がなければ何もしない
then
:
# IPアドレスに変化があったら、バックアップファイルを生成し、
# 接続許可対象のIPアドレスを置換し、munin-nodeを再起動し、メール通知する
else
# Change config
if [ -f $BAK1 ];
then
rm -f $BAK1
fi
if [ -f $BAK0 ];
then
mv $BAK0 $BAK1
fi
if [ -f $CF ];
then
cp -p $CF $BAK0
fi
sed -i '$s/^allow/#allow/' $CF
echo allow\ \^$OA\\.$OB\\.$OC\\.$OD\$ >> $CF
sudo /etc/init.d/munin-node restart
echo "`date` munin-node restarted" | mail -s "New munin-server is $NOW_IP_ADDR" $ADMIN
fi
■■ watcher0がsudoでmunin-nodeのスタートスクリプトを蹴っ飛ばせるようにする
[root@nullpopopo ~]# visudo
ここを
#
# Disable "ssh hostname sudo <cmd>", because it will show the password in clear.
# You have to run "ssh -t hostname sudo <cmd>".
#
Defaults requiretty
こうする
#
# Disable "ssh hostname sudo <cmd>", because it will show the password in clear.
# You have to run "ssh -t hostname sudo <cmd>".
#
#Defaults requiretty
ここを
## Allow root to run any commands anywhere
root ALL=(ALL) ALL
こうする
## Allow root to run any commands anywhere
root ALL=(ALL) ALL
watcher0 ALL=(root) NOPASSWD: /etc/init.d/munin-node *,/bin/egrep
NOPASSWD:を忘れると、自動実行でパスワードを聞かれてしまうので気をつけましょう。また、スタートスクリプトの後ろにアスタリスクをつけることで、引数つきで実行できるようにしてやることも忘れずに。2つのコマンドの区切りは、カンマです。これによって、watcher0はroot権限でmunin-nodeのスタートスクリプトを実行し、また、/var/log/secureのオーナーやパーミッションを変更することなくegrepすることができます。そして、必ず「Defaults requiretty」の部分をコメントアウトしてやらないと、監視サーバのcronからVPSサーバのsudoを実行することができません。
■■ vipwを使って、watcher0のシェルを/home/watcher0/bin/munin-accept.shにする
こうすることで、監視サーバからsshでログインした後、VPSサーバに作成したスクリプトを実行してログアウトします。
[root@nullpopopo cron.d]# vipw
ここを
watcher0:x:10017:2524::/home/watcher0:/bin/bash
こうする
watcher0:x:10017:2524::/home/watcher0:/home/watcher0/bin/munin-accept.sh
シャドウパスワードは編集しません。
■■ 監視サーバが定期的にVPSサーバへログインするようcron設定する
sh-3.00# cd /etc/cron.d
sh-3.00# vi munin-accept
*/5 * * * * oresama ssh -i /home/oresama/.ssh/id_rsa watcher0@nullpopopo.bizcube.info > /dev/null
■■ 試験
VPSサーバ側のmunin-node.confで、最終行の
allow ^123\.456\.789\.012$
の行を、わざと書き換えます。その後、監視サーバからcronを使ってwatcher0アカウントでログインし、munin-accept.shが実行され、前のIPアドレスがコメントアウトされて今のIPアドレスが書き込まれていることを確認します。さらにもう一度cronによるログインを待ち、munin-node.confが書き換わっていないこと、そして新たなバックアップファイルができていないことを確認します。
※ 最初、手動ログインで「できたできたー」とヌカ喜びしてたのはここだけの話 (;^ω^)
以上で、楽々監視システムのできあがりです。
B○G IPとか買うお金がなく、NIC追加したりするためのサーバ停止する機会すらなく、30分でロードバランサを構築したい場合、poundでリバースプロキシを構築すると便利です。
■■ やりたいこと
1台のリバースプロキシを、httpやhttpsアクセスの矢面に立たせる
バックエンドには、2台のhttpサーバがあり、それぞれhttpd.confベタ書きかIPベースのバーチャルホストを構築する
このコンテンツのために構築したサーバは以下の通りです。
リバースプロキシ:www.example.com 192.168.0.4 (CentOS4.6)
httpサーバ1:be1.example.com 192.168.0.120 (CentOS5.1)
httpサーバ2:be2.example.com 192.168.0.121 (CentOS5.1)
なお、www.example.comでapacheが動いている場合、192.168.0.4でhttpとhttpsをlistenしないようにします。
httpd.confで
Listen 80
となっているところを
Listen 192.168.0.3:80
のように、192.168.0.4以外のIPアドレスでlistenするようにします。
ssl.confでも、
Listen 443
となっているところを
Listen 192.168.0.3:443
とします。
■■ poundのインストール
※ ソースRPMをダウンロードして自分でビルドしてRPMパッケージを作るので、あらかじめrpm-buildをインストールし、ホームディレクトリ以下に.rpmmacrosファイルとrpm/{BUILD,RPMS,SOURCES,SPECS,SRPMS}ディレクトリを作成しておいてください。
poundの公式サイトから、パッケージをダウンロードしてきます。
ブラウザをちょっとだけスクロールすると、
・ RPMs for RedHat are available at http://www.invoca.ch/pub/packages/pound/
という文字列が見えるので、右クリックしてリンク先のURLをコピーします。
ターミナルから
$ wget http://www.invoca.ch/pub/packages/pound/pound-2.4.2-1.src.rpm
で、SRPMパッケージをダウンロードしてきます。なお、これは執筆時点での最新バージョンです。
次に、ダウンロードしてきたSRPMファイルを展開します。
$ rpm -ivh pound-2.4.2-1.src.rpm
SPECファイルを確認します。
$ cd rpm/SPECS/
$ less pound.spec
(中略)
BuildRequires: openssl-devel, pkgconfig, pcre-devel, google-perftools-devel
BuildRequires: sed, perl
と書いてあるので、openssl-devel、pkgconfig、pcre-devel、google-perftools-devel、sed、perlがインストールされているか確認します。
sedとperlは、恐らくほとんどの環境ではインストールされていると思います。
openssl-devel、pkgconfig、pcre-develはbaseリポジトリにあるので、何のオプションもなしにyumコマンドでインストールできるはずです。
google-perftools-devel は、RPM Searchから探して持って来ました。なお、google-perftools-develをインストールするには、google-perftoolsも必要になります。
$ wget ftp://ftp.pbone.net/mirror/download.fedora.redhat.com/pub/fedora/epel/4/i386/google-perftools-devel-0.92-1.el4.2.i386.rpm
(↑2行に見えますが実際は1行です)
$ wget ftp://ftp.pbone.net/mirror/download.fedora.redhat.com/pub/fedora/epel/4/i386/google-perftools-0.92-1.el4.2.i386.rpm
(↑2行に見えますが実際は1行です)
$ rpm --test -ivh google-perftools-0.92-1.el4.2.i386.rpm google-perftools-devel-0.92-1.el4.2.i386.rpm
$ su -
# rpm -ivh google-perftools-0.92-1.el4.2.i386.rpm google-perftools-devel-0.92-1.el4.2.i386.rpm
# exit
続いて、一般ユーザ「oresama」に戻ってからpoundのRPMをビルドします。
$ cd ~/rpm/SPECS/
$ rpmbuild -ba pound.spec
ビルドが終わったら、poundの依存関係を確認します。
$ cd ../RPMS/i386/
$ rpm --test -ivh pound-2.4.2-1.i386.rpm
何も問題なければ、インストールを行います。
$ su -
# cd /home/oresama/rpm/RPMS/i386/
# rpm -ivh pound-2.4.2-1.i386.rpm
■■ pound.cfgを編集する
# cd /etc/pound
# cp -p pound.cfg pound.cfg.orig
# vi pound.cfg
User "nobody"
Group "nobody"
RootJail "/usr/share/pound"
Control "/var/run/pound/ctl_socket"
# Main listening ports
ListenHTTP
#Address 0.0.0.0
#poundで待ち受けるIPアドレスを指定する。
Address 192.168.0.4
Port 80
xHTTP 1
End
ListenHTTPS
#Address 0.0.0.0
#poundで待ち受けるIPアドレスを指定する。
Address 192.168.0.4
Port 443
#秘密鍵と証明書がひとまとめになったファイルを指定する。
Cert "/usr/share/ssl/certs/pound.pem"
Ciphers "ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP:+eNULL"
xHTTP 1
End
# Catch-all server(s)
Service
BackEnd
#バックエンドで動くサーバのIPアドレスを指定する。
Address 192.168.0.120
Port 80
#優先度を1から9で指定する。数字が大きいほど優先度が高くなる。
Priority 1
End
BackEnd
Address 192.168.0.121
Port 80
Priority 1
End
Session
#セッションの保持時間を秒単位で指定する。
#ベーシック認証のほか、クッキーなども指定できるようだ。
Type BASIC
TTL 300
End
End
設定ファイルはここまで。
■■ 証明書作成
# cd /usr/share/ssl/certs/
まずはpound.pemのバックアップを取ります。
# cp -p pound.pem pound.pem.orig
# echo -n "" > pound.pem
■ 秘密鍵の生成
# openssl genrsa -des3 -out server-key.pem 1024
■ CSRの生成
# openssl req -new -key server-key.pem -out server-csr.pem
■ 秘密鍵からパスフレーズを取り除く
# cp -p server-key.pem server-key.pem.orig
# openssl req -text -noout -in server-csr.pem
■ 証明書作成
# openssl x509 -in server-csr.pem -out server-crt.pem -req -signkey server-key.pem
■ 秘密鍵と証明書をひとつのファイルにまとめる
# cat server-key.pem > pound.pem
# cat server-crt.pem >> pound.pem
■■ 起動前の確認
poundを起動する前に、poundコマンドの引数に-cをつけて、設定の確認をします。
# pound -c
starting...
Config file /etc/pound/pound.cfg is OK
このように表示されたら、設定ファイルの書式はあっています。
設定ファイルの書式が間違っていると
# pound -c
starting...
line 7: unknown directive "ListenHTTTP " - aborted
というように、設定ファイルの行番号つきで「ここ間違ってるぞ」と教えてくれます。
また、Certファイルが壊れていても
# pound -c
starting...
line 17: SSL_CTX_use_certificate_chain_file "/usr/share/ssl/certs/pound.pem" failed - aborted
error:0906D064:PEM routines:PEM_read_bio:bad base64 decode
このようにエラーが出ます。
■■ pound起動
# /etc/init.d/pound start
Starting pound: [ OK ]
■■ pound自動起動設定
# chkconfig pound --list
pound 0:off 1:off 2:off 3:off 4:off 5:off 6:off
# chkconfig pound on
# chkconfig pound --list
pound 0:off 1:off 2:on 3:on 4:on 5:on 6:off
■■ テスト
2台あるバックエンドサーバのドキュメントルートに、ちょっとずつ内容の違うhtmlファイルを、同じファイル名(ここではindex.htmlとしましょう)でアップロードします。
http://192.168.0.4/
https://192.168.0.4/
にそれぞれアクセスし、何度かリロードして、違う表示が交互に出てきたら、正しく振り分けられています。勿論、規則正しく振り分けてくれるわけではありません。
■■ 運用
poundのログは、/var/log/messagesに出力されます。ここに、アクセスログが記録されます。
バックエンドにあるapacheのアクセスログのフォーマットはデフォルトで
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
と設定されていますが、この状態だと、poundが動いているサーバのIPアドレスが記録されてしまいます。なので、httpd.confの修正を行う必要があります。
ここを
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
こうする
LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
#LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
以上で、ごくごく簡単なロードバランサができました。
ね、簡単でしょ?
■■ やりたいこと
1台のリバースプロキシを、httpやhttpsアクセスの矢面に立たせる
バックエンドには、2台のhttpサーバがあり、それぞれhttpd.confベタ書きかIPベースのバーチャルホストを構築する
このコンテンツのために構築したサーバは以下の通りです。
リバースプロキシ:www.example.com 192.168.0.4 (CentOS4.6)
httpサーバ1:be1.example.com 192.168.0.120 (CentOS5.1)
httpサーバ2:be2.example.com 192.168.0.121 (CentOS5.1)
なお、www.example.comでapacheが動いている場合、192.168.0.4でhttpとhttpsをlistenしないようにします。
httpd.confで
Listen 80
となっているところを
Listen 192.168.0.3:80
のように、192.168.0.4以外のIPアドレスでlistenするようにします。
ssl.confでも、
Listen 443
となっているところを
Listen 192.168.0.3:443
とします。
■■ poundのインストール
※ ソースRPMをダウンロードして自分でビルドしてRPMパッケージを作るので、あらかじめrpm-buildをインストールし、ホームディレクトリ以下に.rpmmacrosファイルとrpm/{BUILD,RPMS,SOURCES,SPECS,SRPMS}ディレクトリを作成しておいてください。
poundの公式サイトから、パッケージをダウンロードしてきます。
ブラウザをちょっとだけスクロールすると、
・ RPMs for RedHat are available at http://www.invoca.ch/pub/packages/pound/
という文字列が見えるので、右クリックしてリンク先のURLをコピーします。
ターミナルから
$ wget http://www.invoca.ch/pub/packages/pound/pound-2.4.2-1.src.rpm
で、SRPMパッケージをダウンロードしてきます。なお、これは執筆時点での最新バージョンです。
次に、ダウンロードしてきたSRPMファイルを展開します。
$ rpm -ivh pound-2.4.2-1.src.rpm
SPECファイルを確認します。
$ cd rpm/SPECS/
$ less pound.spec
(中略)
BuildRequires: openssl-devel, pkgconfig, pcre-devel, google-perftools-devel
BuildRequires: sed, perl
と書いてあるので、openssl-devel、pkgconfig、pcre-devel、google-perftools-devel、sed、perlがインストールされているか確認します。
sedとperlは、恐らくほとんどの環境ではインストールされていると思います。
openssl-devel、pkgconfig、pcre-develはbaseリポジトリにあるので、何のオプションもなしにyumコマンドでインストールできるはずです。
google-perftools-devel は、RPM Searchから探して持って来ました。なお、google-perftools-develをインストールするには、google-perftoolsも必要になります。
$ wget ftp://ftp.pbone.net/mirror/download.fedora.redhat.com/pub/fedora/epel/4/i386/google-perftools-devel-0.92-1.el4.2.i386.rpm
(↑2行に見えますが実際は1行です)
$ wget ftp://ftp.pbone.net/mirror/download.fedora.redhat.com/pub/fedora/epel/4/i386/google-perftools-0.92-1.el4.2.i386.rpm
(↑2行に見えますが実際は1行です)
$ rpm --test -ivh google-perftools-0.92-1.el4.2.i386.rpm google-perftools-devel-0.92-1.el4.2.i386.rpm
$ su -
# rpm -ivh google-perftools-0.92-1.el4.2.i386.rpm google-perftools-devel-0.92-1.el4.2.i386.rpm
# exit
続いて、一般ユーザ「oresama」に戻ってからpoundのRPMをビルドします。
$ cd ~/rpm/SPECS/
$ rpmbuild -ba pound.spec
ビルドが終わったら、poundの依存関係を確認します。
$ cd ../RPMS/i386/
$ rpm --test -ivh pound-2.4.2-1.i386.rpm
何も問題なければ、インストールを行います。
$ su -
# cd /home/oresama/rpm/RPMS/i386/
# rpm -ivh pound-2.4.2-1.i386.rpm
■■ pound.cfgを編集する
# cd /etc/pound
# cp -p pound.cfg pound.cfg.orig
# vi pound.cfg
User "nobody"
Group "nobody"
RootJail "/usr/share/pound"
Control "/var/run/pound/ctl_socket"
# Main listening ports
ListenHTTP
#Address 0.0.0.0
#poundで待ち受けるIPアドレスを指定する。
Address 192.168.0.4
Port 80
xHTTP 1
End
ListenHTTPS
#Address 0.0.0.0
#poundで待ち受けるIPアドレスを指定する。
Address 192.168.0.4
Port 443
#秘密鍵と証明書がひとまとめになったファイルを指定する。
Cert "/usr/share/ssl/certs/pound.pem"
Ciphers "ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP:+eNULL"
xHTTP 1
End
# Catch-all server(s)
Service
BackEnd
#バックエンドで動くサーバのIPアドレスを指定する。
Address 192.168.0.120
Port 80
#優先度を1から9で指定する。数字が大きいほど優先度が高くなる。
Priority 1
End
BackEnd
Address 192.168.0.121
Port 80
Priority 1
End
Session
#セッションの保持時間を秒単位で指定する。
#ベーシック認証のほか、クッキーなども指定できるようだ。
Type BASIC
TTL 300
End
End
設定ファイルはここまで。
■■ 証明書作成
# cd /usr/share/ssl/certs/
まずはpound.pemのバックアップを取ります。
# cp -p pound.pem pound.pem.orig
# echo -n "" > pound.pem
■ 秘密鍵の生成
# openssl genrsa -des3 -out server-key.pem 1024
■ CSRの生成
# openssl req -new -key server-key.pem -out server-csr.pem
■ 秘密鍵からパスフレーズを取り除く
# cp -p server-key.pem server-key.pem.orig
# openssl req -text -noout -in server-csr.pem
■ 証明書作成
# openssl x509 -in server-csr.pem -out server-crt.pem -req -signkey server-key.pem
■ 秘密鍵と証明書をひとつのファイルにまとめる
# cat server-key.pem > pound.pem
# cat server-crt.pem >> pound.pem
■■ 起動前の確認
poundを起動する前に、poundコマンドの引数に-cをつけて、設定の確認をします。
# pound -c
starting...
Config file /etc/pound/pound.cfg is OK
このように表示されたら、設定ファイルの書式はあっています。
設定ファイルの書式が間違っていると
# pound -c
starting...
line 7: unknown directive "ListenHTTTP " - aborted
というように、設定ファイルの行番号つきで「ここ間違ってるぞ」と教えてくれます。
また、Certファイルが壊れていても
# pound -c
starting...
line 17: SSL_CTX_use_certificate_chain_file "/usr/share/ssl/certs/pound.pem" failed - aborted
error:0906D064:PEM routines:PEM_read_bio:bad base64 decode
このようにエラーが出ます。
■■ pound起動
# /etc/init.d/pound start
Starting pound: [ OK ]
■■ pound自動起動設定
# chkconfig pound --list
pound 0:off 1:off 2:off 3:off 4:off 5:off 6:off
# chkconfig pound on
# chkconfig pound --list
pound 0:off 1:off 2:on 3:on 4:on 5:on 6:off
■■ テスト
2台あるバックエンドサーバのドキュメントルートに、ちょっとずつ内容の違うhtmlファイルを、同じファイル名(ここではindex.htmlとしましょう)でアップロードします。
http://192.168.0.4/
https://192.168.0.4/
にそれぞれアクセスし、何度かリロードして、違う表示が交互に出てきたら、正しく振り分けられています。勿論、規則正しく振り分けてくれるわけではありません。
■■ 運用
poundのログは、/var/log/messagesに出力されます。ここに、アクセスログが記録されます。
バックエンドにあるapacheのアクセスログのフォーマットはデフォルトで
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
と設定されていますが、この状態だと、poundが動いているサーバのIPアドレスが記録されてしまいます。なので、httpd.confの修正を行う必要があります。
ここを
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
こうする
LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
#LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
以上で、ごくごく簡単なロードバランサができました。
ね、簡単でしょ?
前のエントリーでping監視の1行コマンドを書きましたが、pingコマンドのオプションで気になったことをメモ。
まず、「-s 1」で1バイトのパケットを投げてるのに、どうして
9 bytes from 192.168.0.1: icmp_seq=0 ttl=255
みたいなレスポンスが帰ってくるかっていうと、「8バイトのICMPヘッダと1バイトのパケットの合計」なので、辻褄があうのです。試しに1バイトのパケットを投げたときの出力結果と8バイトのパケットを投げたときの出力結果を比較してみましょう。
■ 1バイトのパケットを投げたとき
$ ping -c 1 -s 1 192.168.0.1
PING 192.168.0.1 (192.168.0.1) 1(29) bytes of data.
9 bytes from 192.168.0.1: icmp_seq=0 ttl=255
--- 192.168.0.1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
, pipe 2
■ 8バイトのパケットを投げたとき
$ ping -c 1 -s 8 192.168.0.1
PING 192.168.0.1 (192.168.0.1) 8(36) bytes of data.
16 bytes from 192.168.0.1: icmp_seq=0 ttl=255 time=0.552 ms
--- 192.168.0.1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.552/0.552/0.552/0.000 ms, pipe 2
このように、自分が投げたパケットサイズにプラスして、8バイトのヘッダがくっつくのです。
あと、jmanのping(8)の項目を見て思ったのですが、
手元の端末でmanを見てみた。
$ man ping
(中略)
> -s packetsize
> Specifies the number of data bytes to be sent. The default is 56, which translates into 64 ICMP data bytes when combined with
> the 8 bytes of ICMP header data.
とだけ書いてあるので、特にスーパーユーザーでないとこのオプションが使えないってわけじゃないのかなーと。
ちなみに、(っ´∀`)っ ゃーのマシンにインストールされているpingのバージョンは
$ ping -V
ping utility, iputils-ss020927
で、pingコマンドが含まれているパッケージ「iputils」のバージョンは
$ rpm -q iputils
iputils-20020927-19.EL4.5
です。
まず、「-s 1」で1バイトのパケットを投げてるのに、どうして
9 bytes from 192.168.0.1: icmp_seq=0 ttl=255
みたいなレスポンスが帰ってくるかっていうと、「8バイトのICMPヘッダと1バイトのパケットの合計」なので、辻褄があうのです。試しに1バイトのパケットを投げたときの出力結果と8バイトのパケットを投げたときの出力結果を比較してみましょう。
■ 1バイトのパケットを投げたとき
$ ping -c 1 -s 1 192.168.0.1
PING 192.168.0.1 (192.168.0.1) 1(29) bytes of data.
9 bytes from 192.168.0.1: icmp_seq=0 ttl=255
--- 192.168.0.1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
, pipe 2
■ 8バイトのパケットを投げたとき
$ ping -c 1 -s 8 192.168.0.1
PING 192.168.0.1 (192.168.0.1) 8(36) bytes of data.
16 bytes from 192.168.0.1: icmp_seq=0 ttl=255 time=0.552 ms
--- 192.168.0.1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.552/0.552/0.552/0.000 ms, pipe 2
このように、自分が投げたパケットサイズにプラスして、8バイトのヘッダがくっつくのです。
あと、jmanのping(8)の項目を見て思ったのですが、
- > -s packetsize
- >
何バイトのデータが送られるかを指定する。デフォルトは 56 で、
ICMP
ヘッダの 8 バイトを加えて、
- > 64 バイトの ICMP データになる。 スーパーユーザーだけがこのオプションを使用できる。
手元の端末でmanを見てみた。
$ man ping
(中略)
> -s packetsize
> Specifies the number of data bytes to be sent. The default is 56, which translates into 64 ICMP data bytes when combined with
> the 8 bytes of ICMP header data.
とだけ書いてあるので、特にスーパーユーザーでないとこのオプションが使えないってわけじゃないのかなーと。
ちなみに、(っ´∀`)っ ゃーのマシンにインストールされているpingのバージョンは
$ ping -V
ping utility, iputils-ss020927
で、pingコマンドが含まれているパッケージ「iputils」のバージョンは
$ rpm -q iputils
iputils-20020927-19.EL4.5
です。
タイトルのまんま、複数のホストにpingを打ってその結果をわかりやすく出力してみたくなったので、
一行大好きな(っ´∀`)っ ゃーは、pingを打つ対象をfor文でグルグルまわし、pingが通ったら「生きてるよー」
死んでたら「死んでるよー」と出力するようにしました。
以下の例は、192.168.0.1から192.168.0.8まで、それぞれに1発ずつ1バイトのパケットを投げて
pingによる死活監視をし、終了ステータスから判断して生きてたら「(時刻)(TAB)~~~ is alive」、
死んでたら「(時刻)(TAB)~~~ is dead」という返事をさせます。
$ for i in 192.168.0.{1..8}; do ping -c 1 -s 1 $i | grep "bytes from" > /dev/null ; if [ 0 -eq `echo $?` ]; then echo `date`$'\t'$i' is alive'; else echo `date`$'\t'$i' is dead' ; fi; done
出力結果(例)は以下の通りです。
Fri May 2 17:21:12 JST 2008 192.168.0.1 is alive
Fri May 2 17:21:15 JST 2008 192.168.0.2 is dead
Fri May 2 17:21:15 JST 2008 192.168.0.3 is alive
Fri May 2 17:21:18 JST 2008 192.168.0.4 is dead
Fri May 2 17:21:21 JST 2008 192.168.0.5 is dead
Fri May 2 17:21:24 JST 2008 192.168.0.6 is dead
Fri May 2 17:21:27 JST 2008 192.168.0.7 is dead
Fri May 2 17:21:30 JST 2008 192.168.0.8 is dead
コマンドの途中に、pingの出力を/dev/nullに捨てていますが、もし「 > /dev/null」がないと、以下のようになります。
$ for i in 192.168.0.{1..8}; do ping -c 1 -s 1 $i | grep "bytes from" ; if [ 0 -eq `echo $?` ]; then echo `date`$'\t'$i' is alive'; else echo `date`$'\t'$i' is dead' ; fi; done
9 bytes from 192.168.0.1: icmp_seq=0 ttl=255
Fri May 2 17:24:57 JST 2008 192.168.0.1 is alive
Fri May 2 17:25:00 JST 2008 192.168.0.2 is dead
9 bytes from 192.168.0.3: icmp_seq=0 ttl=64
Fri May 2 17:25:00 JST 2008 192.168.0.3 is alive
Fri May 2 17:25:03 JST 2008 192.168.0.4 is dead
Fri May 2 17:25:06 JST 2008 192.168.0.5 is dead
Fri May 2 17:25:09 JST 2008 192.168.0.6 is dead
Fri May 2 17:25:12 JST 2008 192.168.0.7 is dead
Fri May 2 17:25:15 JST 2008 192.168.0.8 is dead
ちょっと見づらいですね。
