Nginx下搭建Nagios

来源:互联网 发布:php培训机构达内教育 编辑:程序博客网 时间:2024/04/25 21:21
Nagios的搭建主要是需要PHP和Perl的CGI的支持

首先,让Nginx支持CGI

1、安装FCGI模块    
wget http://search.cpan.org/CPAN/authors/id/F/FL/FLORA/FCGI-0.73.tar.gz
tar xvzf FCGI-0.73.tar.gz
cd FCGI-0.73
perl Makefile.PL
make
make install


2、安装FCGI-ProcManager模块    
wget http://search.cpan.org/CPAN/authors/id/G/GB/GBJK/FCGI-ProcManager-0.19.tar.gz
tar xvzf FCGI-ProcManager-0.19.tar.gz
cd FCGI-ProcManager-0.19
perl Makefile.PL
make
make install

3、安装IO和IO::ALL模块
wget http://search.cpan.org/CPAN/authors/id/G/GB/GBARR/IO-1.25.tar.gz
tar zxvf IO-1.25.tar.gz
cd IO-1.25
perl Makefile.PL
make
make install
 
wget http://search.cpan.org/CPAN/authors/id/I/IN/INGY/IO-All-0.41.tar.gz
tar zxvf IO-All-0.41.tar.gz
cd IO-All-0.41
perl Makefile.PL
make
make install

4、下载Perl脚本

这个脚本的目的就是产生一个PERL的FastCGI接口,让Nginx可以以CGI方式处理Perl。    
wget http://www.mike.org.cn/wp-content/uploads/2011/07/perl-fcgi.zip
unzip perl-fcgi.zip
cp perl-fcgi.pl /usr/local/nginx/

注:建议把这个脚本放在Nginx安装目录。

修改脚本权限    
chmod 755 /usr/local/nginx/perl-fcgi.pl

5、建立一个CGI启动/停止脚本

这个SHELL脚本只是为了方便管理上面的Perl脚本。脚本中的www为nginx的运行用户,请据自己的实际情况调整。
注意事项:不能用root用户执行(会提示). 要用与Nginx相同身份的用户执行。否则可能会在Nginx Log中提示 Permision Denied。
    
vim /etc/init.d/perl-cgi.sh
 
#!/bin/bash
dir=/usr/local/nginx

stop ()
{
#pkill  -f  $dir/perl-fcgi.pl
kill $(cat $dir/logs/perl-fcgi.pid)
rm $dir/logs/perl-fcgi.pid 2>/dev/null
rm $dir/logs/perl-fcgi.sock 2>/dev/null
echo "stop perl-fcgi done"
}

start ()
{
rm $dir/now_start_perl_fcgi.sh 2>/dev/null

chown www.www $dir/logs
echo "$dir/perl-fcgi.pl -l $dir/logs/perl-fcgi.log -pid $dir/logs/perl-fcgi.pid -S $dir/logs/perl-fcgi.sock" >>$dir/now_start_perl_fcgi.sh

chown www.www $dir/now_start_perl_fcgi.sh
chmod u+x $dir/now_start_perl_fcgi.sh

sudo -u www $dir/now_start_perl_fcgi.sh
echo "start perl-fcgi done"
}

case $1 in
stop)
stop
;;
start)
start
;;
restart)
stop
start
;;
esac


修改SHELL脚本权限
    
chmod 755 /etc/init.d/perl-cgi.sh

启动脚本
/etc/init.d/perl-cgi.sh restart

正常情况下在/usr/local/nginx/logs下生成perl-fcgi.sock这个文件,如果没有生成,请检查下上面的步聚。




nginx下nagios.conf

server
  {
    listen       80;
    server_name nagios.ylwkj.com;
    index index.html index.htm index.php;
    root  /usr/local/nagios/share;
    auth_basic "Nagios Access";
    auth_basic_user_file /usr/local/nagios/etc/.passwd.conf;



    location ~ .*\.(php|php5)?$
    {
      #fastcgi_pass  unix:/tmp/php-cgi.sock;
      fastcgi_pass  127.0.0.1:9000;
      fastcgi_index index.php;
      include fcgi.conf;
    }

    location ~ .*\.(cgi|pl)?$
    {
    gzip off;
    root   /usr/local/nagios/sbin;
    rewrite ^/nagios/cgi-bin/(.*)\.cgi /$1.cgi break;
    fastcgi_pass  unix:/usr/local/nginx/logs/perl-fcgi.sock;
    fastcgi_param SCRIPT_FILENAME /usr/local/nagios/sbin$fastcgi_script_name;
    fastcgi_index index.cgi;
    fastcgi_read_timeout   60;
    fastcgi_param  REMOTE_USER        $remote_user;
    include fcgi.conf;
    }

    location /nagios/
    {
        alias /usr/local/nagios/share/;
    }
    location /nagios/cgi-bin/
    {
        alias /usr/local/nagios/sbin;
    }

    location /nagios/cgi-bin/image/
    {
        alias /usr/local/nagios/share/images/;
    }

    access_log  /data/logs/nagios_access.log  www;
    error_log  /data/logs/nagios_error.log;
  }
   




关于修改通知: 
搜索网上都没解决,我研究了下,找到了解决方法:
关键是提交的时候使用了post,估计是nginx下不支持这个post,到源代码目录的cgi目录,找到cmd.c文件,搜索post,只有一个,修改为get,然后重新make,拷贝新make出来的cmd.cgi到安装目录替换掉旧的
0 0