Centos 7安装与配置nagios监控详细图解(二)
目 录
相关链接:
Centos 7安装与配置nagios监控详细图解(一):
http://seiang.blog.51cto.com/10819863/1916303
一、安装规划
1.1系统环境
系统版本号 |
[[email protected] ~]# cat /etc/centos-release CentOS Linux release 7.1.1503 (Core) |
监控主机 (一台) |
IP地址:10.1.1.66/24 主机名称:seiang.comsys.com |
远程主机 (两台) |
IP地址:10.1.1.55/24 主机名称:nagios-slave-55
IP地址:10.1.1.58/24 主机名称:nagios-slave-58 |
分区情况 |
安装时使用默认分区(使用 df 命令来查看) |
一共3台机器,监控主机我选择根据自己的需求进行安装,两台监控主机按照CentOS 7最小化模式安装系统
1.2所需软件包
(1)监控主机
软件包 |
下载地址 |
nagios-4.2.4.tar.gz |
请到我的百度网盘下载: 链接:http://pan.baidu.com/s/1slaw6xJ 密码:q4fl |
nagios-plugins-2.1.4.tar.gz | |
nrpe-2.15.tar.gz | |
pnp4nagios-0.6.25.tar.gz |
(2)远程主机
软件包 |
下载地址 |
nagios-plugins-2.1.4.tar.gz |
请到我的百度网盘下载: 链接:http://pan.baidu.com/s/1slaw6xJ 密码:q4fl |
nrpe-2.15.tar.gz |
(1)把监控系统里的所有机器都同步一次网络时间(非常重要)
[[email protected] ~]timedatectl #该命令用来检查当前时间和时区
如果发现存在机器的时区不一致,此时就要使用命令来进行修改
[[email protected] ~]timedatectllist-timezones #该命令列出了所有的时区
[[email protected] ~]timedatectlset-timezone Asia/Shanghai #该命令把时区设置为上海
[[email protected] ~]ntpdatetime.nist.gov #该命令同步网络当前的时间
如果提示没有ntpdate命令,则安装ntp,并且配置系统自动更新时间
[[email protected] ~]# yuminstall ntp -y
[[email protected] ~]# /usr/sbin/ntpdatetime.nist.gov
[[email protected] ~]# echo'#time sync' >> /var/spool/cron/root
为了保险起见,首先禁用selinux:
[[email protected] ~]vi/etc/selinux/config
重启机器,检查selinux是否关闭:
[[email protected] ~]getenforce #如果显示enforcing则没有关闭
我在这里使用xftp上传工具,通过这个工具上传安装包到Linux
因为邮件报警服务需要安装mail功能
[[email protected] ~]yuminstall –y mailx
[[email protected] ~]yuminstall –y sendmail
[[email protected] ~]systemctlrestart sendmail.service
[[email protected] ~]mail–s 'wjq-test' [email protected](邮箱地址)
#此时进入输入模式,输入完邮件内容后按ctrl + d退出并且发送
三、监控主机安装
[[email protected] ~]# yuminstall gcc glibc glibc-common -y
[[email protected] ~]# yuminstall php php-gd perl -y
[[email protected] ~]# yuminstall httpd gd gd-devel openssl openssl-devel -y
[[email protected] ~]# systemctlenable httpd.service #设置CentOS开机启动服务
3.2增加用户
[[email protected] ~]useradd-m nagios
[[email protected] ~]groupaddnagcmd
[[email protected] ~]usermod-G nagcmd nagios
[[email protected] ~]usermod-a -G nagcmd apache #把apace用户添加到与nagios的一个组(apache用户会在安装apache时自动创建)
3.3安装nagios
(1)解压安装包
[[email protected] nagios]# tar-zxvf nagios-4.2.4.tar.gz
[[email protected] nagios]# cdnagios-4.2.4
(2)初始化和建立编译的环境
[[email protected] nagios-4.2.4]#./configure--with-command-group=nagcmd
如果能看到下面的基本配置信息则说明初始的环境已经成功配置完成:
(3)按照提示执行命令来进行编译:
[[email protected] nagios-4.2.4]# make all
[[email protected] nagios-4.2.4]# make install
[[email protected] nagios-4.2.4]# make install-init
[[email protected] nagios-4.2.4]# make install-config
[[email protected] nagios-4.2.4]# make install-commandmode
[[email protected] nagios-4.2.4]# makeinstall-webconf
(4)安装完成之后,在/usr/local/nagios目录下如果能够看到这些目录,就表示Naigos安装成功了。
(5)Nagios的样例配置文件默认安装在/usr/local/nagios/etc目录下,配置这些文件就可以使得nagios按要求运行,每个目录及文件的含义如下:
(6)配置文件之间的关系
在nagios的配置过程中涉及到的几个定义有:主机、主机组,服务、服务组,联系人、联系人组,监控时间,监控命令等,从这些定义可以看出,nagios各个配置文件之间是互为关联,彼此引用的。
成功配置出一台nagios监控系统,必须要弄清楚每个配置文件之间依赖与被依赖的关系,最重要的有四点:
第一:定义监控哪些主机、主机组、服务和服务组
第二:定义这个监控要用什么命令实现,
第三:定义监控的时间段,
第四:定义主机或服务出现问题时要通知的联系人和联系人组。
(7)此时应该为email指定您想用来接收nagios警告信息的邮件地址,默认是本机的nagios用户:
[[email protected]]# vi/usr/local/nagios/etc/objects/contacts.cfg
在vi中,通过搜索email,修改邮箱
email [email protected] #描红的地方修改为你的email地址
(8)创建一个登录nagios web程序的用户(用户名配置为nagiosadmin则不需要配置权限,设置为其他用户名就要配置权限),我在这里把用户名设置为nagioseiang,密码为beijing,这个用户帐号在以后通过web登录nagios认证时所用:
[[email protected] ~]# htpasswd-bc /usr/local/nagios/etc/htpasswd.users nagioseiang beijing #把描红的地方修改为你的用户名和密码
(1)如果在上面创建登陆nagios web程序的用户名不是nagiosadmin,我在上面已经设置为nagioseiang,在登陆nagios的web界面后(此时我们还不能登录),是因为nagios默认把全部的权限给nagiosadmin,所以可以通过修改cgi.cfg文件赋予nagioseiang权限,切换到/usr/local/nagios/etc目录下
[[email protected] ~]#cd /usr/local/nagios/etc
[[email protected] etc]# sed-i 's#nagiosadmin#kylinlin#g' cgi.cfg #这条命令将nagiosadmin用户名替换为nagioseiang
[[email protected] etc]# grep nagioseiangcgi.cfg #这条命令检查是否修改成功
以上过程配置结束以后需要重新启动httpd:
[[email protected] etc]# systemctlrestart httpd.service
(2)检查其主配置文件的语法是否正确:
[[email protected] etc]#/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
或者使用下面这个命令来检查语法
[[email protected] etc]# /etc/init.d/nagioscheckconfig
错误数为0,配置成功
刚才已经提到Nagios主程序只是一个控制中心,而能够起到服务监测和系统监测等功能的是众多Nagios的插件,没有插件的Nagios系统其实只是一个空壳。因此在安装了Nagios平台之后我们还需要安装插件。
(1)Nagios插件同样是在其官方网站下载。我将下载的源码包放到/software/nagios目录下,按照下面的步骤进行解压,编译和安装:
[[email protected] src]# tar -zxfnagios-plugins-2.1.4.tar.gz
[[email protected] nagios]# cdnagios-plugins-2.1.4/
[[email protected] nagios-plugins-2.1.4]#./configure --with-nagios-user=nagios --with-nagios-group=Nagios
[[email protected] nagios-plugins-2.1.4]# make&& make install
(2)通过下面的命令查看安装了多少个插件
[[email protected] nagios-plugins-2.1.4]#ls/usr/local/nagios/libexec/ | wc –l
(3)把Nagios加入到服务列表中以使之在系统启动时自动启动:
[[email protected] nagios-plugins-2.1.4]#chkconfig –add nagios
[[email protected] nagios-plugins-2.1.4]# chkconfignagios on
(4)执行下面的命令来验证Nagios的样例配置文件:
[[email protected] nagios-plugins-2.1.4]#/usr/local/nagios/bin/nagios -v/usr/local/nagios/etc/nagios.cfg
(5)如果防火墙的服务没有安装的话,首先使用yum来安装防火墙服务
[[email protected]]# yum install firewall*
(6)在防火墙中打开http端口
[[email protected] nagios-plugins-2.1.4]# firewall-cmd--add-service=http (临时打开)
[[email protected] nagios-plugins-2.1.4]# firewall-cmd--permanent --add-service=http(写入配置文件)
[[email protected] nagios-plugins-2.1.4]# firewall-cmd--reload (重启防火墙)
(7)启动nagios服务
[[email protected] ~]# /usr/local/nagios/bin/nagios-d /usr/local/nagios/etc/nagios.cfg
(8)在浏览器上访问链接http://10.1.1.66/nagios,输入登陆名和密码,如果能够正常看到页面,证明主程序和插件都安装和配置成功,如下图所示:
点击“Host”的链接来查看你本机的监视详情。
点击“Service”的链接来查看你本机的监视详情。此时可能需要给点时间让Nagios来检测你机器上所依赖的服务。
注意:
上图中出现了一个处于WARNING状态的服务,所以我配置过的邮箱里已经收到了报警信息,如果你没有收到报警信息,此时检查你是否已经配置了接收报警信息的邮箱,同时检查是否被当做垃圾邮件而屏蔽了。
[[email protected] nagios]# tar-zxf nrpe-2.15.tar.gz
[[email protected] nrpe-2.15]# cdnrpe-2.15
[[email protected] nrpe-2.15]# ./configure--with-nrpe-user=nagios \
--with-nrpe-group=nagios\
--with-nagios-user=nagios\
--with-nagios-group=nagios\
--enable-command-args\
--enable-ssl
[[email protected] nrpe-2.15]# makeall
[[email protected] nrpe-2.15]# makeinstall-plugin
[[email protected] nrpe-2.15]# makeinstall-daemon
[[email protected] nrpe-2.15]# makeinstall-daemon-config
检查一下/usr/local/nagios/libexec目录下是否已经安装了check_nrpe插件
[[email protected]seianglibexec]# ls | grep check_nrpe
check_nrpe
至此,监控主机已经完成了nagios的安装
四、远程主机安装
4.1配置运行环境
注意:规划中共有两台远程主机(10.1.1.55和10.1.1.58),所以两台远程主机都必须进行配置
(1)关闭防火墙:
[[email protected]nagios-slave-55~]# systemctl stop firewalld.service
[[email protected]nagios-slave-55~]# yum install gcc glibc glibc-common -y
[[email protected]nagios-slave-55~]# yum install gd gd-devel openssl openssl-devel -y
[[email protected]nagios-slave-55~]# yum install php php-gd perl net-tools -y
(1)先添加nagios用户
[[email protected] nagios-slave-55 ~]# useradd-s /sbin/nologin nagios #禁止nagios用户登录
(2)NRPE依赖于nagios-plugins,因此,需要先安装
进入到/software/nagios目录下开始执行下面开始安装
[[email protected] nagios-slave-55 nagios]# tar -zxfnagios-plugins-2.0.3.tar.gz
[[email protected] nagios-slave-55 nagios]# cd nagios-plugins-2.1.4
[[email protected]]# ./configure--with-nagios-user=nagios--with-nagios-group=nagios
[[email protected] nagios-plugins-2.1.4]#make all
[[email protected]]# make install
4.3安装nrpe
(1)进入到/software/nagios目录下开始执行下面的命令
[[email protected] nagios-slave-55 nagios]# tar-zxf nrpe-2.15.tar.gz
[[email protected] nagios-slave-55 nagios]# cdnrpe-2.15
[[email protected] nagios-slave-55 nrpe-2.15]#./configure --with-nrpe-user=nagios \
--with-nrpe-group=nagios \
--with-nagios-user=nagios \
--with-nagios-group=nagios \
--enable-command-args \
--enable-ssl
[[email protected] nagios-slave-55 nrpe-2.15]# makeall
[[email protected] nagios-slave-55 nrpe-2.15]# makeinstall-plugin
[[email protected] nagios-slave-55 nrpe-2.15]# makeinstall-daemon
[[email protected] nagios-slave-55 nrpe-2.15]# makeinstall-daemon-config
[[email protected] nagios-slave-55 nrpe-2.15]# ls/usr/local/nagios/libexec/ #如果安装成功里面有好多NRPE的插件
注意是否有check_nrpe这个插件(没有的话就说明了nrpe没有安装成功)
4.4启动nrpe
(1)用命令启动nrpe
[[email protected] nagios-slave-55 ~]#/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg –d #这条命令启动nrpe
(2)如果要重启nrpe,先执行命令:
[[email protected] nagios-slave-55 ~]# pkillnrpe #关闭nrpe
[[email protected] nagios-slave-55 ~]# netstat-lntp #这条命令用来检查端口,看5666端口是否已经被关闭
[[email protected] nagios-slave-55 ~]# /usr/local/nagios/bin/nrpe-c /usr/local/nagios/etc/nrpe.cfg -d
#重启nrpe进程
(3)检查5666端口是否成功启动
[[email protected] nagios-slave-55 ~]# netstat-tnlp
5666端口已经成功启动
(4)在/usr/local/nagios/etc/nrpe.cfg文件中添加监控主机的ip地址
[[email protected] nagios-slave-55 etc]# vinrpe.cfg +81
将allowed_hosts=127.0.0.1 修改为 allowed_hosts=127.0.0.1,10.1.1.55(这是监控主机的ip地址,本机的ip地址为10.1.1.55)
(5)重启nrpe
[[email protected] ~]#pkill nrpe
[[email protected] ~]# /usr/local/nagios/bin/nrpe-c /usr/local/nagios/etc/nrpe.cfg –d
(6)在监控主机上(ip地址为10.1.1.66)输入下面的命令来检查两个机器之间是否成功通信
[[email protected] ~]# cd/usr/local/nagios/libexec/
[[email protected] libexec]# ./check_nrpe-H 10.1.1.55
NRPEv2.15 #通信成功
至此,远程主机安装nagios完毕
温馨提醒:看清楚你要安装的pnp4nagios的版本,因为0.4和0.6的配置方式是不一致的
[[email protected] ~]# yuminstall rrdtool librrds-perl –y
5.2安装pnp4nagios
这里我的pnp的版本号为0.6
(1)进入到/software/nagios目录下开始执行下面的命令
[[email protected] src]# tar -zxvfpnp4nagios-0.6.25.tar.gz
[[email protected] src]# cdpnp4nagios-0.6.25
[[email protected] pnp4nagios-0.6.25]# ./configure--with-nagios-user=nagios --with-nagios-group=nagios
[[email protected] pnp4nagios-0.6.25]# makeall
[[email protected]]# makeinstall
[[email protected] pnp4nagios-0.6.25]# makeinstall-webconf
[[email protected] pnp4nagios-0.6.25]# makeinstall-config
[[email protected] pnp4nagios-0.6.25]# makeinstall-init
[[email protected] pnp4nagios-0.6.25]# cd./sample-config
[[email protected] sample-config]# makeinstall-webconf
5.3配置pnp4nagios
(1)基本配置
[[email protected] seiang pnp4nagios-0.6.25]# cd/usr/local/pnp4nagios/etc/
[[email protected] seiang etc]# mvmisccommands.cfg-sample misccommands.cfg
[[email protected] seiang etc]# mvrra.cfg-sample rra.cfg
[[email protected] seiang etc]# mvnagios.cfg-sample nagios.cfg
[[email protected] seiang etc]# cdpages/
[[email protected] seiang pages]# mvweb_traffic.cfg-sample web_traffic.cfg
[[email protected] seiang pages]# cd../check_commands/
[[email protected] seiang check_commands]# mvcheck_all_local_disks.cfg-sample check_all_local_disks.cfg
[[email protected] seiang check_commands]# mvcheck_nrpe.cfg-sample check_nrpe.cfg
[[email protected] seiang check_commands]# mvcheck_nwstat.cfg-sample check_nwstat.cfg
[[email protected] seiang check_commands]# /etc/init.d/npcdstart
[[email protected] seiang check_commands]# chkconfignpcd on
(2)配置Nagios数据输出接口(以BULK模式运行)
[[email protected] ~]# vi/usr/local/nagios/etc/nagios.cfg
process_performance_data=1#默认为0,修改为1
并在该文件中添加下面的内容
(3)配置command.cfg
[[email protected] ~]# vi/usr/local/nagios/etc/objects/commands.cfg
在该文件中添加下面的内容 define command{ command_name process-service-perfdata-file command_line /usr/local/pnp4nagios/libexec/process_perfdata.pl --bulk=/usr/local/pnp4nagios/var/service-perfdata } define command{ command_name process-host-perfdata-file command_line /usr/local/pnp4nagios/libexec/process_perfdata.pl --bulk=/usr/local/pnp4nagios/var/host-perfdata } |
(4)在模板配置文件中添加图表图标模板:
[[email protected] ~]# vi/usr/local/nagios/etc/objects/templates.cfg
在该文件中添加下面的内容 define host { name host-pnp action_url /pnp4nagios/index.php/graph?host=$HOSTNAME$&srv=_HOST_ register 0 }
define service { name service-pnp action_url /pnp4nagios/index.php/graph?host=$HOSTNAME$&srv=$SERVICEDESC$ register 0 } |
(5)在监控主机和服务中调用图表模板(在主机和服务后面添加新的模板):
[[email protected] ~]# vi/usr/local/nagios/etc/objects/localhost.cfg
按下面的内容修改(不是添加)该文件 define host{ use linux-server,host-pnp host_name localhost alias localhost address 127.0.0.1 } define service{ use local-service,service-pnp host_name localhost service_description PING check_command check_ping!100.0,20%!500.0,60% } define service{ use local-service,service-pnp host_name localhost service_description Root Partition check_command check_local_disk!20%!10%!/ } …(共8处修改) |
配置Nagios数据输出接口(以BULK模式运行)详情参考官网https://docs.pnp4nagios.org/pnp-0.6/config#bulk_mode
六、图表展示
(1)重启nagios和apache
[[email protected]~]# systemctlrestart nagios.service
[[email protected] ~]# systemctlrestart httpd.service
(2)访问nagios界面即可看到图表小图标:
点击图标会显示pnp4nagios测试页面:全是绿色代表配置正常,如果不是全绿,要逐个解决错误。然后移除或修改install.php文件:
[[email protected] ~]# rm-rf /usr/local/pnp4nagios/share/install.php
再次点击图标就会显示当前监控服务由pnp4nagios生成的图表了:
至此,nagios监控主机的出图配置已经全部完成。
作者:SEian.G ◇本文在ITPUB:http://blog.itpub.net/31015730/ 51CTO:http://seiang.blog.51cto.com/ 上有同步更新 ◇本文itpub地址:http://blog.itpub.net/31015730/viewspace-2136850/ ◇本文51CTO地址:http://seiang.blog.51cto.com/10819863/1916315
|
转载于:https://blog.51cto.com/seiang/1916315