Nagios网上的教程百度一下一大把,发现有些教程讲法有点出入,估计有些都是拷贝乱贴乱飞,按有些说法去做,根本就做不下去,今天我亲自实践,现场抓图让你看得清楚看得明白,以及中间遇到什么问题,怎么解决,都会分享给大家,抓图再写出来有点辛苦,关键能学到东西一切都是值得滴,Follow me~!
下载centOS 6.0然后安装好系统,最好用vmware来做最好.
自行装好系统以后现在开始装备安装前的环境准备
我把命令贴出来,为了方便大家操作你只要跟着我把命令拷贝和粘贴即可,省去敲打命令的麻烦也避免失误出错(小技巧提醒:装好centOS以后进入x-windows里面装个vmware tools就可以实现物理机和虚拟机之间的拷贝和粘贴,方法很简单这里就不说了)
yum -y install httpd gettext mysql mysql-server mysql-devel php php-mysql php-pear
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217469IvCP.png"></a>
yum install php-mysql mysql perl mysql-server perl-DBI perl-DBD-MySQL httpd gcc glibc glibc-common gd gd-devel php
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217474LER6.png"></a>
在线下载nagios程序和插件
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217476YHru.png"></a>
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217479ED58.png"></a>
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217481Zn13.png"></a>
用户和组的准备
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217482d5qg.png"></a>
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_13182174842u03.png"></a>
开始安装主程序
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_13182174865iiA.png"></a>
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217487WM2e.png"></a>
完成以后发现执行make all命令的时候出现以下错误
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217488AEyU.png"></a>
貌似CentOS下默认系统没有安装编译器,安装一下:
yum -y install gcc automake autoconf libtool make
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_13182174892y5i.png"></a>
继续做make all操作,发现最后报错了,
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217490RQVu.png"></a>
先执行make clean操作,然后再执行 ./configure && make all 操作通过(问题分享)接着再执行以下命令即可,没有出现任何状况.
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217492xUWR.png"></a>
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217492bASk.png"></a>
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217493TZ8K.png"></a>
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217494nm7p.png"></a>
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217494YiVY.png"></a>
接下来安装插件
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217495gbQ7.png"></a>
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217496ka7S.png"></a>
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217496sffY.png"></a>
添加访问页面用户nagiosadmin及密码,这里要记住此密码不要忘记了.
htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217497KvQa.png"></a>
接下来启动相关服务
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217498XirC.png"></a>
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217500R9yh.png"></a>
检查配置文件是否正确
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_13182175019f5E.png"></a>
[经验分享:由于这一串检查的命令很长,应用率也比较高,这里我们可以自定义一个命令来代替这一长串的命令
编辑.bashrc这个文件
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217502JpEu.png"></a>
在里面用alias 来自定义一个命令来代替,这里我用check
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217503umwl.png"></a>
用source命令在当前bash环境下读取并执行bashrc中的命令
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217504XzcD.png"></a>
这样我们自定义的命令就生效了,以后只要输入check就可以检查nagios配置文件是否存在错误了,简单方便.]
发现没有警告和错误就表示通过了
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217505K4Bj.png"></a>
开了防火墙记得要开放80端口,要不然连接不到,或者直接关闭防火墙用命令service iptables stop关闭即可.
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217506u11D.png"></a>
问题分享:进入页面的时候点主机或其它选项的时候报此错误,后面百度一下原来是开启了selinux强制访问控制安全模块所致
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_13182175101qA1.png"></a>
注意:如果你开启了selinux 需要配置如下二步:
chcon -R -t httpd_sys_content_t /usr/local/nagios/sbin/
chcon -R -t httpd_sys_content_t /usr/local/nagios/share/
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217516UMYY.png"></a>
这样正宗的中文版出来了,总算迈出了成功的一小步,耐心坚持让我们继续吧~~!
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217521Heh5.png"></a>
[经验分享:我这里用虚拟机做的,记得系统和网络相关参数配置好以后做一个系统的快照,配到这里nagios安装成功以后我们再做一个快照,以免后面配乱出问题的时候我们可以返回到这里重新再配置,方便快捷,记住做快照的时候先把虚拟机关闭以后再做,要不然快照出来的文件大得吓人,而关闭以后做快照文件才几百KB.]
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217535kBxm.png"></a>
ok下面我们接着来
[nrpe_plugins指标采集程序主要是用来分析nrpe客户端的plugin脚本采集性能指标数据。目前实现的有cpu、memory、disk、buffer、singleprocess、oracle、sybase等方面的性能指标的分析,这个插件需要分别安装在主控端和被控端上,这个插件需要openssl的支持,没有就直接在线安装一下(yum install openssl-devel)所以还需在nagios主机和被控端安装,感觉挺麻烦,没办法它需要咱们就跟着做哈.]
先把nrpe下载下来先.
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217539vvq3.png"></a>
解压文件
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217540Gnz5.png"></a>
编译安装
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217541b732.png"></a>
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217541lruz.png"></a>
完成以后没有出错就可以看到以下生成的文件了
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217542PzT5.png"></a>
二:配置PNP
[PNP是一个基于php和perl,用rrdtool将nagios采集的数据绘制图表的工具,所以安装pnp之前必须先安装php perl and rrdtool这里用yum直接来安装rrdtool,单独下载安装有很多依存关系,挺麻烦.]
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_13182175450XZL.png"></a>
我这里找到最新的版本是0.6.15
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217549R9Ax.png"></a>
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217550KoGj.png"></a>
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_13182175541THM.png"></a>
编译的时候报错了.
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217554RfNw.png"></a>
yum install perl-Time-HiRes
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_13182175551BTI.png"></a>
继续编译发现有个perl modules没有发现,好像提示用来加速什么的,暂时不管它了
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217558pQ3T.png"></a>
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217560Tg4d.png"></a>
根据提示需要继续
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217561AJ6b.png"></a>
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217563eYen.png"></a>
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217564iUBq.png"></a>
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217564tLqT.png"></a>
然后把相关后缀带sample文件变更成无sample后缀
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217566tuyv.png"></a>
配置Nagios来启用PNP
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217569juuh.png"></a>
enable_environment_macros=1默认是开启了的
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217571SJoC.png"></a>
pnp4nagios有很多种工作模式,这里我们选用同步模式,想选其它模式可以看说明都有详细配置教程(不同模式配置不同)
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217575mwPA.png"></a>
这里我们按说明的设定方法来做
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217578wuMC.png"></a>
[这里先把pnp4nagios执行数据收集的文件拷贝到nagios的libexec下,方便统一命令的执行路径.]
cp /usr/local/pnp4nagios/libexec/process_perfdata.pl /usr/local/nagios/libexec/
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217581Tla8.png"></a>
然后在commands.cfg里面加入如下(这里是同步模式设定方法)
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217582xBv3.png"></a>
#process-host-perfdata' command definition
define command{
command_name process-host-perfdata
command_line $USER1$/process_perfdata.pl -d HOSTPERFDATA
}
#process-service-perfdata' command definition
command_name process-service-perfdata
command_line $USER1$/process_perfdata.pl
}
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217584gWCZ.png"></a>
然后添加小太阳模版,镶嵌在nagios页面上
[这里有两种查看图的方法,第一种方法是你当太阳的图标的时候会在新窗口中打开图形查看,第二种方法是你只需要把鼠标移动到太阳图标上面就会自动弹出图形出来,以下两种方法你可以根据自己喜好选其中一种即可]
第一种方法在templates.cfg里面加入如下
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217585u0DM.png"></a>
define host {
name host-pnp
action_url /pnp4nagios/index.php/graph?host=$HOSTNAME$&srv=_HOST_
register 0
}
define service {
name srv-pnp
action_url /pnp4nagios/index.php/graph?host=$HOSTNAME$&srv=$SERVICEDESC$
}
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_13182175869OTO.png"></a>
第二种方法按照说明加入如下
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217589fIsM.png"></a>
拷贝status-header.ssi到/usr/local/nagios/share/ssi/目录下面来(这一步很重要,要不然移动到太阳标记上出不来图)
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217595jofT.png"></a>
action_url /pnp4nagios/index.php/graph?host=$HOSTNAME$&srv=_HOST_' class='tips' rel='/pnp4nagios/index.php/popup?host=$HOSTNAME$&srv=_HOST_
action_url /pnp4nagios/index.php/graph?host=$HOSTNAME$&srv=$SERVICEDESC$' class='tips' rel='/pnp4nagios/index.php/popup?host=$HOSTNAME$&srv=$SERVICEDESC$
第二种方法最终效果图
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217599pzVk.png"></a>
为目标主机图标旁边添加一个小太阳标记
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217607oFY2.png"></a>
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217608LpzG.png"></a>
为相关服务添加一个小太阳标记
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217610wwyB.png"></a>
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217610ewIk.png"></a>
先做一下pnp4nagios环境测试,发现php-gd模块无效,用命令yum install php-gd安装成功以后还是不行,后来经老男孩指点用yum install php* -y安装然后重启系统成功通过,
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217614YOuU.png"></a>
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217619snnC.png"></a>
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217621Z9Tn.png"></a>
按要求重命名install.php
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217624iAcv.png"></a>
做好相关配置以后请用命令重启一下nagios的服务
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217625etEd.png"></a>
ok,到此总算配置好了PNP,配置了这么多有点累了,但我们还要继续,配置本章最后一个Nagios WEB管理工具叫NagiosQL(注:还有同样一个Nagios WEB管理工具叫Nconf)
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217626kl3Q.png"></a>
NagiosQL是一个WEB管理工具,只要把其放到下Apache即可。因为这里它要和Nagios整合到一起,为了方便管理,把其放在Nagios目录下
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_13182176272XYu.png"></a>
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_131821762872xP.png"></a>
建立nagiosQL导出nagios配置文件的目录,并修改权限
建目录和改权限
mkdir -p /etc/nagiosql/{hosts,services,backup/{,hosts,services},import}
chown -R apache:nagios /etc/nagiosql/
chmod -R 755 /etc/nagiosql
chmod 777 /usr/local/nagios/nagiosql/config
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217629cK6T.png"></a>
Nagios相关文件权限的设定
chown nagios:apache /usr/local/nagios/etc
chown nagios:apache /usr/local/nagios/etc/{nagios.cfg,cgi.cfg}
chown nagios:apache /usr/local/nagios/etc/*
chmod 664 /usr/local/nagios/etc/{nagios.cfg,cgi.cfg}
chmod 775 /usr/local/nagios/etc
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217631bs7V.png"></a>
设定nagios二进制文件的权限,以便其能够通过Web程序执行
chown nagios:apache /usr/local/nagios/bin/nagios
chmod 750 /usr/local/nagios/bin/nagios
chown -R apache.nagios /usr/local/nagios/var/spool/
chown nagios:apache /usr/local/nagios/var/rw/nagios.cmd
chmod 660 /usr/local/nagios/var/rw/nagios.cmd
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217633CZZz.png"></a>
为apache添加nagiosQL虚拟目录
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217634Enq3.png"></a>
Alias /nagiosQL "/usr/local/nagios/nagiosql"
<Directory "/usr/local/nagios/nagiosql">
# SSLRequireSSL
Options None
AllowOverride None
Order allow,deny
Allow from all
</Directory>
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217635QYQR.png"></a>
重新加载httpd服务和启动mysqld服务
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_13182176378R8p.png"></a>
建立nagiosql安装文件
touch /usr/local/nagios/nagiosql/install/ENABLE_INSTALLER
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217638oDJU.png"></a>
问题分享:我在访问的时候提示没有权限访问,搞了半天原来是SElinux惹的祸,修改SELinux的实时运行模式,setenforce 0 (设置SELinux 成为permissive模式)这个只是临时解决方法,优点是不需重启系统生效,但如果要彻底禁用SELinux 需要在/etc/sysconfig/selinux中设置参数selinux=0
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_13182176399uMw.png"></a>
选取中文然后点开始安装按钮
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217644Ytqa.png"></a>
测试所需环境通过,直接按下一步即可
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217654Q2zr.png"></a>
删除安装目录,这里使用改权限:chmod 000 /usr/local/nagios/nagiosql/install
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217658ao5T.png"></a>
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217663r9HX.png"></a>
到这里安装步骤总算结束了,下面我们还需要进行相关配置工作.
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217673SIGo.png"></a>
管理-域-localhost---点“修改”
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217691hYof.png"></a>
按如图所示更改相应的路径即可
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217700MPXu.png"></a>
工具-导入数据 右边会列出一些配置文件,选择导入即可。
导入只是把配置文件内容导入到Mysql数据库,而使用其配置文件时,是一个个cfg文件,所以下面要进行写入过程
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217710xinc.png"></a>
工具-nagios控制-写入检测数据-写入其他数据
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_13182177327xG9.png"></a>
检查配置文件成功
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217739hcB3.png"></a>
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_13182177440tVA.png"></a>
写入之后就可以看到相应的cfg文件了,查看写入是否成功,由下面的输出可以看出写入是成功滴.
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217747Dgvz.png"></a>
修改nagios的配置文件,将原有以cfg_file开头项全部注释掉,加入上面新写入的配置文件
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217751TvMJ.png"></a>
注释掉
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_13182177559RMK.png"></a>
加入新的配置文件路径
cfg_dir=/etc/nagiosql/hosts/
cfg_dir=/etc/nagiosql/services/
cfg_file=/etc/nagiosql/commands.cfg
cfg_file=/etc/nagiosql/contactgroups.cfg
cfg_file=/etc/nagiosql/contacts.cfg
cfg_file=/etc/nagiosql/contacttemplates.cfg
cfg_file=/etc/nagiosql/hostdependencies.cfg
cfg_file=/etc/nagiosql/hostescalations.cfg
cfg_file=/etc/nagiosql/hostextinfo.cfg
cfg_file=/etc/nagiosql/hostgroups.cfg
cfg_file=/etc/nagiosql/hosttemplates.cfg
cfg_file=/etc/nagiosql/servicedependencies.cfg
cfg_file=/etc/nagiosql/serviceescalations.cfg
cfg_file=/etc/nagiosql/serviceextinfo.cfg
cfg_file=/etc/nagiosql/servicegroups.cfg
cfg_file=/etc/nagiosql/servicetemplates.cfg
cfg_file=/etc/nagiosql/timeperiods.cfg
<a href="http://allanfan.blog.51cto.com/attachment/201110/10/520839_1318217763qBaP.png"></a>
检查nagios的配置文件是否有问题,然后生重启nagios服务,至此整个搭建过程终于完成了,下一节将讲解重头戏,怎么用nagiosQL添加需要监控的主机以及启用nagios报警机制,待续~~!
NagiosQL添加监控主机及Nagios上启用报警机制
<a href="http://allanfan.blog.51cto.com/520839/704288">http://allanfan.blog.51cto.com/520839/704288</a>
本文转自allanhi 51CTO博客,原文链接:http://blog.51cto.com/allanfan/684169,如需转载请自行联系原作者