Nginx 플랫폼 에 Nagios 모니터링 서비스 설치 (0608 업데이트)

설치 환경: centos 5.5
Nginx 플랫폼 설치
1. 관련 소프트웨어 다운로드
FCGI-0.67.tar.gz FCGI-ProcManager-0.18.tar.gz IO-All-0.39.tar.gz nagios-3.2.3.tar.gz nagios-plugins-1.4.15.tar.gz
2. 관련 사용자 만 들 기
useradd nagios groupadd nagcmd usermod -g nagcmd nagios usermod -g nagcmd www
3. Nagios 설치
tar zxvf nagios - 3.2.3. tar. gz cd nagios - 3.2.3. / configure -- with - group = nagios -- with - user = nagios -- with - command - group = nagcmd -- with - gd - lib = / usr / local / gd / lib -- with - gd - inc = / usr / local / gd / include (0608 업데이트) make install make install - init make install - config make install - config make install - commandmode
4. Nagios 플러그 인 설치
tar zxvf nagios-plugins-1.4.15.tar.gz cd nagios-plugins-1.4.15 ./configure --with-nagios-user=nagios --with-nagios-group=nagios make make install
5. Nagios 시작 설정
chkconfig --add nagios chkconfig nagios on service nagios start
6. Perl fcgi 모듈 을 설치 하여 Nginx 가 CGI 를 지원 하도록 합 니 다.
tar -zxf FCGI-0.67.tar.gz cd FCGI-0.67 perl Makefile.PL make && make install tar -zxf FCGI-ProcManager-0.18.tar.gz cd FCGI-ProcManager-0.18 perl Makefile.PL make && make install   tar zxf IO-All-0.39.tar.gz cd IO-All-0.39 perl Makefile.PL make && make install
nginx - fcgi 스 크 립 트 만 들 기
 vi /usr/local/nginx/sbin/nginx-fcgi
다음 내용 을 스 크 립 트 에 쓰 고 실행 권한 을 부여 합 니 다.
   
   
   
   
  1. #!/usr/bin/perl  
  2. #  
  3. #   author      Daniel Dominik Rudnicki  
  4. #   thanks to:  Piotr Romanczuk  
  5. #   email       [email protected]  
  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.  
  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
    "
     if $verbose;  
  28.     print " Running with $> UID" if $verbose;  
  29.     print " Perl $]" if $verbose;  
  30.  
  31. #   if ( $> == "0" ) {  
  32. #       print "
    \tERROR\tRunning as a root!
    ";
     
  33. #       print "\tSuggested not to do so !!!

    ";
     
  34. #       exit 1;  
  35. #   }  
  36.  
  37.         if ( ! $logfile ) {  
  38.         print "
    \tERROR\t log file must declared
    "
     
  39.             . "\tuse $0 with option -l filename

    "
    ;  
  40.         exit 1;  
  41.     }  
  42.     print " Using log file $logfile
    "
     if $verbose;  
  43.     "

    "
     >> 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 "
    \tERROR\tOnly one option can be used!
    "
    ;  
  51.         print "\tSuggested (beacuse of speed) is usage UNIX socket -S 

    "
    ;  
  52.         exit 1;  
  53.     }  
  54.  
  55.     if ($unixsocket) {  
  56.         print " Daemon listening at UNIX socket $unixsocket
    "
     if $versbose;  
  57.         addlog($logfile, "Deamon listening at UNIX socket $unixsocket");  
  58.     } else {  
  59.         print " Daemon listening at TCP/IP socket *:$unixport
    "
     if $verbose;  
  60.         #  
  61.         addlog($logfile, "Daemon listening at TCP/IP socket *:$unixport");  
  62.     }  
  63.  
  64.     if ( -e $filepid ) {  
  65.         print "
    \tERROR\t PID file $filepid already exists

    "
    ;  
  66.         addlog($logfile, "Can not use PID file $filepid, already exists.");  
  67.         exit 1;  
  68.     }  
  69.  
  70.     if ( $unixsocket ) {  
  71.         print " Creating UNIX socket
    "
     if $verbose;  
  72.         $socket = FCGI::OpenSocket( $unixsocket, 10 );  
  73.         if ( !$socket) {  
  74.             print " Couldn't create socket
    "
    ;  
  75.             addlog($logfile, "Couldn't create socket");  
  76.             exit 1;  
  77.         }  
  78.         print " Using UNIX socket $unixsocket
    "
     if $verbose;  
  79.     } else {  
  80.         print " Creating TCP/IP socket
    "
     if $verbose;  
  81.         $portnumber = ":".$unixport;  
  82.         $socket = FCGI::OpenSocket( $unixport, 10 );  
  83.         if ( !$socket ) {  
  84.             print " Couldn't create socket
    "
    ;  
  85.             addlog($logfile, "Couldn't create socket");  
  86.             exit 1;  
  87.         }  
  88.         print " Using port $unixport
    "
     if $verbose;  
  89.     }  
  90.     addlog($logfile, "Socket created");  
  91.  
  92.     if ( ! $filepid ) {  
  93.         print "
    \tERROR\t PID file must declared
    "
     
  94.             . "\tuse $0 with option -pid filename

    "
    ;  
  95.         exit 1;  
  96.     }  
  97.     print " Using PID file $filepid
    "
     if $verbose;  
  98.     addlog($logfile, "Using PID file $filepid");  
  99.  
  100.     my $pidnumber = $$;  
  101.     $pidnumber > io($filepid);  
  102.     print " PID number $$
    "
     if $verbose;  
  103.     addlog($logfile, "PID number $pidnumber");  
  104.       
  105. }  
  106.  
  107. sub addzero {  
  108.     my ($date) = shift;  
  109.     if ($date 10) {  
  110.         return "0$date";  
  111.     }  
  112.        return $date;  
  113. }  
  114.  
  115. sub logformat {  
  116.     my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$iddst) = localtime(time);  
  117.     my $datestring;  
  118.     $year += 1900;  
  119.     $mon++;  
  120.     $mon  = addzero($mon);  
  121.     $mday = addzero($mday);  
  122.     $min  = addzero($min);  
  123.     $datestring = "$year-$mon-$mday $hour:$min";  
  124.     return($datestring);  
  125. }  
  126.  
  127. sub addlog {  
  128.     my ($log_file, $log_message) = @_;  
  129.     my $curr_time = logformat();  
  130.     my $write_message = "[$curr_time]   $log_message";  
  131.     $write_message >> io($log_file);  
  132.     "
    "
     >> io($log_file);  
  133. }  
  134.  
  135. sub printerror {  
  136.     my $message = @_;  
  137.     print "
       Nginx FastCGI\tERROR
    "
     
  138.         . "\t $message

    "
    ;  
  139.     exit 1;  
  140. }  
  141.  
  142. sub usage {  
  143.     print "
       Nginx FastCGI 
    "
     
  144.         . "
    \tusage: $0 [-h] -S string -P int
    "
     
  145.         . "
    \t-h\t\t: this (help) message"
     
  146.         . "
    \t-S path\t\t: path for UNIX socket"
     
  147.         . "
    \t-P port\t\t: port number"
     
  148.         . "
    \t-p file\t\t: path for pid file"
     
  149.         . "
    \t-l file\t\t: path for logfile"
     
  150.         . "

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

    "
    ;  
  151.     exit 1;  
  152. }  
  153.  
  154.  
  155. init;  
  156. #  
  157. END() { } BEGIN() { }  
  158. *CORE::GLOBAL::exit = sub { die "fakeexit
    rc="
    .shift()."
    "
    ; }; eval q{exit};   
  159. if ($@) {   
  160.     exit unless $@ =~ /^fakeexit/;   
  161. } ;  
  162.  
  163. # fork part  
  164. my $pid = fork();  
  165.  
  166. if( $pid == 0 ) {  
  167.     &main;  
  168.     exit 0;  
  169. }  
  170.  
  171. print " Forking worker process with PID $pid
    "
     if $verbose;  
  172. addlog($logfile, "Forking worker process with PID $pid");  
  173. print " Update PID file $filepid
    "
     if $verbose;  
  174. addlog($logfile, "Update PID file $filepid");  
  175. $pid > io($filepid);  
  176. print " Worker process running.
    "
     if $verbose;  
  177. addlog ($logfile, "Parent process $$ is exiting");  
  178. exit 0;  
  179.  
  180. sub main {  
  181.     $request = FCGI::Request( \*STDIN, \*STDOUT, \*STDERR, \%req_params, $socket );  
  182.     if ($request) { request_loop()};  
  183.         FCGI::CloseSocket( $socket );  
  184. }  
  185.  
  186. sub request_loop {  
  187.     while( $request->Accept() >= 0 ) {  
  188.         # processing any STDIN input from WebServer (for CGI-POST actions)  
  189.         $stdin_passthrough = '';  
  190.         $req_len = 0 + $req_params{'CONTENT_LENGTH'};  
  191.         if (($req_params{'REQUEST_METHOD'} eq 'POST') && ($req_len != 0) ){  
  192.             while ($req_len) {  
  193.                 $stdin_passthrough .= getc(STDIN);  
  194.                 $req_len--;   
  195.             }  
  196.         }  
  197.  
  198.         # running the cgi app  
  199.         if ( (-x $req_params{SCRIPT_FILENAME}) &&   
  200.             (-s $req_params{SCRIPT_FILENAME}) &&   
  201.             (-r $req_params{SCRIPT_FILENAME})  
  202.         ){  
  203.             foreach $key ( keys %req_params){  
  204.                 $ENV{$key} = $req_params{$key};  
  205.             }  
  206.             if ( $verbose ) {  
  207.                 addlog($logfile, "running $req_params{SCRIPT_FILENAME}");  
  208.             }  
  209.             # http://perldoc.perl.org/perlipc.html#Safe-Pipe-Opens  
  210.             #  
  211.             open $cgi_app, '-|', $req_params{SCRIPT_FILENAME}, $stdin_passthrough or print("Content-type: text/plain\r
    \r
    "
    ); print "Error: CGI app returned no output - Executing $req_params{SCRIPT_FILENAME} failed !
    "
    # addlog($logfile, "Error: CGI app returned no output - Executing $req_params{SCRIPT_FILENAME} failed !");  
  212.               
  213.             if ($cgi_app) {   
  214.                 print ;   
  215.                 close $cgi_app;   
  216.             }  
  217.         } else {  
  218.             print("Content-type: text/plain\r
    \r
    "
    );  
  219.             print "Error: No such CGI app - $req_params{SCRIPT_FILENAME} may not exist or is not executable by this process.
    "
    ;  
  220.             addlog($logfile, "Error: No such CGI app - $req_params{SCRIPT_FILENAME} may not exist or is not executable by this process.");  
  221.         }  
  222.     }  

chmod +x /usr/local/nginx/sbin/nginx-fcgi
스 크 립 트 실행:
/usr/local/nginx/sbin/nginx-fcgi -l /usr/local/nginx/logs/nginx-fcgi.log -pid /usr/local/nginx/logs/nginx-fcgi.pid -S /usr/local/nginx/logs/nginx-fcgi.sock  
sock 권한 부여 777:
 chmod 777  /usr/local/nginx/logs/nginx-fcgi.sock
7. 로그 인 계 정 및 비밀번호 설정
메모: 여기 서 생 성 된 사용자 가 nagiosadmin 이 아니라면 / usr / local / nagios / etc / cgi. cfg 설정 파일 에 계 정 을 추가 해 야 합 니 다. 그렇지 않 으 면 새로 만 든 lihp 계 정 은 nagios 를 조작 할 수 있 는 권한 이 없습니다.
/usr/local/apache2/bin/htpasswd -c /usr/local/nginx/conf/htpasswd lihp
apache 가 없 으 면 인터넷 에서 htpasswd 를 만 들 수 있 습 니 다. 
8. Nginx 가상 호스트 만 들 기
다음은 제 가상 호스트 설정 입 니 다.
   
   
   
   
  1. server  
  2. {  
  3. listen 80;  
  4. server_name www.nagios.com;  
  5. root /usr/local/nagios/share;  
  6. index index.php;  
  7. auth_basic "lihp";  
  8. auth_basic_user_file /usr/local/nginx/conf/htpasswd;  
  9.  
  10.  
  11. #access_log /usr/local/nginx/logs/nagios.log nagios;  
  12. location ~ \.cgi$ {  
  13. root /usr/local/nagios/sbin;  
  14. rewrite ^/nagios/cgi-bin/(.*)\.cgi /$1.cgi break;  
  15. fastcgi_index index.cgi;  
  16. fastcgi_pass unix:/usr/local/nginx/logs/nginx-fcgi.sock;  
  17. fastcgi_param SCRIPT_FILENAME /usr/local/nagios/sbin$fastcgi_script_name;  
  18. fastcgi_param QUERY_STRING $query_string;  
  19. fastcgi_param REMOTE_ADDR $remote_addr;  
  20. fastcgi_param REMOTE_PORT $remote_port;  
  21. fastcgi_param REQUEST_METHOD $request_method;  
  22. fastcgi_param REQUEST_URI $request_uri;  
  23. fastcgi_param REMOTE_USER $remote_user;  
  24. #fastcgi_param SCRIPT_NAME $fastcgi_script_name;  
  25. fastcgi_param SERVER_ADDR $server_addr;  
  26. fastcgi_param SERVER_NAME $server_name;  
  27. fastcgi_param SERVER_PORT $server_port;  
  28. fastcgi_param SERVER_PROTOCOL $server_protocol;  
  29. fastcgi_param SERVER_SOFTWARE nginx;  
  30. fastcgi_param CONTENT_LENGTH $content_length;  
  31. fastcgi_param CONTENT_TYPE $content_type;  
  32. fastcgi_param GATEWAY_INTERFACE CGI/1.1;  
  33. fastcgi_param HTTP_ACCEPT_ENCODING gzip,deflate;  
  34. fastcgi_param HTTP_ACCEPT_LANGUAGE zh-cn;  
  35. }  
  36. location ~ .*\.(php|php5)?$  
  37. {  
  38. #fastcgi_pass unix:/tmp/php-cgi.sock;  
  39. fastcgi_pass 127.0.0.1:9000;  
  40. fastcgi_index index.php;  
  41. include fcgi.conf;  
  42. }  

마지막 으로 설정 다시 읽 기:
 /usr/local/nginx/sbin/nginx -s reload
그리고 HOSTS 를 연결 하고 브 라 우 저 www. nagios. com 을 엽 니 다.
9. 사진 의 비정상적인 수정 방법:
mkdir -p /usr/local/nagios/share/nagios ln -s /usr/local/nagios/share/p_w_picpaths /usr/local/nagios/share/nagios/p_w_picpaths ln -s /usr/local/nagios/share/stylesheets /usr/local/nagios/share/nagios/stylesheets
10. GD 동적 라 이브 러 리 불 러 오기 (0608 업데이트)
 vi /etc/ld.so.conf
 include ld.so.conf.d/*.conf /usr/local/gd/lib     #GD 동적 라 이브 러 리 경로 추가
그리고 수 동 으로 실행:
 ldconfig
맵 과 trends 오류 해결!

좋은 웹페이지 즐겨찾기