tdfd 发表于 2015-4-22 09:07:08

基于Nginx平台部署Nagios监控系统

环境:CentOS 6.5(最小化安装)
Nginx:1.6.2
Nagios:3.3.1

安装基本环境:

1
2
# yum -y install gcc gcc-c++
# yum -y install perl-devel perl




源码安装PHP:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# yum -y install mysql mysql-devel
# yum -y install gd-2.0.35-11.el6.x86_64.rpm gd-devel-2.0.35-11.el6.x86_64.rpm
# yum -y install libxml2 libxml2-devel
# tar -zxvf zlib-1.2.7.tar.gz &>/dev/null
# cd zlib-1.2.7
# ./configure -prefix=/usr/local/zlib&&make &&make install
# tar -zxvf libmcrypt-2.5.7.tar.gz &>/dev/null
# cd libmcrypt-2.5.7
# ./configure &&make &&make install
# useradd -s /sbin/nologin www
# tar -jxvf php-5.3.8.tar.bz2 &>/dev/null
# cd php-5.3.8
# ./configure --prefix=/usr/local/php --enable-mbstring --enable-soap --enable-wddx=shared --enable-xml --with-gd --with-gettext --with-regex=system --with-zlib-dir=/usr/local/zlib --enable-fpm --with-jpeg-dir --with-fpm-user=www --with-fpm-group=www --enable-shmop --with-mcrypt --with-mysqli=mysqlnd
# make &&make install
# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
# sed -i '/run\/php-fpm.pid/s/^;//g' /usr/local/php/etc/php-fpm.conf
# sed -i '/^;pm.min_spare_servers/s/^;//g' /usr/local/php/etc/php-fpm.conf
# sed -i '/^;pm.max_spare_servers/s/^;//g' /usr/local/php/etc/php-fpm.conf
# sed -i '/^;pm.start_servers/s/^;//g' /usr/local/php/etc/php-fpm.conf
# /usr/local/php/sbin/php-fpm
# echo '/usr/local/php/sbin/php-fpm'>>/etc/rc.local




# netstat -tunlp |grep php
tcp      0      0 127.0.0.1:9000            0.0.0.0:*                   LISTEN      1592/php-fpm      
# ps -ef|grep php|grep -v grep
root       1592      10 12:36 ?      00:00:00 php-fpm: master process (/usr/local/php/etc/php-fpm.conf)
www      1593   15920 12:36 ?      00:00:00 php-fpm: pool www         
www      1594   15920 12:36 ?      00:00:00 php-fpm: pool www         
www      1595   15920 12:36 ?      00:00:00 php-fpm: pool www         
www      1596   15920 12:36 ?      00:00:00 php-fpm: pool www         
www      1597   15920 12:36 ?      00:00:00 php-fpm: pool www         
www      1598   15920 12:36 ?      00:00:00 php-fpm: pool www
www      1599   15920 12:36 ?      00:00:00 php-fpm: pool www         
www      1600   15920 12:36 ?      00:00:00 php-fpm: pool www         
www      1601   15920 12:36 ?      00:00:00 php-fpm: pool www         
www      1602   15920 12:36 ?      00:00:00 php-fpm: pool www         
www      1603   15920 12:36 ?      00:00:00 php-fpm: pool www         
www      1604   15920 12:36 ?      00:00:00 php-fpm: pool www         
www      1605   15920 12:36 ?      00:00:00 php-fpm: pool www         
www      1606   15920 12:36 ?      00:00:00 php-fpm: pool www         
www      1607   15920 12:36 ?      00:00:00 php-fpm: pool www         
www      1608   15920 12:36 ?      00:00:00 php-fpm: pool www         
www      1609   15920 12:36 ?      00:00:00 php-fpm: pool www         
www      1610   15920 12:36 ?      00:00:00 php-fpm: pool www         
www      1611   15920 12:36 ?      00:00:00 php-fpm: pool www         
www      1612   15920 12:36 ?      00:00:00 php-fpm: pool www         
#

安装Nginx:

1
2
3
4
5
6
7
8
9
10
11
# tar -jxvf pcre-8.12.tar.bz2 &>/dev/null
# cd pcre-8.12
# ./configure &&make &&make install
zlib上面安装php时已安装
# tar -zxvf openssl-1.0.1c.tar.gz &>/dev/null
# cd openssl-1.0.1c
# ./config &&make &&make install
# tar -zxvf nginx-1.6.2.tar.gz &>/dev/null
# cd nginx-1.6.2
# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-pcre=/software/pcre-8.12 --with-zlib=/software/zlib-1.2.7 --with-openssl=/software/openssl-1.0.1c --with-http_stub_status_module
# make &&make install




编写/etc/init.d/nginx文件
# /etc/init.d/nginx start
Starting nginx:                                          
#


1
2
3
# chkconfig --add nginx
# chkconfig nginx on
# ln -s /usr/local/nagios/share /usr/local/nginx/html/nagios




# ll /usr/local/nginx/html/
total 8
-rw-r--r-- 1 root root 537 Apr 18 10:59 50x.html
-rw-r--r-- 1 root root 612 Apr 18 10:59 index.html
lrwxrwxrwx 1 root root23 Apr 18 12:43 nagios -> /usr/local/nagios/share
还需要自己编写nginx.conf、fastcgi.conf、cgi.conf、start_perl_cgi.sh、perl-fcgi.pl
#
给Nginx添加对Perl的CGI支持(默认不支持):
安装FCGI模块:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# tar -zxvf FCGI-0.73.tar.gz &>/dev/null
# cd FCGI-0.73
# perl Makefile.PL &&make &&make install
# tar -zxvf FCGI-ProcManager-0.25.tar.gz &>/dev/null
# cd FCGI-ProcManager-0.25
# perl Makefile.PL &&make &&make install
# tar -zxvf IO-1.25.tar.gz &>/dev/null
# cd IO-1.25
# perl Makefile.PL &&make &&make install
# tar -zxvf IO-All-0.39.tar.gz &>/dev/null
# cd IO-All-0.39
# perl Makefile.PL &&make &&make install
# tar -zxvf spawn-fcgi-1.6.3.tar.gz &>/dev/null
# cd spawn-fcgi-1.6.3
# ./configure &&make &&make install
# unzip perl-fcgi.zip
# cp perl-fcgi.pl /usr/local/nginx/
# chmod 755 /usr/local/nginx/perl-fcgi.pl
# cd /usr/local/nginx/
# chmod 755 start_perl_cgi.sh




编写start_perl_cgi.sh脚本

1
# ./start_perl_cgi.sh start




start perl-fcgi done
# ll /usr/local/nginx/logs/perl-fcgi.sock
srwxr-xr-x 1 www www 0 Apr 18 11:02 /usr/local/nginx/logs/perl-fcgi.sock
# echo "/bin/sh /usr/local/nginx/start_perl_cgi.sh start" >>/etc/rc.local
安装Nagios:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# yum -y install libgd2-noxpm libgd2-noxpm-dev
# yum -y install php-gd
# groupadd nagcmd
# useradd -G nagcmd nagios
# usermod -G nagcmd www
# tar -zxvf nagios-3.3.1.tar.gz &>/dev/null
# cd nagios
# ./configure --with-command-group=nagcmd --prefix=/usr/local/nagios --with-gd-lib=/usr/lib --with-gd-nc=/usr/include
# make all &&make install &&make install-init &&make install-config &&make install-commandmode
# yum -y install httpd-tools
# cd /usr/local/nagios/etc/
# htpasswd -cm htpasswd.users yfshare
# sed -i '/authorized\_for\_/s/nagiosadmin/&\,yfshare/g' cgi.cfg
# cd /usr/local/nginx/conf/
# mkdir /usr/local/nagios/logs




# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg |grep ^Total
Total Warnings: 0
Total Errors:   0
# /etc/init.d/nagios start
Starting nagios: done.

1
2
# chkconfig --add nagios
# chkconfig nagios on




也可通过访问http://www.4webhelp.net/us/password.php生成htpasswd.users

# /etc/init.d/nagios restart
Running configuration check...done.
Stopping nagios: done.
Starting nagios: done.
#
# /etc/init.d/nginx restart
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
Stopping nginx:                                          
Starting nginx:                                          
#
安装Nagios-plugin:

1
2
3
4
5
6
7
# tar -zxvf nagios-plugins-1.4.15.tar.gz &>/dev/null
# cd nagios-plugins-1.4.15
# ./configure --with-nagios-user=nagios --with-nagios-group=nagios --enable-perl-modules
# make &&make install
# ls /usr/local/nagios/libexec/|wc -l
58
#





安装NRPE:

1
2
3
4
5
6
7
8
9
# yum -y install openssl openssl-devel
# tar -zxvf nrpe-2.13.tar.gz &>/dev/null
# cd nrpe-2.13
# ./configure
# make all &&make install-plugin &&make install-daemon &&make install-daemon-config
# cd /usr/local/nagios/etc/
# sed -i '/allowed_host/s/127.0.0.1/&,192.168.10.99/g' nrpe.cfg
# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
# echo "/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d" >> /etc/rc.local




# /usr/local/nagios/libexec/check_nrpe -H localhost
NRPE v2.13
#
访问:http://192.168.10.99/nagios

但是,打开页面时,其他选项卡都是显示的502 Bad Gateway
# chown nagios:nagcmd /usr/local/nagios/ -R
# chown www:nagcmd /usr/local/nginx/ -R
# cd /usr/local/nginx/


# ./start_perl_cgi.sh start
ERROR PID file /usr/local/nginx/logs/perl-fcgi.pid already exists
start perl-fcgi done
#
因为perl-fcgi进程意外中断,但是进程未退出变成了孤儿进程,所以就变成这样了
修改下脚本start_perl_cgi.sh
# ./start_perl_cgi.sh start
start perl-fcgi done
#
# /etc/init.d/nagios restart
Running configuration check...done.
Stopping nagios: done.
Starting nagios: done.
# /etc/init.d/nginx restart
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
Stopping nginx:                                          
Starting nginx:                                          
#

OK,在Nginx上安装Nagios成功
如果出现上面的问题,解决方法:

1
# sed -i '/use_authentication/s/1/0/g' /usr/local/nagios/etc/cgi.cfg




# /etc/init.d/nagios restart
Running configuration check...done.
Stopping nagios: done.
Starting nagios: done.
# /etc/init.d/nginx restart
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
Stopping nginx:                                          
Starting nginx:                                          
#
访问http://192.168.10.99/nagios/

OK,Successful
页: [1]
查看完整版本: 基于Nginx平台部署Nagios监控系统