Nagiosインストール(Server)

Apache,PHPはインストール済みであることが前提。
Apache2.2.22インストール - i2bsの日記
PHPインストール - i2bsの日記

以下は全てNagiosサーバ上の作業。
クライアントはNRPEとNagios Pluginsのみをインストールする。

ユーザ追加

groupadd -g 2005 nagios
groupadd -g 2006 nagcmd
useradd -u 2005 -g nagios -m -d /usr/local/nagios -s /bin/sh -G nagcmd nagios
usermod -a -G nagcmd apache
chmod 755 /usr/local/nagios

Nagios Coreダウンロード

cd /usr/local/src
wget -c http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-3.3.1.tar.gz

Nagios Coreインストール

tar zxvf nagios-3.3.1.tar.gz 
cd nagios
./configure --prefix=/usr/local/nagios --with-httpd-conf=/usr/local/apache/conf/conf.d --with-command-group=nagcmd
make all
sed -i 's:for file in includes/rss/\*;:for file in includes/rss/\*.\*;:g' ./html/Makefile
sed -i 's:for file in includes/rss/extlib/\*;:for file in includes/rss/extlib/\*.\*;:g' ./html/Makefile
make install
make install-init
make install-commandmode
make install-config

virtualhost.conf作成(Webインターフェース用)

443がListenしてない場合はhttpd.confにてxx.xx.xx.xx:443をListenしておく。
(NameVirtualの場合はNameVirtualディレクティブの指定も忘れずに)

vi /usr/local/apache/conf/conf.d/xx.co.jp.conf
<VirtualHost xx.xx.xx.xx:443>
    ServerName xx.co.jp
    DocumentRoot /usr/local/nagios/share
    ErrorLog /usr/local/apache/vhosts/xx.co.jp/logs/ssl_error_log
    CustomLog "/usr/local/apache/vhosts/xx.co.jp/logs/ssl_access_log" combined

    SSLEngine on
    SSLCertificateFile /usr/local/apache/vhosts/xx.co.jp/ssl.crt/server.crt
    SSLCertificateKeyFile /usr/local/apache/vhosts/xx.co.jp/ssl.key/server.key

    ScriptAlias /nagios/cgi-bin "/usr/local/nagios/sbin"
    Alias /nagios "/usr/local/nagios/share"
</VirtualHost>

<Directory "/usr/local/nagios/sbin">
    SSLRequireSSL
    Options ExecCGI
    AllowOverride None
    Order allow,deny
    Allow from all
    AuthName "Nagios Access"
    AuthType Basic
    AuthUserFile /usr/local/nagios/etc/htpasswd.users
    Require valid-user
</Directory>


<Directory "/usr/local/nagios/share">
    SSLRequireSSL
    Options None
    AllowOverride None
    Order allow,deny
    Allow from all
    AuthName "Nagios Access"
    AuthType Basic
    AuthUserFile /usr/local/nagios/etc/htpasswd.users
    Require valid-user
</Directory>

自己署名証明書作成,配置

cd $HOME
mkdir -p /usr/local/apache/vhosts/xx.co.jp/{ssl.crt,ssl.key,logs}
openssl genrsa -des3 -out server.key 1024
openssl rsa -in server.key -out server.key
openssl req -new -x509 -out server.crt -key server.key -days 365
mv server.crt /usr/local/apache/vhosts/xx.co.jp/ssl.crt/.
mv server.key /usr/local/apache/vhosts/xx.co.jp/ssl.key/.

htpasswd作成

/usr/local/apache/bin/htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin

起動スクリプト修正

そのままでは自動起動設定時にエラーが出る為、起動スクリプトにLSBを追記。

vi /etc/init.d/nagios
### BEGIN INIT INFO
# Provides:          nagios
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start Nagios daemon script.
# Description:       Start and stop Nagios daemon.
### END INIT INFO

Nagios Pluginsダウンロード

cd /usr/local/src
wget -c http://prdownloads.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.15.tar.gz

Nagios Pluginsインストール

tar zxvf nagios-plugins-1.4.15.tar.gz
cd nagios-plugins-1.4.15
./configure --with-nagios-user=nagios --with-nagios-group=nagios
make
make install

NRPEダウンロード

cd /usr/local/src
wget -c http://prdownloads.sourceforge.net/sourceforge/nagios/nrpe-2.13.tar.gz

NRPEインストール

apt-get update
apt-get install libssl-dev
tar zxvf nrpe-2.13.tar.gz
cd nrpe-2.13
./configure --enable-ssl
make check_nrpe
make install-plugin

Nagios設定

nagios.cfg

以下を追記する。

vi /usr/local/nagios/etc/nagios.cfg
cfg_file=/usr/local/nagios/etc/objects/hostgroup.cfg
cfg_file=/usr/local/nagios/etc/objects/servicegroup.cfg
cfg_dir=/usr/local/nagios/etc/servers
date_format=iso8601
resource.cfg
vi /usr/local/nagios/etc/resource.cfg
###########################################################################
#
# RESOURCE.CFG - Resource File for Nagios 3.3.1
#
###########################################################################

# Sets $USER1$ to be the path to the plugins
$USER1$=/usr/local/nagios/libexec

# Sets $USER2$ to be the path to event handlers
#$USER2$=/usr/local/nagios/libexec/eventhandlers

# Store some usernames and passwords (hidden from the CGIs)
$USER3$=nagiosadmin
$USER4$=[HTPASSWORD]
hostgroup.cfg
vi /usr/local/nagios/etc/objects/hostgroup.cfg
##################################################
# HostGroup Definition
##################################################
# Real Server Group
define hostgroup{
        hostgroup_name  real-servers
        alias           Real Servers
        members         localhost
        }

# Apache VHOST Group
define hostgroup{
        hostgroup_name  apache-vhost
        alias           Apache VHOST
        members         [HOSTNAME]
        }

##################################################
# Host definition
##################################################
# localhost
define host{
        use                     linux-server
        host_name               localhost
        alias                   localhost
        address                 127.0.0.1
        }

# [HOSTNAME]
define host{
        use                     linux-server
        host_name               [HOSTNAME]
        alias                   [HOSTNAME]
        address                 [IPADDR]
        parents                 localhost
        }
servicegroup.cfg
vi /usr/local/nagios/etc/objects/servicegroup.cfg
define servicegroup{
       servicegroup_name	server-resource
       alias			Server Resource
       members			 localhost,DISK,localhost,LOAD,localhost,LOGIN,localhost,PROC,localhost,SWAP,localhost,PING,localhost,SSH
       }

define servicegroup{
       servicegroup_name	apache
       alias			Apache
       members			[HOSTNAME],HTTPS
       }
commands.cfg
vi /usr/local/nagios/etc/objects/commands.cfg
###############################################################################
#
# COMMANDS.CFG - COMMAND DEFINITIONS FOR NAGIOS 3.3.1
#
###############################################################################

################################################################################
# NOTIFICATION COMMANDS
################################################################################

# 'notify-host-by-email' command definition
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/mail -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" $CONTACTEMAIL$
        }

# 'notify-service-by-email' command definition
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/mail -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$
        }

################################################################################
# LOCALHOST SERVICE CHECK COMMANDS
################################################################################

# 'check_local_disk' command definition
define command{
        command_name    check_local_disk
        command_line    $USER1$/check_disk -w $ARG1$ -c $ARG2$ -p $ARG3$
        }

# 'check_local_load' command definition
define command{
        command_name    check_local_load
        command_line    $USER1$/check_load -w $ARG1$ -c $ARG2$
        }

# 'check_local_procs' command definition
define command{
        command_name    check_local_procs
        command_line    $USER1$/check_procs -w $ARG1$ -c $ARG2$
        }

# 'check_local_users' command definition
define command{
        command_name    check_local_users
        command_line    $USER1$/check_users -w $ARG1$ -c $ARG2$
        }

# 'check_local_swap' command definition
define command{
        command_name	check_local_swap
        command_line	$USER1$/check_swap -w $ARG1$ -c $ARG2$
        }

################################################################################
# SERVICE CHECK COMMANDS
################################################################################

# 'check-host-alive' command definition
define command{
        command_name    check-host-alive
        command_line    $USER1$/check_ping -H $HOSTADDRESS$ -w 3000.0,80% -c 5000.0,100% -p 5
        }

# 'check_ping' command definition
define command{
        command_name    check_ping
        command_line    $USER1$/check_ping -H $HOSTADDRESS$ -w $ARG1$ -c $ARG2$ -p 5
        }

# 'check_ssh' command definition
define command{
        command_name	check_ssh
        command_line	$USER1$/check_ssh -p $ARG1$ $HOSTADDRESS$
        }

# 'check_https_nagios' command definition
define command{
        command_name	check_https_nagios
        command_line	$USER1$/check_http -I $HOSTADDRESS$ -S -a $USER3$:$USER4$
        }
contacts.cfg
vi /usr/local/nagios/etc/objects/contacts.cfg
###############################################################################
#
# CONTACTS.CFG - CONTACT/CONTACTGROUP DEFINITIONS
#
###############################################################################

###############################################################################
# CONTACTS
###############################################################################

define contact{
        contact_name                    nagiosadmin
        use				generic-contact
        alias                           Nagios Admin
        email                           [EMAILADDR]
        }

###############################################################################
# CONTACT GROUPS
###############################################################################

define contactgroup{
        contactgroup_name       admins
        alias                   Nagios Administrators
        members                 nagiosadmin
        }
localhost.cfg
vi /usr/local/nagios/etc/objects/localhost.cfg
###############################################################################
#
# LOCALHOST.CFG - OBJECT CONFIG FILE FOR MONITORING THIS MACHINE
#
###############################################################################

###############################################################################
# SERVICE DEFINITIONS
###############################################################################

define service{
       use                             local-service
       host_name                       localhost
       service_description             PING
       check_command			check_ping!100.0,20%!500.0,60%
        }

define service{
       use                             local-service
       host_name                       localhost
       service_description             DISK
       check_command			check_local_disk!30%!10%!/
       }

define service{
       use                             local-service
       host_name                       localhost
       service_description             LOGIN
       check_command			check_local_users!2!4
       }

define service{
       use                             local-service
       host_name                       localhost
       service_description             PROC
       check_command			check_local_procs!150!200
       }

define service{
       use                             local-service
       host_name                       localhost
       service_description             LOAD
       check_command			check_local_load!5.0,4.0,3.0!10.0,6.0,4.0
       }

define service{
       use                             local-service
       host_name                       localhost
       service_description             SWAP
       check_command			check_local_swap!60!30
       }

define service{
       use                             local-service
       host_name                       localhost
       service_description             SSH
       check_command			check_ssh!10022
       notifications_enabled		1
       }
templates.cfg
vi /usr/local/nagios/etc/objects/templates.cfg
###############################################################################
#
# TEMPLATES.CFG -  OBJECT TEMPLATES
#
###############################################################################

###############################################################################
# CONTACT TEMPLATES
###############################################################################

define contact{
        name                            generic-contact
        service_notification_period     24x7
        host_notification_period        24x7
        service_notification_options    w,u,c,r,f,s
        host_notification_options       d,u,r,f,s
        service_notification_commands   notify-service-by-email
        host_notification_commands      notify-host-by-email
        register                        0
        }

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

define host{
        name                            generic-host
        notifications_enabled           1
        event_handler_enabled           1
        flap_detection_enabled          1
        failure_prediction_enabled      1
        process_perf_data               1
        retain_status_information       1 
        retain_nonstatus_information    1
        notification_period		24
        register                        0
        }

define host{
        name				linux-server
        use				generic-host
        check_period			24x7
        check_interval			5
        retry_interval			1
        max_check_attempts		10
        check_command           	check-host-alive
        notification_period		workhours
        notification_interval		120
        notification_options		d,u,r
        contact_groups			admins
        register			0
        }

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

define service{
        name                            generic-service
        active_checks_enabled           1
        passive_checks_enabled          1
        parallelize_check               1
        obsess_over_service             1
        check_freshness                 0
        notifications_enabled           1 
        event_handler_enabled           1
        flap_detection_enabled          1
        failure_prediction_enabled      1
        process_perf_data               1
        retain_status_information       1
        retain_nonstatus_information    1
        is_volatile                     0
        check_period                    24x7
        max_check_attempts              3
        normal_check_interval           10
        retry_check_interval            2
        contact_groups                  admins
        notification_options		w,u,c,r	
        notification_interval           60
        notification_period             24x7
        register                        0
        }

define service{
        name				local-service
        use				generic-service	
        max_check_attempts              4
        normal_check_interval           5
        retry_check_interval            1
        register                        0
        }
timeperiods.cfg
vi /usr/local/nagios/etc/objects/timeperiods.cfg
###############################################################################
#
# TIMEPERIODS.CFG - TIMEPERIOD DEFINITIONS
#
###############################################################################

###############################################################################
# TIME PERIODS
###############################################################################

define timeperiod{
        timeperiod_name 24x7
        alias           24 Hours A Day, 7 Days A Week
        sunday          00:00-24:00
        monday          00:00-24:00
        tuesday         00:00-24:00
        wednesday       00:00-24:00
        thursday        00:00-24:00
        friday          00:00-24:00
        saturday        00:00-24:00
        }

define timeperiod{
        timeperiod_name	workhours
        alias		Normal Work Hours
        monday		09:00-17:00
        tuesday		09:00-17:00
        wednesday	09:00-17:00
        thursday	09:00-17:00
        friday		09:00-17:00
        }

define timeperiod{
        timeperiod_name	none
        alias		No Time Is A Good Time
        }
hostname.conf
mkdir -p /usr/local/nagios/etc/servers
vi /usr/local/nagios/etc/servers/xx.co.jp.cfg
define service{
        use			generic-service
        host_name		[HOSTNAME]
        service_description	HTTPS
        check_command		check_https_nagios
        }
不要ファイルの削除
rm /usr/local/nagios/etc/objects/{printer.cfg,switch.cfg,windows.cfg}
Nagios起動
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
/etc/init.d/nagios start
/etc/init.d/apache stop
/etc/init.d/apache start