Nagios从Apache平台转Nginx配置

安装好nagios,和cacti整合起来了。cacti使用的是nginx,所以现在想把nagios从apache平台上转到nginx上。

nginx需要perl cgi支持才能保证nagios的运行,所以要安装perl cgi环境

1、装PERL的FCGI模块

  1. #perl -MCPAN -e 'install FCGI'
  2. #perl -MCPAN -e 'install FCGI::ProcManager'

2、编写perl启动脚本,运行这个脚本生成的sock文件就是我们需要的。

  1. #vi /usr/local/bin/fastcgi.pl
  1. #!/usr/bin/perl
  2. use FCGI;
  3. #perl -MCPAN -e 'install FCGI'
  4. use Socket;
  5. use POSIX qw(setsid);
  6. #use Fcntl;
  7. require 'syscall.ph';
  8. &daemonize;
  9. #this keeps the program alive or something after exec'ing perl scripts
  10. END() { } BEGIN() { }
  11. *CORE::GLOBAL::exit = sub { die "fakeexit\nrc=".shift()."\n"; };
  12. eval q{exit};
  13. if ($@) {
  14. exit unless $@ =~ /^fakeexit/;
  15. };
  16. &main;
  17. sub daemonize() {
  18. chdir '/' or die "Can't chdir to /: $!";
  19. defined(my $pid = fork) or die "Can't fork: $!";
  20. exit if $pid;
  21. setsid or die "Can't start a new session: $!";
  22. umask 0;
  23. }
  24. sub main {
  25. #$socket = FCGI::OpenSocket( "127.0.0.1:8999", 10 ); #use IP sockets
  26. $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!!
  27. $request = FCGI::Request( \*STDIN, \*STDOUT, \*STDERR, \%req_params, $socket );
  28. if ($request) { request_loop()};
  29. FCGI::CloseSocket( $socket );
  30. }
  31. sub request_loop {
  32. while( $request->Accept() >= 0 ) {
  33. #processing any STDIN input from WebServer (for CGI-POST actions)
  34. $stdin_passthrough ='';
  35. $req_len = 0 + $req_params{'CONTENT_LENGTH'};
  36. if (($req_params{'REQUEST_METHOD'} eq 'POST') && ($req_len != 0) ){
  37. my $bytes_read = 0;
  38. while ($bytes_read < $req_len) {
  39. my $data = '';
  40. my $bytes = read(STDIN, $data, ($req_len - $bytes_read));
  41. last if ($bytes == 0 || !defined($bytes));
  42. $stdin_passthrough .= $data;
  43. $bytes_read += $bytes;
  44. }
  45. }
  46. #running the cgi app
  47. if ( (-x $req_params{SCRIPT_FILENAME}) && #can I execute this?
  48. (-s $req_params{SCRIPT_FILENAME}) && #Is this file empty?
  49. (-r $req_params{SCRIPT_FILENAME}) #can I read this file?
  50. ){
  51. pipe(CHILD_RD, PARENT_WR);
  52. my $pid = open(KID_TO_READ, "-|");
  53. unless(defined($pid)) {
  54. print("Content-type: text/plain\r\n\r\n");
  55. print "Error: CGI app returned no output - Executing $req_params{SCRIPT_FILENAME} failed !\n";
  56. next;
  57. }
  58. if ($pid > 0) {
  59. close(CHILD_RD);
  60. print PARENT_WR $stdin_passthrough;
  61. close(PARENT_WR);
  62. while(my $s = <KID_TO_READ>) { print $s; }
  63. close KID_TO_READ;
  64. waitpid($pid, 0);
  65. } else {
  66. foreach $key ( keys %req_params){
  67. $ENV{$key} = $req_params{$key};
  68. }
  69. # cd to the script's local directory
  70. if ($req_params{SCRIPT_FILENAME} =~ /^(.*)\/[^\/]+$/) {
  71. chdir $1;
  72. }
  73. close(PARENT_WR);
  74. close(STDIN);
  75. #fcntl(CHILD_RD, F_DUPFD, 0);
  76. syscall(&SYS_dup2, fileno(CHILD_RD), 0);
  77. #open(STDIN, "<&CHILD_RD");
  78. exec($req_params{SCRIPT_FILENAME});
  79. die("exec failed");
  80. }
  81. }
  82. else {
  83. print("Content-type: text/plain\r\n\r\n");
  84. print "Error: No such CGI app - $req_params{SCRIPT_FILENAME} may not exist or is not executable by this process.\n";
  85. }
  86. }
  87. }

3、给文件加执行权限。

  1. chmod +x /usr/local/bin/fastcgi.pl

执行之前先确认/var/run/nginx/这个文件夹存在,没有mkdir /var/run/nginx/
否则执行会报错:bind/listen: 没有那个文件或目录

执行之后查看/var/run/nginx目录下是不是多了一个perl_cgi-dispatch.sock文件

4、到nginx配置文件里添加,下面是我的配置

  1. server
  2. {
  3. listen 80;
  4. server_name nagios.zdworks.com;
  5. root /usr/local/nagios/share;
  6. index index.php;
  7. #设置编码方式,我没有加上这个的话界面部分乱码。很奇怪为什么不是全部乱码
  8. charset utf-8;
  9. location ~ .*\.(php|php5)?$
  10. {
  11. #root /usr/local/nagios/share;
  12. fastcgi_pass 127.0.0.1:9000;
  13. fastcgi_index index.php;
  14. fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  15. fastcgi_param HTTP_ACCEPT_LANGUAGE zh-cn;
  16. #fastcgi_params文件以自己的路径为准,我使用了绝对路径
  17. include /etc/nginx/fastcgi_params;
  18. auth_basic "Nagios Login pain";
  19. #认证的文件名和路径按自己的修改
  20. auth_basic_user_file /usr/local/nagios/etc/htpasswd.users;
  21. }
  22. location ~ .*\.cgi$ {
  23. root /usr/local/nagios/sbin;
  24. rewrite ^/nagios/cgi-bin/(.*)\.cgi /$1.cgi break;
    #这个就是前面生成的sock文件,这个设置错误cgi文件就没有办法显示
  25. fastcgi_pass unix:/var/run/nginx/perl_cgi-dispatch.sock;
  26. fastcgi_index index.cgi;
  27. fastcgi_param SCRIPT_FILENAME /usr/local/nagios/sbin/$fastcgi_script_name;
  28. include /etc/nginx/fastcgi_params;
  29. fastcgi_param AUTH_USER $remote_user;
  30. fastcgi_param REMOTE_USER $remote_user;
  31. auth_basic "Nagios Login pain";
  32. auth_basic_user_file /usr/local/nagios/etc/htpasswd.users;
  33. }
  34. location /nagios {
  35. alias /usr/local/nagios/share/;
  36. }
  37. }

没有装cgi前我nginx日志错误

  1. [error] 13964#0: *27 FastCGI sent in stderr: "PHP Warning: Unexpected character in input: ' in /usr/local/nagios/sbin/status.cgi on line 194
  2. PHP Parse error: syntax error, unexpected '<' in /usr/local/nagios/sbin/status.cgi on line 194" while reading response header from upstream, client: 192.168.1.180, server: nagios.com, request: "GET /nagios/cgi-bin/status.cgi?hostgroup=all&style=summary HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "nagios.com", referrer: "http://nagios.com/side.php"

这个是cgi没有装好才会报的错。

相关推荐