首页 > 代码库 > nginx配置支持nagios

nginx配置支持nagios

配置前提:安装了nginx和php。并且php也安装了gd支持

现在需要做的是让nagios在LNMP环境下也能运行

1.首先须需要安装依赖包

1.1.安装perl

Shell> yum install perl

1.2.安装perl fcgi模块

Shell> wget http://search.cpan.org/CPAN/authors/id/F/FL/FLORA/FCGI-0.74.tar.gz

Shell> perl Makefile.PL

Shell> make && make install

1.3.安装FCGI-ProcManager:

Shell> wget http://search.cpan.org/CPAN/authors/id/B/BO/BOBTFISH/FCGI-ProcManager-0.2

4.tar.gz

Shell> perl Makefile.PL

Shell> make && make install

1.4.安装perl-IO

Shell> wget http://search.cpan.org/CPAN/authors/id/G/GB/GBARR/IO-1.25.tar.gz

Shell> perl Makefile.PL

Shell> make && make install

1.5.安装perl-IO-ALL

Shell> wget http://search.cpan.org/CPAN/authors/id/I/IN/INGY/IO-All-0.39.tar.gz

Shell> perl Makefile.PL

Warning: prerequisite IO::String 0 not found.

Writing Makefile for IO::All

解决:

Shell> cpan IO::String

Shell> perl Makefile.PL

Writing Makefile for IO::All

Shell> make && make install

nagios中web界面为cgi程序,我们使用unixsocket来监听perl CGI请求并解析

2.运行perl脚本

2.1 Perl脚本用来产生perlfast-cgi接口,让nginxCGI的形式来处理perl 
shell> cd /usr/local/nginx/sbin

Shell> mkdir logs && chown www.www logs     --这里的www为nginx运行用户

Shell> wget http://addcn.googlecode.com/svn/trunk/conf/nagios/nginx-fcgi.pl

Shell> mv nginx-fcgi.pl perl-fcgi.pl

Shell> chmod 755 perl-fcgi.pl

Shell> cat perl-fcgi.pl

------------------------------------

#!/usr/bin/perl

#

#   author      DanielDominik Rudnicki

#   thanks to:  PiotrRomanczuk

#   email       daniel@sardzent.org

#   version     0.4.3

#   webpage     http://www.nginx.eu/

#

#   BASED @ http://wiki.codemongers.com/NginxSimpleCGI

#

#

useFCGI;

useGetopt::Long;

useIO::All;

useSocket;

 

subinit {

    GetOptions( "h" => \$help,

            "verbose!"=>\$verbose,

            "pid=s" => \$filepid,

            "l=s" => \$logfile,

            "S:s"   => \$unixsocket,

            "P:i"   => \$unixport) or usage();

        usage() if $help;

 

    print " StartingNginx-fcgi\n" if $verbose;

    print " Runningwith $> UID" if $verbose;

    print " Perl$]" if $verbose;

 

    if ( $> == "0" ) {

        print "\n\tERROR\tRunning as aroot!\n";

        print "\tSuggested not to do so!!!\n\n";

        exit 1;

    }

 

        if ( ! $logfile ) {

        print "\n\tERROR\t log file mustdeclared\n"

            . "\tuse $0 with option -lfilename\n\n";

        exit 1;

    }

    print " Usinglog file $logfile\n" if $verbose;

    "\n\n" >> io($logfile);

    addlog($logfile, "StartingNginx-cfgi");

    addlog($logfile, "Running with $>UID");

    addlog($logfile, "Perl $]");

    addlog($logfile, "Testing socketoptions");

 

    if ( ($unixsocket && $unixport) ||(!($unixsocket) && !($unixport)) ) {

        print "\n\tERROR\tOnly one optioncan be used!\n";

        print "\tSuggested (beacuse ofspeed) is usage UNIX socket -S \n\n";

        exit 1;

    }

 

    if ($unixsocket) {

        print " Daemon listening at UNIX socket $unixsocket\n" if $versbose;

        addlog($logfile, "Deamon listeningat UNIX socket $unixsocket");

    } else {

        print " Daemon listening at TCP/IP socket *:$unixport\n" if $verbose;

        #

        addlog($logfile, "Daemon listeningat TCP/IP socket *:$unixport");

    }

 

    if ( -e $filepid ) {

        print "\n\tERROR\t PID file$filepid already exists\n\n";

        addlog($logfile, "Can not use PIDfile $filepid, already exists.");

        exit 1;

    }

 

    if ( $unixsocket ) {

        print " Creating UNIX socket\n" if $verbose;

        $socket = FCGI::OpenSocket( $unixsocket,10 );

        if ( !$socket) {

            print " Couldn‘t create socket\n";

            addlog($logfile, "Couldn‘tcreate socket");

            exit 1;

        }

        print " Using UNIX socket $unixsocket\n" if $verbose;

    } else {

        print " Creating TCP/IP socket\n" if $verbose;

        $portnumber = ":".$unixport;

        $socket = FCGI::OpenSocket( $unixport,10 );

        if ( !$socket ) {

            print " Couldn‘t create socket\n";

            addlog($logfile, "Couldn‘tcreate socket");

            exit 1;

        }

        print " Using port$unixport\n" if $verbose;

    }

    addlog($logfile, "Socketcreated");

 

    if ( ! $filepid ) {

        print "\n\tERROR\t PID file mustdeclared\n"

            . "\tuse $0 with option -pidfilename\n\n";

        exit 1;

    }

    print " UsingPID file $filepid\n" if $verbose;

    addlog($logfile, "Using PID file$filepid");

 

    my $pidnumber = $$;

    $pidnumber > io($filepid);

    print " PID number $$\n" if$verbose;

    addlog($logfile, "PID number$pidnumber");

   

}

 

subaddzero {

    my ($date) = shift;

    if ($date < 10) {

        return "0$date";

    }

       return $date;

}

 

sublogformat {

    my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$iddst) = localtime(time);

    my $datestring;

    $year += 1900;

    $mon++;

    $mon = addzero($mon);

    $mday = addzero($mday);

    $min = addzero($min);

    $datestring = "$year-$mon-$mday$hour:$min";

    return($datestring);

}

 

subaddlog {

    my ($log_file, $log_message) = @_;

    my $curr_time = logformat();

    my $write_message = "[$curr_time]   $log_message";

    $write_message >> io($log_file);

    "\n" >> io($log_file);

}

 

subprinterror {

    my $message = @_;

    print "\n   Nginx FastCGI\tERROR\n"

        . "\t $message\n\n";

    exit 1;

}

 

subusage {

    print "\n   Nginx FastCGI \n"

        . "\n\tusage: $0 [-h] -S string -Pint\n"

        . "\n\t-h\t\t: this (help)message"

        . "\n\t-S path\t\t: path for UNIXsocket"

        . "\n\t-P port\t\t: portnumber"

        . "\n\t-p file\t\t: path for pidfile"

        . "\n\t-l file\t\t: path forlogfile"

        . "\n\n\texample: $0 -S/var/run/nginx-perl_cgi.sock -l /var/log/nginx/nginx-cfgi.log -pid/var/run/nginx-fcgi.pid\n\n";

    exit 1;

}

 

 

init;

#

END(){ } BEGIN() { }

*CORE::GLOBAL::exit= sub { die "fakeexit\nrc=".shift()."\n"; }; eval q{exit};

if($@) {

    exit unless $@ =~ /^fakeexit/;

} ;

 

# forkpart

my$pid = fork();

 

if($pid == 0 ) {

    &main;

    exit 0;

}

 

print" Forking worker process with PID $pid\n" if $verbose;

addlog($logfile,"Forking worker process with PID $pid");

print" Update PID file $filepid\n" if $verbose;

addlog($logfile,"Update PID file $filepid");

$pid> io($filepid);

print" Worker process running.\n" if$verbose;

addlog($logfile, "Parent process $$ is exiting");

exit0;

 

submain {

    $request = FCGI::Request( \*STDIN, \*STDOUT,\*STDERR, \%req_params, $socket );

    if ($request) { request_loop()};

        FCGI::CloseSocket( $socket );

}

 

subrequest_loop {

    while( $request->Accept() >= 0 ) {

        # processing any STDIN input fromWebServer (for CGI-POST actions)

        $stdin_passthrough = ‘‘;

        $req_len = 0 +$req_params{‘CONTENT_LENGTH‘};

        if (($req_params{‘REQUEST_METHOD‘} eq‘POST‘) && ($req_len != 0) ){

            while ($req_len) {

                $stdin_passthrough .=getc(STDIN);

                $req_len--;

            }

        }

 

        # running the cgi app

        if ( (-x $req_params{SCRIPT_FILENAME})&&

            (-s $req_params{SCRIPT_FILENAME})&&

            (-r $req_params{SCRIPT_FILENAME})

        ){

            foreach $key ( keys %req_params){

                $ENV{$key} = $req_params{$key};

            }

            if ( $verbose ) {

                addlog($logfile, "running $req_params{SCRIPT_FILENAME}");

            }

            #http://perldoc.perl.org/perlipc.html#Safe-Pipe-Opens

            #

            open $cgi_app, ‘-|‘,$req_params{SCRIPT_FILENAME}, $stdin_passthrough or print("Content-type:text/plain\r\n\r\n"); print "Error: CGI app returned no output -Executing $req_params{SCRIPT_FILENAME} failed !\n"; # addlog($logfile,"Error: CGI app returned no output - Executing$req_params{SCRIPT_FILENAME} failed !");

           

            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_params{SCRIPT_FILENAME} may not exist or is not executable by thisprocess.\n";

            addlog($logfile, "Error: Nosuch CGI app - $req_params{SCRIPT_FILENAME} may not exist or is not executableby this process.");

        }

    }

}

--------------------------------------------------------------------

2.2.建立一个CGI启动/停止脚本

Shell>pwd

/usr/local/nginx/sbin

Shell> vim start_perl_cgi.sh

-----------------------------------

#!/bin/bash 

#set -x 

dir=/usr/local/nginx/sbin

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

-----------------------------------------------------------------------

2.3.启动脚本

Shell>chmod 755 /usr/local/nginx/sbin/start_perl_cgi.sh

Shell>cd /usr/local/nginx/sbin

Shell>./start_perl_cgi.sh start

Shell> ls logs –lh   #检查文件是否存在

perl-fcgi.log perl-fcgi.pid  perl-fcgi.sock

1.9.配置nginx作为web服务器

Shell> vim /usr/local/nginx/conf/nginx.conf

        includevirtual/nagios.web.com.conf;       #web服务器

shell> cd cd/usr/local/nginx/conf/virtual

shell> cat nagios.web.com.conf

-----------------------------------------------------------

server{

    listen 9180;

    index index.php index.html index.htm;

    root /data/monitor/nagios/share;

    location ~ \.php$ {

            root    /data/monitor/nagios/share;

            fastcgi_pass   127.0.0.1:9000;

            fastcgi_index  index.php;

            include        fastcgi.conf;

             }

    location /nagios/ {

        alias /data/monitor/nagios/share/;

        index index.php index.htm index.html;

    }

    location ~ .*\.(cgi|pl)?$ {

        gzip off;

        root /data/monitor/nagios/sbin;

        rewrite ^/nagios/cgi-bin/(.*)\.cgi/$1.cgi break;

        fastcgi_pass unix:/usr/local/nginx/sbin/logs/perl-fcgi.sock;

        #fastcgi_pass 127.0.0.1:8999;

        fastcgi_param REMOTE_USER $remote_user;

        fastcgi_index index.cgi;

        fastcgi_read_timeout   60;

        fastcgi_param SCRIPT_FILENAME/data/monitor/nagios/sbin$fastcgi_script_name;

        fastcgi_param HTTP_ACCEPT_LANGUAGEzh-cn;        

        include fastcgi_params;

        include fastcgi.conf;

    }

}


server{

    listen      80;

    server_name nagios.medees.com;

    charset utf-8;

    access_log logs/nagios.access.log  main;

    index index.php index.html;

    location / {

        proxy_pass http://127.0.0.1:9180;

        proxy_set_header HOST $host;

        proxy_set_header X-Real-IP$remote_addr;

        proxy_set_header X-Forwarded-For$proxy_add_x_forwarded_for;

    }

   location /server-status {

        stub_status on;

        access_log off;

    }

}

--------------------------------------------

1.11.  重启nginx服务器

Shell>/etc/init.d/nginx configtest

Shell>/etc/init.d/nginx restart

Shell>/etc/init.d/nagios checkconfig

Shell>/etc/init.d/nagios restart

#浏览器访问nagios.medees.com,输入用户名,密码


本文出自 “fightering-cluter” 博客,请务必保留此出处http://3974020.blog.51cto.com/3964020/1543136