[munin] 動的IPアドレスの監視サーバから外部のサーバを監視する

リアル引越しを機に、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が書き換わっていないこと、そして新たなバックアップファイルができていないことを確認します。

※ 最初、手動ログインで「できたできたー」とヌカ喜びしてたのはここだけの話 (;^ω^)

以上で、楽々監視システムのできあがりです。

トラックバック(0)

このブログ記事を参照しているブログ一覧: [munin] 動的IPアドレスの監視サーバから外部のサーバを監視する

このブログ記事に対するトラックバックURL: http://www.blogcube.info/cgi-bin/mt-tb.cgi/123

カスタム検索

カレンダー



このブログ記事について

このページは、(っ´∀`)っ ゃーが2008年5月29日 21:20に書いたブログ記事です。

ひとつ前のブログ記事は「【pound】 poundインストール」です。

次のブログ記事は「[munin] 監視サーバのIPアドレス取得スクリプト修正」です。

最近のコンテンツはインデックスページで見られます。過去に書かれたものはアーカイブのページで見られます。

Powered by Movable Type 4.01