Nagios 整合 pnp4nagios 手动编译安装笔记

之前介绍过《 nagios + pnp4nagios 安装笔记》,但文章中的 apache 和 php 都是通过 yum 直接安装的,这样安装的方式比较方便快捷,但是弊端也很明显,就是不易于维护和升级,特别是对处于内网的生产机来说,更是头疼。遂对 apache + php + nagios + pnp4nagios 手动编译安装笔记进行整理记录,以便后续参考。

1、下载相关应用并上传

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#下载httpd
[http://archive.apache.org/dist/httpd/httpd-2.4.9.tar.gz](http://archive.apache.org/dist/httpd/httpd-2.4.9.tar.gz)

#下载apr
[http://archive.apache.org/dist/apr/apr-1.5.2.tar.gz](http://archive.apache.org/dist/apr/apr-1.5.2.tar.gz)

#下载apr-util
[http://archive.apache.org/dist/apr/apr-util-1.5.4.tar.gz](http://archive.apache.org/dist/apr/apr-util-1.5.4.tar.gz)

#下载pcre
[ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.36.tar.gz](ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.36.tar.gz)

#下载php
[http://cn2.php.net/distributions/php-5.6.14.tar.gz](http://cn2.php.net/distributions/php-5.6.14.tar.gz)

#下载nagios
[http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-3.5.1.tar.gz](http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-3.5.1.tar.gz)

#下载nagios-plugins
[http://www.nagios-plugins.org/download/nagios-plugins-2.0.3.tar.gz](http://www.nagios-plugins.org/download/nagios-plugins-2.0.3.tar.gz)

#下载nrpe
[http://liquidtelecom.dl.sourceforge.net/project/nagios/nrpe-2.x/nrpe-2.15/nrpe-2.15.tar.gz](http://liquidtelecom.dl.sourceforge.net/project/nagios/nrpe-2.x/nrpe-2.15/nrpe-2.15.tar.gz)

#下载pnp4nagios
[http://sourceforge.net/projects/pnp4nagios/files/PNP-0.6/pnp4nagios-0.6.25.tar.gz](http://sourceforge.net/projects/pnp4nagios/files/PNP-0.6/pnp4nagios-0.6.25.tar.gz)

2、服务端安装(192.168.1.128)

新建 nagios 用户

1
2
3
4
5
6
7
8
9
10
groupadd nagios
useradd -g nagios nagios
passwd nagios
visudo

########添加sudo权限########
nagios ALL=(ALL) ALL
########添加sudo权限########

su nagios

安装依赖

1
sudo yum install gcc gcc-c++ libxml2 libxml2-devel openssl-devel xinetd gd gd-devel perl-Time-HiRes rrdtool

这些依赖和软件在系统安装光盘里都有(没有安装光盘的可以到官网下载 iso 解压),加上基本不会对其进行改变或升级,所以建议采用 yum 安装或 rpm 安装的形式,可以节省很多不必要的工作量。自建 yum 仓库安装,请参考《Yum 源本地化配置》。

安装 apr

1
2
3
4
tar zxvf apr-1.5.2.tar.gz
cd apr-1.5.2
sudo ./configure --prefix=/usr/local/apr-1.5
sudo make && sudo make install

安装 apr-util

1
2
3
4
tar zxvf apr-util-1.5.4.tar.gz
cd apr-util-1.5.4
sudo ./configure --prefix=/usr/local/apr-util-1.5 --with-apr=/usr/local/apr-1.5/
sudo make && sudo make install

安装 pcre

1
2
3
4
tar zxvf pcre-8.36.tar.gz 
cd pcre-8.36
sudo ./configure --prefix=/usr/local/pcre-8.36
sudo make && sudo make install

安装 httpd

1
2
3
4
tar zxvf httpd-2.4.9.tar.gz
cd httpd-2.4.9
sudo ./configure --prefix=/usr/local/httpd-2.4 --with-apr=/usr/local/apr-1.5/ --with-apr-util=/usr/local/apr-util-1.5/ --with-pcre=/usr/local/pcre-8.36/ --enable-modules=all
sudo make && sudo make install

新建 conf.d 配置保存目录

1
sudo mkdir -p /usr/local/httpd-2.4/conf.d

安装 php

1
2
3
4
5
tar zxvf php-5.6.14.tar.gz
cd php-5.6.14
sudo ./configure --prefix=/usr/local/php-5.6 --with-apxs2=/usr/local/httpd-2.4/bin/apxs --with-config-file-path=/usr/local/php-5.6/etc --with-zlib --with-gd --with-pear --with-mysql --with-gettext --enable-ftp
sudo make && sudo make install
sudo cp php.ini-production /usr/local/php-5.6/etc/php.ini

配置 apache 和 php 关联并引用 conf.d 目录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
sudo vi /usr/local/httpd-2.4/conf/httpd.conf

#修改如下几个地方:
...
LoadModule cgid_module modules/mod_cgid.so
...
User nagios
Group nagios
...
DirectoryIndex index.html index.php
...

#在结尾加上这句:
AddType application/x-httpd-php .php

建立 apache + php 的测试文件

1
2
3
4
sudo sh -c 'echo "< ?php phpinfo(); ?>" > /usr/local/httpd-2.4/htdocs/index.php'

#重启apache
sudo /usr/local/httpd-2.4/bin/apachectl restart

访问apache地址,如果出现如下phpinfo页面,则apache和php关联成功
http://IP/index.php

phpinfo页面

安装nagios

1
2
3
4
5
6
7
8
9
tar zxvf nagios-3.5.1.tar.gz
cd nagios
sudo ./configure --prefix=/usr/local/nagios --with-httpd-conf=/usr/local/httpd-2.4/conf.d
sudo make all
sudo make install
sudo make install-init
sudo make install-commandmode
sudo make install-config
sudo make fullinstall

设置 apache 引用 nagios 配置

1
sudo sh -c 'echo "Include conf.d/*.conf" >> /usr/local/httpd-2.4/conf/httpd.conf'

设置 nagios 启动权限

1
2
sudo chkconfig --add nagios
sudo chkconfig --level 35 nagios on

安装 nagios-plugins

1
2
3
4
tar zxvf nagios-plugins-2.0.3.tar.gz
cd nagios-plugins-2.0.3
sudo ./configure --prefix=/usr/local/nagios
sudo make && sudo make install

安装 nrpe

1
2
3
4
5
6
7
8
tar zxvf nrpe-2.15.tar.gz
cd nrpe-2.15
sudo ./configure --prefix=/usr/local/nagios
sudo make all
sudo make install
sudo make install-daemon
sudo make install-daemon-config
sudo make install-xinetd

定义 check_nrpe 命令

1
2
3
4
5
6
7
8
sudo vi /usr/local/nagios/etc/objects/commands.cfg

#添加如下内容:
#'check_nrpe' command definition
define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}

设置 nagios 监控页面用户名和密码

1
sudo /usr/local/httpd-2.4/bin/htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin

禁用防火墙并重启 nagios 和 Apache

1
2
3
4
sudo service iptables stop
sudo setenforce 0
sudo service nagios restart
sudo /usr/local/httpd-2.4/bin/apachectl restart

访问 nagios 监控页面,输入用户名密码后点击 services ,可以看到默认监控项目则表示 nagios 安装成功

http://IP/nagios

3、客户端安装(192.168.1.3)

重复服务端安装的如下步骤来安装客户端:

1
2
3
4
新建nagios用户
安装依赖
安装nagios-plugins
安装nrpe

添加信任IP

1
2
3
4
5
6
7
8
9
10
11
sudo vi /etc/xinetd.d/nrpe

########nrpe########
only_from = 127.0.0.1 192.168.1.128
########nrpe########

sudo vi /usr/local/nagios/etc/nrpe.cfg

########nrpe.cfg########
allowed_hosts=127.0.0.1,192.168.1.128
########nrpe.cfg########

添加启动端口

1
sudo sh -c 'echo "nrpe            5666/tcp                #nrpe">>/etc/services'

关闭防火墙并重启 xinetd

1
2
3
sudo service iptables stop
sudo setenforce 0
sudo service xinetd restart

验证客户端

1
2
3
4
5
6
#在服务端执行
/usr/local/nagios/libexec/check_nrpe -H 192.168.1.3

########返回版本号########
NRPE v2.15
########返回版本号########

4、pnp4nagios 安装

编译安装 pnp4nagios

1
2
3
4
5
6
7
8
9
tar zxvf pnp4nagios-0.6.25.tar.gz
cd pnp4nagios-0.6.25
sudo ./configure --prefix=/usr/local/pnp4nagios --with-httpd-conf=/usr/local/httpd-2.4/conf.d
sudo make all
sudo make install
sudo make install-webconf
sudo make install-config
sudo make install-init
sudo make fullinstall

pnp4nagios 与 nagios 整合

1
2
3
4
5
6
7
8
9
sudo vi /usr/local/nagios/etc/nagios.cfg

#修改以下几个内容
...
process_performance_data=1
...
host_perfdata_command=process-host-perfdata
service_perfdata_command=process-service-perfdata
...

修改 nagios 数据记录命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
sudo vi /usr/local/nagios/etc/objects/commands.cfg

#修改以下内容
...
# 'process-host-perfdata' command definition
define command{
command_name process-host-perfdata
command_line /usr/local/pnp4nagios/libexec/process_perfdata.pl -d HOSTPERFDATA
}

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

添加 pnp4nagios 记录模板

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
sudo vi /usr/local/nagios/etc/objects/templates.cfg

#添加以下内容
#pnp4nagios
define host {
name host-pnp
action_url /pnp4nagios/index.php/graph?host=$HOSTNAME$' class='tips' rel='/pnp4nagios/index.php/popup?host=$HOSTNAME$&srv=_HOST_
register 0
#process_perf_data 1
}

define service {
name srv-pnp
action_url /pnp4nagios/index.php/graph?host=$HOSTNAME$&srv=$SERVICEDESC$' class='tips' rel='/pnp4nagios/index.php/popup?host=$HOSTNAME$&srv=$SERVICEDESC$
register 0
#process_perf_data 1
}

添加悬浮 tip

1
sudo cp ~/pnp4nagios-0.6.25/contrib/ssi/status-header.ssi /usr/local/nagios/share/ssi/

在 host 和 service 定义中使用 pnp4nagios 模板

1
2
3
4
5
sudo vi /usr/local/nagios/etc/objects/localhost.cfg

use linux-server,host-pnp ; Name of host template to use
#or
use local-service,srv-pnp ; Name of service template to use

移除 install.php 文件

1
2
cd /usr/local/pnp4nagios/share
sudo mv install.php install.php_bak

修改目录权限

/usr/local/
1
sudo chown -R nagios.nagios httpd-2.4 php-5.6 nagios pnp4nagios

重启 nagios 和 apache

1
2
sudo service nagios restart
sudo /usr/local/httpd-2.4/bin/apachectl restart

重新打开 nagios 页面,就可以看到主机和服务监控旁边都出现了个图表图标,点击图标就可以进入 pnp4nagios 绘图界面了。

附:
Nagios 配置详解
如何自定义 Nagios 监控
Nagios 实现 QQ群 告警

By the way, happy birthday to myself… :)