イントラネットモニター-Nagios

10905 ワード

ゲームのメンテナンスで最も長く使われているのはnagiosを使ってゲームサーバーを監視することです.nagiosは比較的成熟した監視ソフトウェアです.監視されるホストは負荷からプロセス、ネットワークポート、システムサービスなどまで監視でき、問題が発生した場合にメールアラートを発行できます.
一、nagiosのインストール(rootユーザーの下でインストール)
必要なパッケージのインストール
yum install httpd php gcc glibc glibc-common gd gd-devel

適切なユーザーとユーザー・グループの作成
useradd -m nagios
passwd nagios
groupadd nagcmd
usermod -a -G nagcmd nagios
usermod -a -G nagcmd apache

nagiosのインストールパッケージをダウンロードして解凍します
wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-3.2.3.tar.gz
wget http://prdownloads.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.11.tar.gz
tar -zxf nagios-3.2.3.tar.gz

開始configure
cd nagios-3.2.3
./configure --with-command-group=nagcmd
make all
make install
make install-init
make install-config
make install-commandmode

これで基本的にインストールできます.ただし、nagiosを起動するのではなく、/usr/local/nagios/etc/objects/contacts.cfgファイルを変更し、アラームメールボックスを定義します.
Webインタフェースのインストール
make install-webconf  
htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin  #  web         
service httpd restart

nagiosモニタプラグインのインストール
tar xzf nagios-plugins-1.4.11.tar.gz
cd nagios-plugins-1.4.11
./configure --with-nagios-user=nagios --with-nagios-group=nagios
make
make install
#       
chkconfig --add nagios
chkconfig nagios on

プロファイルの検証
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
#  naigos
service nagios start

 二、nagios構成
まずメールアラームコマンドを送信する定義です.例を挙げると、すべてのコマンド定義はcommand.cfgに書いたほうがいいです.
# 'notify-host-by-email' command definition
define command{
     command_name    notify-host-by-email
     command_line    /usr/bin/printf "%b" "***** TW GAME *****

Notification Type: $NOTIFICATIONTYPE$
Host: $HOSTNAME$
State: $HOSTSTATE$
Address: $HOSTADDRESS$
Info: $HOSTOUTPUT$

Date/Time: $LONGDATETIME$
" |/usr/local/bin/sendEmail.pl -f "this is for define mail from" -t $CONTACTEMAIL$ -s "this is you mail smtp server" -u "** $NOTIFICATIONTYPE$ TW GAME : $HOSTALIAS$/$SERVICEDESC$ is $HOSTSTATE$ **" -xu "this is your mail account" -xp "this is your mail password" } # 'notify-service-by-email' command definition define command{ command_name notify-service-by-email command_line /usr/bin/printf "%b" "***** TW GAME *****

Notification Type: $NOTIFICATIONTYPE$

Service:$SERVICEDESC$
Host: $HOSTALIAS$
Address: $HOSTADDRESS$
State: $SERVICESTATE$

Date/Time: $LONGDATETIME$

Additional Info:

$SERVICEOUTPUT$
" | /usr/local/bin/sendEmail.pl -f Lenwood@mail.163.com -t $CONTACTEMAIL$ -s smtp.mail.163.com -u "** $NOTIFICATIONTYPE$ TW GAME : $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$**" -xu Lenwood@mail.163.com -xp passwordforserect }

説明します.
(1)上には多くの変数が使用されています.これはnagiosが提供できるものです.例えば、$HOSTNAME$は問題のあるサーバのホスト名です.
(2)太字部分は自分でメールを送るためのメールボックスに変更する必要があります.これを例に挙げます.
(3)ここで使う
sendEmail.plこのperlスクリプトはメールを送信します.sendmailが適用されない理由はsendmailが信頼できない場合があります.特にDNS解析に問題がある場合です.
 
他のコマンドの定義は書きませんが、check_などの/path/to/nagios/libexec/ディレクトリのプラグインを使用することが多いです.ping、check_smtp、check_httpなど.
 
三、まとめたnagiosの非常に実用的なテクニック
nagiosは主に新しいマシンを追加するのが気持ち悪いのでshellスクリプトで追加します.これにより、1台でも1000台のモニタリングを追加してもhostnameとIPアドレス対応情報があれば、5分しかかかりません.
例えば、hostレコードは以下のように与えられる.
#/home/nagios/hosts.txt    
game_web1     10.96.19.51
game_web2     10.96.19.52
game_ope     10.96.19.53
game_db1     10.96.19.54
game_db2     10.96.19.55

新しいサーバモニタリングを追加するには、nagiosでホストとそのホストがモニタリングする必要があるサービスを定義する必要があります.私の解決策は、2つのテンプレートをそれぞれ書き、テンプレート内のホスト名とIPを/home/nagios/hosts.txtで置き換えることです.
(1)host定義テンプレート
host_template.cfg    
define host {
        use     linux-server
        host_name      hostname
        address ipaddress
        hostgroups      hostgroupname
        contact_groups  lenwood
}

さあ、一括置換を開始します
while read line;do host=`echo $line|awk '{print $1}'`;ip=`echo $line|awk '{print $2}'`;sed  "s/hostname/$host/g" host_template.cfg|sed "s/ipaddress/$ip/g"|sed "s/hostgroupname/GAME/g">>host.cfg;done</home/nagios/hosts.txt

 
これですべて置き換えられ、ゲームは事前に定義された「ホストグループ名」である.
(2)serive定義テンプレート
#service_template.cfg    
define service {
    host_name   hostname
    use generic-service
    service_description SSH
    check_command   check_ssh!22
    servicegroups   SSH
#   notifications_enabled 0
    contact_groups  lenwood
}
..... 。

その後、SSHサービス検出の定義を一括して書くことができます.コマンドは次のとおりです.
while read line;do host=`echo $line|awk '{print $1}'`;sed  "s/hostname/$host/g" service_template.cfg >$host.cfg;done</home/nagios/hosts.txt

これがnagiosを使う過程での体験です.