nginx

nagios平台从apache迁移到nginx

为了学习nginx,又苦于公司生产环境没有打算要换到nginx上,于是就想到拿自己搭建的监控环境开刀了,一路配置下来确实还是遇到不少麻烦,不过还好最终还都是圆满解决掉了,下面就把我这次从apache迁移到nginx的过程整理下来

原来的监控环境是apache+cacti+nagios,按照网上相关的文档,配置起来那真是傻瓜之所及也,我也不太想赘述了,网上google一下后大把大把的。

新的监控环境nginx+cacti+nagios,其实就是把apache换成了nginx而已,cacti和nagios的安装方法我也不多说了,自己去搜好了,就把转换过程和需要注意的地方写下来。

nginx安装我不赘述,首先是cacti相关的配置,详细如下

location /cacti/ {
         alias /data/wwwroot/web/cacti/;
         index index.php index.html index.htm;
         }

location ~ \.php$ {
         root           /data/wwwroot/web/;
         fastcgi_pass   127.0.0.1:9000;
         fastcgi_index  index.php;
         fastcgi_param  SCRIPT_FILENAME  $document_root/$fastcgi_script_name;
         include        fastcgi_params;
        }

下面解释一下上面的配置,因为我的地址是http://ip/cacti/这样的形式,所以我定义了location /cacti/这个,然后把这个目录重定向到了cacti所在的目录,再看后面php的配置,首先定义了root地址是/data/wwwroot/web/这个就是后面$document_root内容,我的访问php的页面是http://ip/cacti/plugins/monitor/monitor.php,在这个url里cacti/plugins/monitor/monitor.php这部分是$fastcgi_script_name的值,因为涉及到了php页面,所以就会到$document_root即/data/wwwroot/web/下去找,这也就是为什么root定义的时候我不是直接的定义到了/data/wwwroot/web/cacti下,之前我就是在这里出的错误,结果就是始终找不到所需要的文件。

因为nginx本身是不支持cgi的,所以需要使用一个perl脚本来进行转换,脚本如下

#!/usr/bin/perl
use FCGI;
use Socket;
use FCGI::ProcManager;
sub shutdown { FCGI::CloseSocket($socket); exit; }
sub restart  { FCGI::CloseSocket($socket); &main; }
use sigtrap ‘handler’, \&shutdown, ‘normal-signals’;
use sigtrap ‘handler’, \&restart,  ‘HUP’;
require ‘syscall.ph’;
use POSIX qw(setsid);

END()   { }
BEGIN() { }
{
 no warnings;
*CORE::GLOBAL::exit = sub { die "fakeexit\nrc=" . shift() . "\n"; };
};
eval q{exit};
if ($@) {
 exit unless $@ =~ /^fakeexit/;
}
&main;

sub daemonize() {
 chdir ‘/’ or die "Can’t chdir to /: $!";
 defined( my $pid = fork ) or die "Can’t fork: $!";
 exit if $pid;
 setsid() or die "Can’t start a new session: $!";
 umask 0;
}

sub main {

 $proc_manager = FCGI::ProcManager->new( {n_processes => 5} );
 $socket = FCGI::OpenSocket( "/usr/local/nginx/logs/cgi.sock", 10 )
 ; #use UNIX sockets – user running this script must have w access to the ‘nginx’ folder!!
 $request =
 FCGI::Request( \*STDIN, \*STDOUT, \*STDERR, \%req_params, $socket,
 &FCGI::FAIL_ACCEPT_ON_INTR );
 $proc_manager->pm_manage();
 if ($request) { request_loop() }
 FCGI::CloseSocket($socket);
}

sub request_loop {
 while ( $request->Accept() >= 0 ) {
 $proc_manager->pm_pre_dispatch();

 #processing any STDIN input from WebServer (for CGI-POST actions)
 $stdin_passthrough = ”;
 { no warnings; $req_len = 0 + $req_params{‘CONTENT_LENGTH’}; };
 if ( ( $req_params{‘REQUEST_METHOD’} eq ‘POST’ ) && ( $req_len != 0 ) )
 {
 my $bytes_read = 0;
 while ( $bytes_read < $req_len ) {
 my $data = ”;
 my $bytes = read( STDIN, $data, ( $req_len – $bytes_read ) );
 last if ( $bytes == 0 || !defined($bytes) );
 $stdin_passthrough .= $data;
 $bytes_read += $bytes;
 }
 }

 #running the cgi app
 if (
 ( -x $req_params{SCRIPT_FILENAME} ) &&    #can I execute this?
 ( -s $req_params{SCRIPT_FILENAME} ) &&    #Is this file empty?
 ( -r $req_params{SCRIPT_FILENAME} )       #can I read this file?
 )
 {
 pipe( CHILD_RD,   PARENT_WR );
 pipe( PARENT_ERR, CHILD_ERR );
 my $pid = open( CHILD_O, "-|" );
 unless ( defined($pid) ) {
 print("Content-type: text/plain\r\n\r\n");
 print
"Error: CGI app returned no output – Executing $req_params{SCRIPT_FILENAME} failed !\n";
 next;
 }
 $oldfh = select(PARENT_ERR);
 $|     = 1;
 select(CHILD_O);
 $| = 1;
 select($oldfh);
 if ( $pid > 0 ) {
 close(CHILD_RD);
 close(CHILD_ERR);
 print PARENT_WR $stdin_passthrough;
 close(PARENT_WR);
 $rin = $rout = $ein = $eout = ”;
 vec( $rin, fileno(CHILD_O),    1 ) = 1;
 vec( $rin, fileno(PARENT_ERR), 1 ) = 1;
 $ein    = $rin;
 $nfound = 0;

 while ( $nfound =
 select( $rout = $rin, undef, $ein = $eout, 10 ) )
 {
 die "$!" unless $nfound != -1;
 $r1 = vec( $rout, fileno(PARENT_ERR), 1 ) == 1;
 $r2 = vec( $rout, fileno(CHILD_O),    1 ) == 1;
 $e1 = vec( $eout, fileno(PARENT_ERR), 1 ) == 1;
 $e2 = vec( $eout, fileno(CHILD_O),    1 ) == 1;

 if ($r1) {
 while ( $bytes = read( PARENT_ERR, $errbytes, 4096 ) ) {
 print STDERR $errbytes;
 }
 if ($!) {
 $err = $!;
 die $!;
 vec( $rin, fileno(PARENT_ERR), 1 ) = 0
 unless ( $err == EINTR or $err == EAGAIN );
 }
 }
 if ($r2) {
 while ( $bytes = read( CHILD_O, $s, 4096 ) ) {
 print $s;
 }
 if ( !defined($bytes) ) {
 $err = $!;
 die $!;
 vec( $rin, fileno(CHILD_O), 1 ) = 0
 unless ( $err == EINTR or $err == EAGAIN );
 }
 }
 last if ( $e1 || $e2 );
 }
 close CHILD_RD;
 close PARENT_ERR;
 waitpid( $pid, 0 );
 } else {
 foreach $key ( keys %req_params ) {
 $ENV{$key} = $req_params{$key};
 }

 # cd to the script’s local directory
 if ( $req_params{SCRIPT_FILENAME} =~ /^(.*)\/[^\/] +$/ ) {
 chdir $1;
 }
 close(PARENT_WR);

 #close(PARENT_ERR);
 close(STDIN);
 close(STDERR);

 #fcntl(CHILD_RD, F_DUPFD, 0);
 syscall( &SYS_dup2, fileno(CHILD_RD),  0 );
 syscall( &SYS_dup2, fileno(CHILD_ERR), 2 );

 #open(STDIN, "<&CHILD_RD");
 exec( $req_params{SCRIPT_FILENAME} );
 die("exec failed");
 }
 } else {
 print("Content-type: text/plain\r\n\r\n");
 print
"Error: No such CGI app – $req_params{SCRIPT_FILENAME} may not exist or is not executable by this process.\n";
 }
 }
}

把上面这个脚本存放在/usr/local/bin下或者你习惯放置的位置,起名cgiwrap-fcgi.pl,当然你也可以使用其他的名字。当执行这个pl脚本的时候会在/usr/local/nginx/logs/生成一个cgi.sock文件,这个生成的文件要对nginx运行用户有访问的权限,因为我nginx用daemon这个用户跑的,所以我设置这个文件对daemon用户有访问的权限。

这个脚本设置好了之后再来看下nginx里的配置

location ~\.cgi$ {
         rewrite ^/nagios/cgi-bin/(.*)\.cgi /$1.cgi break;
         fastcgi_pass unix:/usr/local/nginx/logs/cgi.sock;
         fastcgi_param  SCRIPT_FILENAME  /usr/local/nagios/sbin/$fastcgi_script_name;
         include        fastcgi_params; 
         fastcgi_index  index.cgi;
         auth_basic      "nagios";
        auth_basic_user_file    /usr/local/nagios/etc/htpasswd.users; 
         }

location /nagios/ { 
         alias /usr/local/nagios/share/; 
         index index.html; 
         auth_basic      "nagios"; 
        auth_basic_user_file    /usr/local/nagios/etc/htpasswd.users; 
         }

location /pub/images/ {
                        alias /usr/local/nagios/share/docs/images/; 
         auth_basic      "nagios";
            auth_basic_user_file    /usr/local/nagios/etc/htpasswd.users; 
         }

接下来就是需要生成nagios执行cgi的用户和设置密码

/usr/local/apache2/bin/htpasswd -c /usr/local/nginx/conf/htpasswd.users nagiosadmin

接下来修改/usr/local/nagios/etc/cgi.cfg文件,在其中找到#default_user_name=guest这行,将其改为default_user_name=nagiosadmin,到此迁移工作就已经完成了,下面来启动相应的程序来测试吧,再次我写了一个nginx的启动脚本,内容如下

#!/bin/sh

spswfile=/usr/local/php529/bin/spawn-fcgi
phpcgi=/usr/local/php529/bin/php-cgi
nginxfile=/usr/local/nginx/sbin/nginx
cgifile=/usr/local/bin/cgiwrap-fcgi.pl

isrun() {
    if
        (ps auxf|grep nginx|grep -v grep >/dev/null 2>&1) && (ps auxf|grep php-cgi|grep -v grep >/dev/null 2>&1)
    then
        return 0
    else
        return 1
    fi
}

nginx_start() {
    if isrun
    then
        echo "nginx already start"
        exit 0
    else
        killall -9 php-cgi >/dev/null 2>&1
        killall -9 nginx >/dev/null 2>&1
                killall -9 perl >/dev/null 2>&1
        $spswfile -a 127.0.0.1 -p 9000 -f $phpcgi >/dev/null 2>&1
                /usr/bin/perl $cgifile >/dev/null 2>&1 &
        $nginxfile >/dev/null 2>&1
                sleep 10
                /bin/chown daemon /usr/local/nginx/logs/cgi.sock
    fi
}

nginx_stop() {
        killall -9 php-cgi >/dev/null 2>&1
        killall -9 nginx >/dev/null 2>&1
                killall -9 perl >/dev/null 2>&1
        rm -rf /usr/local/nginx/logs/nginx.pid
}

case $1 in
    start)      nginx_start;;
    stop)       nginx_stop;;
    restart)    nginx_stop
                nginx_start
                ;;
    *)          echo "useage: {start|stop}";;
esac

这样只需要执行这个脚本就可以启动fastcgi和cgi及nginx了,接下来就可以进行真正的测试了

以上文章参考了:

http://wiki.nginx.org/NginxSimpleCGI
http://www.lazysa.com/2009/05/392.html

另外有一点需要注意的就是cgi.cfg的修改,网上很多文章都没有提到,就是我上面写的那个是有默认用户的,如果那个不做修改的话,访问nagios的主机和服务的时候都显示如下,更改之后就没有问题了

无权查看任何主机的信息…
请检查HTTP服务器关于该CGI的访问权限设置。

 

 

Related Post

5 Comments

  1. 博主是否可以将你的nginx+nagios+cacti的nginx配置文件和pl cgi脚本共享给我参考呢?我找了好多配置方法都配置不成功.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.