Nagios 配置指南

运维 系统运维
Nagios是一个Nagios可运行在Linux/Unix平台之上的监视系统,作为运维和系统管理员,安装和配置一个Nagios是必要的,能为自己带来不少轻松,让我们来看看Nagios 配置过程。

Nagios很好很强大,Nagios配置过程也非常简单。

 

Nagios 

图-Nagios

  配置是nagios最复杂的部分,它涉及到多个文件的配置,为了方便描述,这里逐个的进行配置。

  一、apache 配置

  我们分两个步骤来完成这个配置。第一步是修改apache的配置文件httpd.conf,这里的文件路径是 /usr/local/apache/conf/httpd.conf 。把apache的运行用户[1]和运行组改成nagios,往下把下面的行追加到文件httpd.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  
 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.

 

  

 

  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  
 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.

  上述文本块的作用是对nagios的目录进行用户验证,只有合法的授权用户才可以访问nagios的页面文件。第二步是生成用户验证文件:只要执行命令 /usr/local/apache/bin/htpasswd –c /usr/local/nagios/etc/htpasswd sery ,就会生成web的合法访问用户sery;命令交互执行,需要输入2次密码,然后就在文件/usr/local/nagios/etc/htpasswd写入一行-第一个字段是刚生成的用户名,第二个是加密后的密码,如果还要添加更多的用户,执行命令 htpasswd 就不需要选项 “-c”,否则就会覆盖所有已经生成的行。

  配置完成后,执行/usr/local/apache/bin/apachctl –t 检查apache配置文件是否有语法错误,无误后用/usr/local/apache/bin/apachctl start & 把apache启动,然后从另外的机器的浏览器输入nagios 的访问地址(如:[url]http://ip/nagios[/url]),如果正常,将出现下图的登录验证窗口等待用户输入:

  

 

  输入用htpasswd创建的用户名和密码测试一下,没有问题的话,进行下一步配置操作。

#p#

  二、nagios配置

  刚安装完成的nagios,其配置文件的目录是/usr/local/nagios/etc,下图是其etc目录的文件:

  

 

  先把这些文件改名,如 cgi.cfg-sample改成cgi.cfg ,用命令cp cgi.cfg-sample cgi.cfg …依样把余下的几个*.cfg-sample都复制成*.cfg文件。从nagios2.6版开始,不用修改配置文件localhost.cfg就可以直接运行../bin/nagios –v nagios.cfg验证程序是否能正常运行(nagios2.5及以前版本的最小运行的配置文件是minimal.cfg,但需要修改这个文件多处才能验证成功)。当然,我们不能指望这个最小的配置文件能够满足实际的需求,因此,需要对现有的配置文件进行修改,其次增加自定义的一些配置文件。这里,我们分两步进行:先修改配置文件再增添自定义文件。

  (一) 修改配置文件

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

 

  cfg_file=/usr/local/nagios/etc/contactgroups.cfg //联系组配置文件路径  
 
  cfg_file=/usr/local/nagios/etc/contacts.cfg //联系人配置文件路径  
 
  cfg_file=/usr/local/nagios/etc/hostgroups.cfg //主机组配置文件路径  
 
  cfg_file=/usr/local/nagios/etc/hosts.cfg //主机配置文件路径  
 
  cfg_file=/usr/local/nagios/etc/services.cfg //服务配置文件路径  
 
  cfg_file=/usr/local/nagios/etc/timeperiods.cfg //监视时段配置文件路径  
 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

 

  改check_external_commands=0为check_external_commands=1 .这行的作用是允许在web界面下执行重启nagios、停止主机/服务检查等操作。把command_check_interval的值从默认的1改成command_check_interval=10s(根据自己的情况定这个命令检查时间间隔,不要太长也不要太短)。主配置文件要改的基本上就是这些,通过上面的修改,发现/usr/local/nagios/etc并没有文件hosts.cfg等一干文件,怎么办?稍后手动创建它们。

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

 

  authorized_for_system_information=nagiosadmin,sery  
 
  authorized_for_configuration_information=nagiosadmin,sery  
 
  authorized_for_system_commands=sery //多个用户之间用逗号隔开  
 
  authorized_for_all_services=nagiosadmin,sery  
 
  authorized_for_all_hosts=nagiosadmin,sery  
 
  authorized_for_all_service_commands=nagiosadmin,sery  
 
  authorized_for_all_host_commands=nagiosadmin,sery  
 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

 

  那么上述用户名打那里来的呢?是执行命令 /usr/local/apache/bin/htpasswd –c /usr/local/nagios/etc/htpasswd sery 所生成的,这个要注意,不能随便加没有存在的验证用户,为了安全起见,不要添加过多的验证用户。

  第3个修改的配置文件是misccommands.cfg,这个文件的主要功能是用来发送报警短信和报警邮件,对其的修改如下所示:

 

  #host-notify-by-sms //发送短信报警  
 
  define command {  
 
  command_name host-notify-by-sms  
 
  command_line /usr/local/bin/sms_send "Host $HOSTSTATE$ alert for $HOSTNAME$! on '$DATETIME$' " $CONTACTPAGER$  
 
  }  
 
  #service notify by sms //发送短信报警  
 
  define command {  
 
  command_name service-notify-by-sms  
 
  command_line /usr/local/bin/sms_send "'$HOSTADDRESS$' $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$" $CONTACTPAGER$  
 
  }  
 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.

 

  主机和服务的邮件报警通知已经在文件中,不须更改。也可以把短信和邮件报警通知这些配置块写到文件commands.cfg中,效果是一样的。

#p#

  (二)增加新的配置文件

  先创建简单的配置文件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  
 
  }  
 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.

 

  这个文件的定义明晰易懂,不多做说明。另建议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  
 
  }  
 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.

 

  上面的文件定义了2个联系人,如果有更多联系人的话,照这个格式在后面追加即可。服务通知选项(service_notification_options)与主机通知选项(host_notification_options)的几个选项在这里说明一下:w-warning , u-unknown,c-critical,r-recovery;d-down,u-unreachable,注意一下,主机报警和服务报警有些差异。

#p#

  紧接着的第三个手动创建的配置文件是contactgroups.cfg文件,这个文件是依照上一个文件contacts.cfg来的,contactgroups文件相对简单一些,其格式如下:

 

  define contactgroup {  
 
  contactgroup_name sagroup //不要用空格  
 
  alias system administrator group  
 
  members sa,sery //本例有2个成员  
 
  }  
 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

 

  多个成员之间用逗号做分界符,如果有更多的联系组,就依相同的格式在文件中追加余下的组。

  关键的角色终于登场,这就是配置文件hosts.cfg。下面是我定义的两个主机的基本样式:

 

  #define monitor host  
 
  # Wangjing IDC servers #  
 
  • 1.
  • 2.
  • 3.
  • 4.

 

  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 //用逗号间隔多个主机  
 
  }  
 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

 

  多个主机组依上面的格式逐个追加上去。后面给一个主机组的截图。

  

#p#

 

  千辛万苦,终于把这些配置给做好保存,现在几乎有点迫不及待了,运行程序/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  
 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

 

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

 

  [root@netmonitor nagios]# bin/nagios -v etc/nagios.cfg  
 
  Nagios 2.5  
 
  Copyright (c) 1999-2006 Ethan Galstad ([url]http://www.nagios.org[/url])  
 
  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)  
 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.

 

  它告诉我配置文件在什么位置产生错误(实际上我故意在配置文件里加了一个注释符号来测试)。验证通过以后,就可以执行命令/usr/local/nagios –d /usr/local/nagios/etc/nagios.cfg 把nagios作为守护进程。然后用ps –aux | grep nagios 看进程是否处于运行状态。到这一步,nagios服务基本上算是配置完毕。做hosts.cfg、services.cfg等配置时,可以运用一些小技巧来减少出错的概率:如先定义少许的主机、服务,待校验无误后再追加。

通过文章详细的描述,我们都应该学会nagios配置,为自己的系统保驾护航!

【编辑推荐】

  1. Nagios配置之准备工作
  2. Nagios 安装三部曲
  3. Nagios网络监控工作原理
  4. Nagios 简介与功能
  5. Nagios Sendmail无法发送mail邮件的解决方法
  6. Nagios中告警邮件如何处理 Sendmail服务的配置

 

责任编辑:赵鹏 来源: 网络转载
相关推荐

2010-05-31 11:10:37

Nagios pnp

2011-03-21 09:17:06

Nagios

2011-03-21 09:17:06

NagiosFedora

2011-03-21 09:17:06

openSUSENagios

2011-03-21 09:17:06

UbuntuNagios

2011-08-22 12:25:02

nagios

2011-03-31 16:05:18

Redhat配置nagios

2011-11-08 21:58:01

Nagios 配置 流

2011-03-25 16:49:33

nagios服务

2010-05-31 10:22:31

Nagios pnp

2011-03-28 15:22:00

nagios

2011-03-31 16:05:18

Redhat配置nagios

2011-03-23 14:43:10

Nagiosnagios.cfg

2010-05-31 12:22:13

Nagios pnp

2011-03-22 10:10:21

Nagios配置

2011-03-24 09:15:54

Nagios飞信

2011-03-28 11:03:09

配置Nagios

2013-06-08 16:55:22

Nagios

2011-03-31 16:05:15

Redhat配置nagios

2011-03-28 13:57:57

点赞
收藏

51CTO技术栈公众号