Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2111937
  • 博文数量: 317
  • 博客积分: 5670
  • 博客等级: 大校
  • 技术积分: 3677
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-10 17:51
文章分类

全部博文(317)

文章存档

2016年(2)

2015年(44)

2014年(68)

2013年(42)

2012年(23)

2011年(51)

2010年(67)

2009年(17)

2008年(3)

分类:

2009-07-17 14:12:59

版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://jasonyong.blog.51cto.com/47753/159584
本文档参考资料:http://blog.sina.com.cn/s/blog_4e424e210100b6vi.html~type=v5_one&label=rela_prevarticle

[root@station206 ~]# uname -a
Linux station206.example.com 2.6.18-53.el5 #1 SMP Wed Oct 10 16:34:02  EDT 2007 i686 i686 i386 GNU/Linux

第一件事,把selinux改成默许模式。
[root@station203 ~]# setenforce 0
[root@station206 ~]# setenforce 0

安装nagios参考文档:源代码包中的nagios-***/docs/quickstart-fedora.html
安装nagios前,需要这些东西支持:
    * Apache
    * GCC compiler
    * GD development libraries

如果有yum源,可以用下面的指令:
yum install httpd
yum install gcc
yum install glibc glibc-common
yum install gd gd-devel

[root@station206 Server]# rpm -qa | grep -E "zlib-|gd-|freetype- |fontconfig-|libxml|libpng-|libjpeg-"
libjpeg-6b-37
gd-2.0.33-9.3.fc6
zlib-1.2.3-3
libxml2-2.6.26-2.1.2
libxml2-python-2.6.26-2.1.2
libpng-1.2.10-7.0.2
freetype-2.2.1-19.el5
fontconfig-2.4.1-6.el5
sysklogd-1.4.1-40.el5
##我的系统确认这些包都装上了

[root@station206 Server]# rpm -ivh httpd-2.2.3-11.el5.i386.rpm
## 把apache安装上。

开始安装nagios:
下载地址: 3.0.5.tar.gz
1.4.11.tar.gz
http://ncu.dl.sourceforge.net/sourceforge/nagios/nrpe-2.12.tar.gz
%2B%2B-0.3.6-RC9-Win32.msi

## Nagios,Nagios-plugins,nrpe安装在监控服务器上。
## nrpe安装在Linux/Unix被监控端。
## nsclient++安装在Windows被监控端。

[root@station206 Server]# useradd nagios
[root@station206 Server]# echo 123456 | passwd --stdin nagios
创建nagios用户,密码是123456

[root@station206 nagios-3.0.5]# /usr/sbin/groupadd nagcmd
[root@station206 nagios-3.0.5]# /usr/sbin/usermod -a -G nagcmd nagios
[root@station206 nagios-3.0.5]# /usr/sbin/usermod -a -G nagcmd apache
## daemon为运行apache的帐号。

[root@station206 ~]# tar zxvf nagios-3.0.5.tar.gz
[root@station206 ~]# cd nagios-3.0.5
[root@station206 nagios-3.0.5]# ./configure --with-command- group=nagcmd

[root@station206 nagios-3.0.5]# make all
## 编译主程序和CGI

[root@station206 nagios-3.0.5]# make install
## 安装主程序

[root@station206 nagios-3.0.5]# make install-init && make install- commandmode && make install-config
## 安装启动脚本 设置目录权限 安装配置文件

[root@station206 nagios-3.0.5]# vim /usr/local/nagios/etc/nagios.cfg
## 把配置文件修改成自己喜欢的

nagios目录功能的简要说明:
bin    Nagios执行程序所在目录,nagios文件即为主程序
etc    Nagios配置文件位置
sbin    Nagios Cgi文件所在目录,也就是执行外部命令所需文件所在的目录
Share    Nagios网页文件所在的目录
var    Nagios日志文件、spid 等文件所在的目录
var/archives    日志归档目录
var/rw    用来存放外部命令文件

[root@station206 nagios-3.0.5]# make install-webconf
## 安装一个nagios的apache配置文件

[root@station206 nagios-3.0.5]# htpasswd -c  /usr/local/nagios/etc/htpasswd.users nagios
New password:
Re-type new password:
Adding password for user nagios
## 添加一个nagios页面的用户

[root@station206 nagios-3.0.5]# service httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd:                                            [  OK  ]
## 重启apache

## 现在可以测试了。在浏览器中打开
## 输入用户名:nagios   密码:123456(上面刚设置的)。
## 现在应该可以看到nagios的界面了。


## 接下来安装nagios-plugins
[root@station206 ~]# tar zxvf nagios-plugins-1.4.11.tar.gz

[root@station206 nagios-plugins-1.4.11]# cd nagios-plugins-1.4.11    
[root@station206 nagios-plugins-1.4.11]# ./configure --with-nagios- user=nagios --with-nagios-group=nagios
[root@station206 nagios-plugins-1.4.11]# make
[root@station206 nagios-plugins-1.4.11]# make install
[root@station206 nagios-plugins-1.4.11]# chkconfig --add nagios
[root@station206 nagios-plugins-1.4.11]# chkconfig nagios on

[root@station206 nagios-plugins-1.4.11]# /usr/local/nagios/bin/nagios  -v /usr/local/nagios/etc/nagios.cfg
## 校验配置文件是不是有错误

[root@station206 nagios-plugins-1.4.11]# service nagios start
Starting nagios: done.

[root@station206 nagios-plugins-1.4.11]# setenforce 0
## 试验环境,把selinux改成默许模式。

## 如果不改就执行下面的指令吧:
# chcon -R -t httpd_sys_content_t /usr/local/nagios/sbin/
# chcon -R -t httpd_sys_content_t /usr/local/nagios/share/

## 如果开启防火墙,应该允许访问apache(一般为80端口)并允许nagios去抓 取被监控机信息(一般nrpe为5666端口)


## 在要监控的机器上安装nrpe
[root@station206 ~]# tar zxvf nrpe-2.12.tar.gz
[root@station206 ~]# cd nrpe-2.12
[root@station206 nrpe-2.12]# ./configure
## 报错了。。。错误信息如下。
checking for SSL headers... configure: error: Cannot find ssl headers

解决办法:
[root@station206 nrpe-2.12]# yum install openssl-devel
  Installing: keyutils-libs-devel          #########################  [1/7]
  Installing: libsepol-devel               #########################  [2/7]
  Installing: libselinux-devel             #########################  [3/7]
  Installing: e2fsprogs-devel              #########################  [4/7]
  Installing: krb5-devel                   #########################  [5/7]
  Installing: zlib-devel                   #########################  [6/7]
  Installing: openssl-devel                #########################  [7/7]

[root@station206 nrpe-2.12]# ./configure
## 通过了

[root@station206 nrpe-2.12]# make all

[root@station206 nrpe-2.12]# make install-plugin

nagios web界面提示
It appears as though you do not have permission to view information for any of the services you requested...

解决方法:
[root@station206 ~]# vim /usr/local/nagios/etc/cgi.cfg
use_authentication=1
## 改成use_authentication=0

## 到被监控机器上
## 安装nagios-plugins和nrpe
## NRPE总共由两部分组成:
## check_nrpe插件,运行在监控主机上。
## NRPE daemon,运行在远程的linux主机上(通常就是被监控机)
[root@station203 ~]# tar zxvf nagios-plugins-1.4.11.tar.gz
[root@station203 ~]# cd nagios-plugins-1.4.11    
[root@station203 nagios-plugins-1.4.11]# ./configure     
[root@station203 nagios-plugins-1.4.11]# make
[root@station203 nagios-plugins-1.4.11]# make install
[root@station203 nagios-plugins-1.4.11]# chown nagios.nagios  /usr/local/nagios/
[root@station203 nagios-plugins-1.4.11]# chown -R nagios.nagios  /usr/local/nagios/libexec/

[root@station203 ~]# tar zxvf nrpe-2.12.tar.gz
[root@station203 ~]# cd nrpe-2.12
[root@station203 ~]# ./configure
## 同样报错checking for SSL headers... configure: error: Cannot find  ssl headers
同样解决办法:
[root@station206 nrpe-2.12]# yum -y install openssl-devel
[root@station203 ~]# ./configure
[root@station203 ~]# make all
[root@station203 ~]# make install-daemon
## 安装nrpe的主程序
[root@station203 ~]# make install-daemon-config
## 安装nrpe的配置文件

[root@station203 nrpe-2.12]# vim /usr/local/nagios/etc/nrpe.cfg
## 修改配置文件,允许被哪些服务器监控
allowed_hosts=127.0.0.1,192.168.1.206

启动nrpe有两种方法:

方法一:


## 在/etc/services中添加下面行:
nrpe            5666/tcp        # NRPE

[root@station203 nrpe-2.12]# yum install xinet

[root@station203 nrpe-2.12]# vim /etc/xinetd.d/nrpe
## 内容如下
        # default: on
        # description: NRPE
        service nrpe
        {
                flags           = REUSE
                socket_type     = stream
                wait            = no
                user            = 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       = 192.168.1.206 <后面还可以加其他的IP>
        }

[root@station203 nrpe-2.12]# service xinetd restart    
[root@station203 nrpe-2.12]# netstat -an | grep 5666
tcp        0      0 0.0.0.0:5666                0.0.0.0:*                    LISTEN      

[root@station203 nrpe-2.12]# echo "192.168.1.206" >> /etc/hosts.allow     
## 为了避免被TCP Wrappers拒绝,要加上这句。
[root@station203 nrpe-2.12]# chkconfig xinetd on


方法二:
[root@station203 nrpe-2.12]# /usr/local/nagios/bin/nrpe -c  /usr/local/nagios/etc/nrpe.cfg -d
## 如果被监控端部加-n,监控端也不能加-n,-n意思是不使用ssl。-d 是以独 立的进程运行。

[root@station203 nrpe-2.12]# netstat -an | grep 5666
tcp        0      0 0.0.0.0:5666                0.0.0.0:*                    LISTEN      

[root@station203 nrpe-2.12]# echo "/usr/local/nagios/bin/nrpe -c  /usr/local/nagios/etc/nrpe.cfg -d" >> /etc/rc.local
[root@station203 nrpe-2.12]# echo "192.168.1.206" >> /etc/hosts.allow
## 为了避免被TCP Wrappers拒绝,要加上这句。

[root@station203 nrpe-2.12]# vim /usr/local/nagios/etc/nrpe.cfg
# 监控交换分区的使用情况,使用超过20%时为警告状态,超过10%时为严重 状态
command[check_swap]=/usr/local/nagios/libexec/check_swap -w 20% -c 10%
# 监控根分区磁盘使用情况
command[check_disk_root]=/usr/local/nagios/libexec/check_disk -w 20%  -c 10% -p /

现在可以到监控服务器上测试:
[root@station206 objects]#  /usr/local/nagios/libexec/check_nrpe -H  192.168.1.203
NRPE v2.12
## 能够看到版本号,就说明OK(如果被监控服务器运行nrpe时用到了-n,这里 也要有-n参数)
## 不然就会报类似CHECK_NRPE: Error - Could not complete SSL handshake. 这样的错误

测试OK,就可以设置要监控的内容了。

## 修改nagios的主配置文件:
[root@station206 objects]# vim /usr/local/nagios/etc/nagios.cfg
interval_length=60            ## 间隔时间基准由 60s 改为 1s
command_check_interval=10         ## 命令检查时间间隔,-1 表示尽 可能频繁的进行检查
date_format=iso8601             ## 日期格式
## 其他的根据自己喜好来改。

[root@station206 objects]# service nagios restart   

nagios的配置文件:
/usr/local/nagios/etc/contacts.cfg     ## 用来定义联系人联系人组
/usr/local/nagios/etc/cgi.cfg        ## 控制cgi访问的配置文件
/usr/local/nagios/etc/nagios.cfg    ## Nagios主配置文件
/usr/local/nagios/etc/resource.cfg    ## resource.cfg定义了一些变量 ,以便被其他文件引用,如$USER1$
/usr/local/nagios/etc/objects        ## objects是一个目录,用于定义 Nagios对象
/usr/local/nagios/etc/servers        ## 自己创建一个目录存放要监控的服务器的配置文件(需要在nagios.cfg中引用下)

/usr/local/nagios/etc/objects        
/usr/local/nagios/etc/objects/commands.cfg    ## 命令定义配置文件, 里面定义的命令可以被其他文件引用
/usr/local/nagios/etc/objects/contacts.cfg    ## 联系人和联系人组配 置文件
/usr/local/nagios/etc/objects/localhost.cfg    ## 监控本地机器的配置 文件
/usr/local/nagios/etc/objects/printer.cfg    ## 监控打印机的一个事 例配置文件(默认未启用)
/usr/local/nagios/etc/objects/switch.cfg    ## 监控路由器的一个事 例配置文件(默认未启用)
/usr/local/nagios/etc/objects/templates.cfg    ## 模板配置文件,在此 可以定义模板,在其他文件中引用
/usr/local/nagios/etc/objects/timeperiods.cfg    ## 定义监控时间段的配 置文件
/usr/local/nagios/etc/objects/windows.cfg    ## 监控Windows的一个事 例配置文件(默认未启用)

## /usr/local/nagios/libexec下的脚本命令的使用发法可以通过-h参数查看

## 以下配置部分是引用http://blog.sina.com.cn/s/blog_4e424e210100b6vj.html

主机监控的配置:
   1. define host {
   2. host_name host_name    ; 简短的主机名称。
   3. alias alias    ; 别名,可以更详细的说明主机。
   4. address address    ; IP 地址,也可以写主机名。如果不定义这个值,  nagio 将会用 host_name 去寻找主机。
   5. parents host_names    ; 上一节点的名称,也就是指从 nagios 服务器 到被监控主机之间经过的节点,可以是路由器、交换机、主机等等。
   6. hostgroups hostgroup_names    ; 简短的主机组名称。
   7. check_command command_name    ; 检查命令的简短名称,如果此项留空 , nagios 将不会去判断主机是否 alive 。
   8. max_check_attempts 整数    ; 当检查命令的返回值不是 "OK" 时,重 试的次数。
   9. check_interval 数字    ; 循环检查的间隔时间。
  10. active_checks_enabled [0/1]    ; 是否启用 "active_checks"
  11. passive_checks_enabled [0/1]    ; 是否启用 "passive_checks" ,及 “被动检查”
  12. check_period timeperiod_name    ; 检测时间段简短名称,这只是个名 称,具体的时间段要写在其他的配置文件中。
  13. obsess_over_host [0/1]    ; 是否启用主机操作系统探测。
  14. check_freshness [0/1]    ; 是否启用 freshness 检查。freshness 检 查是对于启用被动检查模式的主机而言的,其作用是定期检查主机报告的状态信 息,如果

该状态信息已经过期,freshness 将会强制做主机检查。
  15. freshness_threshold 数字     ; fressness 的临界值,单位为秒。 如 果定义为 "0" ,则为自动定义。
  16. event_handler command_name    ; 当主机发生状态改变时,采用的处理 命令的简短的名字(可以在 commands.cfg 中对其定义)
  17. event_handler_enabled [0/1]    ; 是否启用 event_handler
  18. low_flap_threshold 数字    ; 抖动的下限值。抖动,即在一段时间内 ,主机(或服务)的状态值频繁的发生变化。
  19. high_flap_threshold 数字   ; 抖动的上限值。
  20. flap_detection_enabled [0/1]    ; 是否启用抖动检查。
  21. process_perf_data [0/1]    ; 是否启用 processing of performance  data
  22. retain_status_information [0/1]    ; 程序重启时,是否保持主机状 态相关的信息。
  23. retain_nonstatus_information [0/1]    ; 程序重启时,是否保持主机 状态无关的信息。
  24. contact_groups contact_groups    ; 联系人组,在此组中的联系人都 会收到主机的提醒信息。
  25. notification_interval 整数    ; 重复发送提醒信息的最短间隔时间。 默认间隔时间是 "60" 分钟。如果这个值设置为 "0" ,将不会发送重复提醒。
  26. notification_period timeperiod_name   ; 发送提醒的时间段。非常重 要的主机(服务)定义为 24x7 ,一般的主机(服务)就定义为上班时间。如果 不在定义

的时间段内,无论发生什么问题,都不会发送提醒。
  27. notification_options [d,u,r,f]    ; 发送提醒包括的情况: d = 状 态为 DOWN , u = 状态为 UNREACHABLE , r = 状态恢复为 OK , f = flapping
  28. notifications_enabled [0/1]    ; 是否开启提醒功能。"1" 为开 启,"0" 为禁用。一般,这个选项会在主配置文件 (nagios.cfg) 中定义,效果 相同。
  29. stalking_options [o,d,u]    ; 持续状态检测参数,o = 持续的 UP 状 态 , d = 持续的 DOWN 状态 , u = 持续的 UNREACHABLE 状态
  30. }


配置文件是怎样引用的?

用nagios主要是监控一台主机的各种信息,包括本机资源以及对外的服务等等.这 些在nagios里面都是被定义为一个个的项目(nagios称之为服务,为了与主机提供 的服务相

区别,我这里用项目这个词),而实现每个监控项目,则需要通过 commands.cfg文件中定义的命令。
为了不必重复定义一些项目,Nagios引入了一个模板配置文件(templates.cfg ),将一些共性的属性定义成模板,以便于多次引用。
我们现在有一个监控项目是监控一台机器的web服务是否正常, 我们需要哪些元 素呢?最重要的有下面三点:首先是监控哪台机器,然后是这个监控要用什么命令 实现,最后就

是出了问题的时候要通知哪个联系人。
我们首先应该在commands.cfg中定义监控远程服务和资源的命令,以及如何发送 邮件的命令。大部分监控远程服务和资源的命令的命令通 过/usr/local/nagios/libexec下

的脚本实现,如ping命令为check_ping。

--------------------------------------------------------------------- --------------------
然后我们在contacts.cfg文件中定义联系人和联系人组,在timeperiods.cfg中 定义监控时间段。最后我们在服务器监控配置文件中引用前面定义的元素来监控 服务器状态。

=================================== =================================== ==================
下面引用配置文件中部分配置做说明:
vi /usr/local/nagios/etc/resource.cfg
# 定义$USER1$变量,设置插件路径
$USER1$=/usr/local/nagios/libexec

vi /usr/local/nagios/etc/objects/commands.cfg
# 定义check-host-alive命令
define command{
        command_name    check-host-alive  # 命令名称
        command_line    $USER1$/check_ping -H $HOSTADDRESS$ -w  3000.0,80% -c 5000.0,100% -p 5
        }
# 上面的$USER1$和$HOSTADDRESS$引用自已定义的配置文件。变量不需现定义 才能被引用。
###################################################################### ##
#
# 2008.11.18 add by Stone
# NRPE COMMAND
# 自己定义check_nrpe命令,此命令后接必需接一个参数,用于告诉远程服务器 上的NRPE daemon需要监控的内容,如check_swap参数为监控远程机器的交换分 区。
###################################################################### ##
# 'check_nrpe ' command definition
define command{
        command_name check_nrpe
        command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
        }
## $HOSTADDRESS$和$ARG1$是nagios默认定义的宏。

vi /usr/local/nagios/etc/objects/contacts.cfg
# 定义联系人
define contact{
        contact_name                    nagiosadmin             ;  Short name of user
        use                             generic-contact         ;  Inherit default values from generic-contact template (defined above)
        alias                           Nagios Admin            ; Full  name of user

        email                           test@gmaile.com         ;  <<***** CHANGE THIS TO YOUR EMAIL ADDRESS ******
        }
# 上面的generic-contact在templates.cfg中定义。
# 定义联系人组
define contactgroup{
        contactgroup_name       admins
        alias                   Nagios Administrators
        members                 nagiosadmin   #在此可以加入多个联系人 ,中间用逗号隔开
        }

vi /usr/local/nagios/etc/objects/timeperiods.cfg
# 定义监控的时间段
define timeperiod{
        timeperiod_name 24x7   #监控所有时间段(7*24小时)
        alias           24 Hours A Day, 7 Days A Week
        sunday          00:00-24:00
        monday          00:00-24:00
        tuesday         00:00-24:00
        wednesday       00:00-24:00
        thursday        00:00-24:00
        friday          00:00-24:00
        saturday        00:00-24:00
        }

vi /usr/local/nagios/etc/objects/templates.cfg
# 定义generic-contact联系人模板,并非真正的联系人,真正的联系人在 contacts.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!
        }
--------------------------------------------------------------------- ---------------------------------------------
service_notification_period     24x7
服务出了状况通知的时间段,这个时间段就是上面在timeperiods.cfg中定义的.
host_notification_period        24x7
主机出了状况通知的时间段, 这个时间段就是上面在timeperiods.cfg中定义的
service_notification_options    w,u,c,r
当服务出现w—报警(warning),u—未知(unkown),c—严重(critical),或者r—从 异常情况恢复正常,在这四种情况下通知联系人.
host_notification_options       d,u,r
当主机出现d????—当机(down),u—返回不可达(unreachable),r—从异常情况恢 复正常,在这3种情况下通知联系人
service_notification_commands   notify-service-by-email
服务出问题通知采用的命令notify-service-by-email,这个命令是在 commands.cfg中定义的,作用是给联系人发邮件.
host_notification_commands      notify-host-by-email
同上,主机出问题时采用的也是发邮件的方式通知联系人
--------------------------------------------------------------------- ---------------------------------------------

# 定义generic-host主机模板
define host{
        name                            generic-host    ; The name of  this host template
        notifications_enabled           1               ; Host  notifications are enabled
        event_handler_enabled           1               ; Host event  handler is enabled
        flap_detection_enabled          1               ; Flap  detection is enabled
        failure_prediction_enabled      1               ; Failure  prediction is enabled
        process_perf_data               1               ; Process  performance data
        retain_status_information       1               ; Retain  status information across program restarts
        retain_nonstatus_information    1               ; Retain non- status information across program restarts
        notification_period             24x7            ; Send host  notifications at any time
        register                        0               ; DONT  REGISTER THIS DEFINITION - ITS NOT A REAL HOST, JUST A TEMPLATE!
        }

# 定义Linux主机模板
define host{
        name                            linux-server    ; The name of  this host template
        use                             generic-host    ; This  template inherits other values from the generic-host template
        check_period                    24x7            ; By default,  Linux hosts are checked round the clock
        check_interval                  5               ; Actively  check the host every 5 minutes
        retry_interval                  1               ; Schedule  host check retries at 1 minute intervals
        max_check_attempts              10              ; Check each  Linux host 10 times (max)
        check_command                   check-host-alive ; Default  command to check Linux hosts
        notification_period             workhours       ; Linux admins  hate to be woken up, so we only notify during the day
                                                        ; Note that  the notification_period variable is being overridden from
                                                        ; the value  that is inherited from the generic-host template!
        notification_interval           120             ; Resend  notifications every 2 hours
        notification_options            d,u,r           ; Only send  notifications for specific host states
        contact_groups                  admins          ;  Notifications get sent to the admins by default
        register                        0               ; DONT  REGISTER THIS DEFINITION - ITS NOT A REAL HOST, JUST A TEMPLATE!
        }

# 在nagios.cfg配置文件中开启对/usr/local/nagios/etc/servers/中配置文件的引用。
cfg_dir=/usr/local/nagios/etc/servers

# 远程Linux主机监控文件,如果监控多台主机只需简单复制修改即可。
#我们应该牢记wiki-l-11.cfg用到的命令在commands.cfg中定义,在 commands.cfg中定义的命令用到/usr/local/nagios/libexec下的插件(命令) 。
vi /usr/local/nagios/etc/servers/wiki-l-11.cfg
# 定义主机
define host{
        use                     linux-server            ; Name of host  template to use
                                                        ; This host  definition will inherit all variables that are defined
                                                        ; in (or  inherited by) the linux-server host template definition.
        host_name               wiki
        alias                   Docs
        address                 192.168.0.11
        }
# 定义Ping远程Linux主机
define service{
        use                             generic-service         ; Name  of service template to use
        host_name                       wiki
        service_description             PING
        check_command                   check_ping!100.0,20%!500.0,60%    ;check_ping命令在commands.cfg中定义,后跟两个参数,命令及参数间用!分 割。
        }
#检查远程Linux主机根分区使用情况,check_nrpe命令必须 在/usr/local/nagios/etc/objects/commands.cfg中定义(默认未定义)
define service{
        use                             generic-service         ; Name  of service template to use
        host_name                       wiki
        service_description             Root Partition
        check_command                   check_nrpe!check_disk_root
        }
# 检查远程Linux主机的登录人数
define service{
        use                             generic-service         ; Name  of service template to use
        host_name                       wiki
        service_description             Current Users
        check_command                   check_nrpe!check_users
        }
# 检查远程Linux的主机的负载
define service{
        use                             generic-service         ; Name  of service template to use
        host_name                       wiki
        service_description             Current Load
        check_command                   check_nrpe!check_load
        }
# 检查远程Linux主机swap分区使用情况
define service{
        use                             generic-service         ; Name  of service template to use
        host_name                       wiki
        service_description             Swap Usage
        check_command                   check_nrpe!check_swap
        }
# 检查远程Linux主机的SSH服务
define service{
        use                             generic-service         ; Name  of service template to use
        host_name                       wiki
        service_description             SSH
        check_command                   check_ssh
        notifications_enabled           0
        }
# 检查远程Linux主机的HTTP服务
define service{
        use                             generic-service         ; Name  of service template to use
        host_name                       wiki
        service_description             HTTP
        check_command                   check_http
        notifications_enabled           0
        }

vi /usr/local/nagios/etc/servers/hostgroup.cfg
# 定义主机组(localhost.cfg中有类似的主机组设置,我已将其注释掉,否则 可能会有冲突)
define hostgroup{
        hostgroup_name  linux-servers ; The name of the hostgroup
        alias           Linux Servers ; Long name of the group
        members         localhost,wiki     ; Comma separated list of  hosts that belong to this group
        }

#define hostgroup{
#        hostgroup_name  windows-servers ; The name of the hostgroup
#        alias           Windows Servers ; Long name of the group
#        members         print     ; Comma separated list of hosts  that belong to this group
#        }


# 完成监控主机配置文件的配置后使用下面命令检查配置文件的正确性:
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
# 确定无误后重启Nagios:
service nagios restart


再测试:
应该可以看到localhost和station203两台机器的状态。
localhost的http可以一直会出警告信息,如下:
HTTP WARNING: HTTP/1.1 403 Forbidden
这是应为nagios监控/var/www/html下面有没有index文件,手动建一个就可以了 。


下一步:
将信息保存到mysql中。
下载下面的ndoutils包。
http://ncu.dl.sourceforge.net/sourceforge/nagios/ndoutils-1.4b7.tar.gz

NDOUtils安装需求:
Nagios正确安装运行,版本>=2.0安装目录:/usr/local/nagios
MySQL正确安装并运行

[root@station206 Server]# yum -y install mysql-server
  Installing: perl-DBI                     #########################  [1/4]
  Installing: mysql                        #########################  [2/4]
  Installing: perl-DBD-MySQL               #########################  [3/4]
  Installing: mysql-server                 #########################  [4/4]
[root@station206 Server]# rpm -ivh mysql-devel-5.0.22-2.1.0.1.i386.rpm
## 要装mysql-devel,要不然ndoutils编译后,能够正常使用。这个东西害死我了。:(

[root@station206 ndoutils-1.4b7]# ./configure --enable-mysql --with-mysql-lib=/usr/lib/mysql/ --disable-pgsql
## 可以看到下面内容:
MySQL library and include file(s) were found!

[root@station206 ndoutils-1.4b7]# make

[root@station206 ndoutils-1.4b7]# cp ndomod-3x.o ndo2db-3x log2ndo file2sock /usr/local/nagios/bin

[root@station206 ndoutils-1.4b7]# cp ./config/* /usr/local/nagios/etc/

## 上面编译完成,没有出错,再cp这些文件,否则,重新编译后再cp覆盖。

然后初始化mysql:
[root@station206 ndoutils-1.4b7]# mysqladmin -u root password "******"
## 修改mysql root密码

[root@station206 ndoutils-1.4b7]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6 to server version: 5.0.22

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> grant select,insert,update,delete on *.* to nagios@"%" Identified by "nagios";            
Query OK, 0 rows affected (0.00 sec)
## 创建用户nagios,密码是nagios,他有select,insert,update,delete的权利

mysql> create database nagios;
Query OK, 1 row affected (0.00 sec)
## 创建nagios数据库

[root@station206 ndoutils-1.4b7]# cd db/
[root@station206 db]# ./installdb -u root -p ***** -d nagios
## 导入数据库表,-d 指定导入到哪个数据库


[root@station206 ndoutils-1.4b7]# vim /usr/local/nagios/etc/nagios.cfg
broker_module=/usr/local/nagios/bin/ndomod-3x.o config_file=/usr/local/nagios/et
c/ndomod.cfg
## 添加上面这行

[root@station206 ndoutils-1.4b7]# vim /usr/local/nagios/etc/ndo2db.cfg
## 编辑ndo2db的配置文件


[root@station206 nagios]# more etc/ndo2db.cfg | grep -v "#" | grep -v "^$"               
ndo2db_user=nagios
ndo2db_group=nagios
socket_type=unix
socket_name=/usr/local/nagios/var/ndo.sock
tcp_port=5668
db_servertype=mysql
db_host=localhost
db_port=3306
db_name=nagios
db_prefix=nagios_            ## 好像是指定以nagios开头的数据表
db_user=nagios
db_pass=nagios
max_timedevents_age=1440
max_systemcommands_age=10080
max_servicechecks_age=10080
max_hostchecks_age=10080
max_eventhandlers_age=44640
debug_level=0
debug_verbosity=1
debug_file=@localstatedir@/ndo2db.debug
max_debug_file_size=1000000
## 这个是我的


[root@station206 ndoutils-1.4b7]# /usr/local/nagios/bin/ndo2db-3x -c /usr/local/nagios/etc/ndo2db.cfg
## 启动ndo2db

[root@station206 ndoutils-1.4b7]# tail -f /var/log/message
ndomod: Successfully reconnected to data sink!
有上面的消息就说明OK


如何用NDOUtils将多个Nagios实例的性能数据写入一个数据库?

配置方法类似于上面的单实例单数据库模式,只是在装第二个实例时省去安装DB一步,并需要修改/usr/local/nagios/etc/ndomod.cfg中的实例名。
实例名默认为instance_name=default,如果有多个实例必需修改成不同的实例名。
如:instance_name=nagios1
注意修改/usr/local/nagios/etc/ndo2db.cfg中数据库连接的相关配置。

本文出自 “jasonyong” 博客,请务必保留此出处http://jasonyong.blog.51cto.com/47753/159584

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