Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2158087
  • 博文数量: 317
  • 博客积分: 5670
  • 博客等级: 大校
  • 技术积分: 3677
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-10 17:51
文章分类

全部博文(317)

文章存档

2016年(2)

2015年(44)

2014年(68)

2013年(42)

2012年(23)

2011年(51)

2010年(67)

2009年(17)

2008年(3)

分类: LINUX

2012-03-28 15:21:32

nagios+nginx从环境搭建到配置

   nagios是一个开源免费的系统监控工具,官网地址
    截止2011-07-27    nagios的最新版本是3.x,基于c语言编写,并提供了一个基于php和cgi的web管理界面
    

环境搭建

    nagios需要一个web容器+php的运行环境,由于现在的公司统一使用nginx作为web容器,在这里我选择了nginx+php
    nginx官网地址为    
    php官网地址为    
    
    如果对于nagios的web界面,需要根据用户配置相应的访问权限的话,那还得安装一个apache,没别的用途,就是用htpassword生成账户信息。
    apache官网地址    


    
    我所准备的jar包如下,其中spawn-fcgi是用于nginx重定向到php用的
        lighttpd-1.4.28.tar.gz
        nginx-1.0.5.tar.gz
        php-5.3.6.tar.gz
        spawn-fcgi-1.6.2.tar.gz
        
    安装nginx    

   tar nginx-1.0.5.tar.gz cd nginx-1.0.5/ ./configure --prefix=/usr/local/nginx make && make install


    注意--prefix是在指定软件的安装路径,可以根据个人情况自行修改
    安装php之前最好参考张宴的这篇博客 http://blog.s135.com/post/297/ 安装对应的图形和xml等lib,当然稍后安装也行    

   tar zxvf php-5.3.6.tar.gz cd php-5.3.6/ ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc l --with-zlib --disable-debug --disable-rpath --enable-discard-path --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fastcgi --enable-force-cgi-redirect make && make install cp php.ini-dist /usr/local/php/etc/php.ini cd ../

    注意,一定要带上--enable-fastcgi --enable-force-cgi-redirect,如果执行 ./configure时 提示缺少某个包,比如zlib,gd等等,安装上即可
    
    解压缩并安装 spawn-fcgi,与张宴博客不同的是,在我写这篇博客时spawn-fcgi已经作为单独的包提供使用了


tar spawn-fcgi-1.6.2.tar.gz cd spawn-fcgi-1.6.2/ ./configure --prefix=/usr/local/spawn-fcgi make && make install

    然后仅仅是把spawn-fcgi这个文件copy去php的bin目录下,为什么仅仅只要一个文件呢?好吧,我也不知道。


cp spawn-fcgi /usr/local/php/bin chmod +x /usr/local/php/bin/spawn-fcgi

   
    启动php cgi进程

/usr/local/php/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 250 -u www -f /usr/local/php/bin/php-cgi

  这样一来环境就搭建的差不多了,这中间容易出问题的就是php安装的时候可能会报缺某些工具包
    

安装nagios及其插件nagios-plugin
tar nagios-3.2.3.tar.gz cd nagios-3.2.3/ ./configure --prefix=/usr/local/nagios --with-gd --with-libpng --with-libjpeg make && make install

    注意,在安装gd,libpng,libjpeg时还需要安装对应的devel包才行,即gd-devel,libpng-devel,libjpeg-devel



tar nagios-plugins-1.4.15.tar.gz cd nagios-plugins-1.4.15/ ./configure --prefix=/usr/local/nagios --with-gd --with-libpng --with-libjpeg make && make install


    安装fast-cgi

tar FCGI-ProcManager-0.19.tar.gz cd FCGI-ProcManager-0.19/ ./configure --prefix=/usr/local/fcgi make && make install


    
    在很多nginx+nagios的搭建教程中都提到一个 nginx-fcgi的脚本文件,下载地址是 但当我写这篇博客时该地址已经失效了,脚本文件作为附件奉上
    
    然后启动fcgi

./nginx-fcgi -l /usr/local/logs/nginx-fcgi.log -pid /tmp/nginx-fcgi.pid -S /tmp/nginx-fcgi.sock

    注意, -pid用于指定nginx-fcgi的进程pid, 需要执行以下命令更改运行权限

chmod    777 /tmp/nginx-fcgi.sock

   

配置nginx
cd /usr/local/nginx/conf vi nginx.conf

 添加如下设置
server {
    listen 80;
    server_name nagios.test.com;
    root /usr/local/nagios/share;
    index index.html index.htm index.php default.html default.htm default.php;
    location ~ .*\.php?$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php; include fastcgi.conf;
        fastcgi_param SCRIPT_FILENAME /usr/local/nagios/share$fastcgi_script_name;
        auth_basic "nagios admin";
        auth_basic_user_file /usr/local/nagios/etc/nagiosAdmin.net;
    }
    location ~ .*\.cgi$ {
        root /usr/local/nagios/sbin;
        rewrite ^/nagios/cgi-bin/(.*)\.cgi /$1.cgi break;
        fastcgi_pass unix:/tmp/nginx-fcgi.sock;
        fastcgi_index index.cgi;
        fastcgi_param SCRIPT_FILENAME /usr/local/nagios/sbin$fastcgi_script_name;
        include fastcgi.conf; auth_basic "nagios admin";
        auth_basic_user_file /usr/local/nagios/etc/nagiosAdmin.net;
    }
    location /nagios {
        alias /usr/local/nagios/share;
        auth_basic "nagios admin";
        auth_basic_user_file /usr/local/nagios/etc/nagiosAdmin.net;
    }
    location ~ .*\.pl$ {
        fastcgi_pass unix:/tmp/nginx-fcgi.sock;
        fastcgi_index index.pl; 
        fastcgi_param SCRIPT_FILENAME /usr/local/nagios/sbin$fastcgi_script_name;
        include fastcgi.conf;
    }
}

  其中 auth_basic和auth_basic_user_file用于指定该种URI的访问权限,对应的 /usr/local/nagios/etc/nagiosAdmin.net 需要事先用htpasswd命令生成后缀为.php的URI用

        fastcgi_pass    127.0.0.1:9000;
        fastcgi_index   index.php;

        来指定给spawn-cgi处理后缀为.cgi的URI用
        root    /usr/local/nagios/sbin;
        rewrite ^/nagios/cgi-bin/(.*)\.cgi /$1.cgi break;
        fastcgi_pass unix:/tmp/nginx-fcgi.sock;
        fastcgi_index index.cgi;
        fastcgi_param SCRIPT_FILENAME /usr/local/nagios/sbin$fastcgi_script_name;
        include fastcgi.conf;

    指定给刚才启动的nginx-fcgi进程处理,注意nagios的php程序都在/usr/local/nagios/share 下,而cgi程序都在/usr/local/nagios/sbin下,别搞错路径之后就是启动nagios,nginx,在你当前机器设置host后访问 查看web内容.

nagios的配置

nagios的配置文件都在/usr/local/nagios/etc下,其中    
    nagios.cfg    是主配置文件
    cgi.cfg    用于配置权限之类


    详细配置文件在/usr/local/nagios/etc/object之内,如何配置详见

    

    配置文件修改完毕后,执行命令

/usr/local/nagios/bin/nagios -pv /usr/local/nagios/etc/nagios.cfg

更新缓存,

详见

  下面是nginx-fcgi的源码

点击(此处)折叠或打开

  1. #!/usr/bin/perl
  2. #
  3. #    author        Daniel Dominik Rudnicki
  4. #    thanks to:    Piotr Romanczuk
  5. #    email        daniel@sardzent.org
  6. #    version        0.4.3
  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. sub init {
  18.     GetOptions(    "h"    => \$help,
  19.             "verbose!"=>\$verbose,
  20.             "pid=s"    => \$filepid,
  21.             "l=s" => \$logfile,
  22.             "S:s" => \$unixsocket,
  23.             "P:i" => \$unixport) or usage();
  24.         usage() if $help;

  25.     print "    Starting Nginx-fcgi\n" if $verbose;
  26.     print "    Running with $> UID" if $verbose;
  27.     print "    Perl $]" if $verbose;

  28. #    if ( $> == "0" ) {
  29. #        print "\n\tERROR\tRunning as a root!\n";
  30. #        print "\tSuggested not to do so !!!\n\n";
  31. #        exit 1;
  32. #    }

  33.         if ( ! $logfile ) {
  34.         print "\n\tERROR\t log file must declared\n"
  35.             . "\tuse $0 with option -l filename\n\n";
  36.         exit 1;
  37.     }
  38.     print "    Using log file $logfile\n" if $verbose;
  39.     "\n\n" >> io($logfile);
  40.     addlog($logfile, "Starting Nginx-cfgi");
  41.     addlog($logfile, "Running with $> UID");
  42.     addlog($logfile, "Perl $]");
  43.     addlog($logfile, "Testing socket options");

  44.     if ( ($unixsocket && $unixport) || (!($unixsocket) && !($unixport)) ) {
  45.         print "\n\tERROR\tOnly one option can be used!\n";
  46.         print "\tSuggested (beacuse of speed) is usage UNIX socket -S \n\n";
  47.         exit 1;
  48.     }

  49.     if ($unixsocket) {
  50.         print "    Daemon listening at UNIX socket $unixsocket\n" if $versbose;
  51.         addlog($logfile, "Deamon listening at UNIX socket $unixsocket");
  52.     } else {
  53.         print "    Daemon listening at TCP/IP socket *:$unixport\n" if $verbose;
  54.         #
  55.         addlog($logfile, "Daemon listening at TCP/IP socket *:$unixport");
  56.     }

  57.     if ( -e $filepid ) {
  58.         print "\n\tERROR\t PID file $filepid already exists\n\n";
  59.         addlog($logfile, "Can not use PID file $filepid, already exists.");
  60.         exit 1;
  61.     }

  62.     if ( $unixsocket ) {
  63.         print "    Creating UNIX socket\n" if $verbose;
  64.         $socket = FCGI::OpenSocket( $unixsocket, 10 );
  65.         if ( !$socket) {
  66.             print "    Couldn't create socket\n";
  67.             addlog($logfile, "Couldn't create socket");
  68.             exit 1;
  69.         }
  70.         print "    Using UNIX socket $unixsocket\n" if $verbose;
  71.     } else {
  72.         print "    Creating TCP/IP socket\n" if $verbose;
  73.         $portnumber = ":".$unixport;
  74.         $socket = FCGI::OpenSocket( $unixport, 10 );
  75.         if ( !$socket ) {
  76.             print "    Couldn't create socket\n";
  77.             addlog($logfile, "Couldn't create socket");
  78.             exit 1;
  79.         }
  80.         print " Using port $unixport\n" if $verbose;
  81.     }
  82.     addlog($logfile, "Socket created");

  83.     if ( ! $filepid ) {
  84.         print "\n\tERROR\t PID file must declared\n"
  85.             . "\tuse $0 with option -pid filename\n\n";
  86.         exit 1;
  87.     }
  88.     print "    Using PID file $filepid\n" if $verbose;
  89.     addlog($logfile, "Using PID file $filepid");

  90.     my $pidnumber = $$;
  91.     $pidnumber > io($filepid);
  92.     print " PID number $$\n" if $verbose;
  93.     addlog($logfile, "PID number $pidnumber");
  94.     
  95. }

  96. sub addzero {
  97.     my ($date) = shift;
  98.     if ($date < 10) {
  99.         return "0$date";
  100.     }
  101.        return $date;
  102. }

  103. sub logformat {
  104.     my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$iddst) = localtime(time);
  105.     my $datestring;
  106.     $year += 1900;
  107.     $mon++;
  108.     $mon = addzero($mon);
  109.     $mday = addzero($mday);
  110.     $min = addzero($min);
  111.     $datestring = "$year-$mon-$mday $hour:$min";
  112.     return($datestring);
  113. }

  114. sub addlog {
  115.     my ($log_file, $log_message) = @_;
  116.     my $curr_time = logformat();
  117.     my $write_message = "[$curr_time] $log_message";
  118.     $write_message >> io($log_file);
  119.     "\n" >> io($log_file);
  120. }

  121. sub printerror {
  122.     my $message = @_;
  123.     print "\n    Nginx FastCGI\tERROR\n"
  124.         . "\t $message\n\n";
  125.     exit 1;
  126. }

  127. sub usage {
  128.     print "\n    Nginx FastCGI \n"
  129.         . "\n\tusage: $0 [-h] -S string -P int\n"
  130.         . "\n\t-h\t\t: this (help) message"
  131.         . "\n\t-S path\t\t: path for UNIX socket"
  132.         . "\n\t-P port\t\t: port number"
  133.         . "\n\t-p file\t\t: path for pid file"
  134.         . "\n\t-l file\t\t: path for logfile"
  135.         . "\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";
  136.     exit 1;
  137. }


  138. init;
  139. #
  140. END() { } BEGIN() { }
  141. *CORE::GLOBAL::exit = sub { die "fakeexit\nrc=".shift()."\n"; }; eval q{exit};
  142. if ($@) {
  143.     exit unless $@ =~ /^fakeexit/;
  144. } ;

  145. # fork part
  146. my $pid = fork();

  147. if( $pid == 0 ) {
  148.     &main;
  149.     exit 0;
  150. }

  151. print " Forking worker process with PID $pid\n" if $verbose;
  152. addlog($logfile, "Forking worker process with PID $pid");
  153. print " Update PID file $filepid\n" if $verbose;
  154. addlog($logfile, "Update PID file $filepid");
  155. $pid > io($filepid);
  156. print "    Worker process running.\n" if $verbose;
  157. addlog ($logfile, "Parent process $$ is exiting");
  158. exit 0;

  159. sub main {
  160.     $request = FCGI::Request( \*STDIN, \*STDOUT, \*STDERR, \%req_params, $socket );
  161.     if ($request) { request_loop()};
  162.         FCGI::CloseSocket( $socket );
  163. }

  164. sub request_loop {
  165.     while( $request->Accept() >= 0 ) {
  166.         # processing any STDIN input from WebServer (for CGI-POST actions)
  167.         $stdin_passthrough = '';
  168.         $req_len = 0 + $req_params{'CONTENT_LENGTH'};
  169.         if (($req_params{'REQUEST_METHOD'} eq 'POST') && ($req_len != 0) ){
  170.             while ($req_len) {
  171.                 $stdin_passthrough .= getc(STDIN);
  172.                 $req_len--;    
  173.             }
  174.         }
  175.         # running the cgi app
  176.         if ( (-x $req_params{SCRIPT_FILENAME}) &&
  177.             (-s $req_params{SCRIPT_FILENAME}) &&
  178.             (-r $req_params{SCRIPT_FILENAME})
  179.         ){
  180.             foreach $key ( keys %req_params){
  181.                 $ENV{$key} = $req_params{$key};
  182.             }
  183.             if ( $verbose ) {
  184.                 addlog($logfile, "running $req_params{SCRIPT_FILENAME}");
  185.             }
  186.             # http://perldoc.perl.org/perlipc.html#Safe-Pipe-Opens
  187.             #
  188.             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 !");
  189.             
  190.             if ($cgi_app) {
  191.                 print <$cgi_app>;
  192.                 close $cgi_app;
  193.             }
  194.         } else {
  195.             print("Content-type: text/plain\r\n\r\n");
  196.             print "Error: No such CGI app - $req_params{SCRIPT_FILENAME} may not exist or is not executable by this process.\n";
  197.             addlog($logfile, "Error: No such CGI app - $req_params{SCRIPT_FILENAME} may not exist or is not executable by this process.");
  198.         }
  199.     }
  200. }

阅读(2516) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~