hostgroup.cfg,servicegroup.cfg;nagios配置文件

....

http://www.lingang.gd.cn/?action=show&id=234

这里说说今天改配置文件时候发现的一些技巧,在监控大量服务器时会更省力一些

 

一。在nagios.cfg中加入

#groupfile
cfg_file=/usr/local/nagios/etc/objects/hostsgroup.cfg
cfg_file=/usr/local/nagios/etc/objects/groupservers.cfg
<pre>
 
二。监控组模板配置,这个模板写好以后,可以在需要时调用,非常的方便。
 
/nagios/etc/objectsgroupservers.cfg
 
<pre lang="CONFIG">
define service{
use local-service ; Name of service template to use
hostgroup_name Webserver
service_description PING
check_command check_ping!100.0,20%!500.0,60%
}
 
# Define a service to check SSH on the local machine.
# Disable notifications for this service by default, as not all users may have SSH enabled.
 
define service{
use local-service ; Name of service template to use
hostgroup_name Webserver
service_description SSH
check_command check_ssh
notifications_enabled 1
}
 
 
 
# Define a service to check HTTP on the local machine.
# Disable notifications for this service by default, as not all users may have HTTP enabled.
 
define service{
use local-service ; Name of service template to use
hostgroup_name Webserver
service_description HTTP
check_command check_http
# notifications_enabled 0
}
#Define a service to check squid on the local machine
define service{
use local-service ; Name of service template to use
hostgroup_name Webserver
service_description SQUID
check_command check_squid
}
 
#CPU Load
define service{
use local-service
hostgroup_name Webserver
service_description CPU Load
check_command check_nrpe!check_load
}
#User Login
define service{
use local-service
hostgroup_name Webserver
service_description Current user
check_command check_nrpe!check_users
}
#Total Processes
define service{
use local-service
hostgroup_name Webserver
service_description Total Processes
check_command check_nrpe!check_total_procs
}

三。调用。在此处调用的机器,在hosts.cfg中一定要声明过,否则nagios将无法启动

define hostgroup{
hostgroup_name Webserver
alias Webserver ;在web界面显示的名字
members web1
}



http://www.xinyanghome.com/?p=216

首先在nagios.cfg中添加:
cfg_file=/usr/local/nagios/etc/objects/servicegroup.cfg
在objects目录下建立servicegroup.cfg
 
内容为:
define servicegroup{
servicegroup_name       name
alias                   NA
members                 host,service,host2,service2,…………
}
注意:members的写法主机,服务,主机2,服务2这种写法
效果如下:
 

本文固定链接: http://www.xinyanghome.com/?p=216 | linux

 

http://blog.ixpub.net/html/66/13609566-235442.html

2、增加新的配置文件
先创建简单的配置文件timeperiods.cfg,其内容如下:

define timeperiod{
        timeperiod_name 24x7
        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
        }

这个文件的定义明晰易懂,不多做说明。另建议7X24小时监控。
第二个手动创建的配置文件是 contacts.cfg,其格式如下:

define contact {
        contact_name         sa    //不要有空格
        alias                system administrator
        service_notification_period    24x7
        host_notification_period       24x7
        service_notification_options   w,u,c,r
        host_notification_options       d,u,r
        service_notification_commands  service-notify-by-sms,service-
notify-by-email  //这个命令读配置文件miscommands.cfg
        host_notification_commands     host-notify-by-email,host-noti
fy-by-sms      //这个命令读配置文件miscommands.cfg
        email                          sery@163.com
        pager                          13333333333 //手机号,收报警短信
        }     //不要把这个符号写掉了

define contact {
        contact_name         sery
        alias                system administrator
        service_notification_period    24x7
        host_notification_period       24x7
        service_notification_options   w,u,c,r
        host_notification_options       d,u,r
        service_notification_commands  service-notify-by-sms,service-
notify-by-email
        host_notification_commands     host-notify-by-email,host-noti
fy-by-sms
        email                         sery@sohu.com
        pager                          13312345678
        }

上面的文件定义了2个联系人,如果有更多联系人的话,照这个格式在后面追加即可。服务通知选项(service_notification_options)与主机通知选项(host_notification_options)的几个选项在这里说明一下:w-warning , u-unknown,c-critical,r-recovery;d-down,u-unreachable,注意一下,主机报警和服务报警有些差异。
紧接着的第三个手动创建的配置文件是contactgroups.cfg文件,这个文件是依照上一个文件contacts.cfg来的,contactgroups文件相对简单一些,其格式如下:

define contactgroup {
        contactgroup_name    sagroup  //不要用空格
        alias                system administrator group
        members              sa,sery  //本例有2个成员
}

多个成员之间用逗号做分界符,如果有更多的联系组,就依相同的格式在文件中追加余下的组。
关键的角色终于登场,这就是配置文件hosts.cfg。下面是我定义的两个主机的基本样式:

#define monitor  host

#################################################################
# Wangjing IDC servers                                          #
#################################################################
define host {
       host_name                  nagios-server
       alias                      nagios server
       address                    61.x..x.49
       contact_groups             sagroup //多个联系组用逗号分隔,
数据来源于contactgroups.cfg check_command check-host-alive max_check_attempts 5 notification_interval 10 //值可调,大小什么值合适需自己测定 notification_period 24x7 notification_options d,u,r } define host { host_name 24-25 alias server 24-25 address 202.X.24.25 contact_groups sagroup check_command check-host-alive //down机就发报警通知 max_check_attempts 5 notification_interval 10 notification_period 24x7 notification_options d,u,r }

更多的主机依此格式逐个追加进来。小技巧,如果是连续的ip段,最好自己写个脚本生成hosts.cfg文件,为了以后维护方便,尽可能在文件中使用易读的注释(如本例# Wangjing IDC servers #)。
再一个重量级的配置文件是services.cfg,没有这个文件,什么监控也没用。下面给出一个样式文件:

#service definition

##############################################################
#  Wangjing IDC servers service for host-live                #
##############################################################
define service {
        host_name        nagios-server  //来源:hosts.cfg
        service_description   check-host-alive
        check_period          24x7
        max_check_attempts    4
        normal_check_interval 3
        retry_check_interval  2
        contact_groups        sagroup  //来源:contactgroups.cfg
        notification_interval   10
        notification_period     24x7
        notification_options    w,u,c,r
        check_command           check-host-alive  //检查主机是否存活
        }
define service {
        host_name        74-210
        service_description   check_tcp 80
        check_period          24x7
        max_check_attempts    4
        normal_check_interval 3
        retry_check_interval  2
        contact_groups        sagroup
        notification_interval   10
        notification_period     24x7
        notification_options    w,u,c,r
        check_command      check_tcp!80 //检查tcp 80端口服务是否正常
        }

书写时要注意的是,check_tcp与要监控的服务端口之间要用”!”做分隔符。如果服务太多,以应该考虑用脚本来生成。
主机组配置文件hostgroups.cfg,这是一个可选的项目,它建立在文件hosts之上,其格式如下:

define hostgroup {
         hostgroup_name  sa-servers
         alias           sa servers
         members         nagios-server,24-25,24-26  //用逗号间隔多个主机
         }

多个主机组依上面的格式逐个追加上去。后面给一个主机组的截图。
千辛万苦,终于把这些配置给做好保存,现在几乎有点迫不及待了,运行程序/usr/local/nagios –v /usr/local/nagios/etc/nagios.cfg来检查所有配置文件的正确性。如果十分幸运的话,运行完毕将在输出尾部出现:

Total Warnings: 0
Total Errors:   0

Things look okay - No serious problems were detected during the pre-flight check

这样的情况,大功告成;但我却没有这么幸运,修改了好多个地方才成功。不过值得庆幸的是,这个校验的错误报告时非常有用的(不象有的系统的帮助文档中看不中用)。看我故意设置的一个错误产生的输出:

[root@netmonitor nagios]# bin/nagios -v etc/nagios.cfg

Nagios 2.5
Copyright (c) 1999-2006 Ethan Galstad (http://www.nagios.org)
Last Modified: 07-13-2006
License: GPL

Reading configuration data...

Error: Could not find any host matching 'nagios-server'
Error: Could not expand member hosts specified in hostgroup
(config file '/usr/local/nagios/etc/hostgroups.cfg', starting on line 2) ………………………

它告诉我配置文件在什么位置产生错误(实际上我故意在配置文件里加了一个注释符号来测试)。验证通过以后,就可以执行命令/usr/local/nagios –d  /usr/local/nagios/etc/nagios.cfg把nagios作为守护进程。然后用ps –aux | grep nagios 看进程是否处于运行状态。到这一步,nagios服务基本上算是配置完毕。做hosts.cfg、services.cfg等配置时,可以运用一些小技巧来减少出错的概率:如先定义少许的主机、服务,待校验无误后再追加。
验收
用浏览器输入nagios所在服务器的ip及目录,如http://61.135.X..X/nagios,再输验证所需的用户名和密码,就可点击页面右边的相关连接来查看各种状态。关掉某个被nagios监控主机的服务或者拔掉某个服务器的网线,等几分钟,点击超连接“Service Detail”观察页面状态看是否有红色的醒目的报警出现。
一会儿,就会收到报警短信和报警邮件,然后在把测试所有的服务开启或把拔下来的网线查上去,片刻后,网页里的红色报警表格消失,手机短信或邮件通知故障恢复。如果你的情况也这样,那么真正大功告成。
Nagios的功能十分强大,在我的项目里,因为我的需求不同而尽可能的简化了nagios而没有使用代理、多更多插件等功能,在一个不超过1000个服务器的网络规模里,它工作得很好。如果有更多的服务器,建议使用mysql数据来管理监控对象。在部署nagios的过程中,我多很多选项作了取舍,更详细的情况请参照官方的文档。

 

http://blog.csdn.net/yang_dk/article/details/1827343

3、nagios配置文件的关系和理解
       Nagios能实现的具体的功能正是通过配置文件来表现的。而这些配置文件之间有着千丝万缕的关系。比如services依赖于host,timeperiodcontactgroup等。
       Nagios.cfg
这是nagios的主配置文件,它主要负责管理其他所有跟nagios有关的文件。他定义了nagios的日志文件,缓冲文件,状态信息存放文件等。还有其他所有的配置文件等。这些主要的配置文件如下:
1.        hosts.cfg
define host{
host_name                                 #这一项是用来定义标识主机的名字。我们用这个名字在host group和service里标识这个主机。一个主机能定义多个服务。使用适当时,宏$HOSTNAME$里存放了这一项的值。
alias                                          #这一项用来定义主机的一个完整名字或描述。主要是和使你能理容易的标识一个主机。使用适当时,宏$HOSTALIAS$里存放了这一项的值。
address                                    #这一项是用来定义主机的地址。一般而言是主机的IP。当然,你也能够使用一个FQDN来标识你的主机,在没有可访问DNS服务器服务的情况下这种方法会引起问题。使用适当时,宏$ HOSTADDRESS $里存放了这一项的值。
max_check_attempts                  #这一项用来定义在检测返回结果不是OK时,nagios重试检测命令的次数。设置这个值为1会导致nagios一次也不重试就报警。
check_period                            #这一项用一个time period项的名字来定义在哪段时间内激活对这台主机的主动检测。time period是定义在别的文件里的配置项,我们可以在这里用名字来引用她。
contact_groups                          #这是一个联系组列表。我们用联系组的名字来引用她们。多个联系组间用“,”来分隔。
notification_interval                     #这一项用来定义当一个服务仍然downunreachable时,我们间隔多久重发一次通知给联系组。
notification_period                      #这一项用一个time period定义来标识什么时间段内给联系组送通知。这里我们用time period定义的名字来引用她。
notification_options                   #这一项用来决定发送通知的时机。选项有:d = 当有down状态时发送通知,u = 当有unreachable状态时发送通知, r = 当有服务recoveries时发送通知,f = 当主机启动或停机时发送通知。如果你给一个n选项,那么永远不会发送通知。
            }
2.        hostgroups.cfg
define hostgroup{
               hostgroup_name                          #主机组名称,通常定义得较短 
               alias                                          #主机组别名,通常定义得较长
               members                                    #主机组成员
              }
3.        services.cfg
define service{
               host_name                                  #主机名称
               service_description                      #服务描述
               check_command                         #执行命令
       max_check_attempts                   #最大失败尝试次数,值为1时只报警不重
新检测
                            normal_check_interval                 #常规检测间隔时间,默认为60分钟
                                                                             (常规检测是指无论服务状态是否正常,
检测次数达到“最大次数”时)
                            retry_check_interval                    #失败尝试间隔时间,默认为60分钟
                                                                             (失败尝试是指服务状态不正常,检查
次数达到“最大次数”时)
               check_period                              #检测时间段
               notification_interval                     #当服务状态不正常时通知联系人的间隔
时间,值为0时不通知联系人
notification_period                       #通知联系人时间段
               notification_options                     #通知联系人选项,w警告,u未知,c
危急,f启动和停止,n不发送通知
               contact_groups                           #联系人组
              }
4.        servicegroups.cfg
define servicegroup{
               servicegroup_name                      #服务组名称,通常定义得较短       
               alias                                #服务组别名,通常定义得较长
               members                          #服务组成员
              }
5.        contacts.cfg
define contact{
contact_name                             #这个指令用来定义一个联系人的简称。他会在定义contactgroup时被引用到。在相应的环境中,宏定义$CONTACTNAME$会包含这个值。
alias                                           #这个指令是为了定义一个联系人的具体的描述。在相应的环境中,宏定义$CONTACTALIAS$会包含这个值。
host_notification_period               #这个指令是为了定义,能够通知Contact中定义的那个简称联系人,关于主机有问题或者恢复正常状态的时间段。你可以把他想象成能够通知Contact关于主机的在线时间。
service_notification_period                  #这个指令是为了定义,能够通知Contact中定义的那个简称联系人,关于服务的问题或恢复正常的时间段。
host_notification_options                     #这个指令为了定义主机在什么状态下会给联系人发通知。各个参数的描述如下:d=当主机的状态处于down时,发送通知;f=当主机状态处于stop时发送通知。r=当主机恢复up状态时发送通知。n=什么状态下都不发送通知。
service_notification_options                 #这个指令为了定义服务在什么状态下会给联系人发通知。各个参数的描述如下:w=当服务处于警告状态时发送通知u=当服务的状态处于unknown时,发送通知;f=当服务状态处于启动和停止时发送通知。c=当服务处于Critical状态时发送通知。n=什么状态下都不发送通知。
host_notification_commands               #这个指令是为了定义一个通知联系人关于主机问题或恢复正常的联系手段的一个列表。多个手段之间用逗号隔开。
service_notification_commands    #这个指令是为了定义一个通知联系人关于服务问题或恢复正常的联系手段的一个列表。多个手段之间用逗号隔开。
email                                                 #这个指令是为了定义联系人的email地址。这个将取决于你是如何定义你的notification commands.它可以用来给联系人发送紧急邮件。在相应的环境中。宏定义$CONTACTEMAIL$将会包含它的值。
}
6.        contactgroups.cfg
define contactgroup{
               contactgroup_name                            #联系组名称,通常定义得较短
               alias                                #联系组别名,通常定义得较长
               members                          #联系组成员
              }
 
7.        timeperiods.cfg
define timeperiod{
               timeperiod_name                         #时间段名称,通常定义得较短
               alias                                           #时间段别名,通常定义得较长
               sunday                                       #星期日时间段
               monday                                      #星期一时间段
               tuesday                                      #星期二时间段
               wednesday                                 #星期三时间段
               thursday                                     #星期四时间段
               friday                                         #星期五时间段
               saturday                                     #星期六时间段
            }
8.        commands.cfg
       define command{
           command_name        #定义命令的简称
           command_line        #定义当服务进行时Nagios要执行的动作。在命令执行以前,所有合法的宏都要被他们的值代替。
           }







posted @ 2012-08-16 09:41  陳聽溪  阅读(1128)  评论(0)    收藏  举报