不管是用nagios监控本地还是远程服务器,都需要对nagios进行配置。
nagios这种可插拔的配置非常简单易懂。
Nagios的配置文件结构如下:
在nagios的安装包sample-config/有配置的参考
/usr/local/Nagios/etc/
nagios.cfg
cgi.cfg
resource.cfg
object/
…
############################
nagios.cfg:
指定对象配置文件的位置:
cfg_file=/usr/local/nagios/etc/hosts.cfg
或
cfg_dir=/usr/local/nagios/etc
check_external_commands=1 #使用外部命令
############################
fabfile:定义fab命令的.
具体怎样写fab命令参考python的fab文档。
###########################
Templates表示模板,分两种:
Use顺序:
Ohtpl
Bhtpl
Htpl
-----------------
Hosts:
App:主要使用这个文件夹里的模板
创建hostgroups之后创建这个hosts模板在hosts中调用。
define host{
use <template-name1>,\
<template-name2> #use表示在这里嵌套调用其它的更底层的模板。
name bhtpl/htpl_<dir>_<file-name> #用templates/hosts/目录下的路径dir来命名。
hostgroups +<hostgroup-name1>,\ #以+开头表示添加
<hostgroup-name2> #这些文件定义在hostgroups中
alias <file-name-upper> - <alias-name> #将这个文件名大写,然后取个别名
_<name> <value> #这里可以定义某些变量的默认值
register 0 #表示没有注册
host的name是 前缀_路径_文件名 组成的
-------------------
Services:
App:主要用这个里面的模板。
defineservice{
use template-name1>,\
<template-name2> #嵌套调用更基础的模板
name <file-name> #和文件名一样,都是用cstpl/stpl开头加路径加名字
check_command <check_XXX>\
!–options $VAR$ \
!-options/- -options $VAR$ \
! $ARGS$ #用!分割参数,同时用$表示一个参数
servicegroups +<servicegroups-name1>,\ #以+开头表示添加
<servicegroups-name2> #这些服务分组名在servicesgroup定义。
alias <file-name-upper> - <alias-name> #将这个文件名大写,然后取个别名
_<VAR> value #在这里定义变量的默认值
register 0 #注册为0
}
Service的name是文件名,也是前缀_路径_名字 组成
###########################
Incgroups:增加或删除对hosts的检查
App:主要用这个里面的
增加一个service就是增加一个incgroups,然后再hosts和services里面同时调用这个incgroups就可以了。
define hostgroup{
hostgroup_name inc_<dir>_<file-name> #inc为前缀
alias <name> - <alias>
register 0
}
define hostgroup{
hostgroup_name exc_<dir>_<file-name> #exc为前缀
alias <name> - <alias>
register 0
}
############################
Commands:
定义宏与外部程序的映射关系。
#说明命令用法
#
#说明参数有哪些
#
#说明返回值
definecommand{ | ||
| command_name | command_name |
| command_line | command_line |
| } |
definehost{ | ||
| host_name | host_name |
| alias | alias |
| display_name | display_name |
| address | address |
| parents | host_names |
| hourly_value | # |
| hostgroups | hostgroup_names |
| check_command | command_name |
| initial_state | [o,d,u] |
| max_check_attempts | # |
| check_interval | # |
| retry_interval | # |
| active_checks_enabled | [0/1] |
| passive_checks_enabled | [0/1] |
| check_period | timeperiod_name |
| obsess_over_host|obsess | [0/1] |
| check_freshness | [0/1] |
| freshness_threshold | # |
| event_handler | command_name |
| event_handler_enabled | [0/1] |
| low_flap_threshold | # |
| high_flap_threshold | # |
| flap_detection_enabled | [0/1] |
| flap_detection_options | [o,d,u] |
| process_perf_data | [0/1] |
| retain_status_information | [0/1] |
| retain_nonstatus_information | [0/1] |
| contacts | contacts |
| contact_groups | contact_groups |
| notification_interval
| #再次发出通知的时间间隔,分钟。0表示不重新发送。 |
| first_notification_delay | # |
| notification_period | timeperiod_name#再次发送通知的时间。 |
| notification_options | [d,u,r,f,s] |
| notifications_enabled | [0/1] |
| stalking_options | [o,d,u] |
| notes | note_string |
| notes_url | url |
| action_url | url |
| icon_image | image_file |
| icon_image_alt | alt_string |
| vrml_image | image_file |
| statusmap_image | image_file |
| 2d_coords | x_coord,y_coord |
| 3d_coords | x_coord,y_coord,z_coord |
| } |
definehostgroup{ | ||
| hostgroup_name | hostgroup_name |
| alias | alias |
| members | hosts |
| hostgroup_members | hostgroups |
| notes | note_string |
| notes_url | url |
| action_url | url |
| }
|
defineservice{ | ||
| host_name | host_name |
| hostgroup_name | hostgroup_name |
| service_description | service_description |
| display_name | display_name |
| parents | service_descriptions |
| hourly_value | # |
| servicegroups | servicegroup_names |
| is_volatile | [0/1] |
| check_command | command_name |
| initial_state | [o,w,u,c] |
| max_check_attempts | # |
| check_interval | # |
| retry_interval | # |
| active_checks_enabled | [0/1] |
| passive_checks_enabled | [0/1] |
| check_period | timeperiod_name |
| obsess_over_service|obsess | [0/1] |
| check_freshness | [0/1] |
| freshness_threshold | # |
| event_handler | command_name |
| event_handler_enabled | [0/1] |
| low_flap_threshold | # |
| high_flap_threshold | # |
| flap_detection_enabled | [0/1] |
| flap_detection_options | [o,w,c,u] |
| process_perf_data | [0/1] |
| retain_status_information | [0/1] |
| retain_nonstatus_information | [0/1] |
| notification_interval | # |
| first_notification_delay | # |
| notification_period | timeperiod_name |
| notification_options | [w,u,c,r,f,s] |
| notifications_enabled | [0/1] |
| contacts | contacts |
| contact_groups | contact_groups |
| stalking_options | [o,w,u,c] |
| notes | note_string |
| notes_url | url |
| action_url | url |
| icon_image | image_file |
| icon_image_alt | alt_string |
| }
|
defineservicegroup{ | ||
| servicegroup_name | servicegroup_name |
| alias | alias |
| members | services |
| servicegroup_members | servicegroups |
| notes | note_string |
| notes_url | url |
| action_url | url |
| }
|
definecontact{ | ||
| contact_name | contact_name |
| alias | alias |
| contactgroups | contactgroup_names |
| minimum_value | # |
| host_notifications_enabled | [0/1] |
| service_notifications_enabled | [0/1] |
| host_notification_period | timeperiod_name主机故障通知联系人的时间 |
| service_notification_period | timeperiod_name服务故障通知联系人的时间 |
| host_notification_options | [d,u,r,f,s,n] |
| service_notification_options | [w,u,c,r,f,s,n] |
| host_notification_commands | command_name |
| service_notification_commands | command_name |
| | email_address |
| pager | pager_numberor pager_email_gateway |
| addressx | additional_contact_address |
| can_submit_commands | [0/1] |
| retain_status_information | [0/1] |
| retain_nonstatus_information | [0/1] |
| } |
definecontactgroup{ | ||
| contactgroup_name | contactgroup_name |
| alias | alias |
| members | contacts |
| contactgroup_members | contactgroups |
| } |
definetimeperiod{ | ||
| timeperiod_name | timeperiod_name |
| alias | alias |
| [weekday] | timeranges |
| [exception] | timeranges |
| exclude | [timeperiod1,timeperiod2,...,timeperiodn] |
| } |
definehostescalation{ | ||
| host_name | host_name |
| hostgroup_name | hostgroup_name |
| contacts | contacts |
| contact_groups | contactgroup_name |
| first_notification | #正常规则的第几次通知开始激活这个升级规则。 |
| last_notification | #0表示升级规则持续到主机正常。 |
| notification_interval | # |
| escalation_period | timeperiod_name |
| escalation_options | [d,u,r] |
| } |
defineserviceescalation{ | ||
| host_name | host_name |
| hostgroup_name | hostgroup_name |
| service_description | service_description |
| contacts | contacts |
| contact_groups | contactgroup_name |
| first_notification | # |
| last_notification | # |
| notification_interval | # |
| escalation_period | timeperiod_name |
| escalation_options | [w,u,c,r] |
| } |
definehostdependency{ | ||
| dependent_host_name | host_name |
| dependent_hostgroup_name | hostgroup_name |
| host_name | host_name |
| hostgroup_name | hostgroup_name |
| inherits_parent | [0/1] |
| execution_failure_criteria | [o,d,u,p,n] |
| notification_failure_criteria | [o,d,u,p,n] |
| dependency_period | timeperiod_name |
| } |
defineservicedependency{ | ||
| dependent_host_name | host_name |
| dependent_hostgroup_name | hostgroup_name |
| servicegroup_name | servicegroup_name |
| dependent_servicegroup_name | servicegroup_name |
| dependent_service_description | service_description |
| host_name | host_name |
| hostgroup_name | hostgroup_name |
| service_description | service_description |
| inherits_parent | [0/1] |
| execution_failure_criteria | [o,w,u,c,p,n] |
| notification_failure_criteria | [o,w,u,c,p,n] |
| dependency_period | timeperiod_name |
| } |
command_line中使用宏和参数
$USER1$… 变量宏
$ARG1$… 参数宏
这些宏定义在/usr/local/Nagios/etc/resource.cfg
自定义变量通过下划线_开头,前面有HOST、SERVICE、CONTACT这样的前缀。
通过check_command调用:
check_command command_name\
!$_HOSTNAME1$\
!$_SERVICENAME2$\
!$_SERVICEEXTRA_ARGS$
参数用!分割,
_EXTRA_ARGS直接传插件的选项和参数,多个选项用,分割。
###########################
Hosts:
定义主机
Use决定了使用哪些模板中的基础服务。
Hostgroups决定了这个host特有的服务。
在host的hostgroups里添加services的hostgroup_name就是让host具有这个service。
如果包含的模板中有这个service就不用再添加hostgroups了。
Host中的变量要和service里面调用command的变量一样,去掉HOST就可以了。
###########################
Hostgroups:
主机可以属于任何用户定义的主机组
新建之前需要创建一个hostgroups,然后在templates中调用。
###########################
Services:
定义由主机提供的一个或多个服务
Hostgroup_name中的inc和exc开头的定义在incgroups里面。
Check_command使用!来分隔参数。
hostgroup_name决定了这个服务被哪个组中的host调用。
###########################
Servicegroups:
服务可以属于任何用户定义的分组。
###########################
Contacts:增加nagios的用户
定义具体联系人。
###########################
Contactgroups:
发送通知的对象一般是联系人组,一个联系人可以属于多个组。
###########################
Timeperiods:
定义检查hosts和service的时间
00:00-11:59,12:00-23:59 #使用空格来添加多个时间段.
定义24* 7 表示一直监控。
###########################
Escalations:
升级规则,通过升级规则能使nagios通知另一组联系人。
###########################
denpendency:
依赖关系会基于其它对象的状态过滤某个对象的测试或通知。
###########################