[关闭]
@cdmonkey 2015-04-30T03:02:09.000000Z 字数 7363 阅读 990

Nagios(2)客户端安装

Nagios


六、模板

1. 主机参数

  1. define host {
  2. use linux-server #指定使用的模版。
  3. host_name WEB-A1 #定义的主机名,可以根据实际情况(例如提供的服务及地址等)进行配置。
  4. alias WEB-A1
  5. address 172.16.1.10
  6. check_command check_host_alive
  7. #检测主机存活的指令。如果此项留空,Nagios将不会去判断主机是否存活。该指令同样必须由配置文件进行定义。
  8. max_check_attempts 3 #取值整数,当主机存活检查命令的返回值不是"OK"时,重试的次数。
  9. normal_check_interval 2 #正常情况下检查的时间间隔,单位是分钟。
  10. retry_check_interval 2 #主机故障后再次检查的时间间隔,单位是分钟。
  11. check_period 24×7 #检测时间段名称,这里只是个名称,具体的时间段要写在其他的配置文件中。
  12. notification_interval 300 #主机故障后,两次报警的通知间隔,单位是分钟。
  13. notification_period 24×7 #发送提醒的时间段。如果不在该时间段内,无论发生什么问题,都不会报警。
  14. notification_options d,u,r #主机状态通知选项。
  15. contact_groups admin #指定联系人组,在此组中的联系人都会收到主机的报警。
  16. }

2. 服务参数

  1. define service {
  2. use generic-service #定义该监控项目使用的模版。
  3. host_name WEB-A1,WEB-A2 #监控该项目的主机。
  4. service_description Disk Partition #这个监控项目的描述,或者说是这个项目的名称。
  5. check_command check_nrpe!check_disk #定义本监控项目所使用的指令。
  6. max_check_attempts 2 #取值整数,表示尝试检测的次数。
  7. normal_check_interval 3 #正常情况下检查的时间间隔,单位是分钟。
  8. retry_check_interval 2 #检测故障后再次检查的时间间隔,单位是分钟。
  9. check_period 24x7
  10. notification_interval 600
  11. notification_period 24x7
  12. notification_options w,u,c,r #通知的服务状态选项。
  13. contact_groups admin #指定联系人组。
  14. process_perf_data 1
  15. }

process_perf_data [0/1]:其作用是指出是否启用Nagios的数据输出功能,与PNP出图记录数据相关。

此处输入图片的描述

3. 配置模板

上面的主机参数及服务参数中的许多相同部分的内容可以写入模板文件中,而不用每个定义的对象都写上很多行的条目。

  1. [root@Nagios ~]# cd /usr/local/nagios/etc/objects/
  2. [root@Nagios objects]# vim templates.cfg
  3. ----------------------
  4. # Generic contact definition template:
  5. define contact{
  6. name generic-contact
  7. service_notification_period 24x7 #服务出现异常时,发送通知的时间段。
  8. host_notification_period 24x7 #主机出现异常时,发送通知的时间段。
  9. service_notification_options w,u,c,r,f,s #定义服务在什么状态下需要发送通知。
  10. host_notification_options d,u,r,f,s #定义主机在什么状态下需要发送通知。
  11. service_notification_commands notify-service-by-email
  12. host_notification_commands notify-host-by-email
  13. #上面的两行定义服务或主机故障时,发送通知的方式,可以是邮件和短信,这里发送的方式是邮件。
  14. register 0
  15. }
  16. ----------------------
  17. # Generic host definition template:
  18. define host{
  19. name generic-host
  20. notifications_enabled 1
  21. event_handler_enabled 1
  22. flap_detection_enabled 1
  23. failure_prediction_enabled 1
  24. process_perf_data 1
  25. retain_status_information 1
  26. retain_nonstatus_information 1
  27. notification_period 24x7
  28. register 0
  29. }
  30. ----------------------
  31. # Linux host definition template:
  32. define host{
  33. name linux-server
  34. use generic-host
  35. check_period 24x7
  36. check_interval 5 #对主机的检查时间间隔,单位是分钟。
  37. retry_interval 1 #再次检查的时间间隔,单位是分钟。
  38. max_check_attempts 10
  39. check_command check-host-alive
  40. notification_period workhours
  41. notification_interval 120
  42. notification_options d,u,r
  43. contact_groups admins
  44. register 0
  45. }
  46. ----------------------
  47. # Generic service definition template:
  48. define service{
  49. name generic-service
  50. active_checks_enabled 1 #是否启用主动服务检查。
  51. passive_checks_enabled 1 #是否启用被动服务检查。
  52. parallelize_check 1
  53. obsess_over_service 1
  54. check_freshness 0
  55. notifications_enabled 1
  56. event_handler_enabled 1
  57. flap_detection_enabled 1
  58. failure_prediction_enabled 1
  59. process_perf_data 1
  60. retain_status_information 1
  61. retain_nonstatus_information 1
  62. is_volatile 0
  63. check_period 24x7
  64. max_check_attempts 3
  65. normal_check_interval 10
  66. retry_check_interval 2
  67. contact_groups admins
  68. notification_options w,u,c,r
  69. notification_interval 60
  70. notification_period 24x7
  71. register 0
  72. }
  73. ----------------------
  74. # Local service definition template:
  75. define service{
  76. name local-service
  77. use generic-service
  78. max_check_attempts 4
  79. normal_check_interval 5
  80. retry_check_interval 1
  81. register 0
  82. }

4. 报警

报警的方式是在与联系人的相关的配置文件(templates.cfgcontacts.cfg)中进行定义的。

  1. [root@Nagios ~]# vim /usr/local/nagios/etc/objects/contacts.cfg
  2. define contact{
  3. contact_name nagiosadmin
  4. use generic-contact
  5. alias Nagios Admin
  6. email brucemx@126.com
  7. }
  8. define contactgroup{
  9. contactgroup_name admins
  10. alias Nagios Administrators
  11. members nagiosadmin
  12. }

七、Nagios客户端的安装

此处输入图片的描述

Nagios客户端(被监控端)不需要LAMP环境,并且也不需要安装Nagios服务端软件包。

  1. #Step 1: config yum
  2. cd /etc/yum.repos.d/
  3. wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
  4. #Step 2: config profile
  5. echo 'export LC_ALL=C'>> /etc/profile
  6. source /etc/profile
  7. #Step 3: stop iptables and SELinux
  8. /etc/init.d/iptables stop
  9. chkconfig iptables off
  10. setenforce 0
  11. #Step 4: config time sync
  12. /usr/sbin/ntpdate pool.ntp.org
  13. echo '#time sync by oldboy at 2010-2-1'>>/var/spool/cron/root
  14. echo '*/10 * * * * /usr/sbin/ntpdate pool.ntp.org >/dev/null 2>&1'>>/var/spool/cron/root
  15. crontab -l
  1. #create Nagios user
  2. [root@WEB-A1 ~]# /usr/sbin/useradd -m nagios -s /sbin/nologin
  3. #安装客户端工具:
  4. [root@WEB-A1 ~]# yum -y install perl-devel perl-CPAN
  5. [root@WEB-A1 ~]# cd tools/
  6. [root@WEB-A1 tools]# tar zxf nagios-plugins-1.4.16.tar.gz
  7. [root@WEB-A1 tools]# cd nagios-plugins-1.4.16
  8. ------------------
  9. ./configure --prefix=/usr/local/nagios --enable-perl-modules --enable-redhat-pthread-workaround
  10. make && make install
  11. ------------------
  12. #注意:客户端的安装要求有LAMP的环境(目前已知:MySQL可以没有)。
  13. [root@WEB-A1 ~]# ls /usr/local/nagios/libexec/|wc -l
  14. 59

注意:NRPE daemon需要“Nagios-plugins”插件的支持,否则守护进程不能进行任何的监控工作。

然后安装NRPE:

  1. [root@WEB-A1 ~]# cd tools/
  2. [root@WEB-A1 tools]# tar zxvf nrpe-2.12.tar.gz
  3. [root@WEB-A1 tools]# cd nrpe-2.12
  4. ------------------
  5. ./configure
  6. make all
  7. make install-plugin
  8. make install-daemon
  9. make install-daemon-config
  10. ------------------
  1. #Install soft for iostat
  2. cd tools/
  3. tar zxvf Params-Validate-0.91.tar.gz
  4. cd Params-Validate-0.91
  5. perl Makefile.PL
  6. make
  7. make install
  8. cd ..
  9. ##########
  10. cd tools/
  11. tar zxvf Class-Accessor-0.31.tar.gz
  12. cd Class-Accessor-0.31
  13. perl Makefile.PL
  14. make
  15. make install
  16. cd ..
  17. ##########
  18. cd tools/
  19. tar zxvf Config-Tiny-2.12.tar.gz
  20. cd Config-Tiny-2.12
  21. perl Makefile.PL
  22. make
  23. make install
  24. cd ..
  25. ##########
  26. cd tools/
  27. tar zxvf Math-Calc-Units-1.07.tar.gz
  28. cd Math-Calc-Units-1.07
  29. perl Makefile.PL
  30. make
  31. make install
  32. cd ..
  33. ##########
  34. cd tools/
  35. tar zxvf Regexp-Common-2010010201.tar.gz
  36. cd Regexp-Common-2010010201
  37. perl Makefile.PL
  38. make
  39. make install
  40. cd ..
  41. ##########
  42. cd tools/
  43. tar zxvf Nagios-Plugin-0.34.tar.gz
  44. cd Nagios-Plugin-0.34
  45. perl Makefile.PL
  46. make
  47. make install
  48. cd ..
  49. #for monitor iostat
  50. yum install sysstat -y
  51. /bin/cp /root/tools/check_memory.pl /usr/local/nagios/libexec
  52. /bin/cp /root/tools/check_iostat /usr/local/nagios/libexec
  53. chmod 755 /usr/local/nagios/libexec/check_memory.pl
  54. chmod 755 /usr/local/nagios/libexec/check_iostat
  55. yum install -y dos2unix unix2dos
  56. dos2unix /usr/local/nagios/libexec/check_memory.pl
  57. dos2unix /usr/local/nagios/libexec/check_iostat
  58. #至此,被监控的客户端安装完毕。

修改客户端NRPE配置文件:

  1. [root@WEB-A1 tools]# cd /usr/local/nagios/etc/
  2. [root@WEB-A1 etc]# ls
  3. nrpe.cfg
  4. [root@WEB-A1 etc]# cp nrpe.cfg nrpe.cfg.bak
  5. [root@WEB-A1 etc]# vim nrpe.cfg
  6. #在第79行处,将监控服务器端的IP加入,即客户端允许指定的服务端进行监控:
  7. allowed_hosts=127.0.0.1,172.16.1.22
  8. #然后将199-203的五行删除或者注释掉,替换为下面的五行内容:
  9. command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,6 -c 30,25,20
  10. command[check_mem]=/usr/local/nagios/libexec/check_memory.pl -w 6% -c 3%
  11. command[check_disk]=/usr/local/nagios/libexec/check_disk -w 20% -c 8% -p /
  12. command[check_swap]=/usr/local/nagios/libexec/check_swap -w 20% -c 10%
  13. command[check_iostat]=/usr/local/nagios/libexec/check_iostat -w 6 -c 10
  14. #上面这几行的作用是指出应该要如何调用插件程序。

客户端需要修改的配置文件只有nrpe.cfg

至此,我们可以启动NRPE服务了。

  1. #start nagios client
  2. pkill nrpe
  3. sleep 2
  4. #下面这行指令就是客户端启动NRPE守护进程的方式:
  5. /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
  6. echo "#nagios nrpe process cmd by oldboy 2012-6-7" >> /etc/rc.local
  7. echo "/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d">>/etc/rc.local
  8. netstat -lnt|grep 5666 && echo "nagios client is ok"
  9. ------------------
  10. #如果出现下面的内容,则代表客户端安装完成:
  11. tcp 0 0 0.0.0.0:5666 0.0.0.0:* LISTEN
  12. nagios client is ok

八、用户管理

在监控的维护过程中,或许会碰到以下两种情况:监控的界面不光显示了主机当前状态,而且有很多的附加功能可以采用通过web界面向监控系统传送命令,例如暂时停止监控系统检查某个主机或者暂时停止某个监控项目的报警等。对于一般的管理员而言,我们只希望他能看,而不能做这些命令操作,在此不妨称之为只读(READ-ONLY)。也就是说,决不能够将“admin”权限开放给普通的浏览人员。

那么要如何让创建只读账号呢?首先要编辑文件cgi.cfg,开启只读账号:

  1. #119:
  2. authorized_for_system_information=admin,view
  3. #157:
  4. authorized_for_all_services=admin,view
  5. authorized_for_all_hosts=admin,view
  6. #182:
  7. #READ-ONLY USERS
  8. authorized_for_read_only=view

其次,创建只读账号:

  1. #在原有的认证文件中添加一个用户:
  2. [root@nagios-new etc]# htpasswd -b /usr/local/nagios/etc/htpasswd.users view view
  3. Adding password for user view
  4. ------------------
  5. #查看当前已有的用户:
  6. [root@nagios-new etc]# cat htpasswd.users
  7. admin:sG1oBNuC6OvlU
  8. view:c5PCEix0WlL2A
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注