---
title: "nagios的安装及配置实现网络监控"
author: "swvip"
date: "2011-03-08T17:37:40Z"
tags: []
categories: ["操作系统"]
slug: "nagios"
draft: false
lastmod: "2011-03-08T17:37:40Z"
id: 946693
url: "https://swvip.iteye.com/blog/946693"
---

Nagios通常由一个主程序(Nagios)、一个插件程序(Nagios-plugins)和四个可选的ADDON(NRPE、NSCA、 NSClient++和NDOUtils)组成。Nagios的监控工作都是通过插件实现的，因此，Nagios

和Nagios-plugins是服务器端工作所必须的组件。而四个ADDON中（1）NRPE：用来在监控的远程Linux/Unix主机上执行脚本插件以实现对这些主机资源的监控（2）NSCA：用来让 被监控的远

程Linux/Unix主机主动将监控信息发送给Nagios服务器(这在冗余监控模式中特别要用到)（3）NSClient++：用来监控 Windows主机时安装在Windows主机上的组件（4）NDOUtils：则用来将

Nagios的配置信息和各event产生的数据存入数据库，以实现 这些数据的快速检索和处理这四个ADDON(附件)中，NRPE和NSClient++工作于客户端，NDOUtils工作于服务器端，而NSCA则需要同

时安装在服务器端和客户端

[![](http://blog.chinaunix.net/attachment/201103/7/23914782_1299462373KPJF.jpg)](http://blog.chinaunix.net/link.php?url=http%3A%2F%2Fblog.chinaunix.net%2Fattachment%2F201103%2F7%2F23914782_1299462373KPJF.jpg)

安装前的准备工作

(1)解决安装Nagios的依赖关系：

Nagios基本组件的运行依赖于httpd、gcc和gd。可以通过以下命令来检查nagios所依赖的rpm包是否已经完全安装：  
\# yum -y install httpd gcc glibc glibc-common \*gd* php php-mysql mysql mysql-server

（2）所需要安装组件：

服务端所用的安装包为 nagios nagios-plugins ndoutils(非必须)  
linux客户端 nrpe  
windows客户端 NSClient++

 

（3）  
进行selinux设置，或者关闭selinux

 

开始搭建：  
(1)添加nagios运行所需要的用户和组：

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

把apache加入到nagcmd组，以便于在通过web Interface操作nagios时能够具有足够的权限：  
\# usermod -a -G nagcmd apache

 

 

2、编译安装nagios：

\# tar zxf nagios-3.1.2.tar.gz   
\# cd nagios-3.1.2  
\# ./configure --with-command-group=nagcmd --enable-event-broker

\# make all  
\# make install  
\# make install-init  
\# make install-config  
\# make install-commandmode

为email指定您想用来接收nagios警告信息的邮件地址，默认是本机的nagios用户:  
\# vi /usr/local/nagios/etc/objects/contacts.cfg   
email        [nagios@localhost](mailto:nagios@localhost)       #这个是默认设置

在httpd的配置文件目录(conf.d)中创建Nagios的Web程序配置文件：  
\# make install-webconf

创建一个登录nagios web程序的用户，这个用户帐号在以后通过web登录nagios认证时所用：  
\# htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin

以上过程配置结束以后需要重新启动httpd：  
\# service httpd restart

 

3、编译、安装nagios-plugins

nagios的所有监控工作都是通过插件完成的，因此，在启动nagios之前还需要为其安装官方提供的插件。  
\# tar zxf nagios-plugins-1.4.13.tar.gz   
\# cd nagios-plugins-1.4.13  
\# ./configure --with-nagios-user=nagios --with-nagios-group=nagios  
\# make  
\# make install

 

 

4、配置并启动Nagios

(1)把nagios添加为系统服务并将之加入到自动启动服务队列：  
\# chkconfig --add nagios  
\# chkconfig nagios on

(2)检查其主配置文件的语法是否正确：  
\# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg  
这里为了达到方便编辑~/.bashrc添加这样一行PATH=$PATH:/usr/local/nagios/bin/  
执行. ~/.bashrc  
做一下连接ln -sv /usr/local/nagios/etc/nagios.cfg /etc/nagios.cfg  
#nagiso -v /etc/nagios.cfg

 

(3)如果上面的语法检查没有问题，接下来就可以正式启动nagios服务了：  
\# service nagios start

(4)配置selinux  
如果您的系统开启了selinux服务，则默认为拒绝nagios web cgi程序的运行。您可以通过下面的命令来检查您的系统是否开启了selinux：  
#getenforce

如果上面命令的结果显示开启了selinux服务，您可以通过下面的命令暂时性的将其关闭：  
#setenforce 0

如果您想在以后完全关闭selinux，可以通过编辑/etc/sysconfig/selinux文件，将其中的selinux后面的值“force”修改为“disable”即可。

当然，您也可以通过以下方式将nagios的CGI程序运行于SELinux/targeted模式而不用关闭selinux：  
\# chcon -R -t httpd\_sys\_content\_t /usr/local/nagios/sbin  
\# chcon -R -t httpd\_sys\_content\_t /usr/local/nagios/share

(5)通过web界面查看nagios：  
[http://your\_nagios\_IP/nagios](http://blog.chinaunix.net/link.php?url=http%3A%2F%2Fyour_nagios_ip%2Fnagios)

登录时需要指定前面设定的web认证帐号和密码。

[![](http://blog.chinaunix.net/attachment/201103/7/23914782_1299462869do7k.png)](http://blog.chinaunix.net/link.php?url=http%3A%2F%2Fblog.chinaunix.net%2Fattachment%2F201103%2F7%2F23914782_1299462869do7k.png)[](http://blog.chinaunix.net/link.php?url=http%3A%2F%2Fblog.chinaunix.net%2Fattachment%2F201103%2F7%2F23914782_1299462869do7k.png)

 

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

 

windos端  
1.被监控端安装NSClient++-0.3.8-Win32.msi  
2.安装完成后修改配置文件NSC.ini把需要的库都打开

3.在监控服务器上修改nagios配置文件/usr/local/nagios/etc/nagios.cfg  
去掉注释cfg\_file=/usr/local/nagios/etc/objects/windows.cfg  
定义windows.cfg  
define host{  
        use             windows-server  ; Inherit default values from a template  
        host\_name       winserver       ; The name we're giving to this host  
        alias           My Windows Server       ; A longer name associated with the host  
        address         192.168.0.191   ; 你主机的IP  
        }

 

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

 

linux端

被监控端  
先添加nagios用户c  
useradd nagios  
为了安装nrpe，先安装nagios-plugins-1.4.15.tar.gz 插件  
\# tar zxf 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

tar -zxvf nrpe-2.12.tar.gz  
cd nrpe-2.12.tar.gz  
 cd nrpe-2.12  
./configure  (会自动加载SSL)  
#如果后面make报错,加如下参数  
./configure --enable-ssl --with-ssl-lib=/usr/lib/(当然前提要有openssl)  
make all  
make install-plugin  
make install-daemon  
make install-daemon-config

 

4.配置nrpe信息  
vim /usr/local/nagios/etc/nrpe.cfg  
allowed\_hosts=127.0.0.1  //允许监控的IP  
/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg –d

 

 

服务端  
3.安装NRPE  
cd nagios-nrpe\_2.8.1  
./configure   #默认自动添加了openssl  
#因为传送过程要加密,如果后面make报错,加如下参数  
rpm -qa| grep ssl  
openssl-devel-0.9.7a-43.17.el4\_6.1  
rpm -ql openssl-devel-0.9.7a-43.17.el4\_6.1 | more  
./configure --enable-ssl --with-ssl-lib=/lib/(当然前提要有openssl)  
make all  
make install-plugin

 

4.commands.cfg定义外部构件nrpe  
vi /usr/local/nagios/etc/objects/commands.cfg  
#添加  
#check nrpe  
define command{  
        command\_name check\_nrpe  
        command\_line $USER1$/check\_nrpe -H $HOSTADDRESS$ -c $ARG1$  
        }

 

定义 mylinux.cfg  
define host{  
           use             linux-server  
          host\_name     mylinux  
          alias            mylinux  
          address        192.168.10.10(客户端IP既被监控的IP)  
        }  
define service{  
        use                      generic-service  
        host\_name             mylinux  
        service\_description     check-swap  
        check\_command        check\_nrpe!check\_swap  
               }  
define service{  
        use                      generic-service  
        host\_name             mylinux  
        service\_description     check-load  
       check\_command         check\_nrpe!check\_load  
               }  
define service{  
        use                      generic-service  
        host\_name             mylinux  
       service\_description     check-disk  
       check\_command        check\_nrpe!check\_had1  
 }  
define service{  
        use                      generic-service  
        host\_name             mylinux  
       service\_description     check-users  
       check\_command        check\_nrpe!check\_users  
               }  
define service{  
        use                    generic-service  
        host\_name           mylinux  
        service\_description  otal\_procs  
        check\_command     check\_nrpe!check\_total\_procs  
}   
编辑nagios.cfg配置文件写入mylinux.cfg

 

基本已经完成

[![](http://blog.chinaunix.net/attachment/201103/7/23914782_1299462959339S.png)](http://blog.chinaunix.net/attachment/201103/7/23914782_1299462959339S.png)[](http://blog.chinaunix.net/link.php?url=http%3A%2F%2Fblog.chinaunix.net%2Fattachment%2F201103%2F7%2F23914782_1299462869do7k.png)

 

 

[![](http://blog.chinaunix.net/attachment/201103/7/23914782_1299462972Sc91.png)](http://blog.chinaunix.net/link.php?url=http%3A%2F%2Fblog.chinaunix.net%2Fattachment%2F201103%2F7%2F23914782_1299462972Sc91.png)

 

实现飞信报警功能：

飞信报警需要下载飞信的插件  
1.修改模板  
修改concont.cfg  
2\.  
cp \*.so /usr/bin/  
mv li* /usr/bin/  
修改  
 commands.cfg  
define command{  
        command\_name notify-service-by-fetion  
        command\_line      /usr/bin/fetion --mobile 150XXXXXXXX --pwd XXXXX --to 150XXXXXXXX --msg-tpye=0  --msg-utf8 $HOSTNAME

转载于[http://blog.chinaunix.net/space.php?uid=23914782&do=blog&id=158541](http://blog.chinaunix.net/space.php?uid=23914782&do=blog&id=158541)