马哥Linux运维原创作者投稿nagios

将nagios监控数据可视化

2017-04-15  本文已影响199人  天之蓝色

前言
nagios系统可以像zabbix、cacti类软件一样,将收集到的各项监控指标性能数据以图形的形式动态展示出来。使监控更加直观化,可进行各项灵活的定制,方便对性能数据进行对比分析。nagios可以用于监控数据画图的插件有多种,这里以pnp4nagios软件为例进行说明。

本文所用到系统环境
OS:CentOS release 6.8 (Final) 2.6.32-642.el6.x86_64
还有一个重要环境:互联网(yum、百度、Google)。
各软件包:

组件 软件包名
nagios daemon nagios-4.3.1.tar.gz
nrpe nrpe-2.15.tar.gz
nagios plugin nagios-plugins-2.2.0.tar.gz
pnp4nagios pnp4nagios-0.6.25.tar.gz

1. 获取pnp4nagios软件

笔者是在pnp4nagios的官网上下载的,现将它和nagios其它软件包一起放在此处百度网盘 密码:ayhk

2. 安装pnp4nagios

Perl >= 5.x without additional modules
    RRDtool >= 1.x, better 1.2 but not compulsory
    Attention: installing RRDtool without a packet manager might lead to missing dejavu fonts. If you see graphs without text then this may be the cause.
    PHP >= 5.1.6 for the Webfrontend based on Kohana
    Nagios >= 2.x or Icinga
    Kohana needs the module “mod_rewrite” to be enabled. For details please have a look at the documentation of your web-server specific to your distribution.

其中perl、rrdtool都可以通过yum来进行安装,web server apache默认即已包含mod_rewrite了。需要使用yum安装的包大致包括以下几个:

yum install rrdtool rrdtool-perl perl-devel 
make all
make install
make fullinstall

这里对make fullinstall进行一下说明,这一指令其实包含有三个指令:

  1. make install-webconf
    复制一个名为pnp4nagios.conf的文件到/etc/httpd.conf.d/目录下,主要用于从web可以访问pnp4nagios。
  2. make install-config 复制process_perfdata.pl和npcd的配置文件到/etc/pnp中,为可选项。
    3.make install-init 安装npcd init脚本。

3. 测试pnp4nagios安装结果

重启web服务,并验证pnp4nagios运行环境是否已准备就绪。

service httpd restart

打开浏览器输入上面汇总信息的HTML URL即:http://localhost/pnp4nagios。正常情况下会得到如下页面:

pnp4nagios运行环境测试图1 pnp4nagios运行环境测试图2

4. 了解pnp4nagios的工作模式

进行pnp4nagios的配置之前,需要先确定要使用pnp4nagios的哪种工作模式,它支持的有sync mode(应该是默认模式)、bulk mode、bulk mode with npcd、bulk with npcd and npcdmod和gearman mode共5种运行模式,按照官网的说明,我这里选择buld mode with npcd。以下为这种模式的官方说明:

Viewing from Nagios this is the best way of processing because Nagios will not be blocked.

Nagios again uses a temporary file to store the data and executes a command after expiration of a certain time. Instead of immediate processing by process_perfdata.pl the file is moved to a spool directory. As moving a file inside the same filesystem nearly takes no time nagios is able to execute crucial work immediately.

The NPCD daemon (Nagios Performance C Daemon) will monitor the directory for new files and will pass the names to process_perfdata.pl. Processing of performance data is decoupled completely from nagios. NPCD itself is able to start multiple thread for processing the data.

简单来说这种模式的好处就是它将收集到原始的性能统计数据工作和对这些数据进行处理然后生成RRD和XML文件的工作分开了,前者由nagios完成,后者则由专门的守护进程NPCD来完成,提高了nagios的工作效率。
以下为这种模式的工作原理示图:

pnp4nagios bulk mode with npcd

5. 配置pnp4nagios及nagios

要想pnp4nagios工作起来必须对pnp4nagios和nagios的配置文件进行修改,让它们能够联动起来。按照以下步骤进行配置:

process_performance_data=1   #默认为0
host_perfdata_file=/usr/local/pnp4nagios/var/host-perfdata
service_perfdata_file=/usr/local/pnp4nagios/var/service-perfdata 
#记得pnp4nagios安装时的配置信息汇总信息吧,这里用到了
host_perfdata_file_template=DATATYPE::HOSTPERFDATA\tTIM
ET::$TIMET$\tHOSTNAME::$HOSTNAME$\tHOSTPERFDATA::$
HOSTPERFDATA$\tHOSTCHECKCOMMAND::$HOSTCHECKC
OMMAND$\tHOSTSTATE::$HOSTSTATE$\tHOSTSTATETYPE::
$HOSTSTATETYPE$   #内容很长,修改的时候记得写在一行不要人为分行  
host_perfdata_file_template=DATATYPE::HOSTPERFDATA\tTIM
ET::$TIMET$\tHOSTNAME::$HOSTNAME$\tHOSTPERFDATA::$
HOSTPERFDATA$\tHOSTCHECKCOMMAND::$HOSTCHECKC
OMMAND$\tHOSTSTATE::$HOSTSTATE$\tHOSTSTATETYPE::
$HOSTSTATETYPE$   #内容很长,修改的时候记得写在一行不要人为分行  
host_perfdata_file_mode=a
service_perfdata_file_mode=a   #默认为即a,append
host_perfdata_file_processing_interval=15  #设置每15秒处理一次性能数据文件
service_perfdata_file_processing_interval=15
host_perfdata_file_processing_command=process-host-perfdata-file
service_perfdata_file_processing_command=process-service-perfdata-file  

这两个指令非常关键,它需要在nagios的commands.cfg中定义好,且命令名称跟此处完全一致。

service npcd start
chkconfig npcd on
service npcd status  #确认一下它是否启动

6. 观察画图效果

nagios中添加了action_url以后,web 页面相应的主机和服务后会多一个小的图标,如下:

pnp4nagios图标

点击其中一个图标可以看到其动态输出效果,如下所示:

pnp4nagios效果图

7. pnp4nagios的官方文档

pnp4nagios还支持很多高级功能,如在配置 文件中使用正则表达式,以实现更为复杂的匹配,支持自定义page,将想要放在一起的主机及服务呈现在一个页面中,支持数据导出。这些在它的官方网站上都有介绍,文中的原理图也来自pnp4nagios的官网。

上一篇 下一篇

猜你喜欢

热点阅读