3,497 views

■概要

サーバーに不具合が起きても、気づくまでに時間がかかるのが常です。統合監視システムNagios(ナギオス)は、サーバーに不具合が起きるとメールをくれるので、不具合に気づくまでの時間を短縮できます。今回は、パッケージから簡単インストールで構築してみました。最新版でないのが欠点ですが、初心者向けの構築法ではあります。監視項目を増やすと敷居も高くなりますが・・・。

■事前準備

Webサーバー構築済であること
リポジトリ導入(EPEL,RPMforge)を参照してEPELリポジトリが導入済であること

■導入手順

統合監視システムNagiosをyumでインストールし、バックアップしたファイルをコピーするという単純な手順です。ついでに、Sambaを監視項目に追加すると不明のエラーが出る問題も応急手当で解消しています。

■Nagiosインストール

[root@ufuso ~]# yum --enablerepo=epel -y install nagios nagios-plugins nagios-plugins-all
 ← EPELからNagios、基本プラグインをインストール
Installed:
  nagios.x86_64 0:3.4.1-2.el6              nagios-plugins.x86_64 0:1.4.15-7.el6
  nagios-plugins-all.x86_64 0:1.4.15-7.el6

Dependency Installed:
  fping.x86_64 0:2.4b2-10.el6                                                   
  lm_sensors.x86_64 0:3.1.1-10.el6                                              
  nagios-common.x86_64 0:3.4.1-2.el6                                            
  nagios-plugins-breeze.x86_64 0:1.4.15-7.el6                                   
  nagios-plugins-by_ssh.x86_64 0:1.4.15-7.el6                                   
  nagios-plugins-cluster.x86_64 0:1.4.15-7.el6                                  
  nagios-plugins-dhcp.x86_64 0:1.4.15-7.el6                                     
  nagios-plugins-dig.x86_64 0:1.4.15-7.el6                                      
  nagios-plugins-disk.x86_64 0:1.4.15-7.el6                                     
  nagios-plugins-disk_smb.x86_64 0:1.4.15-7.el6                                 
  nagios-plugins-dns.x86_64 0:1.4.15-7.el6                                      
  nagios-plugins-dummy.x86_64 0:1.4.15-7.el6                                    
  nagios-plugins-file_age.x86_64 0:1.4.15-7.el6                                 
  nagios-plugins-flexlm.x86_64 0:1.4.15-7.el6                                   
  nagios-plugins-fping.x86_64 0:1.4.15-7.el6                                    
  nagios-plugins-game.x86_64 0:1.4.15-7.el6                                     
  nagios-plugins-hpjd.x86_64 0:1.4.15-7.el6                                     
  nagios-plugins-http.x86_64 0:1.4.15-7.el6                                     
  nagios-plugins-icmp.x86_64 0:1.4.15-7.el6                                     
  nagios-plugins-ide_smart.x86_64 0:1.4.15-7.el6                                
  nagios-plugins-ircd.x86_64 0:1.4.15-7.el6                                     
  nagios-plugins-ldap.x86_64 0:1.4.15-7.el6                                     
  nagios-plugins-load.x86_64 0:1.4.15-7.el6                                     
  nagios-plugins-log.x86_64 0:1.4.15-7.el6                                      
  nagios-plugins-mailq.x86_64 0:1.4.15-7.el6                                    
  nagios-plugins-mrtg.x86_64 0:1.4.15-7.el6                                     
  nagios-plugins-mrtgtraf.x86_64 0:1.4.15-7.el6                                 
  nagios-plugins-mysql.x86_64 0:1.4.15-7.el6                                    
  nagios-plugins-nagios.x86_64 0:1.4.15-7.el6                                   
  nagios-plugins-nt.x86_64 0:1.4.15-7.el6                                       
  nagios-plugins-ntp.x86_64 0:1.4.15-7.el6                                      
  nagios-plugins-nwstat.x86_64 0:1.4.15-7.el6                                   
  nagios-plugins-oracle.x86_64 0:1.4.15-7.el6                                   
  nagios-plugins-overcr.x86_64 0:1.4.15-7.el6                                   
  nagios-plugins-perl.x86_64 0:1.4.15-7.el6                                     
  nagios-plugins-pgsql.x86_64 0:1.4.15-7.el6                                    
  nagios-plugins-ping.x86_64 0:1.4.15-7.el6                                     
  nagios-plugins-procs.x86_64 0:1.4.15-7.el6                                    
  nagios-plugins-real.x86_64 0:1.4.15-7.el6                                     
  nagios-plugins-rpc.x86_64 0:1.4.15-7.el6                                      
  nagios-plugins-sensors.x86_64 0:1.4.15-7.el6                                  
  nagios-plugins-smtp.x86_64 0:1.4.15-7.el6                                     
  nagios-plugins-snmp.x86_64 0:1.4.15-7.el6                                     
  nagios-plugins-ssh.x86_64 0:1.4.15-7.el6                                      
  nagios-plugins-swap.x86_64 0:1.4.15-7.el6                                     
  nagios-plugins-tcp.x86_64 0:1.4.15-7.el6                                      
  nagios-plugins-time.x86_64 0:1.4.15-7.el6                                     
  nagios-plugins-ups.x86_64 0:1.4.15-7.el6                                      
  nagios-plugins-users.x86_64 0:1.4.15-7.el6                                    
  nagios-plugins-wave.x86_64 0:1.4.15-7.el6                                     
  net-snmp-utils.x86_64 1:5.5-41.el6                                            
  postgresql-libs.x86_64 0:8.4.12-1.el6_2                                       
  qstat.x86_64 0:2.11-9.20080912svn311.el6                                      

Complete!

[root@ufuso ~]# yum -y install gd-devel ← 描画処理に必要なソフトをインストール
Installed:
  gd-devel.x86_64 0:2.0.35-10.el6                                               

Dependency Installed:
  fontconfig-devel.x86_64 0:2.8.0-3.el6                                         
  freetype-devel.x86_64 0:2.3.11-6.el6_2.9                                      
  gd.x86_64 0:2.0.35-10.el6                                                     
  libX11-devel.x86_64 0:1.3-2.el6                                               
  libXau-devel.x86_64 0:1.0.5-1.el6                                             
  libXdmcp-devel.x86_64 0:1.0.3-1.el6                                           
  libXpm-devel.x86_64 0:3.5.8-2.el6                                             
  libjpeg-devel.x86_64 0:6b-46.el6                                              
  libpng-devel.x86_64 2:1.2.49-1.el6_2                                          
  libxcb-devel.x86_64 0:1.5-1.el6                                               
  xorg-x11-proto-devel.noarch 0:7.6-13.el6                                      
  zlib-devel.x86_64 0:1.2.3-27.el6                                              

Complete!

[root@ufuso ~]#  vi /etc/nagios/objects/contacts.cfg ← contacts.cfgの設定
###############################################################################
#
# CONTACTS
#
###############################################################################
###############################################################################

# Just one contact defined by default - the Nagios admin (that's you)
# This contact definition inherits a lot of default values from the 'generic-contact'
# template which is defined elsewhere.

define contact{
        contact_name                    nagiosadmin             ; Short name of user
        use                             generic-contact         ; Inherit default values from generic-contact template (defined above)
        alias                           Nagios Admin            ; Full name of user

        email                           ufuso@gmail.com     ; <<***** CHANGE THIS TO YOUR EMAIL ADDRESS ******
        } ← Nagiosからのメールを受け取るアドレスに変更。「ufuso@gmail.com」は例示

[root@ufuso ~]#   vi /etc/nagios/nagios.cfg ← nagios.cfgの編集
# DATE FORMAT OPTION
# This option determines how short dates are displayed. Valid options
# include:
#       us              (MM-DD-YYYY HH:MM:SS)
#       euro            (DD-MM-YYYY HH:MM:SS)
#       iso8601         (YYYY-MM-DD HH:MM:SS)
#       strict-iso8601  (YYYY-MM-DDTHH:MM:SS)
#

date_format=iso8601 ← 日付表示をusをiso8601に変更

[root@ufuso ~]# htpasswd /etc/nagios/passwd nagiosadmin  ← Nagios管理ユーザー(nagiosadmin)
のパスワード作成
New password:  ← nagiosadminのパスワード作成入力(表示されない)
Re-type new password:  ← 作成したパスワード再入力(表示されない)
Updating password for user nagiosadmin

[root@ufuso ~]# /etc/rc.d/init.d/nagios start ← nagios起動
Starting nagios: done.

[root@ufuso ~]# chkconfig nagios on ← 自動起動の設定

■監視サービスの追加

(2)NTP

[root@scientific ~]# vi /etc/nagios/objects/commands.cfg ← commands.cfg編集
以下を最終行へ追加

# 'check_ntp' command definition
define command{
        command_name    check_ntp
        command_line    $USER1$/check_ntp -H $ARG1$ -w $ARG2$ -c $ARG3$
        }

[root@scientific ~]# vi /etc/nagios/objects/localhost.cfg ← localhost.cfg編集
以下を最終行へ追加

define service{
        use                             generic-service
        host_name                       localhost
        service_description             NTP
        check_command                   check_ntp!210.173.160.27!1!2 ← 外部NTPサーバー
(「210.173.160.27」は例示)と時間が1秒ずれていたら警告、2秒ずれていたら異常とする
        }

[root@scientific ~]# /etc/rc.d/init.d/nagios restart ← Nagios再起動
Running configuration check...done
Stopping nagios: done.
Starting nagios: done.

(3)clamdClam AntiVirus導入済の場合のみ

[root@scientific ~]# vi /etc/nagios/objects/commands.cfg ← commands.cfg編集
以下を最終行へ追加

# 'check_clamd' command definition
define command{
        command_name    check_clamd
        command_line    $USER1$/check_clamd -H $ARG1$
        }

[root@scientific ~]# vi /etc/nagios/objects/localhost.cfg ← localhost.cfg編集
以下を最終行へ追加

define service{
        use                             generic-service
        host_name                       localhost
        service_description             clamd
        check_command                   check_clamd!/var/run/clamav/clamd.sock ←
 /var/run/clamav/clamd.sockはclamd.confのLocalSocketオプションで指定した値
        }

[root@scientific ~]# /etc/rc.d/init.d/nagios restart ← Nagios再起動
Running configuration check...done
Stopping nagios: done.
Starting nagios: done.

(4)DNSBIND導入済の場合のみ

[root@scientific ~]# vi /etc/nagios/objects/commands.cfg ← commands.cfg編集
以下を最終行へ追加

# 'check_dig' command definition
define command{
        command_name    check_dig
        command_line    $USER1$/check_dig -H $HOSTADDRESS$ -l $ARG1$
        }

[root@scientific ~]# vi /etc/nagios/objects/localhost.cfg ← localhost.cfg編集
以下を最終行へ追加

define service{
        use                             generic-service
        host_name                       localhost
        service_description             DNS
        check_command                   check_dig!ufuso.dip.jp ← 名前解決ホスト名に自ドメイン
名を指定
        }

[root@scientific ~]# /etc/rc.d/init.d/nagios restart ← Nagios再起動
Running configuration check...done
Stopping nagios: done.
Starting nagios: done.

(5)SMTPメールサーバー導入済の場合のみ

[root@scientific ~]# vi /etc/nagios/objects/localhost.cfg ← localhost.cfg編集
以下を最終行へ追加

define service{
        use                             generic-service
        host_name                       localhost
        service_description             SMTP
        check_command                   check_smtp
        }

[root@scientific ~]# /etc/rc.d/init.d/nagios restart ← Nagios再起動
Running configuration check...done
Stopping nagios: done.
Starting nagios: done.

(6)POPメールサーバー導入済の場合のみ

[root@scientific ~]# vi /etc/nagios/objects/commands.cfg ← commands.cfg編集
# 'check_pop' command definition
define command{
        command_name    check_pop
        command_line    $USER1$/check_pop -H $HOSTADDRESS$ $ARG1$ -s quit← 「-s quit」追加
        }
※「-s quit」はDovecotがpop3-login:Abortedlogin:rip=::ffff:127.0.0.1,lip=::ffff:127.0.0.1, 
securedというエラーメッセージをログに出力しないようにする対処

[root@scientific ~]# vi /etc/nagios/objects/localhost.cfg ← localhost.cfg編集
以下を最終行へ追加

define service{
        use                             generic-service
        host_name                       localhost
        service_description             POP
        check_command                   check_pop
        }

[root@scientific ~]# /etc/rc.d/init.d/nagios restart ← Nagios再起動
Running configuration check...done
Stopping nagios: done.
Starting nagios: done.

(7)IMAPメールサーバー導入済の場合のみ

[root@scientific ~]# vi /etc/nagios/objects/commands.cfg ← commands.cfg編集
# 'check_imap' command definition
define command{
        command_name    check_imap
        command_line    $USER1$/check_imap -H $HOSTADDRESS$ -s "A2 logout"← 「-s "A2 logout"」追加
        }
※「-s "A2 logout"」はDovecotがimap-login: Aborted login: rip=::ffff:127.0.0.1,
 lip=::ffff:127.0.0.1, securedというエラーメッセージをログに出力しないようにする対処

[root@scientific ~]# vi /etc/nagios/objects/localhost.cfg ← localhost.cfg編集
以下を最終行へ追加

define service{
        use                             generic-service
        host_name                       localhost
        service_description             IMAP
        check_command                   check_imap
        }

[root@scientific ~]# /etc/rc.d/init.d/nagios restart ← Nagios再起動
Running configuration check...done
Stopping nagios: done.
Starting nagios: done.

(8)amavisdamavisd導入済の場合のみ

[root@scientific ~]# vi /etc/nagios/objects/commands.cfg ← commands.cfg編集
以下を最終行へ追加

# 'check_amavisd' command definition
define command{
        command_name    check_amavisd
        command_line    $USER1$/check_tcp -H $HOSTADDRESS$ -p 10024
        }

[root@scientific ~]# vi /etc/nagios/objects/localhost.cfg ← localhost.cfg編集
以下を最終行へ追加

define service{
        use                             generic-service
        host_name                       localhost
        service_description             amavisd
        check_command                   check_amavisd
        }

[root@scientific ~]# /etc/rc.d/init.d/nagios restart ← Nagios再起動
Running configuration check...done
Stopping nagios: done.
Starting nagios: done.

(9)spamdSpamAssassin導入済の場合のみ

[root@scientific ~]# vi /etc/nagios/objects/commands.cfg ← commands.cfg編集
以下を最終行へ追加

# 'check_spamd' command definition
define command{
        command_name    check_spamd
        command_line    $USER1$/check_tcp -H $HOSTADDRESS$ -p 783
        }

[root@scientific ~]# vi /etc/nagios/objects/localhost.cfg ← localhost.cfg編集
以下を最終行へ追加

define service{
        use                             generic-service
        host_name                       localhost
        service_description             spamd
        check_command                   check_spamd
        }

[root@scientific ~]# /etc/rc.d/init.d/nagios restart ← Nagios再起動
Running configuration check...done
Stopping nagios: done.
Starting nagios: done.

(10)SambaSamba導入済の場合のみ

[root@ufuso ~]# yum install samba-client ← sambaの監視に必要なのでインストール
Loaded plugins: priorities, refresh-packagekit, security
google-chrome                                            |  951 B     00:00 ... 
sl                                                       | 3.2 kB     00:00     
sl-security                                              | 1.9 kB     00:00     
Setting up Install Process
Package samba-client-3.5.10-125.el6.x86_64 already installed and latest version
Nothing to do ← すでにインストール済みと出る

[root@ufuso ~]#  vi /usr/lib64/nagios/plugins/check_disk_smb ← check_disk_smbの編集
my $smbclient= "/usr/bin/smbclient" ; ← smbclientコマンドパス設定

[root@ufuso ~]# echo 192.168.24.240 ufuso.dip.jp >> /etc/samba/lmhosts ← lmhostsへSamba
サーバーのIPアドレス(例示「192.168.24.240」)/ホスト名(例示「ufuso.dip.jp」)を追加
※SambaサーバーIPアドレスにループバックアドレス(127.0.0.1)は指定できない

[root@scientific ~]# vi /etc/nagios/objects/commands.cfg ← commands.cfg編集
以下を最終行へ追加

# 'check_disk_smb' command definition
define command{
        command_name    check_disk_smb
        command_line    $USER1$/check_disk_smb -H $ARG1$ -s $ARG2$ -W $ARG3$ -u $ARG4$ -p $ARG5$
        }

[root@scientific ~]# vi /etc/nagios/objects/localhost.cfg ← localhost.cfg編集
以下を最終行へ追加

define service{
        use                             generic-service
        host_name                       localhost
        service_description             Samba
        check_command                   check_disk_smb!ufuso.dip.jp!public!workgroup!user!password
        } ← 「ufuso.dip.jp」はSambaサーバーホスト名の例示、「public」はSamba共有名の例示、
「workgroup」はSambaワークグループ名の例示、「user」はSambaユーザー名の例示、「password」は
Sambaユーザーパスワードの例示。Sambaで設定したとおり入力のこと

[root@scientific ~]# /etc/rc.d/init.d/nagios restart ← Nagios再起動
Running configuration check...done
Stopping nagios: done.
Starting nagios: done.

(11)MySQLMySQL導入済の場合のみ

[root@scientific ~]# vi /etc/nagios/objects/commands.cfg ← commands.cfg編集
以下を最終行へ追加

# 'check_mysql' command definition
define command{
        command_name    check_mysql
        command_line    $USER1$/check_mysql -H $HOSTADDRESS$ -u $ARG1$ -p $ARG2$
        }

[root@scientific ~]# vi /etc/nagios/objects/localhost.cfg ← localhost.cfg編集
以下を最終行へ追加

define service{
        use                             generic-service
        host_name                       localhost
        service_description             MySQL
        check_command                   check_mysql!root!password
        } ← 「password」はMySQLのrootユーザーパスワードの例示
[root@scientific ~]# /etc/rc.d/init.d/nagios restart ← Nagios再起動
Running configuration check...done
Stopping nagios: done.
Starting nagios: done.

■Samba監視ができない不具合対応

[root@ufuso ~]# vi /usr/share/perl5/Getopt/Long.pm ← Long.pmファイルの487行目当たりを表示
    # Process argument list
    my $goon = 1;
    while ( $goon && @$argv > 0 ) {

        # Get next argument.
        $opt = shift (@$argv) || ''; ← || ''(which is 2 | (pipe characters) and
 2 ' (single quotes)を追記
        print STDERR ("=> arg "", $opt, ""n") if $debug;

[root@ufuso ~]# /etc/rc.d/init.d/nagios restart ← Nagios再起動
Running configuration check...done.
Stopping nagios: done.
Starting nagios: done.

■メモリ空き状況監視を追加する

[root@ufuso ~]# wget "http://downloads.sourceforge.net/project/nagiosplug/nagiosplug/1.4.16/nagios-plugins-1.4.16.tar.gz?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Fnagiosplug%2Ffiles%2Fnagiosplug%2F&ts=1308577771&use_mirror=jaist"
 ← yumではcheck_mem.plがインストールされないので、Nagiosプラグインをダウンロード
`nagios-plugins-1.4.16.tar.gz' に保存中

100%[======================================>] 2,087,089   2.63M/s 時間 0.8s    

2012-08-11 07:26:12 (2.63 MB/s) - `nagios-plugins-1.4.16.tar.gz' へ保存完了 [2087089/2087089]

[root@ufuso ~]# tar zxvf nagios-plugins-1.4.16.tar.gz ← Nagiosプラグイン展開
・・・
nagios-plugins-1.4.16/contrib/check_mem.pl
・・・

[root@ufuso ~]# cd nagios-plugins-1.4.16 ← Nagiosプラグイン展開先ディレクトリへ移動

[root@ufuso nagios-plugins-1.4.16]# cp contrib/check_mem.pl /usr/lib64/nagios/plugins/
 ← check_mem.plをNagiosプラグイン保存フォルダにコピー

[root@ufuso nagios-plugins-1.4.16]# vi /usr/lib64/nagios/plugins/check_mem.pl
 ← check_mem.plの編集

[root@ufuso nagios-plugins-1.4.16]# chmod +x /usr/lib64/nagios/plugins/check_mem.pl
 ← メモリチェックプラグインへ実行権限付加

[root@ufuso nagios-plugins-1.4.16]# cd ← ルートディレクトリへ戻る

[root@scientific ~]# vi /etc/nagios/objects/commands.cfg ← commands.cfg編集
以下を最終行へ追加

# 'check_mem' command definition
define command{
        command_name    check_mem
        command_line    $USER1$/check_mem.pl -f -w $ARG1$ -c $ARG2$
        }

[root@scientific ~]# vi /etc/nagios/objects/localhost.cfg ← localhost.cfg編集
以下を最終行へ追加

define service{
        use                             generic-service
        host_name                       localhost
        service_description             Memory Free
        check_command                   check_mem!20!10 ← 空きメモリ量が20%で警告、10%で異常とする
        }

[root@scientific ~]# /etc/rc.d/init.d/nagios restart ← Nagios再起動
Running configuration check...done.
Stopping nagios: done.
Starting nagios: done.

http://サーバー名/nagios/へアクセスし、「サービス」で追加したサービスが表示されることを確認。
我が家では18項目を監視しています。黄色のWarningがあるので、定期的にメールが飛んできます。

One Reply to “自宅サーバーを24時間自動監視(Nagios)”

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

CAPTCHA


このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください