天天看点

手把手教你部署Nagios

 本文档是Nagios3.2.0基于 Red Hat 安装配置及部署,文档内容包含如下       1、安装环境及准备软件包   2、Nagios安装过程   3、安装过程报错注意事项   4、Nagios监控服务器配置介绍及个配置文件作用   5、Linux系统被监控端配置   6、Window系统被监控端配置   7、监控项设置、参数介绍,错误排错     一、安装环境和软件包   Os:Red Hat Enterprise Linux Server release 5.2 nagios-3.2.0.tar.gz nagios-plugins-1.4.13.tar.gz nrpe-2.12.tar.gz   环境支持包:gcc gcc-c++ gd gd-devel php httpd openssl-devel         二、安装过程   ① 、安装支持包   #yum -y install gcc gcc-c++ gd gd-devel php httpd openssl-devel 注 :3.2的nagios,web首页已经由html变为php,必须安装php支持 (尽量采用 yum方式安装、配置yum源过程省略)   ② 、安装 nagios   创建用户 #useradd nagios #passwd nagios   创建组 #groupadd nagcmd #usermod -G nagcmd nagios #usermod -G nagcmd apache   安装 #tar xzf nagios-3.2.0.tar.gz #cd nagios-3.2.0 #./configure --with-command-group=nagcmd --prefix=/usr/local/nagios #make all #make install              # 使用 make install来安装主程序,CGI和HTML文件 #make install-init         # 使用 make install-init在/etc/rc.d/init.d安装启动脚本 #make install-config       # 使用 make install-cofig来安装示例配置文件,安装的路径是/usr/local/nagios/etc. #make install-commandmode # 使用 make install-commandmode来配置目录权限   配置 apache #vi /etc/httpd/conf/httpd.conf         #加入末尾 ScriptAlias /nagios/cgi-bin "/usr/local/nagios/sbin" <Directory "/usr/local/nagios/sbin"> # SSLRequireSSL    Options ExecCGI    AllowOverride None    Order allow,deny    Allow from all # Order deny,allow # Deny from all # Allow from 127.0.0.1    AuthName "Nagios Access"    AuthType Basic    AuthUserFile /usr/local/nagios/etc/htpasswd.users    Require valid-user </Directory> Alias /nagios "/usr/local/nagios/share" <Directory "/usr/local/nagios/share"> # SSLRequireSSL    Options None    AllowOverride None    Order allow,deny    Allow from all # Order deny,allow # Deny from all # Allow from 127.0.0.1    AuthName "Nagios Access"    AuthType Basic    AuthUserFile /usr/local/nagios/etc/htpasswd.users    Require valid-user </Directory>   创建 apache目录验证文件 #htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin New password: (输入密码 ) Re-type new password: (再输入一次密码 ) Adding password for user nagiosadmin   #service httpd restart   配置 nagios配置文件 #vi /usr/local/nagios/etc/objects/contacts.cfg         # 将里面的 email地址改为自己的email地址   ③ 、安装 nagios插件   #tar xzf nagios-plugins-1.4.13.tar.gz #cd nagios-plugins-1.4.13 #./configure --with-nagios-user=nagios --with-nagios-group=nagios --prefix=/usr/local/nagios #make && make install ④ 、启动 nagios   添加服务 #chkconfig --add nagios #chkconfig nagios on   检查配置文件 #/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg   启动 nagios #service nagios start   启动 apache #service httpd start   访问 http://localhost/nagios 输入用户名密码 id: nagiosadmin       三、安装过程报错注意事项   1、 nagios提示http 500   Internal Server Error The server encountered an internal error or misconfiguration and was unable to complete your request.   查询日志: /usr/local/nagios/var [Thu Sep 17 22:50:02 2009] [error] [client 10.0.0.73] Premature end of script headers: status.cgi, referer: http://10.0.0.193/side.html   试试 apache的SuExec: [[email protected] sbin]# setenforce 0   2、报错 [1297704346] SERVICE ALERT: localhost;HTTP;WARNING;SOFT;3;HTTP WARNING: HTTP/1.1 403 Forbidden [1297704406] SERVICE ALERT: localhost;HTTP;WARNING;HARD;4;HTTP WARNING: HTTP/1.1 403 Forbidden [1297704506] Caught SIGTERM, shutting down... [1297704506] Successfully shutdown... (PID=16538) [1297704600] Nagios 3.2.0 starting... (PID=4313) [1297704600] Local time is Tue Feb 15 01:30:00 CST 2011         四、Nagios监控服务器配置介绍及个配置文件作用     配置文件路径 [[email protected] nagios]# pwd /usr/local/nagios [[email protected] nagios]# ls bin etc   libexec sbin share var   bin:Nagios执行程序所在目录, nagios文件即为主程序 etc Nagios配置文件位置 libexec Nagios的监控插件 sbin     Nagios Cgi文件所在目录,也就是执行外部命令所需文件所在的目录 Share     Nagios网页文件所在的目录 var     Nagios日志文件、 spid 等文件所在的目录 var/archives     日志归档目录 var/rw     用来存放外部命令文件     /usr/local/nagios/etc/nagios.cfg Nagios的主配置文件   cfg_file=/usr/local/nagios/etc/objects/commands.cfg              # 命令的配置文件路径 cfg_file=/usr/local/nagios/etc/objects/contacts.cfg              # 联系人配置文件路径 cfg_file=/usr/local/nagios/etc/objects/contactgroups.cfg         # 联系组配置文件路径 cfg_file=/usr/local/nagios/etc/objects/timeperiods.cfg           # 监视时段配置文件路径 cfg_file=/usr/local/nagios/etc/objects/templates.cfg             # 模板的配置文件路径 cfg_file=/usr/local/nagios/etc/objects/hostgroups.cfg            # 主机组配置文件路径 cfg_file=/usr/local/nagios/etc/objects/hosts.cfg                 # 主机配置文件路径 cfg_file=/usr/local/nagios/etc/objects/services.cfg              # 服务配置文件路径   关系图  

手把手教你部署Nagios

  官方说明:在 Nagios里每个要监控的服务都必须给出一个绑定在刚才定义出的主机上的一个服务对象。可以把服务对象放在任何一个由 cfg_file域指向的对象配置文件里或是放在 cfg_dir域所指向的目录下。 也就是说只需要在 nagios.cfg 的配置文件里指定cfg_dir 就可以实现监控机器的目的。   #把关于 windows的配置选项前面的#号去掉 cfg_file=/usr/local/nagios/etc/objects/windows.cfg                #把 server目录配置前面的#去掉,记得手动创建目录 cfg_dir=/usr/local/nagios/etc/servers   #如果需要查看日志就把下面的配置加上,记得自己手动创建目录 log_file=/usr/local/nagios/var/nagios.log debug_file=/usr/local/nagios/var/nagios.debug debug_level=32   #修改 CGI脚本控制文件cgi.cfg vi /usr/local/nagios/etc/cgi.cfg #是否开启验证, 1是开启0是关闭 use_authentication=1 #修改默认用户 default_user_name=test #多个用户之间用逗号隔开 authorized_for_system_information=nagiosadmin,test authorized_for_configuration_information=nagiosadmin,test authorized_for_system_commands=test authorized_for_all_services=nagiosadmin,test authorized_for_all_hosts=nagiosadmin,test authorized_for_all_service_commands=nagiosadmin,test authorized_for_all_host_commands=nagiosadmin,test     具体每一个配置文件的内容,不再一一介绍可以查看223服务器我的定义,写的很直观有注释     作为监控服务器时,需配置如下内容 在运行 nagios的监控主机上   安装 check_nrpe插件 在 commands.cfg中创建check_nrpe的命令定义,因为只有在commands.cfg中定义过的命令才能在services.cfg中使用 创建对被监控主机的监控项目 安装 check_nrpe插件 [[email protected] yahoon]# tar -zxvf nrpe-2.12.tar.gz [[email protected] yahoon]# cd nrpe-2.12 [[email protected] nrpe-2.12]# ./configure [[email protected] nrpe-2.12]# make all [[email protected] nrpe-2.12]# make install-plugin 只运行这一步就行了 ,因为只需要check_nrpe插件   在 dbpi上我们刚装好了nrpe,现在我们测试一下监控机使用check_nrpe与被监控机运行的nrpedaemon之间的通信. [[email protected] nrpe-2.12]# /usr/local/nagios/libexec/check_nrpe -H 192.168.1.225 NRPE v2.12 看到已经正确返回了 NRPE的版本信息,说明一切正常.     在 commands.cfg中增加对check_nrpe的定义 vi /usr/local/nagios/etc/commands.cfg 在最后面增加如下内容 ######################################################################## # # NRPE COMMAND # ######################################################################## # 'check_nrpe ' command definition define command{         command_name check_nrpe         command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$         } 意义如下 command_name check_nrpe 定义命令名称为 check_nrpe,在services.cfg中要使用这个名称. command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$ 这是定义实际运行的插件程序 .这个命令行的书写要完全按照check_nrpe这个命令的用法.不知道用法的就用check_nrpe –h查看 -c后面带的 $ARG1$参数是传给nrpe daemon执行的检测命令,之前说过了它必须是nrpe.cfg中所定义的那5条命令中的其中一条.在services.cfg中使用check_nrpe的时候要用!带上这个参数       五、Linux系统被监控端配置  

原理如下图
手把手教你部署Nagios
NRPE 总共由两部分组成:

– check_nrpe 插件,位于在监控主机上 – NRPE daemon,运行在远程的linux主机上(通常就是被监控机) 按照上图 ,整个的监控过程如下: 当 nagios需要监控某个远程linux主机的服务或者资源情况时 1.nagios会运行 check_nrpe这个插件,告诉它要检查什么. 2.check_nrpe插件会连接到远程的 NRPE daemon,所用的方式是SSL 3.NRPE daemon会运行相应的 nagios插件来执行检查 4.NRPE daemon将检查的结果返回给 check_nrpe插件,插件将其递交给nagios做处理. 注意 :NRPE daemon需要nagios插件安装在远程的linux主机上,否则,daemon不能做任何的监控.  

通过NRPE的检测分为两种: 1). 直接检测

:检测的对象是运行 NRPE的那台linux主机的本地资源,原理如下图  

手把手教你部署Nagios
2). 间接检测

:当运行 nagios的监控主机无法访问到某台被监控机,但是运行NRPE的机器可以访问到时,NRPE就可以充当一个代理,将监控请求发送到被监控机   在被监控主机上 1增加用户 [[email protected] root]# useradd nagios 设置密码 [[email protected] root]# passwd nagios   2安装nagios插件 解压缩 tar -zxvf nagios-plugins-1.4.13.tar.gz cd nagios-plugins-1.4.13 编译安装 ./configure make make install 这一步完成后会在/usr/local/nagios/下生成两个目录libexec和share [[email protected] nagios]# ls /usr/local/nagios/ libexec share 修改目录权限 [ro[email protected] nagios]# chown nagios.nagios /usr/local/nagios [[email protected] nagios]# chown -R nagios.nagios /usr/local/nagios/libexec   3安装nrpe 解压缩 tar -zxvf nrpe-2.12.tar.gz cd nrpe-2.12 编译 ./configure 输出如下 *** Configuration summary for nrpe 2.12 05-10-2007 ***:  General Options:  -------------------------  NRPE port:     5666  NRPE user:    nagios  NRPE group:    nagios  Nagios user: nagios  Nagios group: nagios Review the options above for accuracy. If they look okay, type 'make all' to compile the NRPE daemon and client. 可以看到NRPE的端口是5666,下一步是make all   make all 输出如下 *** Compile finished *** If the NRPE daemon and client compiled without any errors, you can continue with the installation or upgrade process. Read the PDF documentation (NRPE.pdf) for information on the next steps you should take to complete the installation or upgrade. 接下来安装NPRE插件,daemon和示例配置文件   安装check_nrpe这个插件 make install-plugin 之前说过监控机需要安装check_nrpe这个插件,被监控机并不需要,我们在这里安装它是为了测试的目的 安装deamon make install-daemon 安装配置文件 make install-daemon-config   现在再查看nagios目录就会发现有4个目录了 [[email protected] nrpe-2.12]# ls /usr/local/nagios/ bin       etc      libexec share 按照安装文档的说明,是将NRPE deamon作为xinetd下的一个服务运行的.在这样的情况下xinetd就必须要先安装好,不过一般系统已经默认装了   4.安装xinetd脚本 [[email protected] nrpe-2.12]# make install-xinetd 输出如下 /usr/bin/install -c -m 644 sample-config/nrpe.xinetd /etc/xinetd.d/nrpe 可以看到创建了这个文件/etc/xinetd.d/nrpe 编辑这个脚本 vi /etc/xinetd.d/nrpe # default: on # description: NRPE (Nagios Remote Plugin Executor) service nrpe {         flags           = REUSE         socket_type     = stream         port            = 5666         wait            = no         user            = nagios         group           = nagios         server          = /usr/local/nagios/bin/nrpe         server_args     = -c /usr/local/nagios/etc/nrpe.cfg --inetd         log_on_failure += USERID         disable         = no         only_from       = 127.0.0.1在后面增加监控主机的地址1.223,以空格间隔 } 改后      only_from       = 127.0.0.1 192.168.1.223   编辑/etc/services文件,增加NRPE服务 vi /etc/services 增加如下 # Local services nrpe             5666/tcp                        # nrpe 重启xinetd服务 [[email protected] nrpe-2.12]# service xinetd restart Stopping xinetd: [ OK ] Starting xinetd: [ OK ]   查看NRPE是否已经启动 [[email protected] nrpe-2.12]# netstat -at|grep nrpe tcp         0      0 *:nrpe                  *:*                     LISTEN   [[email protected] nrpe-2.12]# netstat -an|grep 5666 tcp         0      0 0.0.0.0:5666            0.0.0.0:*               LISTEN  可以看到5666端口已经在监听了   5.测试NRPE是否则正常工作 之前我们在安装了check_nrpe这个插件用于测试,现在就是用的时候.执行 /usr/local/nagios/libexec/check_nrpe -H localhost 会返回当前NRPE的版本 [[email protected] nrpe-2.12]# /usr/local/nagios/libexec/check_nrpe -H localhost NRPE v2.12 也就是在本地用check_nrpe连接nrpe daemon是正常的 注:为了后面工作的顺利进行,注意本地防火墙要打开5666能让外部的监控机访问       附录 : 1.重启 nagios的方法 之前我说重启 nagios的时候都是用的杀进程的方式,其实也可以不这么做.如果在安装nagios的时候安装了启动脚本就可以使用/etc/init.d/nagios restart 还可以带的参数有stop, start,status 如果报错了 ,有可能是脚本里面的路径设置错误,解决办法 vi /etc/init.d/nagios 将 prefix=/usr/local/nagiosaa改为安装的目录/etc/init.d/nagios 注 :在nagios安装的时候说是将脚本安装到了/etc/rc.d/init.d,其实这和/etc/init.d是一个目录   2.不以 xinetd的方式运行nrpe 因为我们按照 nrpe的安装文档安装下来,nrpe是在xinetd下面运行的,个人比较喜欢像nagios那样以单独的daemon来运行.这样比较好控制. 方法 : 编辑 /etc/services将nrpe注释掉 # Local services #nrpe            5666/tcp                        # nrpe 编辑 nrpe.cfg,增加监控主机的地址 # NOTE: This option is ignored if NRPE is running under either inetd or xinetd allowed_hosts=127.0.0.1,192.168.1.223 注意两个地址以逗号隔开 以单独的 daemon启动nrpe [[email protected] etc]# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d 查看 [[email protected] etc]# ps -ef|grep nrpe nagios    22125     1 0 14:04 ?        00:00:00 [nrpe] [[email protected] nagios]# netstat -an|grep 5666 tcp         0      0 0.0.0.0:5666            0.0.0.0:*               LISTEN  说明已经正常启动了 在 /etc/rc.d/rc.local里面加入下面一行就实现开机启动nrpe了 /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg – d 同理要开机运行 nagios就在/etc/rc.d/rc.local里面增加下面这行 /usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg     六、Window系统被监控端配置    

手把手教你部署Nagios

  监控原理 在 windows服务器内安装NSClient++的监控引擎,nagios服务器通过check_nt来获取监控数据   http://nsclient.org/nscp/downloads 安装方式,解压缩到 C:\NSClient++   在指令模式下,在该目录下面运行以下命令,注册 NSCient++的服务       nsclient++ /install   将 NSCient++作为启动进程       nsclient++ SysTray   在服务列表中找到 NSClientpp的服务。双击打开该服务的配置窗口则在登录分页中将“允许服务与桌面交互”勾选上。   编辑 NSC.INI,做以下更改    * 在 [Module]部分,除了CheckWMI.dll和RemoteConfiguration.dll这两行的注释不要去掉,其他DLL的注释都去掉。       * 在 [Setting]部分的allowed_hosts部分设定Nagios的服务器IP或者设定一个IP段,并把前面的注释去掉。       allowed_hosts=192.168.1.223       * 在 [NSClient]部分将port前的注释去掉,确认port的端口号是12489   启动 nsclient++服务   查看端口     七、监控项设置、错误排错   错误: CHECK_NRPE: Error - Could not complete SSL handshake vi /etc/xinetd.d/nrpe 加空格 ip service xinetd restart    cd /usr/local/nagios/etc/          vi nrpe.cfg 加 ip注意,好隔开。 /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d   Connection refused or timed out 检查 nrpe 端口 检查 nrpe.cfg中allowed_hosts是否包含监控机ip地址 检查 /etc/hosts.allow文件中监控机ip地址nrpe:192.168.1.91 检查 iptables   开放 5666端口      1. iptables -L    2. iptables -A RH-Firewall-1-INPUT -p tcp -m state --state NEW -m tcp --dport 5666 -j ACCEPT    3. #注意顺序    4. iptables -L    5. service iptables save    6. service iptables restart  

转载于:https://blog.51cto.com/renguanghui/1174556