当前位置: > Linux服务器 > 监控工具 >

nginx搭建nagios监控环境

时间:2014-10-26 01:17来源:linux.it.net.cn 作者:it

一.安装nagios这里不做说明
1.nagios map显示出不来,要装GD 和GD-DEV*  然后再重新编译nagios,要么就一开始就装好。

二.nginx部分
1.安装
2.配置部分,nginx不能执行外部程序,所以我们要用fastcgi协议来调用外部程序。
Nginx并不提供支持对外部程序的直接调用或者解析(所以缺少像apache里的mod_php这样的模块),所有的外部程序(包括PHP)必须通过fastcgi接口来调用,在Linux下接口是socket (文件socket或者Internet socket)。所以为了调用CGI程序,我们需要一个fastcgi的wrapper,这个wrapper绑定在某个固定socket上(比如端口或者文件socket),当nginx将CGI请求发送给这个socket的时候,wrapper接纳请求并fork一个新的线程,这个线程调用外部的程序或者解释器处理脚本并读取返回值,而wrapper再将返回的数据(网页或者图片等)通过fastcgi将数据通过那个固定的socket传递给nginx
目前有网上流行的两种fastcgi管理器,perl-cgi和spwan-cgi,用spwan-cgi启动php部分
例:spwan-cgi -a 127.0.0.1 -p  9000 -f /usr/local/php-cgi  -C 20
这里我们也可以用-s 参数来启用sock通信,例:unix:/var/run/nginx.sock
命令用法可以用--help来查看,然后再nginx.conf写入
 

复制代码代码如下:
location ~ \.php$ {
          root           /usr/local/nagios/share/;
          fastcgi_pass    127.0.0.1:9000;
          # fastcgi_pass unix:/var/run/nagios.sock;
          fastcgi_index  index.php;
          fastcgi_param  SCRIPT_FILENAME /usr/local/nagios/share/$fastcgi_script_name;
          include        fastcgi_params;
        }
 

把所有php结尾的访问交给php来处理。
下面是perl-cgi的内容,它也是根据fastcgi协议写的
 

复制代码代码如下:
#!/usr/bin/perl
use FCGI;
#perl -MCPAN -e 'install FCGI'
use Socket;
#this keeps the program alive or something after exec'ing perl scripts
END() { } BEGIN() { }
*CORE::GLOBAL::exit = sub { die "fakeexit\nrc=".shift()."\n"; }; eval q{exit}; if ($@) { exit unless $@ =~ /^fakeexit/; } ;
&main;
sub main {
    #$socket = FCGI::OpenSocket( ":3461", 10 ); #use IP sockets
    $socket = FCGI::OpenSocket( "/var/run/nagios.sock", 10 ); #use UNIX sockets - user running this script must have w access to the 'nginx' folder!!
    $request = FCGI::Request( \*STDIN, \*STDOUT, \*STDERR, \%ENV, $socket );
    if ($request) {request_loop()};
               FCGI::CloseSocket( $socket );
}
sub request_loop {
                while( $request->Accept() >= 0 ) {
                   #processing any STDIN input from WebServer (for CGI-GET actions)
                   $env = $request->GetEnvironment();
                   $stdin_passthrough ='';
                   $req_len = 0 + $ENV{CONTENT_LENGTH};
                   if ($ENV{REQUEST_METHOD} eq 'GET'){
                                $stdin_passthrough .= $ENV{'QUERY_STRING'};
                        }
                        #running the cgi app
                        if ( (-x $ENV{SCRIPT_FILENAME}) && #can I execute this?
                                 (-s $ENV{SCRIPT_FILENAME}) && #Is this file empty?
                                 (-r $ENV{SCRIPT_FILENAME})     #can I read this file?
                        ){
                                #http://perldoc.perl.org/perlipc.html#Safe-Pipe-Opens
                open $cgi_app, '-|', $ENV{SCRIPT_FILENAME}, $stdin_passthrough or print("Content-type: text/plain\r\n\r\n"); print "Error: CGI app returned no output - Executing $ENV{SCRIPT_FILENAME} failed !\n";
                                if ($cgi_app) {print <$cgi_app>; close $cgi_app;}
                        }
                        else {
                                print("Content-type: text/plain\r\n\r\n");
                                print "Error: No such CGI app - $req_len - $ENV{CONTENT_LENGTH} - $ENV{REQUEST_METHOD} - $ENV{SCRIPT_FILENAME} may not exist or is not executable by this process.\n";
                        }
                }
}

用一个脚本启动
 

复制代码代码如下:
#!/bin/bash
## start_nginx_cgi.sh: start nginx cgi mode
## ljzhou, 2007.08.20
PERL="/usr/bin/perl"
NGINX_CGI_FILE="/usr/local/nagios/bin/perl-cgi.pl"
#bg_num=`jobs -l |grep "NGINX_CGI_FILE"`
#PID=`ps aux|grep "perl-cgi"|cut -c10-14|xargs kill -9`
PID=`ps aux|grep 'perl-cgi'|cut -c10-14|sed -n "1P"`
echo $PID
sockfiles="/var/run/nagios.sock"
kill -9 $PID
$PERL $NGINX_CGI_FILE &
sleep 3
`chown nobody.nobody $sockfiles`
 

 然后再加如下配置到nginx
 

复制代码代码如下:
location ~ \.cgi$ {
     rewrite ^/nagios/cgi-bin/(.*)\.cgi /$1.cgi break;
     fastcgi_pass unix:/var/run/nagios.sock;
     fastcgi_index index.cgi;
     fastcgi_param SCRIPT_FILENAME /usr/local/nagios/sbin$fastcgi_script_name;
     fastcgi_param HTTP_ACCEPT_LANGUAGE en_US;
     include fastcgi_params;
  }
     location ~ \.pl$ {
     fastcgi_pass  unix:/var/run/nagios.sock;
     fastcgi_index index.pl;
     fastcgi_param SCRIPT_FILENAME  /usr/local/nagios/sbin$fastcgi_script_name;
     include       fastcgi_params;
  }
 
(责任编辑:IT)
------分隔线----------------------------