Nagiosのアップデート

統合監視システムのNagios(4.3.4)をインストールして使用してはいたものの、アップデートせずに放置していたので最新版(4.4.6)にアップデートしました。

◆Nagios最新版のダウンロードとインストール

# wget https://sourceforge.net/projects/nagios/files/nagios-4.x/nagios-4.4.6/nagios-4.4.6.tar.gz
# tar zxvf nagios-4.4.6.tar.gz
# cd nagios-4.4.6/
# wget http://ftp.momo-i.org/pub/security/nagios/patches/nagios-jp-4.4.6.patch.gz
# gzip -dc nagios-jp-4.4.6.patch.gz |patch -p1
# ./configure && make all && make fullinstall && make install-config
# cd
# rm -rf nagios-4.4.6*

1行目:最新版を確認してダウンロード
2行目:展開
3行目:展開したディレクトリへ移動
4行目:日本語化パッチ(最新版)のダウンロード
5行目:パッチの適用
6行目:インストール
7行目:展開先ディレクトリから抜ける
8行目:ダウンロード及び展開ディレクトリの削除

◆Nagiosプラグインのダウンロードとインストール

# wget https://nagios-plugins.org/download/nagios-plugins-2.3.3.tar.gz
# tar zxvf nagios-plugins-2.3.3.tar.gz
# cd nagios-plugins-2.3.3/
# ./configure && make && make install
# cd
# rm -rf nagios-plugins-2.3.3*

1行目:Nagiosプラグイン(最新版)のダウンロード
2行目:展開
3行目:展開したディレクトリへ移動
4行目:インストール
5行目:展開先ディレクトリから抜ける
6行目:ダウンロード及び展開ディレクトリの削除

◆Nagiosの設定

最新バージョンをインストールすると各nagios配下のディレクトリにある「.cfg」ファイルが自動でバックアップ「.cfg~」でリネームされていました。
旧バージョンで設定した「.cfg」ファイルの変更箇所を同様に変更する必要があります。

# vi /usr/local/nagios/etc/nagios.cfg

# You can also tell Nagios to process all config files (with a .cfg
# extension) in a particular directory by using the cfg_dir
# directive as shown below:

cfg_dir=/usr/local/nagios/etc/servers

process_performance_data=1

host_perfdata_command=process-host-perfdata
service_perfdata_command=process-service-perfdata

#
# service performance data
#
service_perfdata_file=/usr/local/pnp4nagios/var/service-perfdata
service_perfdata_file_template=DATATYPE::SERVICEPERFDATA\tTIMET::$TIMET$\tHOSTNAME::$HOSTNAME$\tSERVICEDESC::$SERVICEDESC$\tSERVICEPERFDATA::$SERVICEPERFDATA$\tSERVICECHECKCOMMAND::$SERVICECHECKCOMMAND$\tHOSTSTATE::$HOSTSTATE$\tHOSTSTATETYPE::$HOSTSTATETYPE$\tSERVICESTATE::$SERVICESTATE$\tSERVICESTATETYPE::$SERVICESTATETYPE$
service_perfdata_file_mode=a
service_perfdata_file_processing_interval=15
service_perfdata_file_processing_command=process-service-perfdata-file

#
# host performance data starting with Nagios 4.0
#
host_perfdata_file=/usr/local/pnp4nagios/var/host-perfdata
host_perfdata_file_template=DATATYPE::HOSTPERFDATA\tTIMET::$TIMET$\tHOSTNAME::$HOSTNAME$\tHOSTPERFDATA::$HOSTPERFDATA$\tHOSTCHECKCOMMAND::$HOSTCHECKCOMMAND$\tHOSTSTATE::$HOSTSTATE$\tHOSTSTATETYPE::$HOSTSTATETYPE$
host_perfdata_file_mode=a
host_perfdata_file_processing_interval=15
host_perfdata_file_processing_command=process-host-perfdata-file

1行目:「nagios.cfg」の編集
7行目:51行目の行頭の「#」を削除(コメント解除)して、サーバ監視設定の有効化
9行目:812行目の値を「0」から「1」に変更して、パフォーマンスデータ取得を有効化
11・12行目:824・825行目をコメント解除して、ホストパフォーマンスデータ・サービスパフォーマンスデータ取得コマンドを有効化
14行目~:最終行へ追加

# vi /usr/local/nagios/etc/objects/contacts.cfg

# 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 (de
fined above)
    alias                   Nagios Admin            ; Full name of user
    email                   xxxxxxxx@xxxxx.xxx      ; <<***** CHANGE THIS TO YOUR EMAIL ADDRESS ******
}

1行目:「contacts.cfg」の編集
13行目:32行目のemailに管理者用メールアドレスを指定

# vi /usr/local/nagios/etc/objects/localhost.cfg

# Define a service to check SSH on the local machine.
# Disable notifications for this service by default, as not all users may have SSH enabled.

define service {

    use                     local-service           ; Name of service template to use
    host_name               localhost
    service_description     SSH
    check_command           check_ssh
    notifications_enabled   1
}

# Define a service to check HTTP on the local machine.
# Disable notifications for this service by default, as not all users may have HTTP enabled.

define service {

    use                     local-service           ; Name of service template to use
    host_name               localhost
    service_description     HTTP
    check_command           check_http
    notifications_enabled   1
}

1行目:「localhost.cfg」の編集
12行目:「1」に変更し、SSHの状態変化時の通知を有効化
24行目:「1」に変更し、HTTPの状態変化時の通知を有効化

# vi /usr/local/nagios/etc/objects/commands.cfg

define command {

    command_name    notify-host-by-email
    command_line    /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /usr/bin/nkf -j | /bin/mail -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" $CONTACTEMAIL$
}

define command {

    command_name    notify-service-by-email
    command_line    /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$\n" | /usr/bin/nkf -j | /bin/mail -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$
}

1行目:「commands.cfg」の編集
6・12行目:「| /usr/bin/nkf -j」メール本文文字化け対処のため、nkfコマンドの追加

◆監視サービス設定追加

# vi /usr/local/nagios/etc/objects/commands.cfg 

#define command {

#    command_name    process-host-perfdata
#    command_line    /usr/bin/printf "%b" "$LASTHOSTCHECK$\t$HOSTNAME$\t$HOSTSTATE$\t$HOSTATTEMPT$\t$HOSTSTATETYPE$\t$HOSTEXECUTIONTIME$\t$HOSTOUTPUT$\t$HOSTPERFDATA$\n" >> /usr/local/nagios/var/host-perfdata.out
#}

#define command {

#    command_name    process-service-perfdata
#    command_line    /usr/bin/printf "%b" "$LASTSERVICECHECK$\t$HOSTNAME$\t$SERVICEDESC$\t$SERVICESTATE$\t$SERVICEATTEMPT$\t$SERVICESTATETYPE$\t$SERVICEEXECUTIONTIME$\t$SERVICELATENCY$\t$SERVICEOUTPUT$\t$SERVICEPERFDATA$\n" >> /usr/local/nagios/var/service-perfdata.out
#}

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

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

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

define command{
        command_name    check_mem
        command_line    $USER1$/check_mem -w $ARG1$ -c $ARG2$ -W $ARG3$ -C $ARG4$
        }

define command{
        command_name    check_traffic
        command_line    $USER1$/check_traffic_pnp4nagios.sh -i $ARG1$ -w $ARG2$ -c $ARG3$
        }

define command{
        command_name    process-service-perfdata-file
        command_line    /bin/mv /usr/local/pnp4nagios/var/service-perfdata /usr/local/pnp4nagios/var/spool/service-perfdata.$TIMET$
}

define command{
        command_name    process-host-perfdata-file
        command_line    /bin/mv /usr/local/pnp4nagios/var/host-perfdata /usr/local/pnp4nagios/var/spool/host-perfdata.$TIMET$
}

1行目:続けて「command.cfg」の編集
3~13行目:コメントアウト
15行目~:最下行に必要な監視サービスを追加

# vi /usr/local/nagios/etc/objects/localhost.cfg 

#define service {
#    use                     local-service           ; Name of service template to use
#    host_name               localhost
#    service_description     Swap Usage
#    check_command           check_local_swap!20%!10%
#}

define service{
        use                             generic-service
        host_name                       localhost
        #service_description             Memory Free
        service_description             Memory Usage
        check_command                   check_mem!80!90!80!90
        }

define service{
        use                             generic-service
        host_name                       localhost
        service_description             NTP
        check_command                   check_ntp!ntp.nict.jp!1!2
        }

define service{
        use                             generic-service
        host_name                       localhost
        service_description             clamd
        check_command                   check_clamd!/var/run/clamd.scan/clamd.sock
        }

define service{
        use                             generic-service
        host_name                       localhost
        service_description             MySQL
        check_command                   check_mysql!root!sqlroot
        }

define service{
       use                             generic-service
       host_name                       localhost
       service_description             Traffic Usage
       check_command                   check_traffic!venet0!80000000!90000000
       }

1行目:「localhost.cfg」の編集
3~8行目:「check_mem」プラグインに包含されるためコメントアウト
10行目~:最下行に必要な監視サービスを追加

# vi /usr/local/nagios/etc/objects/ templates.cfg

###############################################################################
#
# HOST TEMPLATES
#
###############################################################################

# Generic host definition template
# This is NOT a real host, just a template!

define host {

    name                            generic-host            ; The name of this host template
    notifications_enabled           1                       ; Host notifications are enabled
    event_handler_enabled           1                       ; Host event handler is enabled
    flap_detection_enabled          1                       ; Flap detection is enabled
    process_perf_data               1                       ; Process performance data
    retain_status_information       1                       ; Retain status information across program rest
arts
    retain_nonstatus_information    1                       ; Retain non-status information across program
restarts
    notification_period             24x7                    ; Send host notifications at any time
    register                        0                       ; DON'T REGISTER THIS DEFINITION - ITS NOT A REAL HOST, JUST A TEMPLATE!
    action_url /pnp4nagios/graph?host=$HOSTNAME$' onmouseover="get_g('$HOSTNAME$','_HOST_')" onmouseout="clear_g()"
}


###############################################################################
#
# SERVICE TEMPLATES
#
###############################################################################

# Generic service definition template
# This is NOT a real service, just a template!

define service {

    name                            generic-service         ; The 'name' of this service template
    active_checks_enabled           1                       ; Active service checks are enabled
    passive_checks_enabled          1                       ; Passive service checks are enabled/accepted
    parallelize_check               1                       ; Active service checks should be parallelized (disabling this can lead to major performance problems)
    obsess_over_service             1                       ; We should obsess over this service (if necessary)
    check_freshness                 0                       ; Default is to NOT check service 'freshness'
    notifications_enabled           1                       ; Service notifications are enabled
    event_handler_enabled           1                       ; Service event handler is enabled
    flap_detection_enabled          1                       ; Flap detection is enabled
    process_perf_data               1                       ; Process performance data
    retain_status_information       1                       ; Retain status information across program restarts
    retain_nonstatus_information    1                       ; Retain non-status information across program restarts
    is_volatile                     0                       ; The service is not volatile
    check_period                    24x7                    ; The service can be checked at any time of the day
    max_check_attempts              3                       ; Re-check the service up to 3 times in order to determine its final (hard) state
    check_interval                  10                      ; Check the service every 10 minutes under normal conditions
    retry_interval                  2                       ; Re-check the service every two minutes until a hard state can be determined
    contact_groups                  admins                  ; Notifications get sent out to everyone in the 'admins' group
    notification_options            w,u,c,r                 ; Send notifications about warning, unknown, critical, and recovery events
    notification_interval           60                      ; Re-notify about service problems every hour
    notification_period             24x7                    ; Notifications can be sent out at any time
    register                        0                       ; DON'T REGISTER THIS DEFINITION - ITS NOT A REAL SERVICE, JUST A TEMPLATE!
    action_url /pnp4nagios/graph?host=$HOSTNAME$&srv=$SERVICEDESC$' onmouseover="get_g('$HOSTNAME$','$SERVICEDESC')" onmouseout="clear_g()"<br>}

1行目:「templates.cfg」の編集
25行目:Nagios画面からPNP画面へのリンク表示(ホスト)の追加
62行目:Nagios画面からPNP画面へのリンク表示(サービス)の追加

◆設定反映

# systemctl restart nagios.service

Nagiosサービスを再起動して、設定を反映する。
ブラウザでNagios監視画面にアクセスし確認する。
併せて、性能統計グラフが表示されることを確認する。

◆不要ファイルの削除

リネームされた各設定ファイルを削除

# rm -f /usr/local/nagios/etc/*.cfg~
# rm -f /usr/local/nagios/etc/objects/*.cfg~

必ず「~(チルダ)」を付けるのを忘れないでください。
不安であれば、それぞれのディレクトリへ移動し、一つ一つコピペして削除するといいと思います。

これで、アップデートした際の要領がまとまったので迷わなくなりそうです…

参考サイト:
統合監視システム構築(Nagios)
性能統計グラフ追加(Nagios+PNP4Nagios)

いいね! & シェア お願いします。

コメントは受け付けていません。