一.安装nagios这里不做说明
二.nginx部分
复制代码代码如下:
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来处理。
复制代码代码如下:
#!/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; } |