(一)安装Nagios   (Nagios服务器为:192.168.6.6    Nagios客户端为: 192.168.2.33)

1.基础支持套件:gcc glibc glibc-common gd gd-devel xinetd openssl-devel httpd php   注:php和httpd均用源码包安装,安装配置方法此处不在详述

# yum install -y gcc glibc glibc-common gd gd-devel xinetd openssl-devel

2.创建Nagios账户和组

#useradd -m nagios
#groupadd nagcmd
#usermod -a -G nagcmd nagios
#usermod -a -G nagcmd apache

3.编译安装

#tar xvf nagios-3.5.1.tar.gz
#cd nagios-3.5.1
#./configure prefix=/usr/local/nagios --with-command-group=nagcmd --with-nagios-user=nagios --with-nagios-group=nagios
#make all
#make install
#make install-init        (生成init启动脚本)
#make install-config      (生成一些模板配置文件)
#make install-commandmode (设置相应的权限)
#make install-webconf     (生成Apache配置文件nagios.conf)

4.为Nagios设置Web验证的密码

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

5.设置Nagios的开机启动

chkconfig --add nagios
chkconfig nagios on

6.安装Nagios的插件nagios-plugin

#tar zxvf nagios-plugins-1.4.16.tar.gz
#cd nagios-plugins-1.4.16
#./configure --prefix=/usr/local/nagios --with-nagios-user=nagios --with-nagios-group=nagios
 --with-apt-get-command --with-ping6-command --with-ping-command --with-mysql
 --with-gnutls --enable-extra-opts
#make
#make install


7.此时完成初步安装,可以监控查看本机的一些服务,检测配置文件并启动nagios

#/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

Nagios Core 3.5.1
Copyright (c) 2009-2011 Nagios Core Development Team and Community Contributors
Copyright (c) 1999-2009 Ethan Galstad
Last Modified: 08-30-2013
License: GPL

Website: http://www.nagios.org
Reading configuration data...
   Read main config file okay...
Processing object config file '/usr/local/nagios/etc/objects/commands.cfg'...
Processing object config file '/usr/local/nagios/etc/objects/contacts.cfg'...
Processing object config file '/usr/local/nagios/etc/objects/timeperiods.cfg'...
Processing object config file '/usr/local/nagios/etc/objects/templates.cfg'...
Processing object config directory '/usr/local/nagios/etc/servers'...
Processing object config file '/usr/local/nagios/etc/servers/localhost.cfg'...
   Read object config files okay...

Running pre-flight check on configuration data...

Checking services...
 Checked 6 services.
Checking hosts...
 Checked 1 hosts.
Checking host groups...
 Checked 0 host groups.
Checking service groups...
 Checked 0 service groups.
Checking contacts...
 Checked 1 contacts.
Checking contact groups...
 Checked 1 contact groups.
Checking service escalations...
 Checked 0 service escalations.
Checking service dependencies...
 Checked 0 service dependencies.
Checking host escalations...
 Checked 0 host escalations.
Checking host dependencies...
 Checked 0 host dependencies.
Checking commands...
 Checked 25 commands.
Checking time periods...
 Checked 5 time periods.
Checking for circular paths between hosts...
Checking for circular host and service dependencies...
Checking global event handlers...
Checking obsessive compulsive processor commands...
Checking misc settings...

Total Warnings: 0
Total Errors:   0

Things look okay - No serious problems were detected during the pre-flight check

出现此处,表明,配置文件没有错误,可以启动nagios和apache

#service nagios start
#/usr/local/apache/sbin/apchectl

访问nagios
http://192.168.6.6/nagios/ 

此时提示页面无法访问,原因在于由于Apache是源码包安装,默认路径和rpm包不一样,需要在Apache的httpd.conf配置文件中添加指定访问路径


8. 配置apache并加载nagios登录页面 
找到apache 的配置文件/usr/local/apache/conf/httpd.conf

找到:

User daemon
Group daemon 修改为

User nagios
Group nagios

为了安全起见,一般情况下要让nagios 的web 监控页面必须经过授权才能访问,这需要增加验证配置,即在httpd.conf 文件最后添加如下信息:

下面信息在编译nagios(make install-webconf )时就已经生成,配置信息在:/etc/httpd/confd.d/nagios.conf 文件中

#######################################################################
#setting for nagios
ScriptAlias /nagios/cgi-bin "/usr/local/nagios/sbin"

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

Alias /nagios "/usr/local/nagios/share"

     AuthType Basic
     Options None
     AllowOverride None
     Order allow,deny
     Allow from all
     AuthName "nagios Access"
     AuthUserFile /usr/local/nagios/etc/htpasswd.user
     Require valid-user

###########################################################################

9.重启nagios、apache并访问nagios

#service nagios restart
#/usr/local/apache/bin/apachectl restart
http://192.168.6.6/nagions

提示输入用户名密码,访问成功

但是登陆进去后,nagios页面右侧全部乱码
解决方法:

主要是apache没有开启cgi脚本的缘故

进入apache的主配置文件httpd.conf
#vim /usr/local/apache/conf/httpd.conf
 
#LoadModule cgid_module modules/mod_cgid.so
#LoadModule actions_module modules/mod_actions.so

将上面2行的#去掉,重启apache就OK

再次访问 ,乱码消失OK!

(二)配置Nagios

1.nagios配置目录信息

# cd /usr/local/nagios/etc/
# ls
cgi.cfg  htpasswd.user  nagios.cfg  objects  resource.cfg
[root@localhost etc]# ll
total 68
-rw-rw-r-- 1 nagios nagios 11669 Nov 29 14:18 cgi.cfg (CGI配置文件)
-rw-r--r-- 1 root   root      50 Nov 29 14:20 htpasswd.user (Apache的验证密码文件)
-rw-rw-r-- 1 nagios nagios 44710 Nov 29 14:18 nagios.cfg (主配置文件)
drwxrwxr-x 2 nagios nagios  4096 Nov 29 14:18 objects (对象定义文件目录)
-rw-rw---- 1 nagios nagios  1340 Nov 29 14:18 resource.cfg (资源配置文件)

2.修改nagios.cfg主配置文件

#vim nagios.cfg

注释掉:cfg_file=/usr/local/nagios/etc/objects/localhost.cfg  ———— #cfg_file=/usr/local/nagios/etc/objects/localhost.cfg
将 #cfg_dir=/usr/local/nagios/etc/servers  的 #(注释)去掉 -----  cfg_dir=/usr/local/nagios/etc/servers

在/usr/local/nagios/etc/目录中新建 servers子目录,在里面可以直接添加主机配置文件
#mkdir servers

3.配置object目录中的配置文件

#cd objects/
#ll
total 48
-rw-rw-r-- 1 nagios nagios  7716 Nov 29 14:18 commands.cfg (命令定义文件)
-rw-rw-r-- 1 nagios nagios  2166 Nov 29 14:18 contacts.cfg (联系人信息定义文件)
-rw-rw-r-- 1 nagios nagios  5403 Nov 29 14:18 localhost.cfg
-rw-rw-r-- 1 nagios nagios  3124 Nov 29 14:18 printer.cfg
-rw-rw-r-- 1 nagios nagios  3293 Nov 29 14:18 switch.cfg
-rw-rw-r-- 1 nagios nagios 10812 Nov 29 14:18 templates.cfg
-rw-rw-r-- 1 nagios nagios  3208 Nov 29 14:18 timeperiods.cfg (时间周期定义文件)
-rw-rw-r-- 1 nagios nagios  4019 Nov 29 14:18 windows.cfg

配置联系人信息(邮件接收者邮箱地址)

联系人定义:
#vim contacts.cfg
将 email 字段后边的  nagios@localhost  改成自己的邮箱,将报警信息发送的此邮箱,比如 15566058@139.com 
如果是设置提醒多个邮箱可以在后跟其它邮箱地址,以逗号隔开,比如: 15566058@139.com,abc@163.com

保存,退出。

(三)nrpe安装配置

1.安装nrpe

#tar zxvf nrpe-2.12.tar.gz
#cd nrpe-1.12
# ./configure && make all
#make install-plugin
#make install-daemon
#make install-daemon-config
#make install-xinetd

2.配置nrpe

#vim /etc/xinetd.d/nrpe
在only_from=127.0.0.1 后添加 192.168.6.6 以空格隔开

3.添加端口

#vim /etc/services       在最后添加   nrpe  5666/tcp   #nrpe


修改配置文件/usr/local/nagios/etc/objects/commands.cfg加入对nrpe的支持

#vim /usr/local/nagios/etc/objects/commands.cfg     在末尾添加如下内容

##############################################################################
#nrpe set
define command{
command_name check_nrpe
command_line /usr/local/nagios/libexec/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}
##############################################################################


4.配置/usr/local/nagios/etc/nrpe.cfg 文件

#cd /usr/local/nagios/etc/nrpe.cfg
#vim nrpe.cfg
将command[check_hda1]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/hda1 中的hda1改为: sda  如下:
command[check_sda]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/sda

将command[check_total_procs]=/usr/local/nagios/libexec/check_procs -w 150 -c 200  改为:
  command[check_total_procs]=/usr/local/nagios/libexec/check_procs -w 400 -c 450

添加 command[check_swap]=/usr/local/nagios/libexec/check_swap -w 90% -c 80%

保存,退出。

重启xinetd服务

OK,Nagios服务端安装成功!!!

 

(四)Nagios客户端安装配置


#yum install -y openssl openssl-devel

#useradd -s /sbin/nlogin nagios


2.安装nagios-plugin

# cd /opt/software/
#tar zxvf nagios-plugins-1.4.16.tar.gz
#cd nagios-plugin-1.4.16
#./configure --prefix=/usr/local/nagios --with-nagios-user=nagios --with-nagios-group=nagios --enable-libtap
--enable-redhat-pthread-workaround --with-apt-get-command --with-ping6-command --with-ping-command
--with-mysql --with-gnutls --enable-extra-opts --with-openssl --with-trusted-path
#make
#make install

3.安装配置nrpe

#yum install xinetd -y

#tar zxvf nrpe-2.12.tar.gz
#cd nrpe-2.12
#./configure && make all
#make install-plugin
#make install-daemon
#make install-daemon-config
#make install-xinetd

#vim /etc/xinetd.d/nrpe 
在only_from    = 127.0.0.1 后添加 nagios服务器端IP地址 192.168.6.6 如下:
  only_from    = 127.0.0.1 192.168.6.6

#vim /etc/services
在文件末尾添加 nrpe   5666/tcp      #nrpe

修改/usr/local/nagios/etc/nrpe.cfg

#cp -p  /usr/local/nagios/etc/nrpe.cfg    /usr/local/nagios/etc/nrpe.cfg.default   -修改前先备份一下该配置文件

#vim /usr/local/nagios/etc/nrpe.cfg
将nrpe.cfg文件中的 hda1 字符全部修改为 sda 
或者使用sed命令批量修改,如下:
sed  -i 's/hda1/sda/g'  /usr/local/nagios/etc/nrpe.cfg

添加swep分区监控:  command[check_swap]=/usr/local/nagios/libexec/check_swap -w 90% -c 80%

保存,退出。

重启xinetd服务 :
#service xinetd restart
 

(五) 在服务端/usr/local/nagios/etc/servers/目录中添加编辑被监控主机配置文件

1.本地主机配置文件模版

#cd /usr/local/nagios/etc/servers
#touch localhost.cfg   注:localhost.cfg 为本地主机配置文件
#vim localhost.cfg   添加如下模版内容
##########################################################################################
#define  host

define host{
host_name 192.168.6.6-nagios server     #主机名称,可随便定义
alias nagios Server                     #服务器别名,监控端为Server 被监控端为 Client
address 192.168.6.6                     #服务器端IP地址 或者被监控端IP地址
check_command check-host-alive          #检查的命令
check_interval 1
#retry_interval 1
max_check_attempts 1
check_period 24x7                       #检查的时间范围
process_perf_data 0
retain_nonstatus_information 0
contact_groups admins                   #联系人组
notification_interval 10                #检查时间间隔,单位为分钟
notification_period 24x7
notification_options d,u,r              #通知选项,d-宕机(down)  w-报警(warning)  u-未知(unkown) c-严重(critical) r-从异常情况恢复
}

#define services

#define check-host-alive
define service {
host_name 192.168.6.6-nagios server
service_description check-host-alive
check_period 24x7
max_check_attempts 1
normal_check_interval 1
#retry_check_interval 1
contact_groups admins
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
check_command check-host-alive
}

define service {
host_name 192.168.6.6-nagios server
service_description check-users
check_period 24x7
max_check_attempts 4
normal_check_interval 1
retry_check_interval 1
contact_groups admins
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
check_command check_nrpe!check_users
}

define service {
host_name 192.168.6.6-nagios server
service_description check-load
check_period 24x7
max_check_attempts 4
normal_check_interval 1
retry_check_interval 1
contact_groups admins
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
check_command check_nrpe!check_load
}

define service {
host_name 192.168.6.6-nagios server
service_description check-total-procs
check_period 24x7
max_check_attempts 4
normal_check_interval 1
retry_check_interval 1
contact_groups admins
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
check_command check_nrpe!check_total_procs
}

define service {
host_name 192.168.6.6-nagios server
service_description check_sda
check_period 24x7
max_check_attempts 4
normal_check_interval 1
retry_check_interval 1
contact_groups admins
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
check_command check_nrpe!check_sda
}

define service {
host_name 192.168.6.6-nagios server
service_description check_swap
check_period 24x7
max_check_attempts 4
normal_check_interval 1
retry_check_interval 1
contact_groups admins
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
check_command check_nrpe!check_swap
}
########################################################################################

2.其它主机配置文件模版

#cd /usr/local/nagios/etc/servers/
#touch 192.168.2.33.cfg
#vim 192.168.2.33.cfg   添加如下内容

##################################################################################
#define  host

define host{
host_name 192.168.2.33-Test
alias nagios Client
address 192.168.2.33
check_command check-host-alive
check_interval 1
#retry_interval 1
max_check_attempts 1
check_period 24x7
process_perf_data 0
retain_nonstatus_information 0
contact_groups admins
notification_interval 10
notification_period 24x7
notification_options d,u,r
}

#define check-host-alive
define service {
host_name 192.168.2.33-Test
service_description check-host-alive
check_period 24x7
max_check_attempts 1
normal_check_interval 1
#retry_check_interval 1
contact_groups admins
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
check_command check-host-alive
}

define service {
host_name 192.168.2.33-Test
service_description check-users
check_period 24x7
max_check_attempts 4
normal_check_interval 1
retry_check_interval 1
contact_groups admins
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
check_command check_nrpe!check_users
}

define service {
host_name 192.168.2.33-Test
service_description check-load
check_period 24x7
max_check_attempts 4
normal_check_interval 1
retry_check_interval 1
contact_groups admins
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
check_command check_nrpe!check_load
}

define service {
host_name 192.168.2.33-Test
service_description check-total-procs
check_period 24x7
max_check_attempts 4
normal_check_interval 1
retry_check_interval 1
contact_groups admins
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
check_command check_nrpe!check_total_procs
}

define service {
host_name 192.168.2.33-Test
service_description check_sda
check_period 24x7
max_check_attempts 4
normal_check_interval 1
retry_check_interval 1
contact_groups admins
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
check_command check_nrpe!check_sda
}

define service {
host_name 192.168.2.33-Test
service_description check_swap
check_period 24x7
max_check_attempts 4
normal_check_interval 1
retry_check_interval 1
contact_groups admins
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
check_command check_nrpe!check_swap
}
##############################################################################################

3.监控服务器监控运行的端口

(1)Nagios客户端配置

#vim /usr/local/nagios/etc/nrpe.cfg  文件末尾添加定义端口信息,如下:

command[check_TemplateUpload:8080]=/usr/local/nagios/libexec/check_tcp -H localhost -p 8080  -w 120 -c 180

保存,退出

重启xinetd服务

#service xinetd restart


(2)Nagios服务端配置

#vim /usr/local/nagios/etc/services/192.168.2.33.cfg 文件末尾添加如下内容

define service {
host_name 192.168.2.33-Test
service_description check_TemplateUpload:8080 
check_period 24x7
max_check_attempts 4
normal_check_interval 1
retry_check_interval 1
contact_groups admins
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
check_command check_nrpe!check_TemplateUpload:8080
}

保存,退出

重启nagios服务

#service nagios restart

访问Nagios服务

http://192.168.6.6/nagios

大功告成!!!!!