Chinaunix首页 | 论坛 | 博客
  • 博客访问: 133180
  • 博文数量: 11
  • 博客积分: 171
  • 博客等级: 入伍新兵
  • 技术积分: 387
  • 用 户 组: 普通用户
  • 注册时间: 2012-01-18 20:43
文章分类

全部博文(11)

文章存档

2013年(3)

2012年(8)

我的朋友

分类: 架构设计与优化

2012-01-21 04:32:56

  Nagios是一款开源的免费网络监视工具,能有效监控Windows、Linux和Unix的主机状态,交换机路由器等网络设置,打印机等。在系统或服务状态异常时发出邮件或短信报警第一时间通知网站运维人员,在状态恢复后发出正常的邮件或短信通知。
  --以上来自百度百科nagios
  作为监控,nagios的确是最佳选择,它能有效地监控各种网络服务(WEB服务,邮件服务……),并且可以在服务异常时,第一时间通知系统管理员。监控的各种服务,有些既可以通过插件来完成,有些也可以写自定义监控程序。
  我这里用的是nagios3.2.3版本,系统是Red Hat Enterprise Linux AS release 4 (Nahant Update 7)(用centos 5.5 5.6 5.7版本也可以)。我的WEB服务平台是LNMP,需要安装LNMP平台的,请参考我的另一篇文章,《安装LNMP》。
  (1)所需要准备的软件(nagios与nagios-plugins的版本不需要对应)
  1. nagios-3.2.3.tar.gz
  2. nagios-plugins-1.4.15.tar.gz
  3. FCGI-0.74.tar.gz
  4. FCGI-ProcManager-0.24.tar.gz
  (2)创建用户及群组:
  1. useradd nagios  #nagios为安全起见,服务用此用户启动
  2. groupadd nagcmd #这个群组主要是让WEB页面可以设置nagios
  3. usermod -G nagcmd nagios
  4. usermod -G nagcmd www #把WEB服务的启动用户加入群组
  5. 因为nagios用户不需要登录,所以修改passwd文件
  6. vi /etc/passwd
  7. 把nagios:x:513:513::/home/nagios:/bin/bash改为nagios:x:513:513::/home/nagios:/sbin/nologin
  (3)安装nagios及插件,及启动
  1. tar -zxvf nagios-3.2.3.tar.gz
  2. cd nagios
  3. cd nagios-3.2.3
  4. ./configure --prefix=/usr/local/nagios/ --with-group=nagios --with-user=nagios --with-command-group=nagcmd --with-gd-lib=/usr/local/gd/lib/ --with-gd-inc=/usr/local/gd/include/
  5. make all && make install && make install-init && make install-config && make install-commandmode
  6. tar -zxvf nagios-plugins-1.4.15.tar.gz
  7. cd nagios-plugins-1.4.15
  8. ./configure --prefix=/usr/local/nagios/
  9. make && make install

  mkdir -p /usr/local/nagios/share/nagios
 ln -s /usr/local/nagios/share/images /usr/local/nagios/share/nagios/images
 service nagios start 

  (4)安装perl (FastCGI),并配置nginx。
  由于nagios含有CGIs程序,要让要在web页面显示nagios监控情况,必须要让nginx支持perl cgi
  1. tar -zxvf FCGI-0.74.tar.gz
  2. cd FCGI-0.74
  3. perl Makefile.PL
  4. make && make install
  5. tar -zxvf FCGI-ProcManager-0.24.tar.gz
  6. cd FCGI-ProcManager-0.24
  7. perl Makefile.PL
  8. make && make install
  9. 或者直接用yum安装
  10. yum install perl*
  11. perl -MCPAN -e 'install FCGI'
  12. perl -MCPAN -e 'install FCGI::ProcManager'
vi /usr/bin/cgiwrap-fcgi.pl 输入如下代码,保存
  1. #!/usr/bin/perl -w
  2. use FCGI;
  3. use Socket;
  4. use FCGI::ProcManager;
  5. sub shutdown { FCGI::CloseSocket($socket); exit; }
  6. sub restart  { FCGI::CloseSocket($socket); &main; }
  7. use sigtrap 'handler', \&shutdown, 'normal-signals';
  8. use sigtrap 'handler', \&restart,  'HUP';
  9. require 'syscall.ph';
  10. use POSIX qw(setsid);
  11.  
  12. #&daemonize; we don't daemonize when running under runsv
  13. #this keeps the program alive or something after exec'ing perl scripts
  14. END()   { }
  15. BEGIN() { }
  16. {
  17.     no warnings;
  18.     *CORE::GLOBAL::exit = sub { die "fakeexit\nrc=" . shift() . "\n"; };
  19. };
  20. eval q{exit};
  21. if ($@) {
  22.     exit unless $@ =~ /^fakeexit/;
  23. }
  24. &main;
  25.  
  26. sub daemonize() {
  27.     chdir '/' or die "Can't chdir to /: $!";
  28.     defined( my $pid = fork ) or die "Can't fork: $!";
  29.     exit if $pid;
  30.     setsid() or die "Can't start a new session: $!";
  31.     umask 0;
  32. }
  33.  
  34. sub main {
  35.  
  36. #$socket = FCGI::OpenSocket( "127.0.0.1:8999", 10 ); #use IP sockets
  37. #$socket = FCGI::OpenSocket( "/var/run/nginx/perl_cgi-dispatch.sock", 10 ); #use UNIX sockets - user running this script must have w access to the 'nginx' folder!!
  38. #foreach $item (keys %ENV) { delete $ENV{$item}; }
  39.     $proc_manager = FCGI::ProcManager->new( {n_processes => 5} );
  40.     $socket = FCGI::OpenSocket( "127.0.0.1:8999", 10 ); #use UNIX sockets - user running this script must have w access to the 'nginx' folder!!
  41.     $request =
  42.        FCGI::Request( \*STDIN, \*STDOUT, \*STDERR, \%req_params, $socket,
  43.         &FCGI::FAIL_ACCEPT_ON_INTR );
  44.     $proc_manager->pm_manage();
  45.     if ($request) { request_loop() }
  46.    FCGI::CloseSocket($socket);
  47. }
  48.  
  49. sub request_loop {
  50.     while ( $request->Accept() >= 0 ) {
  51.         $proc_manager->pm_pre_dispatch();
  52.  
  53.         #processing any STDIN input from WebServer (for CGI-POST actions)
  54.         $stdin_passthrough = '';
  55.         { no warnings; $req_len = 0 $req_params{'CONTENT_LENGTH'}; };
  56.         if ( ( $req_params{'REQUEST_METHOD'} eq 'POST' ) && ( $req_len != 0 ) )
  57.         {
  58.             my $bytes_read = 0;
  59.             while ( $bytes_read < $req_len ) {
  60.                 my $data = '';
  61.                 my $bytes = read( STDIN, $data, ( $req_len - $bytes_read ) );
  62.                 last if ( $bytes == 0 || !defined($bytes) );
  63.                 $stdin_passthrough .= $data;
  64.                 $bytes_read = $bytes;
  65.             }
  66.         }
  67.  
  68.         #running the cgi app
  69.         if (
  70.             ( -x $req_params{SCRIPT_FILENAME} ) &&    #can I execute this?
  71.             ( -s $req_params{SCRIPT_FILENAME} ) &&    #Is this file empty?
  72.             ( -r $req_params{SCRIPT_FILENAME} )       #can I read this file?
  73.             )
  74.         {
  75.             pipe( CHILD_RD,   PARENT_WR );
  76.             pipe( PARENT_ERR, CHILD_ERR );
  77.             my $pid = open( CHILD_O, "-|" );
  78.             unless ( defined($pid) ) {
  79.                 print("Content-type: text/plain\r\n\r\n");
  80.                 print
  81. "Error: CGI app returned no output - Executing $req_params{SCRIPT_FILENAME} failed !\n";
  82.                 next;
  83.             }
  84.             $oldfh = select(PARENT_ERR);
  85.             $|     = 1;
  86.             select(CHILD_O);
  87.             $| = 1;
  88.             select($oldfh);
  89.             if ( $pid > 0 ) {
  90.                 close(CHILD_RD);
  91.                 close(CHILD_ERR);
  92.                 print PARENT_WR $stdin_passthrough;
  93.                 close(PARENT_WR);
  94.                 $rin = $rout = $ein = $eout = '';
  95.                 vec( $rin, fileno(CHILD_O),    1 ) = 1;
  96.                 vec( $rin, fileno(PARENT_ERR), 1 ) = 1;
  97.                 $ein    = $rin;
  98.                 $nfound = 0;
  99.  
  100.                 while ( $nfound =
  101.                     select( $rout = $rin, undef, $ein = $eout, 10 ) )
  102.                 {
  103.                     die "$!" unless $nfound != -1;
  104.                     $r1 = vec( $rout, fileno(PARENT_ERR), 1 ) == 1;
  105.                     $r2 = vec( $rout, fileno(CHILD_O),    1 ) == 1;
  106.                     $e1 = vec( $eout, fileno(PARENT_ERR), 1 ) == 1;
  107.                     $e2 = vec( $eout, fileno(CHILD_O),    1 ) == 1;
  108.  
  109.                     if ($r1) {
  110.                         while ( $bytes = read( PARENT_ERR, $errbytes, 4096 ) ) {
  111.                             print STDERR $errbytes;
  112.                         }
  113.                         if ($!) {
  114.                             $err = $!;
  115.                             die $!;
  116.                             vec( $rin, fileno(PARENT_ERR), 1 ) = 0
  117.                                 unless ( $err == EINTR or $err == EAGAIN );
  118.                         }
  119.                     }
  120.                     if ($r2) {
  121.                         while ( $bytes = read( CHILD_O, $s, 4096 ) ) {
  122.                             print $s;
  123.                         }
  124.                         if ( !defined($bytes) ) {
  125.                             $err = $!;
  126.                             die $!;
  127.                             vec( $rin, fileno(CHILD_O), 1 ) = 0
  128.                                 unless ( $err == EINTR or $err == EAGAIN );
  129.                         }
  130.                     }
  131.                     last if ( $e1 || $e2 );
  132.                 }
  133.                 close CHILD_RD;
  134.                 close PARENT_ERR;
  135.                 waitpid( $pid, 0 );
  136.             } else {
  137.                 foreach $key ( keys %req_params ) {
  138.                     $ENV{$key} = $req_params{$key};
  139.                 }
  140.  
  141.                 # cd to the script's local directory
  142.                 if ( $req_params{SCRIPT_FILENAME} =~ /^(.*)\/[^\/] $/ ) {
  143.                     chdir $1;
  144.                 }
  145.                 close(PARENT_WR);
  146.  
  147.                 #close(PARENT_ERR);
  148.                 close(STDIN);
  149.                 close(STDERR);
  150.  
  151.                 #fcntl(CHILD_RD, F_DUPFD, 0);
  152.                 syscall( &SYS_dup2, fileno(CHILD_RD),  0 );
  153.                 syscall( &SYS_dup2, fileno(CHILD_ERR), 2 );
  154.  
  155.                 #open(STDIN, "<&CHILD_RD");
  156.                 exec( $req_params{SCRIPT_FILENAME} );
  157.                 die("exec failed");
  158.             }
  159.         } else {
  160.             print("Content-type: text/plain\r\n\r\n");
  161.             print
  162. "Error: No such CGI app - $req_params{SCRIPT_FILENAME} may not exist or is not executable by this process.\n";
  163.         }
  164.     }
  165. }
  运行脚本nohup perl /usr/local/bin/cgiwrap-fcgi.pl >/dev/null 2>&1 &
  并把这一句加入/etc/rc.local文件中,让其开机执行
  配置nginx,vi /usr/local/nginx/conf/nginx.conf
  1. server
  2.         {
  3.                listen 80;
  4.                server_name wisp.nagios.com;
  5.                index index.html index.htm index.php;
  6.                root /usr/local/nagios/share;
  7.                access_log /usr/local/nagios/access_nagios.log main;

  8.                location ~ .*\.(php|php5)?$
  9.                {
  10.                         fastcgi_pass 127.0.0.1:9000;
  11.                         fastcgi_index index.php;
  12.                         include fcgi.conf;
  13.                }

  14.                location ~ \.cgi$
  15.                {
  16.                 root /usr/local/nagios/sbin;
  17.                 rewrite ^/nagios/cgi-bin/(.*)\.cgi /$1.cgi break;
  18.                 fastcgi_index index.cgi;
  19.                 fastcgi_pass 127.0.0.1:8999;

  20.                 fastcgi_param SCRIPT_FILENAME /usr/local/nagios/sbin$fastcgi_script_name;
  21.                 fastcgi_param QUERY_STRING $query_string;

  22.                 fastcgi_param REMOTE_ADDR $remote_addr;
  23.                 fastcgi_param REMOTE_PORT $remote_port;
  24.                 fastcgi_param REQUEST_METHOD $request_method;
  25.                 fastcgi_param REQUEST_URI $request_uri;

  26.                 #fastcgi_param SCRIPT_NAME $fastcgi_script_name;
  27.                 fastcgi_param SERVER_ADDR $server_addr;
  28.                 fastcgi_param SERVER_NAME $server_name;
  29.                 fastcgi_param SERVER_PORT $server_port;
  30.                 fastcgi_param SERVER_PROTOCOL $server_protocol;
  31.                 fastcgi_param SERVER_SOFTWARE nginx;

  32.                 fastcgi_param CONTENT_LENGTH $content_length;
  33.                 fastcgi_param CONTENT_TYPE $content_type;
  34.                 fastcgi_param GATEWAY_INTERFACE CGI/1.1;
  35.                 fastcgi_param HTTP_ACCEPT_ENCODING gzip,deflate;
  36.                 fastcgi_param HTTP_ACCEPT_LANGUAGE zh-cn;
  37.                }
  38.         }
绑host或者在上面的配置用上真实域名,在浏览器里输入(或者你的域名),即可看到nagios的监控页面。
阅读(2617) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~