[Nagios]关于对象定义的配置

标签: nagios

复制了一篇文章过来,里面有些参数懒得去官网查。。。原文地址http://blog.chinaunix.net/uid-25266990-id-3444609.html


Nagios对象格式的一个特点是可以创建上下继承关系的对象定义。

可以对不同对象定义,具体可以参考官方文档:

http://nagios-cn.sourceforge.net/nagios-cn/cgiconfig.html#objectdefinitions_host

这里对主机,主机组,服务,服务组定义配置作说明为例,目的是为了能够更好的管理服务器项目。

(一)   修改配置文件

1Nagios的主配置文件是nagios.cfg,我们就从这个文件开始修改。用vmi编辑nagios.cfg,注释行 #cfg_file=/usr/local/nagios/etc/localhost.cfg,然后把下面几行的注释去掉或添加

 

cfg_file=/usr/local/nagios/etc/objects/contacts.cfg       //联系人和联系组配置文件路径

cfg_file=/usr/local/nagios/etc/objects/hostgroups.cfg     //主机组配置文件路径

cfg_file=/usr/local/nagios/etc/objects/servicegroups.cfg     //服务组配置文件路径

cfg_file=/usr/local/nagios/etc/objects/hosts.cfg          //主机配置文件路径

cfg_file=/usr/local/nagios/etc/objects/services.cfg       //服务配置文件路径

cfg_file=/usr/local/nagios/etc/objects/timeperiods.cfg    //监视时段配置文件路径

2修改的配置文件是cgi.cfg,它的作用是控制相关cgi脚本。先确保use_authentication=1。曾看过不少的文章,都是建议把 use_authentication的值设置成”0”来取消验证,这是一个十分糟糕的想法。接下来修改default_user_name=admin ,再后面的修改在下表列出:

 

authorized_for_system_information=nagiosadmin,admin 

authorized_for_configuration_information=nagiosadmin,admin

authorized_for_system_commands=admin  //多个用户之间用逗号隔开

authorized_for_all_services=nagiosadmin,amdin

authorized_for_all_hosts=nagiosadmin,min

authorized_for_all_service_commands=nagiosadmin,amin

authorized_for_all_host_commands=nagiosadmin,admin

admin用户是apache访问控制用户。这里我的创建命令为:

/usr/local/apache/bin/htpasswd –c /usr/local/nagios/etc/htpasswd admin 

3修改的配置文件是commands.cfg这个文件的主要功能是用来发送报警短信和报警邮件,默认是以邮件方式发送,这里就不修改了。如果要支持报警短信,那么可以使用飞信,详细配置大家可以互联网搜索。

(二)增加新的配置文件

1、先创建简单的配置文件timeperiods.cfg默认已经存在了,我也不作修改。

2、添加联系人和联系组。修改配置文件是 contacts.cfg,添加用户中心监控组如下:


  1. #template which is defined passport

  2. define contact{

  3.                contact_name nagios-mail-passport ;联系名称

  4.                alias passport ;别名

  5.                service_notification_period 24x7

  6.                host_notification_period 24x7

  7.                service_notification_options w,u,c,r,f,s

  8.                host_notification_options d,u,r,f,s

  9.                service_notification_commands notify-service-by-email

  10.                host_notification_commands notify-host-by-email

  11.                email xxxxxx@qq.com ;邮箱地址

  12. }

  13. #contact group of passport

  14. define contactgroup{

  15.               contactgroup_name passport_group ;联系组

  16.               alias Nagios Administrators passport

  17.               members nagios-mail-passport ;对应contact_name

  18. }

可以参考generic-contact联系人的配置;多个成员之间用逗号做分界符如果有更多的联系组,就依相同的格式在文件中追加余下的组。

3、定义主机。

配置文件hosts.cfg下面是我定义的主机的基本样式:


  1. # Linux host definition template - This is NOT a real host, just a template!

  2. define host{

  3.             name linux-passport-server ; The name of this host template

  4.             use passport-host ; This template inherits other values from the generic-host template

  5.             check_period 24x7 ; By default, Linux hosts are checked round the clock

  6.             check_interval 5 ; Actively check the host every 5 minutes

  7.             retry_interval 1 ; Schedule host check retries at 1 minute intervals

  8.             max_check_attempts 10 ; Check each Linux host 10 times (max)

  9.             check_command check-host-alive ; Default command to check Linux hosts

  10.             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!

  11.             notification_interval 120 ; Resend notifications every 2 hours

  12.             notification_options d,u,r ; Only send notifications for specific host states

  13.             contact_groups passport_group ; Notifications get sent to the admins by default

  14.             register 0 ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL HOST, JUST A TEMPLATE!

  15. }

  16. #blog

  17. define host{

  18.            name blog-host ; The name of this host template

  19.            notifications_enabled 1 ; Host notifications are enabled

  20.            event_handler_enabled 1 ; Host event handler is enabled

  21.            flap_detection_enabled 1 ; Flap detection is enabled

  22.            failure_prediction_enabled 1 ; Failure prediction is enabled

  23.            process_perf_data 1 ; Process performance data

  24.            retain_status_information 1 ; Retain status information across program restarts

  25.            retain_nonstatus_information 1 ; Retain non-status information across program restarts

  26.            notification_period 24x7 ; Send host notifications at any time

  27.            register 0 ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL HOST, JUST A TEMPLATE!

  28. }

“;”后为英文注释,具体的参数可以调整。

4、定义service。

配置文件是services.cfg,如果没有配置,那么监控的服务器将不能报警。下面给出一个样式文件:


  1. # Generic service definition template - This is NOT a real service, just a template!

  2. define service{

  3.            name passport-service ; The 'name' of this service template

  4.            active_checks_enabled 1 ; Active service checks are enabled

  5.            passive_checks_enabled 1 ; Passive service checks are enabled/accepted

  6.            parallelize_check 1 ; Active service checks should be parallelized (disabling this can lead to major performance problems)obsess_over_service 1 ; We should obsess over this service (if necessary)

  7.            check_freshness 0 ; Default is to NOT check service 'freshness'

  8.            notifications_enabled 1 ; Service notifications are enabled

  9.            event_handler_enabled 1 ; Service event handler is enabled

  10.            flap_detection_enabled 1 ; Flap detection is enabled

  11.            failure_prediction_enabled 1 ; Failure prediction is enabled

  12.            process_perf_data 1 ; Process performance data

  13.            retain_status_information 1 ; Retain status information across program restarts

  14.            retain_nonstatus_information 1 ; Retain non-status information across program restartsis_volatile 0 ; The service is not volatile

  15.            check_period 24x7 ; The service can be checked at any time of the day

  16.            max_check_attempts 3 ; Re-check the service up to 3 times in order to determine its final (hard) statenormal_check_interval 10 ; Check the service every 10 minutes under normal conditionsretry_check_interval 2 ; Re-check the service every two minutes until a hard state can be determined

  17.            contact_groups passport_group ; Notifications get sent out to everyone in the 'admins' group

  18.            notification_options w,u,c,r ; Send notifications about warning, unknown, critical, and recovery events

  19.            notification_interval 60 ; Re-notify about service problems every hour

  20.            notification_period 24x7 ; Notifications can be sent out at any time

  21.            register 0 ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL SERVICE, JUST A TEMPLATE!

  22. }

  23. # Local service definition template - This is NOT a real service, just a template!

  24. define service{

  25.            name local-passport-service ; The name of this service template

  26.            use passport-service ; Inherit default values from the generic-service definition

  27.            max_check_attempts 4 ; Re-check the service up to 4 times in order to determine its final (hard) state

  28.            normal_check_interval 5 ; Check the service every 5 minutes under normal conditions

  29.            retry_check_interval 1 ; Re-check the service every minute until a hard state can be determined

  30.            register 0 ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL SERVICE, JUST A TEMPLATE!

  31. }

5、主机组配置文件hostgroups.cfg,这是一个可选的项目,它建立在文件hosts之上,其格式如下:


  1. #passport

  2. define hostgroup {

  3.           hostgroup_name sa-passport-servers

  4.           alias passport-DB-servers

  5.           members 192.168.10.178 ;用户中心真实主机

  6. }

6、服务组的配置。

配置文件hostgroups.cfg,内容为:


  1. define servicegroup{

  2.            servicegroup_name Up and Running

  3.            alias Ping

  4.            members 192.168.10.178,PING

  5. }

注:格式必须为host,service格式。

(三)监控主机调用联系人、定义主机和定义服务。


  1. # Define a host for the local machine

  2. define host{

  3.            use linux-passport-server ; Name of host template to use; This host definition will inherit all variables that are defined; in (or inherited by) the local-passport-service host template definition.

  4.            host_name 192.168.10.178

  5.            alias 192.168.10.178

  6.            address 192.168.10.178

  7. }

  8. ###############################################################################

  9. ###############################################################################

  10. #

  11. # SERVICE DEFINITIONS

  12. #

  13. ###############################################################################

  14. ###############################################################################

  15. # Define a service to "ping" the local machine

  16. define service{

  17.           use local-passport-service ; Name of service template to use

  18.           host_name 192.168.10.178

  19.           service_description PING

  20.           check_command check_ping!100.0,20%!500.0,60%

  21. }

最后的监控显示:


收到的邮件:

版权所有:《Hoiwan's 》 => 《[Nagios]关于对象定义的配置
本文地址:http://ihoiwan.com/?post=25
除非注明,文章均为 《Hoiwan's 》 原创,欢迎转载!转载请注明本文地址,谢谢。

发表评论: