14.5.2 配置Nagios服务器端监控项
您可以在百度里搜索“跟老男孩学Linux运维:Web集群实战 艾草文学(www.321553.xyz)”查找最新章节!
14.5.2 配置Nagios服务器端监控项
1.定义要监控的Nagios客户端主机
hosts.cfg一般用来存放Nagios要监控的主机相关配置,下面是hosts.cfg中的主机定义部分的配置参数详解。
define host { #<==define host为关键字,意思是 定义主机,主机内容用一对大括号括起来 uselinux-server #<==定义主机使用的模版,具体参见 templates.cfg host_name 08-web01#<==直接定义主机名称,根据服务功 能可随意定义 alias 08-web01 #<==直接定义主机别名,同上address 10.0.0.8 #<==直接定义被监控服务器的IP check_commandcheck-host-alive #<==检测主机存活命令,来自com- mands.cfgmax_check_attempts 3 #<==故障后,最大尝试检测次数 normal_check_interval 2#<==正常的检查间隔,默认单位分钟 retry_check_interval 2#<==故障后重试的检查间隔,默认单位分钟 check_period 24x7#<==检查周期24x7,具体参见timeperiods.cfg notification_interval 300#<==故障后,两次报警的通知间隔,默认单位分钟 notification_period 24x7#<==一天之内通知的周期。比如全天还是半天等, 具体参见timeperiods.cfgnotification_options d,u,r #<==主机状态通知选项 d-down,u-unreache-able,r-recovery contact_groups admins #<==报警到admins用户组。在contacts. cfg里定义 } #<==这个括号不能丢
主机也可以只配置关键选项,多数选项可采取linux-server模版的默认值,有能力的同学还可以先调整linux-server模版,然后让所有机器统一采用这种默认值,这看起来更加简单方便。示例如下:
# web01 define host { use linux-server host_name 08-web01 alias08-web01 address 10.0.0.8 }
下面将采用上述简化版的配置,服务器多时可批量部署,当然也可以安装图形配置界面Centreon、NagiosQL,但不推荐。
现在开始配置hosts.cfg,增加web01、web02主机。此时,需要在hosts.cfg里添加所有需要监控的客户端主机和主机组(HOSTGROUP)。示例如下:
[root@nagios-server ~]# cd /usr/local/nagios/etc/objects/#<==进入到辅助配置文件的目录 [root@nagios-server objects]# cat -n hosts.cfg#<==默认情况下hosts.cfg是不存 在的,需要手工创建 1# Define a host for the localmachine 2define host{ 3 use linux-server #<==主机模板,在templates.cfg里定义的,很多此处没有的参数就都 在templates.cfg里 4 host_name web01#<==第一个Nagios客户端主机名 5 alias web01 #<==第一个Nagios客户端别名 6address 10.0.0.8 #<==第一个Nagios客户端IP 7 } 8define host{ 9 uselinux-server 10 host_name web02 11 alias web02 12 address 10.0.0.913 } #提示 #<==host_name web01 不同的主机名这行需要改改 #<==alias web01不同的主机别名这行需要改改 #<==address 10.0.0.8 客户端服务器地址 1415################################################# 16 17# Definean optional hostgroup for Linux machines 18 19define hostgroup{ 20hostgroup_name linux-servers 21 alias Linux Servers 22 membersweb01,web02 #<==把前面定义的每一个Nagios客 户端主机名在这里用逗号隔开列出来 23 }
2.配置services.cfg,定义要监控的资源服务
services.cfg文件是配置监控服务的,是Nagios最重要的配置文件之一,如果服务器数量比较少(50台以内),则需要监控的大部分服务配置都可以在这里面添加。这个配置文件默认是不存在的,需要人为定义。
先来看看services.cfg配置文件的service配置参数,详细说明如下:
define service { #<==define service为关键字,意思是定义一个服务,服务内容用一对大括号括起来 use generic-service#<==定义该服务使用的模版,具体参见templates. cfg host_name 08-web01#<==被监控的主机名,来自hosts.cfg,可在 hosts.cfg中自定义 service_descriptionCurrent Load #<==报警服务描述,根据内容取有意义的名称 check_commandcheck_nrpe!check_load #<==检查服务的命令,这个很关键,注意被动 服务的监控均由check_nrpe调用max_check_attempts 2 #<==尝试检查的最大次数 normal_check_interval 4#<==正常状态检查时间间隔,每4分钟去检查 一次是否正常 retry_check_interval 4#<==重试检查时间间隔,默认单位是分 check_period 24x7 #<==检查的周期,24x7仅仅是个字符串而已notification_interval 1440 #<==通知的间隔,即1440分通知一次notification_period 24x7 #<==通知的周期,这个参数来自timeperiods.cfg中的配置,例如可以定义半夜不报警到短信手机 notification_options w,u,c,r#<==要通知的服务状态选项 w-warning,u- unknown,c-critical,r-recovery。contact_groups admins #<==要通知的用户组,其定义来自于contacts. cfgprocess_perf_data 1 #<==PNP出图记录数据相关。 } #<==defineservice结尾的大括号
下面来看看配置监控客户端本地资源的示例。
磁盘分区监控(被动监控)如下:
define service { use generic-service host_name 08-web01#<==这里可以指定多台机器,通过逗号隔开 service_description Disk Partitioncheck_command check_nrpe!check_disk }
swap监控(被动监控)如下:
define service { use generic-service host_name 08-web01service_description Swap Useage check_command check_nrpe!check_swap}
内存监控(被动监控)如下:
define service { use generic-service host_name 08-web01service_description MEM Useage check_command check_nrpe!check_mem}
通过使用模板templates.cfg,可以把上面的配置简写为如下几行。
系统负载监控(被动监控):
define service { use generic-service host_name 08-web01service_description Current Load check_commandcheck_nrpe!check_load }
磁盘I/O监控(被动监控):
define service { use generic-service host_name 08-web01service_description Disk Iostat check_commandcheck_nrpe!check_iostat!5!11 }
ping监控(主动监控):
define service { use generic-service host_name 08-web01service_description PING check_commandcheck_ping!100.0,20%!500.0,60% }
以下是客户端本地资源监控的实际配置。
define service { use generic-service host_name 08-web01service_description Disk Partition check_commandcheck_nrpe!check_disk } define service { use generic-servicehost_name 08-web01 service_description Swap Useage check_commandcheck_nrpe!check_swap } define service { use generic-servicehost_name 08-web01 service_description MEM Useage check_commandcheck_nrpe!check_mem } define service { use generic-servicehost_name 08-web01 service_description Current Load check_commandcheck_nrpe!check_load } define service { use generic-servicehost_name 08-web01 service_description Disk Iostat check_commandcheck_nrpe!check_iostat!5!11 } define service { use generic-servicehost_name 08-web01 service_description PING check_commandcheck_ping!100.0,20%!500.0,60% }
提示:
1)上述配置中的check_nrpe是服务器端的插件(是commands.cfg里预先定义的命令名),负责和客户端的nrpe进程交流并执行check_nrpe叹号后的插件,所以,check_nrpe!check_load配置中的check_load是客户端的插件名,是在与客户端的nrpe进程对应的配置nrpe.cfg里定义的命令名。
2)以上services.cfg中添加了对磁盘分区、Load、Mem、Swap、磁盘IO、ping的监控。
Nagios软件默认没有提供客户端的内存和I/O插件,但本文在配置时已经复制进去了,因此,只需在commands.cfg里配置即可,详细请看下文的commands.cfg说明。
3.调试hosts.cfg和service.cfg的所有配置
若在调试前执行如下检查Nagios语法的命令:
[root@nagios-server objects]# /etc/init.d/nagios checkconfig#<==修改了调试输出配置 Running configuration check... Nagios Core 3.5.1Copyright (c) 2009-2011 Nagios Core Development Team and CommunityContributors Copyright (c) 1999-2009 Ethan Galstad Last Modified:08-30-2013 License: GPL Website: http:// www.nagios.org Readingconfiguration data... Read main config file okay... Processingobject config file '/usr/local/nagios/etc/objects/commands.cfg'...Processing object config file'/usr/local/nagios/etc/objects/contacts.cfg'... Processing objectconfig file '/usr/local/nagios/etc/objects/timeperiods.cfg'...Processing object config file'/usr/local/nagios/etc/objects/templates.cfg'... Processing objectconfig file '/usr/local/nagios/etc/objects/hosts.cfg'... Processingobject config file '/usr/local/nagios/etc/objects/services.cfg'...Processing object config directory'/usr/local/nagios/etc/objects/services'... Read object configfiles okay... Running pre-flight check on configuration data...Checking services... Error: Service check command 'check_nrpe'specified in service 'Current Load' for host 'web01' not definedanywhere! Error: Service check command 'check_nrpe' specified inservice 'Disk Iostat' for host 'web01' not defined anywhere! Error:Service check command 'check_nrpe' specified in service 'DiskPartition' for host 'web01' not defined anywhere! Error: Servicecheck command 'check_nrpe' specified in service 'MEM Useage' forhost 'web01' not defined anywhere! Error: Service check command'check_nrpe' specified in service 'Swap Useage' for host 'web01'not defined anywhere! Error: Service check command 'check_nrpe'specified in service 'Current Load' for host 'web02' not definedanywhere! Error: Service check command 'check_nrpe' specified inservice 'Disk Iostat' for host 'web02' not defined anywhere! Error:Service check command 'check_nrpe' specified in service 'DiskPartition' for host 'web02' not defined anywhere! Error: Servicecheck command 'check_nrpe' specified in service 'MEM Useage' forhost 'web02' not defined anywhere! Error: Service check command'check_nrpe' specified in service 'Swap Useage' for host 'web02'not defined anywhere! Checked 12 services. Checking hosts...Checked 2 hosts. Checking host groups... Checked 1 host groups.Checking service groups... Checked 0 service groups. Checkingcontacts... Checked 1 contacts. Checking contact groups... Checked1 contact groups. Checking service escalations... Checked 0 serviceescalations. Checking service dependencies... Checked 0 servicedependencies. Checking host escalations... Checked 0 hostescalations. Checking host dependencies... Checked 0 hostdependencies. Checking commands... Checked 24 commands. Checkingtime periods... Checked 5 time periods. Checking for circular pathsbetween hosts... Checking for circular host and servicedependencies... Checking global event handlers... Checkingobsessive compulsive processor commands... Checking miscsettings... Total Warnings: 0 Total Errors: 10 ***> One or moreproblems was encountered while running the pre-flight check...Check your configuration file(s) to ensure that they contain validdirectives and data defintions. If you are upgrading from aprevious version of Nagios, you should be aware that somevariables/definitions may have been removed or modified in thisversion. Make sure to read the HTML documentation regarding theconfig files, as well as the 'Whats New' section to find out whathas changed. CONFIG ERROR! Check your Nagios configuration.
根据错误提示,我们可以知道,是check_nrpe插件没有定义导致的。来看一下解决方法。
首先,需要在commands.cfg中加入check_nrpe的插件配置。
[root@nagios-server objects]# vi commands.cfg#<==进入后按shift+g键切到结尾 加入下面内容 # 'check_nrpe' command definitiondefine command{ command_name check_nrpe command_line$USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$ }
此时重新执行检查语法的命令:
[root@nagios-server objects]# /etc/init.d/nagios checkconfig#<==这是Nagios启动脚本里检查语法的命令,默认情况只能检查是否有错,但不显示错误的详细信息 TotalWarnings: 0 Total Errors: 0
此时,警告和错误都为0,这表示已经OK了,一般来讲警告可以忽略,错误必须要解决掉,否则,无法继续,调试完成后启动Nagios服务。
[root@nagios-server objects]# /etc/init.d/nagios start Startingnagios: done. #<==如果Nagios已经启动了,就执行/etc/init.d/nagiosreload,修改配置文件可以不用restart
此时可以看到自己配置的本地各系统状态的监控成果了,如图14-13所示。
图14-13 监控主机针对本地各系统状态监控的成果
事实上,很可能有不少读者配置到此处时会报错,一起来看看都会碰到哪些错误。
当打开浏览器,查看左边的Hosts和Services时,如果无服务内容,而是出现下面英文错误提示:
It appears as though you do not have permission to viewinformation for any of the services you requested...
If you believe this is an error,check the HTTP serverauthentication requirements for accessing this CGIand check theauthorization options in your CGI configuration file.
则表示登录Web端的用户oldboy没有被许可查看这些服务资源,可按照如下方法解决上面的问题。
[root@nagios-server objects]# cd /usr/local/nagios/etc[root@nagios-server etc]# grep ^"authorized_for" cgi.cfgauthorized_for_system_information=nagiosadminauthorized_for_configuration_information=nagiosadminauthorized_for_system_commands=nagiosadminauthorized_for_all_services=nagiosadminauthorized_for_all_hosts=nagiosadminauthorized_for_all_service_commands=nagiosadminauthorized_for_all_host_commands=nagiosadmin
上述authorized开头的行对应的都是不同权限,可以看到,在结尾的许可处用户都是nagiosadmin。此时,把在前文中建立的NagiosWeb登录用户oldboy加到每一个许可项的后面,注意两者用逗号隔开。
[root@nagios-server etc]# sed -i 's#nagiosadmin#oldboy#g'cgi.cfg [root@nagios-server etc]# grep ^"authorized_for" cgi.cfgauthorized_for_system_information=oldboyauthorized_for_configuration_information=oldboyauthorized_for_system_commands=oldboyauthorized_for_all_services=oldboy authorized_for_all_hosts=oldboyauthorized_for_all_service_commands=oldboyauthorized_for_all_host_commands=oldboy
提示:
1)理想情况最好换掉默认管理员用户nagiosadmin,替换成自己的oldboy等,然后根据需求加适合自己的权限,比如,给别人看只给浏览权限就可以了。具体见cgi.cfg。
2)遇到调试问题注意查看/usr/local/nagios/var/nagios.log,这点很重要。高手都这么做的。
3)vi命令行替换。使用:g/nagiosadmin/s//oldboy/g或:%s/oldboy/test/g或sed替换。
4)reload nagios命令为:/etc/init.d/nagiosreload,此处为cgi.cfg的修改,也可以不执行reload。
现在,重新加载配置使得修改生效。
[root@nagios-server etc]# /etc/init.d/nagios reload Runningconfiguration check...done. Reloading nagiosconfiguration...done
正常监控后的效果图见表14-5。
表14-5 主机监控效果图
下面看看服务监控效果图。刚配置完的主机监控图如图14-14所示。
图14-14 新增主机重启未确定时的显示图
这里“PENDING”的意思是服务器还没确定其状态,过一会再刷新查看,状态如图14-15所示。
其中绿色的表示一切正常,红色背景的表示有故障,在Web02还没开启nrpe服务的情况下,开启之后整体效果如图14-16所示。
如果出现如图14-17所示的报错,对于第①个问题“NRPE:Unable to readoutput”,解决办法见下文。第②个问题“NRPE:Command'check_disk'notdefined”,一共三个类似的错误,提示NRPE命令check_disk等未定义,解决办法也在下文中。
图14-15 新增主机重启后监控显示图
图14-16 新增主机重启后正常后监控显示图
图14-17 常见监控报错显示图
4.被动模式下基于Nagios监控原理排错的案例
还记得前文的nrpe原理图么?来回顾一下,见图14-18。
根据Nagios被动模式的监控原理进行错误排查的方法为:查看check_nrpe插件帮助。
Nagios被动模式的监控原理离不开check_nrpe这个插件,下面我们就看看check_nrpe插件帮助。
图14-18 回顾nrpe原理图
[root@nagios-server libexec]# ./check_nrpe --help NRPE Pluginfor Nagios…… Usage: check_nrpe -H [-n] [-u] [-p ] [-t ] [-c ] [-a] Options: -n = Do no use SSL -u = Make socket timeouts return anUNKNOWN state instead of CRITICAL = The address of the hostrunning the NRPE daemon #==>客户端的IP地址 [port] = The port on whichthe daemon is running (default=5666) [timeout] = Number of secondsbefore connection times out (default=10) [command] = The name ofthe command that the remote daemon should run#==>客户端nrpe配置文件里的命令名 [arglist] = Optional arguments that shouldbe passed to the command. Multiple arguments should be separated bya space. If provided, this must be the last option supplied on thecommand line.……
Nagios被动模式的监控原理其实就是利用下面这个命令工作的:
/usr/local/nagios/libexec/check_nrpe -H 10.0.0.8 -ccheck_mem
下面来看两个模拟Nagios配置错误的案例。
第一个案例模拟:取消check_memory.pl脚本的执行权限,模拟前面的错误。
[root@web01 ~]# cd /usr/local/nagios/libexec/ [root@web01libexec]# chmod a-x check_memory.pl [root@web01 libexec]# ls -lcheck_memory.pl
重启Nagios后,此时页面内存服务监控行出现出错提示。
NRPE: Unable to read output
第二个案例模拟:把nrpe.cfg中的命令名写错,如下:
command[check_disk1]=/usr/local/nagios/libexec/check_disk -w 15%-c 7% -p / #==>check_disk1多个数字1。
重启Nagios后,此时页面磁盘服务监控行出现如下出错提示。
NRPE: Command 'check_disk' not defined
下面就针对上面两个案例进行故障排查。
案例1:NRPE:Unable to read output排查
1)在Nagios服务器端执行如下命令:
[root@nagios-server libexec]#/usr/local/nagios/libexec/check_nrpe -H 10.0.0.8 -c check_mem NRPE:Unable to read output #==可以看到这里和页面报错是一样的
2)在客户端本地执行命令脚本检查(就是command[check_mem]=后面对应的脚本)。
[root@web01 libexec]# /usr/local/nagios/libexec/check_memory.pl-w 10% -c 3% #==>这个命令最好是复制nrpe.cfg里的配置,不要手敲,防止出错 -bash:/usr/local/nagios/libexec/check_memory.pl: Permission denied#==>提示拒绝,所以,原因就找到了。因为check_memory.pl脚本无执行权限导致的NRPE: Unable toread output错误 [root@web01 libexec]# chmod a+x/usr/local/nagios/libexec/check_memory.pl [root@web01 libexec]#/usr/local/nagios/libexec/check_memory.pl -w 10% -c 3% CHECK_MEMORYOK - 95M free | free=100634624b;12019302.4:;3605790.72:
3)此时在Nagios服务器端执行如下命令:
[root@nagios-server libexec]#/usr/local/nagios/libexec/check_nrpe -H 10.0.0.8 -c check_memCHECK_MEMORY OK - 95M free | free=99807232b;12019302.4:;3605790.72:#==>提示OK了,那么页面应该也是正常的了
特别说明:如果步骤2正常但是步骤3依然不正常,此时可以在客户端本地修改nrpc.cfg的allows_hosts,增加一个本机IP地址10.0.0.8,然后在本机执行:
/usr/local/nagios/libexec/check_nrpe-H 10.0.0.8-c check_mem
来检查,从而排除是不是nrpe进程及nrpe配置文件的问题。
如果上面的可以,但从服务器端依然不行,一般就是网络原因或者软件版本等问题了,这类问题发生的几率很小。
案例2:NRPE:Command'check_disk'not defined排查
该案例涉及的是磁盘监控故障,排查思路如下:
在Nagios服务器端执行如下命令:
[root@nagios-server libexec]#/usr/local/nagios/libexec/check_nrpe -H 10.0.0.8 -c check_diskNRPE: Command 'check_disk' not defined
如果大家理解了Nagios被动监控的原理,很容易判断上面问题的故障在于nrpe.cfg中check_disk相关命令写错了,或者没配。
根据Nagios被动模式监控原理排查问题的思路如下。
1)在服务器端执行如下命令看是否返回数据。
/usr/local/nagios/libexec/check_nrpe -H 10.0.0.8 -c check_mem#<==10.0.0.8为客户端IP地址。
2)如果前面无法正确返回数据,可在客户端10.0.0.8本地执行如下命令看是否返回数据。
/usr/local/nagios/libexec/check_nrpe -H 127.0.0.1 -ccheck_mem
3)如果前面还是无法正确返回数据,则执行nrpe.cfg里下面配置的内容中等号后面的命令。
command[check_mem]=/usr/local/nagios/libexec/check_memory.pl -w10% -c 3%
即执行
/usr/local/nagios/libexec/check_memory.pl -w 10% -c 3%
如果以上3步都OK,那么就找服务器端原因,一般是配置文件以及命令插件问题。
5.添加http服务的URL地址及端口监控
现在增加从Nagios服务器端发起的监控,如URL地址、端口监控等。此类服务一般都开启了对外提供服务的业务。这样的业务,一般采用主动监控的方式,当然了,也可以写脚本通过被动的方式来监控,但一般不这么做。
URL监控的实质是通过命令行理解HTTP的监控原理,如下:
[root@nagios-server service]#/usr/local/nagios/libexec/check_http -H 10.0.0.8 HTTP OK HTTP/1.1200 OK - 266 bytes in 0.006 seconds |time=0.006011s;;;0.000000size=266B;;;0 [root@nagios-server service]#/usr/local/nagios/libexec/check_http -H 10.0.0.8 -u / index.htmlHTTP OK HTTP/1.1 200 OK - 266 bytes in 0.001 seconds|time=0.001147s;;;0.000000 size=266B;;;0
下面是对域名URL地址http://blog.etiantian.org进行监控的配置,将要监控的服务配置到services.cfg中即可。
#url examples http:// blog.etiantian.org define service{ usegeneric-service host_name web01 service_description blog_urlcheck_command check_weburl!-H blog.etiantian.org }
当然也可以不用自己定义check_weburl,而使用配置自带的check_http。
define service { use generic-service host_name web01service_description http_url check_command check_http }
若是要对复杂URL地址进行监控(例如:http://blog.etiantian.org/oldboy/test.html),可采用如下方式:
define service{ use generic-service host_name web01service_description blog_oldboy_url check_command check_weburl!-Hblog.etiantian.org -u /oldboy/test.html#<==check_weburl是检查命令,在command.cfg里定义的,后面就是真正检查的URL地址了,即http://blog.etiantian.org/oldboy/test.html,在该命令中,-u后加域名后面的地址。#<==[root@nagios-server objects]#/usr/local/nagios/libexec/check_http --help #<==-u, --url=PATH#<== URL to GET or POST (default: /) }
若是集群中有多个URL,可采用如下方式监控。
define service { use generic-service host_name web01service_description http_url_oldboy check_command check_http! -u/oldboy/test.html }
对特殊的带传参的URL地址进行监控时,方式如下:
例如:http://blog.etiantian.org/article/index.phpm=article&a=list&id=670define service{ use generic-service host_name web01service_description blog_oldboy_url check_command check_weburl!-Hblog.etiantian.org -u"/article/index.phpm=article&a=list&id=670" }
6.配置好URL后检查Nagios语法
若在配置好URL后,就执行如下命令检查Nagios语法:
[root@nagios-server objects]#/etc/init.d/nagios checkconfig
会发现报错了:
Checking services... Error: Service check command 'check_weburl'specified in service 'blog_oldboy_url' for host '08-web01' notdefined anywhere! Error: Service check command 'check_weburl'specified in service 'blog_url' for host '08-web01' not definedanywhere!…省略若干… Total Warnings: 0 Total Errors: 2
根据报错信息可以知道,是因为在command.cfg配置文件中没有定义check_weburl插件导致的。因此,需要在commands.cfg中加入check_weburl的插件配置。
# 'check_weburl' command definition define command{ command_namecheck_weburl command_line $USER1$/check_http $ARG1$ -w 10 -c 30 } #'check_http' command definition define command{ command_namecheck_http command_line $USER1$/check_http -I $HOSTADDRESS$ $ARG1$} #==>实际就是利用check_http插件,查看帮助的方法如下: [root@web01 ~]# cd/usr/local/nagios/libexec/ [root@web01 libexec]# ./check_http-help
如果是测试域名URL监控,注意在Nagios服务器端的/etc/hosts下加如下域名解析:
10.0.0.8 blog.etiantian.org #10.0.0.8 为要监控的URL所在的服务器IP地址#如果你在客户端没有配也可以不加,在笔者的公网博客已经给出了测试地址。
特别说明:为了防止生产服务器频繁误报,在生产环境有时也会将域名解析成对应DNSA记录的IP并放在/etc/hosts里保存,这样可以防止因监控服务器本身的DNS问题导致误报。但这也有一个小问题,就是不能监控到DNS导致的域名解析故障了。
此外,利用别名可实现对集群下面同样节点的URL监控。
web1 blog.etiantian.org,blog1.etiantian.org web2blog.etiantian.org,blog2.etiantian.org
此时执行检查语法的命令为:
[root@nagios-server objects]#/etc/init.d/nagios checkconfigTotal Warnings: 0 Total Errors: 0
使配置文件生效的命令为:
/etc/init.d/nagios reload
到这里,可以看到自己配置的URL监控成果了,如图14-19所示。
图14-19 新增监控URL服务正常后结果显示图
说明:未生效前为灰色PENDING状态,需要等待。
7.监控任意TCP端口举例
端口监控的实质就是执行如下命令去监控:
[root@nagios-server service]#/usr/local/nagios/libexec/check_tcp -H 10.0.0.8 -p 80 TCP OK -0.000 second response time on port80|time=0.000461s;;;0.000000;10.000000 [root@nagios-serverobjects]#vi services.cfg define service{ use generic-servicehost_name web01 service_description ssh_22 check_commandcheck_tcp!22 } define service{ use generic-service host_name web01service_description http_80 check_command check_tcp!80 }
这里的check_tcp为Nagiosplugin默认插件,commands.cfg会自动配置进去,不需要添加。此外,注意多端口同时监控的写法。
说明:从多年的监控经验看,端口检查也是很不错的辅助监控方式!对于要求高的业务,一定要模拟真正用户的访问行为监控才好。
监控结果如图14-20所示。
图14-20 监控端口效果图
如果想对端口做精细控制,则须在commands.cfg里做配置。下面以对Memcached服务进行监控为例,首先在commands.cfg里添加如下监控Memcached的命令:
define command { command_name check_memcached_11111 command_line$USER1$/check_tcp -H $HOSTADDRESS$ -p 11111 -t 5 -E -s 'statsquit'-e 'uptime' -M crit }
然后在services.cfg里添加如下命令:
######memcached check start #### # memcached验证码缓存create byoldboy 20080201 define service { use generic-service host_nameweb01 service_description Memcache_11111 check_commandcheck_memcached_11111 }
别忘了检查语法,reload看结果(如图14-21所示)。
图14-21 配置监控Memcached效果图
Memcached服务监控很管用,在生产环境下,即使是Memcached连接数不够用也都能报警。当然Memcached监控还有更精细的监控方法,下面会给出一定的样例,有问题可加开篇的QQ群交流。
样例如下(需要单独安装Memcached插件才可以按如下配置):
### check response time(msec) for memcached define command {command_name check_memcached_response command_line$USER1$/check_memcached -H $HOSTADDRESS$ -w 300 -c 500 } ### checkcache size ratio(bytes/limit_maxbytes[%]) for memcached definecommand { command_name check_memcached_size command_line$USER1$/check_memcached -H $HOSTADDRESS$ --size-warning 90--size-critical 95 } ### check cache hit ratio(get_hits/cmd_get[%])for memcached define command { command_name check_memcached_hitcommand_line $USER1$/check_memcached -H $HOSTADDRESS$ --hit-warning40 --size-critical 20 }
第13章中监控Memcached服务的脚本,大家还记得么?可酌情使用。
实例暂时就讲这些,关于如何让不同服务在不同时间报警给不同用户,如何配置nfs、rsync、drbd、MySQL、Oracle等特殊服务的监控,如何分组监控,如何让多个运维值班协调解决问题等内容,见后文的手工开发插件部分。Nagios很复杂,不仅可以做上面的事,还可以写插件监控业务层的问题,甚至可以监控到服务器的温度及硬件信息。而且,Nagios给你扩展开发的机会很多,请大家多去摸索。这里针对上面的内容小结一下:
一般客户端对外开启的服务,都会采用主动模式监控,例如:port、URL。
主动模式的监控配置过程如下:
1)在服务器端的命令行把要监控的命令先调试好。
2)在commands.cfg里定义Nagios命令,同时调用命令行的插件。
3)在服务的配置文件里定义要监控的服务,调用commands.cfg里定义Nagios的监控命令!
课后作业:
·请用主动及被动模式分别监控MySQL主从同步(check_mysql)!
·根据不同管理员显示不同的主机和服务,根据用户分类显示主机和服务。 跟老男孩学Linux运维:Web集群实战