Nagios+apache+NRPE+NSCA+PNP


wget http://apache.freelamp.com/httpd/httpd-2.2.13.tar.gz
wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-3.2.0.tar.gz
wget http://prdownloads.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.13.tar.gz
wget http://prdownloads.sourceforge.net/sourceforge/nagios/nrpe-2.12.tar.gz
wget http://prdownloads.sourceforge.net/sourceforge/nagios/nsca-2.7.2.tar.gz
httpd-2.2.21.tar.gz, nagios-3.3.1.tar.gz, nagios-plugins-1.4.15.tar.gz, nrpe-2.12.tar.gz, php-5.3.8.tar.gz,
pnp4nagios-0.6.15.tar.gz, verify_pnp_config
APACHE
tar -zxvf httpd-2.2.13.tar.gz
./configure --prefix=/sliao/apache
make
make install
cd apache/
bin/apachectl -t
bin/apachectl start
netstat -lnpt
ps -ef |grep httpd
NAGIOS
chown -R nagios.nagios nagios/
tar -zxvf nagios-3.2.0.tar.gz
./configure --prefix=/sliao/nagios
make all
useradd nagios   (apache,nagios的配置中都使用nagios用户和组httpd.conf,nagios.cfg)
make install
make install-config
make install-commandmode
plugins
tar -zxvf nagios-plugins-1.4.13.tar.gz
cd nagios-plugins-1.4.13
./configure --prefix=/sliao/nagios --enable-redhat-pthread-workaround
make
make install
------------------
配置apache整合
cd /sliao/apache/conf/
vi httpd.conf
ScriptAlias /nagios/cgi-bin "/sliao/nagios/sbin"
<Directory "/sliao/nagios/sbin">
SetEnv TZ "Asia/Shanghai"
#  SSLRequireSSL
Options ExecCGI
AllowOverride None
Order allow,deny
Allow from all
#  Order deny,allow
#  Deny from all
#  Allow from 127.0.0.1
AuthName "Nagios Access"
AuthType Basic
AuthUserFile /sliao/nagios/etc/htpasswd.users
Require valid-user
</Directory>
Alias /nagios "/sliao/nagios/share"
<Directory "/sliao/nagios/share">
#  SSLRequireSSL
Options None
AllowOverride None
Order allow,deny
Allow from all
#  Order deny,allow
#  Deny from all
#  Allow from 127.0.0.1
AuthName "Nagios Access"
AuthType Basic
AuthUserFile /sliao/nagios/etc/htpasswd.users
Require valid-user
</Directory>

配置web账户
/sliao/apache/bin/htpasswd -c /sliao/nagios/etc/htpasswd stars
/sliao/apache/bin/htpasswd -c /sliao/nagios/etc/htpasswd.users nagiosadmin
apache/bin/apachectl -t
apache/bin/apachectl stop
apache/bin/apachectl start
chown -R nagios.nagios /sliao/
/sliao/apache/bin/apachectl start
NRPE
 服务器端
tar xzf nrpe-2.8.1.tar.gz
cd nrpe-2.8.1
./configure
make all
make install-plugin
客户端
useradd nagios
tar xzf nrpe-2.8.1.tar.gz
cd nrpe-2.8.1
./configure  --prefix=/sliao/nagios
make all
make install-plugin
make install-daemon
make install-daemon-config
tar xzf nagios-plugins-1.4.9.tar.gz
cd nagios-plugins-1.4.9
./configure --prefix=/sliao/nagios --enable-redhat-pthread-workaround
make && make install
chown nagios.nagios /usr/local/nagios/
chown -R nagios.nagios /usr/local/nagios/libexec/
RUN
服务器端
/sliao/nagios/bin/nagios -d /sliao/nagios/etc/nagios.cfg
客户端
/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d

###############################
nagios web界面提示
It appears as though you do not have permission to view information for any of the services you requested...

打开/sliao/nagios/etc/cgi.cfg配置文件,里面有个参数:
use_authentication=1
为了保障系统的安全性,nagios设置了这个参数,默认为1,改为0即可
或者创建账户:nagiosadmin
web界面的Status Map、Alert Histogram打不开链接,提示找不到statusmap.cgi和histogram.cgi.这个问题是因为gd-devel没有安装的问题,造成NAGIOS在编译时不生成这statusmap.cgi和histogram.cgi文件
yum install gd-devel

发送的监控邮件标题有中文时:收到的邮件标题出现很长的杂乱英文字符(非乱码)解决方法:
编辑commands.cfg 
找到notify-service-by-email项:
 /usr/bin/mutt -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$
发邮件的命令中把Service Alert: 的":"改成全角的冒号":" 即可.

NSCA
tar zxvf libmcrypt-2.5.7.tar.gz
./configure
make
make install
tar zxvf mhash-0.9.1.tar.gz
./configure
make
make install
tar zxvf mcrypt-2.6.4.tar.gz
LD_LIBRARY_PATH=/usr/local/lib ./configure
make
make install
vi /etc/ld.so.conf
/usr/lib/
/usr/local/lib
ldconfig
tar -zxvf nsca-2.7.2.tar.gz
./configure
make all
/sliao/nagios/bin/nsca -d -c /sliao/nagios/etc/nsca.cfg
--------------------------
PNP是一个小巧的开源软件包,它是基于PHP和PERL,利用rrdtool将Nagios采集的数据绘制成图表。如果你要安装PNP,那么准备工作有如下3项:
1、整合Apache和PHP
2、安装rrdtools
3、安装Perl
rrdtools:
yum install rrdtool* --nogpgcheck
或者
http://www.express.org/~wrl/rrdtool/
###########
php:
tar -zxvf php-5.3.0.tar.gz
./configure --prefix=/sliao/php5 --with-gd --with-zlib --enable-sockets --with-apxs2=/sliao/apache/bin/apxs --with-config-file-path=/sliao/php5
make
make install
vi /sliao/apache/conf/httpd.conf.
在
LoadModule php5_module        modules/libphp5.so
后增加:
#
AddType application/x-httpd-php .php .php3 .htm .phtml .php4
AddType application/x-httpd-php-source .phps
修改:
<IfModule dir_module>
   DirectoryIndex index.html index.php
</IfModule>
重启apache
/sliao/apache/bin/httpd -k restart
pnp:
nagios安装在非默认目录,pnp安装则需要修改configure配置里的路径
tar -zxvf pnp-0.6.15.tar.gz
PREFIX=/sliao/pnp4nagios
USER=nagios
GROUP=nagios
./configure
--with-nagios-user=$USER
--with-nagios-group=$GROUP
--prefix=$PREFIX
--with-httpd-conf=/sliao/apache/conf/conf.d
make all
make install
make install-webconf
make install-config

再把以下内容放到apache配置文件的最后
Alias /pnp4nagios "/sliao/pnp4nagios/share"
<Directory "/sliao/pnp4nagios/share">
AllowOverride None
Order allow,deny
Allow from all
#
# Use the same value as defined in nagios.conf
#
AuthName "Nagios Access"
AuthType Basic
AuthUserFile /sliao/nagios/etc/htpasswd.users
Require valid-user
<IfModule mod_rewrite.c>
# Turn on URL rewriting
RewriteEngine On
Options FollowSymLinks
# Installation directory
RewriteBase /pnp4nagios/
# Protect application and system files from being viewed
RewriteRule ^(application|modules|system) - [F,L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT,L]
</IfModule>
</Directory>
#####
重启apache

web界面编辑主机工具

  • centreon
  • nconf

PNP

介绍PNP工作原理,首先要说明一下Nagios提供的数据接口,也就是PNP的数据来源。在前面的文章中,我提到过,在定义host或service中 都有一个定义项,名为process_perf_data,其值可以定义为0或1,其作用是是否启用Nagios的数据输出功能。如果你将此项赋值为1, 那么Nagios就会将收集的数据写入到某个文件中,以备提取。所以,如果你想让Nagios将数据输出的话,首先要将Nagios的主配置文件 nagios.cfg中相关的配置修改:

enable_environment_macros=1
process_performance_data=1
service_perfdata_command=process-service-perfdata #默认此句被注释掉了

如果想要对某个监控对象做数据图表,则需在所对应的host或者service定义中(一般写在hosts.cfg或者services.cfg文件中),包含如下的定义:

process_perf_data 1
action_url                      /pnp4nagios/index.php/graph?host=$HOSTNAME$&srv=$SERVICEDESC$

这 样,Nagios就会调用相应的命令来输出数据了。Nagios的command定义中默认有一项“process-service- perfdata”,该命令声明了Nagios输出哪些值到输出的文件中。不过其定义相对简单,PNP提供了一个perl脚本,更详尽的定义了一个输出数 据的方法。如果要使用PNP的话,我们需要在command的定义中,将“process-service-perfdata”命令对应的执行命令行的内 容替换成该脚本:

define command {
       command_name    process-service-perfdata
       command_line    /usr/bin/perl /usr/local/pnp4nagios/libexec/process_perfdata.pl
}

define command {
       command_name    process-host-perfdata
       command_line    /usr/bin/perl /usr/local/pnp4nagios/libexec/process_perfdata.pl -d HOSTPERFDATA
}

这样设置了之后,Nagios就会利用PNP提供的脚本进行相关的工作了。
ok,现在执行一下Nagios配置检查命令

/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

如果没有什么错误,那么我们重新启动Nagios。

/etc/init.d/nagios restart

验收一下成果,在浏览器的地址栏中输入:



[url]http://IP/pnp4nagios/[/url]

如果可以看到图表,那就恭喜啦!贴上我的图表给大家看看。

FAQ


可能会缺少各种php扩展
php53-zlib-5.3.8
php53-gd-5.3.8
php53-sockets-5.3.8
php53-iconv-5.3.8
直接yum 安装就行.

1、为什么/usr/local/nagios/share/perfdata/目录中生成不出数据
嘿嘿,确实是因为权限问题。因为当时装好PNP时太性急了,看到PERFDATA目录没有生成数据就自己手动创建了两个主机名称的目录,(因为RRDTOOL需要过一会才会创建数据)
[root@pnp perfdata]# ll
total 8
drwxr-xr-x 2 root root 4096 Mar 17 21:15 cacti
drwxr-xr-x 2 root root 4096 Mar 17 21:16 nagios-server
而创建这两目录所属用户和组成了root所以说nagios跑的是nagios用户怎么有权限写入到root权限的目录中呢。
所以才创建不出数据。
[root@pnp perfdata]# chown nagios.nagios cacti
[root@pnp perfdata]# ll
total 8
drwxr-xr-x 2 nagios nagios 4096 Mar 17 21:15 cacti
[root@pnp perfdata]# cd cacti/
[root@pnp cacti]# ls
check-tcp-8008.rrd  check-tcp-8008.xml
哈哈,数据出来了,唉只怪自己太大意了

2、为什么/usr/local/nagios/share/perfdata目录中还是没有数据生成,
解决方法:安装pnp的时候是否make install-config安装了模板安装了这些模板后进入/usr/local/nagios/etc/pnp目录中去掉后面的扩展。
    是否在services.cfg文件中为服务添加了process_perf_data 1
这一项。否则不会生成数据的

我们在做完上面的这些后发现还是没有数据产生,这时你就要等五分到十分钟。因为pnp需要这么久才能产生数据,才能出图。

如果只是个别服务不出图,就要查看# ls /usr/local/nagios/share/pnp/templates.dist/ 这个目中是否有这个服务,如果没有则不可以出图。此处相当于是rrd数据的源模板
3.PNP出的图里面的中文显示方块:
用windows或者下载一个宋体simsun.ttf,将该文件复制到/usr/share/fonts目录下即可.
 apt-get install msttcorefonts