Chinaunix首页 | 论坛 | 博客
  • 博客访问: 44585
  • 博文数量: 22
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 260
  • 用 户 组: 普通用户
  • 注册时间: 2013-05-24 10:51
文章存档

2013年(22)

我的朋友

分类: 系统运维

2013-05-24 11:38:11

 

Nagios搭建与配置详解

系统平台:

系统版本详细信息:

Red Hat Enterprise Linux Server release 5.5 (Tikanga)

内核版本信息:Linux 2.6.18-194.el5

使用的软件包:

1httpd

2php

3nagios-3.2.3.tar.gz

4nagios-plugins-1.4.1.tar.gz

安装次序:1,2,3,4

安装步骤:

由于安装步骤比较常规,所以这里说一下各个包在安装时候注意的事项。

1,安装rpm

rpm -ivh httpd-* php-*

2,安装nagios

安装nagios前,需要新建nagios用户,否则安装时会报错。

useradd nagios

usermod -G nagios apache

编译前的配置 ./configure --prefix=/usr/local/nagios

编译:make all

安装: make install安装主要的程序、CGIHTML文件

       make install-commandmode 给外部命令访问nagios配置文件的权限

       make install-config    把配置文件的例子复制到nagios的安装目录

       make install-init        nagios做成一个运行脚本,使nagios随系统开机启动

       make install-webconf    创建naigos网页配置文件

验证: 切换目录到安装路径(这里是/usr/local/nagios,看是否存在 etcbin sbin share var这五个目录,如果存在则可以表明程序被正确的安装到系统了。后表是五个目录功能的简要说明:

bin

Nagios执行程序所在目录,这个目录只有一个文件nagios

etc

Nagios配置文件位置,初始安装完后,只有几个*.cfg-sample文件

sbin

Nagios Cgi文件所在目录,也就是执行外部命令所需文件所在的目录

Share

Nagios网页文件所在的目录

Var

Nagios日志文件、spid 等文件所在的目录

3,安装nagios插件

编译前的配置: ./configure --with-nagios-user=nagios --with-nagios-group=nagios --prefix=/usr/local/nagios &&make && make install

验证:cd /usr/local/nagios/libexec ; ls

chown nagios.nagios /usr/local/nagios

chown -R nagios.nagios /usr/local/nagios/libexec

配置过程指定的安装路径是/usr/local/nagios,而不是/usr/local/nagios-plus,安装完成后,将在目录/usr/local/nagios生成目录libexec(里面有很多文件),这正是nagios所需要的。

监控软件nagios-扩展

NRPE功能和意义

基本的Nagios 监测服务只能是本地系统监测以及对远程主机的连通性监测。为了使Nagios的监测服务器能够远程对被监测主机系统上的信息进行获取,比如远程系统上的进程数、磁盘空间使用状况、所运行的服务等等这些必须要登录远程主机系统上才能了解的信息的话,就必须要依靠NRPE这个核心扩展插件程序,NRPE作为中间的代理程序,扮演着一手接受着Nagios监测服务器发来的请求,另一手在远程主机系统上获取指定的信息的中间人角色。

NRPE工作方式

如以上提到的实现Nagios对远程系统的监测,那么首先必须要在被监测的远程主机上除了安装Nagios-plugins插件程序之外还必须安装NRPE 核心扩展插件程序,并将NRPE在被监测的远程主机系统上以守护进程的方式运行起来,开放指定的NRPE监听端口监听着Nagios监测服务器发送过来的所有监测请求。另外,在Nagios监测服务器上,在必须要安装Nagios-plugins插件程序和NRPE核心扩展插件程序。唯一不同的是, Nagios监测服务器不需要将NRPE作为守护进程运行着,因为它本身一般不需要被别人监测着而是去监测别人,对Nagios监测服务器而言,它只需要使用到Nagio-plugins插件程序和NRPE扩展插件程序就足够了

安装nrpe:

nrpe的安装非常简单,只需要执行常规的安装步骤就可以安装成功。

./configure

make all

make install-plugin

make install-daemon

make install-daemon-config

make install-xinetd

相关文件的修改

service xinetd restart

netstat -at | grep nrpe

tcp 0      0 *:nrpe *:*                         LISTEN

配置步骤:

一,配置apache

1编写测试php网页:

vi /var/www/html/test.php

    phpinfo();       ?>

测试: 看能否出来php说明文字

如果没有成功则配置apache支持.php网页

编辑/etc/httpd/conf/httpd.conf

添加AddType application/x-httpd-php        .php

添加AddType application/x-httpd-php-source     .phps

DirectoryIndex字样修改为:

DirectoryIndex index.html index.php index.htm

2,配置apache虚拟主机

查看/etc/httpd/conf.d/nagios.conf文件,如果文件存在则无需编辑,否则进行如下编辑   

#setting for nagios

ScriptAlias /nagios/cgi-bin /usr/local/nagios/sbin

     //Cgi文件所在目录

    AuthType Basic

    Options ExecCGI

    AllowOverride None

    Order allow,deny

    Allow from all

    AuthName "Nagios Access"

    AuthUserFile /usr/local/nagios/etc/htpasswd  //验证文件路径

    Require valid-user

Alias /nagios /usr/local/nagios/share

   //nagios页面文件目录

    AuthType Basic

    Options None

    AllowOverride None

    Order allow,deny

    Allow from all

    AuthName "nagios Access"

    AuthUserFile /usr/local/nagios/etc/htpasswd  //验证文件路径

    Require valid-user

生成用户验证文件及web合法访问用户nagiosadmin,执行命令:

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

如要添加更多的用户,不要使用“-c”,否则会覆盖所有生成的行。

重启apache

/usr/local/apache/bin/apachectl restart

启动nagios

/etc/init.d/nagios start

此时就可以访问nagios了。

没有问题的话,进行nagios进一步的配置。

二,配置nagios

因为/usr/local/nagios-plus没有关于内存,cpuuptime的监控,所以需要将脚本拷贝的nagios-plus目录下,并设置相应的配置文件。

1)编辑/usr/local/nagios/etc/nrpe.cfg添加

command[check_mem]=/usr/local/nagios/libexec/check_mem -w 20 -c 15

command[check_cpu.sh]=/usr/local/nagios/libexec/check_cpu.sh -w 20 -c 15

command[check_uptime.sh]=/usr/local/nagios/libexec/check_uptime.sh -w 20 -c 15

2) 编辑/usr/local/nagios/etc/objects/commands.cfg

#nrpe set

define command{

command_name check_nrpe    #定义的命令名就叫作check_nrpe  

command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$

}

#说明:

#$USER1$/check_nrpe会通过引用resource.cfg里的函数$USER1$获得#/usr/local/nagios/libexec/check_nrpe这个绝对路径。

#-H $HOSTADDRESS$ 用来获得指定被监测主机的IP地址,$HOSTADDRESS$变量会

#通过定义主机名查找到host段中的IP地址。

#-c $ARG1$ 用来指定被监测主机上NRPE守护进程运行着的NRPE命令名。

define command{

command_name check_mem

command_line $USER1$/check_mem -w $ARG1$ -c $ARG2$

}

define command{

command_name check_cpu.sh

command_line $USER1$/check_cpu.sh -w $ARG1$ -c $ARG2$

}

define command{

command_name check_uptime.sh

command_line $USER1$/check_uptime.sh -w $ARG1$ -c $ARG2$

修改nagios主配文件/usr/local/nagios/etc/nagios.cfg

反注释cfg_file=/usr/local/nagios/etc/objects/host.cfg        此条指定nagios的配置文件路径。

配置host.cfg文件

define host{

        host_name Nagios-Server

        alias Nagios Server

        address 127.0.0.1

        check_command check-host-alive

        check_interval 5

        max_check_attempts 5

        retry_interval 1

        check_period 24x7

        process_perf_data 0

        retain_nonstatus_information 0

        contact_groups admins

        notification_interval 30

        notification_period 24x7

        notification_options d,u,r

}

define host{

        host_name Cacti-Server

        alias Cacti Server

        address 10.2.5.130

        check_command check-host-alive

        check_interval 5

        max_check_attempts 5

        retry_interval 1

        check_period 24x7

        process_perf_data 0

        retain_nonstatus_information 0

        contact_groups admins

        notification_interval 30

        notification_period 24x7

        notification_options d,u,r

}

配置services.cfg文件,监控的项目有:查看主机存活(网络ping),ssh服务,内存使用量,硬盘或目录使用率,当前用户登录数,cpu使用率,系统负载,系统运行时间,进程数等

若有多个主机,复制此配置文件只将host_name更改一下即可。

define service {

host_name Nagios-Server

service_description check-host-alive

check_period 24x7

max_check_attempts 4

normal_check_interval 3

retry_check_interval 2

contact_groups admins

notification_interval 10

notification_period 24x7

notification_options w,u,c,r

check_command check-host-alive

}

define service {

host_name Nagios-Server

service_description check-mem

check_period 24x7

max_check_attempts 4

normal_check_interval 3

retry_check_interval 2

contact_groups admins

notification_interval 10

notification_period 24x7

notification_options w,u,c,r

check_command check_nrpe!check_mem

}

define service {

host_name Nagios-Server

service_description check-disk

check_period 24x7

max_check_attempts 4

normal_check_interval 3

retry_check_interval 2

contact_groups admins

notification_interval 10

notification_period 24x7

notification_options w,u,c,r

check_command check_nrpe!check_sda3

}

define service {

host_name Nagios-Server

service_description check-users

check_period 24x7

max_check_attempts 4

normal_check_interval 3

retry_check_interval 2

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 Nagios-Server

service_description check-load

check_period 24x7

max_check_attempts 4

normal_check_interval 3

retry_check_interval 2

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 Nagios-Server

service_description check-cpu

check_period 24x7

max_check_attempts 4

normal_check_interval 3

retry_check_interval 2

contact_groups admins

notification_interval 10

notification_period 24x7

notification_options w,u,c,r

check_command check_nrpe!check_cpu.sh

}

define service {

host_name Nagios-Server

service_description check-uptime

check_period 24x7

max_check_attempts 4

normal_check_interval 3

retry_check_interval 2

contact_groups admins

notification_interval 10

notification_period 24x7

notification_options w,u,c,r

check_command check_nrpe!check_uptime.sh

}

define service {

host_name Nagios-Server

service_description check-total-procs

check_period 24x7

max_check_attempts 4

normal_check_interval 3

retry_check_interval 2

contact_groups admins

notification_interval 10

notification_period 24x7

notification_options w,u,c,r

check_command check_nrpe!check_total_procs

}

define service{

        use                  local-service         ; Name of service template to use

        host_name                 Nagios-Server

        service_description             SSH

        check_command               check_ssh

        notifications_enabled           0

        }

配置文件检查命令:

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

重启nagios服务:

/etc/init.d/nagios restart

/etc/init.d/httpd restart

验证:

再次打开   确认配置已经生效。

配置服务开机启动

chkconfig --add nagios

chkconfig nagios on

chkconfig --level 35 httpd on

注:用nagios监控windows机器,需要在被监控的机器上安装NSClient软件。

被监控端的安装配置

首先为被监控端准备nagios-pluginsnrpe的安装包。

添加用户nagios

usradd    nagios

我的安装包放在/usr目录下面;

cd   /usr

tar xzvf nagios-plugins-XXX.tar.gz

cd nagios-plugins-XXX

./configure --prefix=/usr/local/nagios

make

make install

./configure --with-nagios-user=nagios --with-nagios-group=nagios --prefix=/usr/local/nagios &&make && make install

 chown nagios.nagios /usr/local/nagios

 chown -R nagios.nagios /usr/local/nagios/libexec

再在被监控主机上安装NRPE扩展插件程序:

tar -zxvf nrpe-xxx.tar.gz

cd nrpe-xxx

./configure

make all

make install-plugin

make install-daemon

make install-daemon-config

make install-xinetd

 相关文件的修改

vi   /etc/xinetd.d/nrpe

only_from       = 127.0.0.1 10.2.5.130

(注意,10.2.5.130监控服务器的IP,也就是所允许10.2.5.130来监控本机的服务)

vi    /etc/services

nrpe          5666/tcp             # NRPE(添加一行)

service xinetd restart

 

如果配置没有问题的话,输入如下命令。查看配置是否配好

netstat -at | grep nrpe

tcp 0      0 *:nrpe *:*                         LISTEN

找到NRPE运行的执行程序

ll /usr/local/nagios/bin/nrpe

-rwxrwxr-x 1 nagios nagios 93420 Oct  7 01:16 /usr/local/nagios/bin/nrpe

 

了解NRPE的执行程序的使用方式

/usr/local/nagios/bin/nrpe -h

测试NEPE本地是否正常启动

/usr/local/nagios/libexec/check_nrpe H  localhost

如果正确的话,回返回NRPE的版本号

NRPE运行添加加入系统启动脚本中

echo "/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d" >> /etc/rc.d/rc.local

到此,被监控主机要做的工作已经完成了。

阅读(646) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~