@cdmonkey
2015-04-30T03:02:09.000000Z
字数 7363
阅读 1202
Nagios
define host {use linux-server #指定使用的模版。host_name WEB-A1 #定义的主机名,可以根据实际情况(例如提供的服务及地址等)进行配置。alias WEB-A1address 172.16.1.10check_command check_host_alive#检测主机存活的指令。如果此项留空,Nagios将不会去判断主机是否存活。该指令同样必须由配置文件进行定义。max_check_attempts 3 #取值整数,当主机存活检查命令的返回值不是"OK"时,重试的次数。normal_check_interval 2 #正常情况下检查的时间间隔,单位是分钟。retry_check_interval 2 #主机故障后再次检查的时间间隔,单位是分钟。check_period 24×7 #检测时间段名称,这里只是个名称,具体的时间段要写在其他的配置文件中。notification_interval 300 #主机故障后,两次报警的通知间隔,单位是分钟。notification_period 24×7 #发送提醒的时间段。如果不在该时间段内,无论发生什么问题,都不会报警。notification_options d,u,r #主机状态通知选项。contact_groups admin #指定联系人组,在此组中的联系人都会收到主机的报警。}
define service {use generic-service #定义该监控项目使用的模版。host_name WEB-A1,WEB-A2 #监控该项目的主机。service_description Disk Partition #这个监控项目的描述,或者说是这个项目的名称。check_command check_nrpe!check_disk #定义本监控项目所使用的指令。max_check_attempts 2 #取值整数,表示尝试检测的次数。normal_check_interval 3 #正常情况下检查的时间间隔,单位是分钟。retry_check_interval 2 #检测故障后再次检查的时间间隔,单位是分钟。check_period 24x7notification_interval 600notification_period 24x7notification_options w,u,c,r #通知的服务状态选项。contact_groups admin #指定联系人组。process_perf_data 1}
process_perf_data [0/1]:其作用是指出是否启用Nagios的数据输出功能,与PNP出图记录数据相关。

上面的主机参数及服务参数中的许多相同部分的内容可以写入模板文件中,而不用每个定义的对象都写上很多行的条目。
[root@Nagios ~]# cd /usr/local/nagios/etc/objects/[root@Nagios objects]# vim templates.cfg----------------------# Generic contact definition template:define contact{name generic-contactservice_notification_period 24x7 #服务出现异常时,发送通知的时间段。host_notification_period 24x7 #主机出现异常时,发送通知的时间段。service_notification_options w,u,c,r,f,s #定义服务在什么状态下需要发送通知。host_notification_options d,u,r,f,s #定义主机在什么状态下需要发送通知。service_notification_commands notify-service-by-emailhost_notification_commands notify-host-by-email#上面的两行定义服务或主机故障时,发送通知的方式,可以是邮件和短信,这里发送的方式是邮件。register 0}----------------------# Generic host definition template:define host{name generic-hostnotifications_enabled 1event_handler_enabled 1flap_detection_enabled 1failure_prediction_enabled 1process_perf_data 1retain_status_information 1retain_nonstatus_information 1notification_period 24x7register 0}----------------------# Linux host definition template:define host{name linux-serveruse generic-hostcheck_period 24x7check_interval 5 #对主机的检查时间间隔,单位是分钟。retry_interval 1 #再次检查的时间间隔,单位是分钟。max_check_attempts 10check_command check-host-alivenotification_period workhoursnotification_interval 120notification_options d,u,rcontact_groups adminsregister 0}----------------------# Generic service definition template:define service{name generic-serviceactive_checks_enabled 1 #是否启用主动服务检查。passive_checks_enabled 1 #是否启用被动服务检查。parallelize_check 1obsess_over_service 1check_freshness 0notifications_enabled 1event_handler_enabled 1flap_detection_enabled 1failure_prediction_enabled 1process_perf_data 1retain_status_information 1retain_nonstatus_information 1is_volatile 0check_period 24x7max_check_attempts 3normal_check_interval 10retry_check_interval 2contact_groups adminsnotification_options w,u,c,rnotification_interval 60notification_period 24x7register 0}----------------------# Local service definition template:define service{name local-serviceuse generic-servicemax_check_attempts 4normal_check_interval 5retry_check_interval 1register 0}
报警的方式是在与联系人的相关的配置文件(templates.cfg、contacts.cfg)中进行定义的。
[root@Nagios ~]# vim /usr/local/nagios/etc/objects/contacts.cfgdefine contact{contact_name nagiosadminuse generic-contactalias Nagios Adminemail brucemx@126.com}define contactgroup{contactgroup_name adminsalias Nagios Administratorsmembers nagiosadmin}

Nagios客户端(被监控端)不需要LAMP环境,并且也不需要安装Nagios服务端软件包。
#Step 1: config yumcd /etc/yum.repos.d/wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo#Step 2: config profileecho 'export LC_ALL=C'>> /etc/profilesource /etc/profile#Step 3: stop iptables and SELinux/etc/init.d/iptables stopchkconfig iptables offsetenforce 0#Step 4: config time sync/usr/sbin/ntpdate pool.ntp.orgecho '#time sync by oldboy at 2010-2-1'>>/var/spool/cron/rootecho '*/10 * * * * /usr/sbin/ntpdate pool.ntp.org >/dev/null 2>&1'>>/var/spool/cron/rootcrontab -l
#create Nagios user[root@WEB-A1 ~]# /usr/sbin/useradd -m nagios -s /sbin/nologin#安装客户端工具:[root@WEB-A1 ~]# yum -y install perl-devel perl-CPAN[root@WEB-A1 ~]# cd tools/[root@WEB-A1 tools]# tar zxf nagios-plugins-1.4.16.tar.gz[root@WEB-A1 tools]# cd nagios-plugins-1.4.16------------------./configure --prefix=/usr/local/nagios --enable-perl-modules --enable-redhat-pthread-workaroundmake && make install------------------#注意:客户端的安装要求有LAMP的环境(目前已知:MySQL可以没有)。[root@WEB-A1 ~]# ls /usr/local/nagios/libexec/|wc -l59
注意:NRPE daemon需要“Nagios-plugins”插件的支持,否则守护进程不能进行任何的监控工作。
然后安装NRPE:
[root@WEB-A1 ~]# cd tools/[root@WEB-A1 tools]# tar zxvf nrpe-2.12.tar.gz[root@WEB-A1 tools]# cd nrpe-2.12------------------./configuremake allmake install-pluginmake install-daemonmake install-daemon-config------------------
#Install soft for iostatcd tools/tar zxvf Params-Validate-0.91.tar.gzcd Params-Validate-0.91perl Makefile.PLmakemake installcd ..##########cd tools/tar zxvf Class-Accessor-0.31.tar.gzcd Class-Accessor-0.31perl Makefile.PLmakemake installcd ..##########cd tools/tar zxvf Config-Tiny-2.12.tar.gzcd Config-Tiny-2.12perl Makefile.PLmakemake installcd ..##########cd tools/tar zxvf Math-Calc-Units-1.07.tar.gzcd Math-Calc-Units-1.07perl Makefile.PLmakemake installcd ..##########cd tools/tar zxvf Regexp-Common-2010010201.tar.gzcd Regexp-Common-2010010201perl Makefile.PLmakemake installcd ..##########cd tools/tar zxvf Nagios-Plugin-0.34.tar.gzcd Nagios-Plugin-0.34perl Makefile.PLmakemake installcd ..#for monitor iostatyum install sysstat -y/bin/cp /root/tools/check_memory.pl /usr/local/nagios/libexec/bin/cp /root/tools/check_iostat /usr/local/nagios/libexecchmod 755 /usr/local/nagios/libexec/check_memory.plchmod 755 /usr/local/nagios/libexec/check_iostatyum install -y dos2unix unix2dosdos2unix /usr/local/nagios/libexec/check_memory.pldos2unix /usr/local/nagios/libexec/check_iostat#至此,被监控的客户端安装完毕。
修改客户端NRPE配置文件:
[root@WEB-A1 tools]# cd /usr/local/nagios/etc/[root@WEB-A1 etc]# lsnrpe.cfg[root@WEB-A1 etc]# cp nrpe.cfg nrpe.cfg.bak[root@WEB-A1 etc]# vim nrpe.cfg#在第79行处,将监控服务器端的IP加入,即客户端允许指定的服务端进行监控:allowed_hosts=127.0.0.1,172.16.1.22#然后将199-203的五行删除或者注释掉,替换为下面的五行内容:command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,6 -c 30,25,20command[check_mem]=/usr/local/nagios/libexec/check_memory.pl -w 6% -c 3%command[check_disk]=/usr/local/nagios/libexec/check_disk -w 20% -c 8% -p /command[check_swap]=/usr/local/nagios/libexec/check_swap -w 20% -c 10%command[check_iostat]=/usr/local/nagios/libexec/check_iostat -w 6 -c 10#上面这几行的作用是指出应该要如何调用插件程序。
客户端需要修改的配置文件只有
nrpe.cfg。
至此,我们可以启动NRPE服务了。
#start nagios clientpkill nrpesleep 2#下面这行指令就是客户端启动NRPE守护进程的方式:/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -decho "#nagios nrpe process cmd by oldboy 2012-6-7" >> /etc/rc.localecho "/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d">>/etc/rc.localnetstat -lnt|grep 5666 && echo "nagios client is ok"------------------#如果出现下面的内容,则代表客户端安装完成:tcp 0 0 0.0.0.0:5666 0.0.0.0:* LISTENnagios client is ok
在监控的维护过程中,或许会碰到以下两种情况:监控的界面不光显示了主机当前状态,而且有很多的附加功能可以采用通过web界面向监控系统传送命令,例如暂时停止监控系统检查某个主机或者暂时停止某个监控项目的报警等。对于一般的管理员而言,我们只希望他能看,而不能做这些命令操作,在此不妨称之为只读(READ-ONLY)。也就是说,决不能够将“admin”权限开放给普通的浏览人员。
那么要如何让创建只读账号呢?首先要编辑文件cgi.cfg,开启只读账号:
#119:authorized_for_system_information=admin,view#157:authorized_for_all_services=admin,viewauthorized_for_all_hosts=admin,view#182:#READ-ONLY USERSauthorized_for_read_only=view
其次,创建只读账号:
#在原有的认证文件中添加一个用户:[root@nagios-new etc]# htpasswd -b /usr/local/nagios/etc/htpasswd.users view viewAdding password for user view------------------#查看当前已有的用户:[root@nagios-new etc]# cat htpasswd.usersadmin:sG1oBNuC6OvlUview:c5PCEix0WlL2A