Nagios 安装部署
前期工作需要关闭防火墙:iptables 和selinux
修改/etc/selinux/config文件中设置SELINUX=disabled ,然后重启服务器。
service iptables stop
一.监控端安装部署
1.service服务器ip:10.1.15.222
user:nagios   group nagios
2.监控机ip:10.1.15.224
user:nagios
Nagios (一)、
1安装准备:检查系统包:httpd, gcc ,glibc ,glibc-common , gd ,gd-devel
#yum  -y install httpd gcc glibc glibc-common gd gd-devel
#yum -y install openssl-devel  (不做这步,安装nrpe会出现checking for SSL headers... configure: error: Cannot find ssl headers错误)
2创建用户
#useradd  nagios
#passwd nagios
创建组
#groupadd nagios
#usermod -G nagios nagios
#usermod -G nagios apache
3修改文件目录权限
#chown nagios.nagios /usr/local/nagios

1.安装(在安装的时候出现问题,要实现安装必须先下载安装软件 http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-3.2.0.tar.gz
通过这个地址找到安装包  )
注意使用tar解压是在有nagios-3.2.0.tar.gz这个目录下进行,本文将安装包放在/home/nagios目录下
#tar xzf nagios-3.2.0.tar.gz
#cd nagios-3.2.0        以下命令都在 nagios-3.2.0目录下进行
#./configure --prefix=/usr/local/nagios --with-gd-lib=/usr/local/lib --with-gd-inc=/usr/local/include --with-command-group=nagios
#make all
#make install//来安装主程序,CGI 和HTML 文件
#make install-init//在/etc/rc.d/init.d 安装启动脚本
#make install-commandmode//来配置目录权限
#make install-config//来安装示例配置文件,安装的路径是     /usr/local/nagios/etc.
验证是否安装成功:  验证程序是否被正确安装。切换目录到安装路径(这是/usr/local/nagios),看是否存在  etc、bin、sbin、share、var 这五个目录,如果存在则可以表明程序被正确的安装到系统 了。
后表是五个目录功能的简要说明:
Bin Nagios 执行程序所在目录,
nagios 文件即为主程序
Etc Nagios 配置文件位置,初始安装完后,只有几个*.cfg-sample 文件  
Sbin NagiosCgi 文件所在目录,也就是执行外部命令所需文件所在的目录
Share Nagios 网页文件所在的目录
Var Nagios 日志文件、spid 等文件所在的目录
var/archives Empty directory for the archived logs  
/var/rw Empty directory for the external commandfile

2.编译安装nagios插件
http://prdownloads.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.14.tar.gz
[root@locakhost]# tar zxvf nagios-plugins-1.4.14.tar.gz
[root@locakhost]# cd nagios-plugins-1.4.14
[root@locakhost nagios-plugins-1.4.14]#./configure --prefix=/usr/local/nagios/ --enable-redhat-pthread-workaround --with-nagios-user=nagios --with-nagios-group=nagios
[root@locakhost nagios-plugins-1.4.14]# make all
[root@locakhost nagios-plugins-1.4.14]# make install
安装完成后,在/usr/local/nagios/libexec建立许多插件
3. 编译安装nagios中文插件(非必须)
http://sourceforge.net/projects/nagios-cn/files/sourcecode/zh_CN%203.2.3/nagios-cn
.3.2.3.tar.bz2/download
[root@nlocakhost]# tar jxvf nagios-cn-3.2.3.tar.bz2
[root@locakhost]# cd nagios-cn-3.2.3
[root@locakhost nagios-cn-3.2.3]# ./configure --prefix=/usr/local/nagios/
[root@locakhost nagios-cn-3.2.3]# make all
[root@locakhost nagios-cn-3.2.3]# make install
4.安装p_w_picpathpak-base.tar.gz
#tar –xvzf p_w_picpathpak-base.tar.gz
#cd p_w_picpathpak-base
#mv base/ /usr/local/nagios/share/p_w_picpaths/logos/

5.将nagios添加服务设置开机启动项
#chkconfig --add nagios 将nagios 添加到服务中
#chkconfig nagios on 设置nagios开机启动
#/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg此命令用于检测nagios 配置是否正确,在后面的配置中,建议一配置完一个文件就运行一次,以保证配置正确,发现错误及时更改
6.启动nagios   #service nagios start
7.配置apache 修改apache 的配置文件,增加nagios 的目录,并且访问此目录需要进行身份验证。
# vi /usr/local/apache/conf/httpd.conf
下文添加的配置文件
ScriptAlias /nagios/cgi-bin /usr/local/nagios/sbin
<Directory "/usr/local/nagios/sbin">
Options ExecCGI
AllowOverride None
Order allow,deny
Allow from all
AuthName "NagiosAccess"
AuthType Basic
AuthUserFile /usr/local/nagios/etc/htpasswd
Require valid-user
</Directory>
Alias /nagios /usr/local/nagios/share
<Directory "/usr/local/nagios/share">
Options None
AllowOverride None
Order allow,deny
Allow from all
AuthName "NagiosAccess"
AuthType Basic
AuthUserFile /usr/local/nagios/etc/htpasswd
Require valid-user
</Directory>
运行/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg确认配置文件没有错误

8.增加验证用户: 也就是通过web 访问nagios 的时候,必须要用这个用户登陆.在这里我们增加用户nagiosadmin:密 码为123456
#/usr/local/apache/bin/htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin //用户名

注:这个用户以后会在cgi.cfg文件中填写一致:
authorized_for_system_commands=nagiosadmin

==============
问题,无权查看任何主机信息
故障解决:
设置登录帐问题。#/usr/local/apache/bin/htpasswd -c /usr/local/nagios/etc/htpasswd nagios
就会出现无法查看主机信息,但是页面可以登录===不解
因为在cig.cfg文件中定义的用户全是nagiosadmin 所以,使用nagios无法登录

nagiosadmin:密 码为123456
#/usr/local/apache/bin/htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin //用户名

二.被监控端安装部署
监控端需要安装的程序:nagios-plugins 、nrpe-2.12.tar
nagios插件程序: http://prdownloads.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.13.tar.gz
nrpe软件: http://prdownloads.sourceforge.net/sourceforge/nagios/nrpe-2.12.tar.gz  
1.增加用户
#useradd -s /usr/sbin/nologin nagios
#passwd nagios
创建组
#groupadd nagios
#usermod -G nagios nagios

2安装nagios-plugins
安装文件放在/home/nagios
使用make编译要GCC支持,
#yum -y install gcc*
#tar xzvf nagios-plugins-1.4.14.tar.gz
#cd nagios-plugins-1.4.14
#./configure --enable-redhat-pthread-workaround --with-nagios-user=nagios --with-nagios-group=nagios(如果编译报错可以取消这行--enable-redhat-pthread-workaround)
#make all
#make install
#chown nagios.nagios /usr/local/nagios/
#chown -R nagios.nagios /usr/local/nagios/libexec/
3.安装nrpe-2.12.tar
#yum install xinetd
#tar ‐zxvf nrpe‐2.12.tar.gz  
#cd nrpe‐2.12  
#./configure
#make all      
#make install-plugin         #安装nrpe插件,本监控端可以不装      
#make install-daemon        # 安装nrpe守护进程      
#make install-daemon-config         #安装nrpe配置文件
#vi /etc/xinetd.d/nrpe    修改或添加:only_from 127.0.0.1  你的监控主机IP    添加端口:# vi /etc/services    nrpe                   5666/tcp                          #nrpe
重启xinetd:
#service xinetd restart  
测试服务:netstat –an |grep 5666                         LISTEN
#/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
#vi /usr/local/nagios/etc/nrpe.cfg        #修改nrpe配置文件,允许Nagios监控服务器(10.1.15.222)监控
allowed_hosts=127.0.0.1,10.1.15.222        #多台机器用逗号隔开
#/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d        以独立守护进程启动nrpe,也可以使用xinetd启动nrpe

# /usr/local/nagios/libexec/check_nrpe -H localhost        #检查nrpe是否安装正常
nagios的全部配置文件在:/usr/local/nagios/etc目录下面
首先cgi.cfg
refresh_rate=30         #nagios主页的刷新时间,我设置成30秒自动刷新
use_authentication=0  #关闭认证功能,当nagios配置好后,建议开回来

三.修改主程序配置

如果我们要添加一台监控服务器,使用的方法很多,现在我使用的是在添加service文件,通过在该文件下写过配置文件实现监控:

1.修改nagios.cfg配置文件./usr/local/nagios/etc
添加:监控主机的配置文件路径
cfg_dir=/usr/local/nagios/etc/services

要明白nagios.cfg配置文件
cfg_file=/usr/local/nagios/etc/objects/commands.cfg  #nagios可调用的监控命令
cfg_file=/usr/local/nagios/etc/objects/contacts.cfg     #联系人配置
cfg_file=/usr/local/nagios/etc/objects/timeperiods.cfg  #监控时间配置
cfg_file=/usr/local/nagios/etc/objects/templates.cfg   #模板配置
cfg_dir=/usr/local/nagios/etc/services                       #新添加,把需要添加的主机文件放进去,就不必在这里一行行添加
#cfg_file=/usr/local/nagios/etc/objects/hostgroups.cfg  #新添加,主机组配置
cfg_file=/usr/local/nagios/etc/objects/localhost.cfg    #本地信息监迭

2.在/usr/local/nagios/etc/services路径下添加文件,
10.1.15.224.cfg
该文件定义了要监控的内容:ping、ssh、ftp、http等。按个人需求添加
define host{
use linux-server
host_name 10.1.15.224
alias 10.1.15.224
address 10.1.15.224
}
define service{
use generic-service
host_name 10.1.15.224
service_description load
check_command check_nrpe!check_load
}
define service{
use generic-service
host_name 10.1.15.224
service_description check_ping
check_command check_ping!100.0,20%!200.0,50%
max_check_attempts 5
normal_check_interval 1
}
define service{
use generic-service
host_name 10.1.15.224
service_description check_ftp
check_command check_ftp!21
max_check_attempts 5
normal_check_interval 1
}
define service{
use generic-service
host_name 10.1.15.224
service_description check_ssh
check_command check_ssh
max_check_attempts 5
normal_check_interval 1
}
define service{
use generic-service
host_name 10.1.15.224
service_description check_http
check_command check_http
max_check_attempts 5
normal_check_interval 1
}

修改完配置文件使用命令检查是否有语法错误:
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

3定义监控模版:/usr/local/nagios/etc/objects/templates.cfg
一般不需要修改,安装配置文件中基本定义好了
define contact{
       name                            generic-contact         ; The name of this contact template
       service_notification_period     24x7                    ; service notifications can be sent anytime
       host_notification_period        24x7                    ; host notifications can be sent anytime

     service_notification_options    w,u,c,r,f,s             ; send
notifications for all service states, flapping events, and scheduled
downtime events
       host_notification_options       d,u,r,f,s
            ; send notifications for all host states, flapping events,
and scheduled downtime events
       service_notification_commands   notify-service-by-email ; send service notifications via email
       host_notification_commands      notify-host-by-email    ; send host notifications via email

     register                        0                       ; DONT
REGISTER THIS DEFINITION - ITS NOT A REAL CONTACT, JUST A TEMPLATE!
       }

如果该文件有错误,nagios无法正常启动,也不能实现对客户机的监控

四.修改监控机配置文件:
#/usr/local/nagios/libexec/check_nrpe -H 127.0.0.1

#vim/usr/local/nagios/etc/nrpe.cfg
command[check_users]=/usr/local/nagios/libexec/check_users -w 5 -c 10
command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20
command[check_hda1]=/usr/local/nagios/libexec/check_disk -w 20 -c 10 -p /dev/hda1
command[check_zombie_procs]=/usr/local/nagios/libexec/check_procs -w 5 -c 10 -s Z
command[check_total_procs]=/usr/local/nagios/libexec/check_procs -w 150 -c 200
command[check_swap]=/usr/local/nagios/libexec/check_swap -w 20% -c 10%

以上是nagios安装部署,现实了简单的监控,但是很多细节还是需要优化,如:添加服务器组,短信提醒等,都没有实现....
120955955.jpg