Rootop 服务器运维与web架构

nagios添加图表(小太阳)支持

| 暂无评论

nagios监控服务器只显示当前的信息,没有图表功能,不能查看历史状态,为了解决这个问题
需要安装PNP来实现这个功能,pnp基于PHP和Perl可以利用rrdtool工具将Nagios采集的数据绘制成图表显示主机或服务在一段时间内的运行情况。

系统环境:
centos6.4_x64,selinux关闭,iptables关闭。

下载rrdtool:
http://oss.oetiker.ch/rrdtool/pub/rrdtool.tar.gz
下载pnp:
wget -c http://nchc.dl.sourceforge.net/project/pnp4nagios/PNP/pnp-0.4.14/pnp-0.4.14.tar.gz

安装rrdtool和pnp需要几个rpm包支持,有gd、gd-devel、zlib、zlib-devel、pango、pango-devel、perl-devel、perl-CPAN、perl-Time-HiRes

[root@localhost rrdtool-1.4.8]# yum install pango pango-devel perl-devel perl-CPAN perl-Time-HiRes -y  #其它的包之前已经装过
[root@localhost rrdtool-1.4.8]# ./configure --prefix=/usr/local/rrdtool
[root@localhost rrdtool-1.4.8]# make && make install

安装pnp:
在安装pnp之前需要执行一个命令来检查是否有相关模块:

[root@localhost pnp-0.4.14]# perl -MRRDs -le 'print q(ok!)'
Can't locate RRDs.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .).
BEGIN failed--compilation aborted.

提示找不到RRDs.pm,通过软链接解决:

[root@localhost pnp-0.4.14]# ln -s /usr/local/rrdtool/lib/perl/5.10.1/x86_64-linux-thread-multi/RRDs.pm /usr/lib64/perl5/

再次检查,出现另一个错误:

[root@localhost pnp-0.4.14]# perl -MRRDs -le 'print q(ok!)'
Can't locate loadable object for module RRDs in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at -e line 0
Compilation failed in require.
BEGIN failed--compilation aborted.

提示库文件找不到,解决办法:

[root@localhost pnp-0.4.14]# ln -s /usr/local/rrdtool/lib/perl/5.10.1/x86_64-linux-thread-multi/auto/RRDs/RRDs.so /usr/lib64/perl5/
[root@localhost pnp-0.4.14]# perl -MRRDs -le 'print q(ok!)'  #重新检查,通过。
ok!
[root@localhost pnp-0.4.14]# mkdir /usr/local/nagios/share/perfdata  #保存RRD Database Files
[root@localhost pnp-0.4.14]# ./configure --with-nagios-user=nagios --with-nagios-group=nagios --with-rrdtool=/usr/local/rrdtool/bin/rrdtool --with-perfdata-dir=/usr/local/nagios/share/perfdata --with-perl_lib=/usr/local/rrdtool/lib/perl/5.10.1/x86_64-linux-thread-multi/
[root@localhost pnp-0.4.14]# make all
[root@localhost pnp-0.4.14]# make install
[root@localhost pnp-0.4.14]# make install-config
[root@localhost pnp-0.4.14]# make install-init

配置pnp:
在PNP安装完成后,默认安装目录下自带了模板配置文件,只需将模板文件全部复制一份即可。

[root@localhost pnp-0.4.14]# cd /usr/local/nagios/etc/pnp/
[root@localhost pnp]# ls
background.pdf  check_commands  config.php  npcd.cfg-sample  pages  pnp4nagios_release  process_perfdata.cfg-sample  rra.cfg-sample
[root@localhost pnp]# cp process_perfdata.cfg-sample process_perfdata.cfg
[root@localhost pnp]# cp npcd.cfg-sample npcd.cfg
[root@localhost pnp]# cp rra.cfg-sample rra.cfg
[root@localhost pnp]# chown nagios:nagios *  #将所有配置文件属主属组都改为nagios运行账户nagios

编辑process_perfdata.cfg:
LOG_LEVEL = 2 #把0改为2
修改nagios配置文件来增加小太阳图标:
修改templates.cfg,增加一个定义PNP的host和service。

[root@localhost ~]# vi /usr/local/nagios/etc/objects/templates.cfg
#pnp host definition
define host {
        name                            hosts-pnp
        register                        0
        action_url                      /nagios/pnp/index.php?host=$HOSTNAME$
        process_perf_data               0
}
#pnp service
define service {
        name                            services-pnp
        register                        0
        action_url                      /nagios/pnp/index.php?host=$HOSTNAME$&srv=$SERVICEDESC$
        process_perf_data               0
}

修改nagios.cfg:

[root@localhost ~]# vi /usr/local/nagios/etc/nagios.cfg
process_performance_data=1  #0改为1
host_perfdata_command=process-host-perfdata  #将前面的注释去掉
service_perfdata_command=process-service-perfdata

修改commands.cfg:
process-host-perfdata和process-service-perfdata指令声明了nagios输出哪些值到输出文件中。不过这些定义相对简单,而PNP提供了一个Perl脚本,非常详细地定义了一个输出数据的方法,process_perfdata.pl就是PNP自带的一个脚本,这个脚本在PNP安装完成后会自动生成。因此可以将process-host-perfdata和process-service-perfdata指令中对应的执行命令的内容替换成此脚本。

[root@localhost ~]# vi /usr/local/nagios/etc/objects/commands.cfg
# 'process-host-perfdata' command definition
define command{
        command_name    process-host-perfdata        command_line    /usr/local/nagios/libexec/process_perfdata.pl
        }


# 'process-service-perfdata' command definition
define command{
        command_name    process-service-perfdata        command_line    /usr/local/nagios/libexec/process_perfdata.pl
        }

修改hosts.cfg文件和services.cfg文件:
将hosts-pnp和services-pnp引用到hosts.cfg和services.cfg中

define host{
        use                     linux-server,hosts-pnp            ; Name of host template to use
                                                        ; This host definition will inherit all variables that are defined
                                                        ; in (or inherited by) the linux-server host template definition.
        host_name               localhost
        alias
        address                 127.0.0.1
        }

define service{
        use                             local-service,services-pnp         ; Name of service template to use
        host_name                       localhost
        service_description             PING
        check_command                   check_ping!100.0,20%!500.0,60%
        }

重启nagios服务。
完成。

PS:
编译rrdtool时的一个错误,因为当时没安装pango和pango-devel包。

configure: error: Please fix the library issues listed above and try again
[root@localhost rrdtool-1.4.8]# yum install pango pango-devel -y

编译rrdtool时在make的时候一个报错,因为当时没安装perl-devel perl-CPAN:

Can't locate ExtUtils/MakeMaker.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at Makefile.PL line 1.
BEGIN failed--compilation aborted at Makefile.PL line 1.

解决方法:
[root@localhost rrdtool-1.4.8]# yum install -y perl-devel perl-CPAN
重新configure

如果不通过perl -MRRDs -le ‘print q(ok!)’来检查环境,可能在编译安装pnp的时候报错:
WARNING: The RRDs Perl Modules are not found on your system
Using RRDs will speedup things in larger installations.

安装pnp,在configure时一个报错,因为最开始的时候没有安装perl-Time-HiRes:
checking for Perl Module Time::HiRes… no
configure: error: Perl Module Time::HiRes not available
解决办法:
[root@localhost pnp-0.4.14]# yum install -y perl-Time-HiRes

 

原创文章,转载请注明。本文链接地址: https://www.rootop.org/pages/2578.html

作者:Venus

服务器运维与性能优化

发表回复