配置文件如下:

请注意,网上提供的官方文档在运行时可能会出现问题,此文中保证无问题。

  1 #!/usr/bin/perl
  2 #
  3 #       author          Daniel Dominik Rudnicki
  4 #       thanks to:      Piotr Romanczuk
  5 #       email           daniel@sardzent.org
  6 #       version         0.4.
  7 #       webpage         http://www.nginx.eu/
  8 #
  9 #       BASED @ http://wiki.codemongers.com/NginxSimpleCGI
 10 #
 11 #
 12 # use strict;
 13 use FCGI;
 14 use Getopt::Long;
 15 use IO::All;
 16 use Socket;
 17 
 18 sub init {
 19         GetOptions(     "h"     => \$help,
 20                         "verbose!"=>\$verbose,
 21                         "pid=s" => \$filepid,
 22                         "l=s" => \$logfile,
 23                         "S:s"   => \$unixsocket,
 24                         "P:i"   => \$unixport) or usage();
 25                 usage() if $help;
 26 
 27         print " Starting Nginx-fcgi\n" if $verbose;
 28         print " Running with $> UID" if $verbose;
 29         print " Perl $]" if $verbose;
 30 
 31 #       if ( $> == "" ) {
 32 #               print "\n\tERROR\tRunning as a root!\n";
 33 #               print "\tSuggested not to do so !!!\n\n";
 34 #               exit ;
 35 #       }
 36 
 37         if ( ! $logfile ) {
 38                 print "\n\tERROR\t log file must declared\n"
 39                         . "\tuse $0 with option -l filename\n\n";
 40                 exit ;
 41         }
 42         print " Using log file $logfile\n" if $verbose;
 43         "\n\n" >> io($logfile);
 44         addlog($logfile, "Starting Nginx-cfgi");
 45         addlog($logfile, "Running with $> UID");
 46         addlog($logfile, "Perl $]");
 47         addlog($logfile, "Testing socket options");
 48 
 49         if ( ($unixsocket && $unixport) || (!($unixsocket) && !($unixport)) ) {
 50                 print "\n\tERROR\tOnly one option can be used!\n";
 51                 print "\tSuggested (beacuse of speed) is usage UNIX socket -S \n\n";
 52                 exit ;
 53         }
 54 
 55         if ($unixsocket) {
 56                 print " Daemon listening at UNIX socket $unixsocket\n" if $versbose;
 57                 addlog($logfile, "Deamon listening at UNIX socket $unixsocket");
 58         } else {
 59                 print " Daemon listening at TCP/IP socket *:$unixport\n" if $verbose;
 60                 #
 61                 addlog($logfile, "Daemon listening at TCP/IP socket *:$unixport");
 62         }
 63 
 64         if ( -e $filepid ) {
 65                 print "\n\tERROR\t PID file $filepid already exists\n\n";
 66                 addlog($logfile, "Can not use PID file $filepid, already exists.");
 67                 exit ;
 68         }
 69 
 70         if ( $unixsocket ) {
 71                 print " Creating UNIX socket\n" if $verbose;
 72                 $socket = FCGI::OpenSocket( $unixsocket,  );
 73                 if ( !$socket) {
 74                         print " Couldn't create socket\n";
 75                         addlog($logfile, "Couldn't create socket");
 76                         exit ;
 77                 }
 78                 print " Using UNIX socket $unixsocket\n" if $verbose;
 79         } else {
 80                 print " Creating TCP/IP socket\n" if $verbose;
 81                 $portnumber = ":".$unixport;
 82                 $socket = FCGI::OpenSocket( $unixport,  );
 83                 if ( !$socket ) {
 84                         print " Couldn't create socket\n";
 85                         addlog($logfile, "Couldn't create socket");
 86                         exit ;
 87                 }
 88                 print " Using port $unixport\n" if $verbose;
 89         }
 90         addlog($logfile, "Socket created");
 91 
 92         if ( ! $filepid ) {
 93                 print "\n\tERROR\t PID file must declared\n"
 94                         . "\tuse $0 with option -pid filename\n\n";
 95                 exit ;
 96         }
 97         print " Using PID file $filepid\n" if $verbose;
 98         addlog($logfile, "Using PID file $filepid");
 99 
         my $pidnumber = $$;
         $pidnumber > io($filepid);
         print " PID number $$\n" if $verbose;
         addlog($logfile, "PID number $pidnumber");
 
 }
 
 sub addzero {
         my ($date) = shift;
         if ($date < ) {
                 return "0$date";
         }
        return $date;
 }
 
 sub logformat {
         my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$iddst) = localtime(time);
         my $datestring;
         $year += ;
         $mon++;
         $mon  = addzero($mon);
         $mday = addzero($mday);
         $min  = addzero($min);
         $datestring = "$year-$mon-$mday $hour:$min";
         return($datestring);
 }
 
 sub addlog {
         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);
 }
 
 sub printerror {
         my $message = @_;
         print "\n       Nginx FastCGI\tERROR\n"
                 . "\t $message\n\n";
         exit ;
 }
 
 sub usage {
         print "\n       Nginx FastCGI \n"
                 . "\n\tusage: $0 [-h] -S string -P int\n"
                 . "\n\t-h\t\t: this (help) message"
                 . "\n\t-S path\t\t: path for UNIX socket"
                 . "\n\t-P port\t\t: port number"
                 . "\n\t-p file\t\t: path for pid file"
                 . "\n\t-l file\t\t: path for logfile"
                 . "\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 ;
 }
 
 
 init;
         exit unless $@ =~ /^fakeexit/;
 } ;
 
 # fork part
 my $pid = fork();
 
 if( $pid ==  ) {
         &main;
         exit ;
 }
 
 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");
 exit ;
 
 sub main {
         $request = FCGI::Request( \*STDIN, \*STDOUT, \*STDERR, \%req_params, $socket );
         if ($request) { request_loop()};
                 FCGI::CloseSocket( $socket );
 }
 
 sub request_loop {
         while( $request->Accept() >=  ) {
                 # processing any STDIN input from WebServer (for CGI-POST actions)
                 $stdin_passthrough = '';
                 $req_len =  + $req_params{'CONTENT_LENGTH'};
                 if (($req_params{'REQUEST_METHOD'} eq 'POST') && ($req_len != ) ){
                         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 !");
                         open $cgi_app, '-|', "echo '$stdin_passthrough' | '$req_params{SCRIPT_FILENAME}'" 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 this process.\n";
                         addlog($logfile, "Error: No such CGI app - $req_params{SCRIPT_FILENAME} may not exist or is not executable by this process.");
                 }
         }
 }

221

nginx环境下配置nagios-关于perl-fcgi.pl的更多相关文章

  1. nginx环境下配置nagios-关于nagios配置文件nginx.conf

    接上文:nginx环境下配置nagios-关于nginx.conf 配置如下: ;          location ~ .*\.(php|php5)?$          {            ...

  2. nginx环境下配置nagiosQL-关于nagiosql配置文件

    接上文:nginx环境下配置nagios-关于nginx.conf nagiosql文件应该处于conf/domain/目录下 nagiosql配置如下: ;                  gzi ...

  3. Nginx环境下配置PHP使用的SSL认证(https)

    最近一段时间发现好多网站都从http协议变成了加密的https协议,比如说百度.吾志等等.https看起来比http高端了好多,而且在不同的浏览器向上还会显示出不同于http的URL展示效果(比如说c ...

  4. nginx环境下搭建nagios 3.5.0,及配置pnp4nagios画图

    本文基于<LNMP最新源码安装脚本>,Nagios依赖PHP环境和perl环境,由于Nginx不支持Perl的CGI,需先来搭建Perl环境,Nagios原理介绍略.一.下载最新稳定源码包 ...

  5. <nginx+PHP>nginx环境下配置支持php7

    [root@redhat7 ~]# wget http://am1.php.net/get/php-7.1.2.tar.gz/from/this/mirror [root@redhat7 ~]# ta ...

  6. nginx环境下配置nagios-关于start_perl_cgi.sh

    >/dev/ rm $dir/logs/perl-fcgi.sock >/dev/ echo  }    start ()  {  rm $dir/now_start_perl_fcgi. ...

  7. 如何在 Linux 环境下配置 Nagios Remote Plugin Executor (NRPE)

    为 NRPE 配置自定义命令 远程服务器上安装 下面列出了一些可以用于 NRPE 的自定义命令.这些命令在远程服务器的 /etc/nagios/nrpe.cfg 文件中定义. ## 当 1.5.15 ...

  8. phpmyadmin在nginx环境下配置错误

    location ~ \.css {           add_header  Content-Type    text/css;        } location ~ \.js {        ...

  9. nginx环境下配置nagios-关于commands.cfg

    -w $ARG1$ -c $ARG2$ -M -b% -c % -f% -c % -f% -c % -f #  define command{         command_name    chec ...

随机推荐

  1. 讨论贴:在sp_executesql 中生成的临时表的可见性

    首先创建数据表 IF object_id('TestTable') IS NOT NULL DROP TABLE TestTable GO ,),Info )) GO INSERT TestTable ...

  2. [读书笔记]java中的类加载器

    以下内容大多来自周志明的<深入理解Java虚拟机>. 类加载器是java的一项创新,也是java流行的重要原因之一,它最初是为了满足java applet的需求而开发出来. 什么是appl ...

  3. 通过SSH远程使用ipython notebook

    本文讲述如何在本地用浏览器运行远程服务器上的iPython notebook服务. 在远程机器上,启动IPython notebooks服务: remote_user@remote_host$ ipy ...

  4. 有向图强连通分量 Tarjan算法

    [有向图强连通分量] 在有向图G中,如果两个顶点间至少存在一条路径,称两个顶点强连通(strongly connected).如果有向图G的每两个顶点都强连通,称G是一个强连通图.非强连通图有向图的极 ...

  5. F1 分数

    F1 分数会同时考虑精确率和召回率,以便计算新的分数. 可将 F1 分数理解为精确率和召回率的加权平均值,其中 F1 分数的最佳值为 1.最差值为 0: F1 = 2 * (精确率 * 召回率) / ...

  6. Nginx日志常用分析命令汇总 (转)

    三.常用日志分析命令 1.总请求数 1 wc -l access.log |awk '{print $1}' 2.独立IP数 1 awk '{print $1}' access.log|sort |u ...

  7. [转]Windows系统中监控文件复制操作的几种方式

    1. ICopyHook 作用: 监视文件夹和打印机移动,删除, 重命名, 复制操作. 可以得到源和目标文件名. 可以控制拒绝操作. 缺点: 不能对文件进行控制. 只对Shell文件操作有效, 对原生 ...

  8. SpringMVC学习系列(10) 之 异常处理

    在项目中如何处理出现的异常,在每个可能出现异常的地方都写代码捕捉异常?这显然是不合理的,当项目越来越大是也是不可维护的.那么如何保证我们处理异常的代码精简且便于维护呢?这就是本篇要讲的内容—>异 ...

  9. SQL 创建一个只读账户 .

    1.进入sqlserver management studio 2.选择安全性->登录名->右键新建登录名 3.在常规里输入用户名和密码 4.在"用户映射"里“映射到此 ...

  10. oracle直通车第二周习题

    1.教材第二章课后作业 1,2,3,4题. 答:1. 创建一查询,显示与Blake在同一部门工作的雇员的项目和受雇日期,但是Blake不包含在内. 2. 显示位置在Dallas的部门内的雇员姓名.变化 ...