> CentOS > CentOS服务器 >

CentOS下nginx+nagios 监控平台搭建

随着linux服务器可用性、稳定性逐步增强,企业对线上服务也要求越来越严格,需要有专门的监控系统监控服务的正常运转,并有问题及时发邮件、报警给linux SA。我们大多数nagios、cacti监控都基于apache搭建,今天我们讲一种新平台搭建nginx平台的nagios监控!

一、系统运行环境


	
  1. 服务器系统版本:Centos 5.3  x64

  2.  

  3. nginx版本:nginx-0.8.54

  4.  

  5. nagios版本:nagios-3.2.3

二、下载所需包


	
  1. #所有安装所需的源码包:本站点集成下载:(以下包地址已经失效,请去官方网站下载相应的包)

  2. http://blog.mgcrazy.com/download/nginx-0.8.54.tar.gz

  3. http://blog.mgcrazy.com/download/pcre-8.01.tar.gz

  4. http://blog.mgcrazy.com/download/nagios/FCGI-0.67.tar.gz

  5. http://blog.mgcrazy.com/download/nagios/FCGI-ProcManager-0.18.tar.gz

  6. http://blog.mgcrazy.com/download/nagios/nagios-3.2.3.tar.gz

  7. http://blog.mgcrazy.com/download/nagios/nagios-plugins-1.4.13.tar.gz

  8. http://blog.mgcrazy.com/download/nagios/nrpe-2.8.1.tar.gz

  9. #Nginx 安装请参考:http://blog.mgcrazy.com/?p=792 【nginx安装】

  10. nginx安装完毕后。

三、正式安装nagios


	
  1. #创建nagios用户和组,并把www加入nagios管理组:

  2. useradd  nagios &&groupadd nagcmd && usermod -g nagcmd nagios && usermod -g nagcmd www

  3. cd /usr/scr && tar xzf nagios-3.2.3.tar.gz && cd nagios-3.2.3

  4. ./configure --with-command-group=nagcmd --prefix=/usr/local/nagios && make all

  5. make install &&make install-init && make install-config && make install-commandmode

  6. #nagios安装完毕!

   验证程序是否被正确安装。切换目录到安装路径(这里是/usr/local/nagios),看是否存在 etc、bin、 sbin、 share、 var这五个目录,如果存在则可以表明程序被正确的安装到系统了。接下来编译并安装nagios插件 nagios-plugins


	
  1. cd /usr/src &&tar zxvf nagios-plugins-1.4.13.tar.gz &&cd nagios-plugins-1.4.13

  2. ./configure --prefix=/usr/local/nagios --with-nagios-user=nagios --with-nagios-group=nagios

  3. make && make install

  4. #插件安装完毕!

  5. #ls /usr/local/nagios/libexec 会显示安装的插件文件,即所有的插件都安装在libexec这个目录下。

四、创建一个nagiosadmin用户


	
  1. #用于Nagios的WEB接口登录。  

  2. #记下你所设置的登录口令,一会儿你会用到它  

  3. 这里要借助于apche的htpasswd,在有apache机器上执行下列命令  

  4. /usr/local/apache2/bin/htpasswd -c /usr/local/nagios/etc/htpasswd nagiosadmin  

  5. 输入两次一样的密码即可。  

  6. chkconfig –add nagios    

  7. #添加到service 中,chkconfig nagios –level 35 on 设置成开机启动。  

  8. #把selinux、iptables关闭,或者更改iptables策略需要访问nagios  

  9. #然后启动nagios  

  10. /etc/init.d/nagios start  

五、安装Perl FCGI


	
  1. #安装php可参考  http://chinaapp.sinaapp.com/thread-627-1-1.html  

  2. #安装FCGI

  3. cd /usr/src && tar -zxvf FCGI-0.67.tar.gz &&cd FCGI-0.67 && perl Makefile.PL

  4. make && make install

  5. #安装FCGI-ProcManager:

  6. cd ../ && tar -xzxf FCGI-ProcManager-0.18.tar.gz &&cd FCGI-ProcManager-0.18

  7. perl Makefile.PL &&make && make install

  8. #安装完毕即可。

  9. mkdir -p /usr/local/nagios/share/nagios

  10. ln -s /usr/local/nagios/share/images /usr/local/nagios/share/nagios/images

  11. ln -s /usr/local/nagios/share/stylesheets /usr/local/nagios/share/nagios/stylesheets

  12. #避免无法显示图片。

   接下来配置cgi脚本、nginx配置文件: cd /usr/local/nagios/bin/ 下新建文件,并写入以下内容 vi perl-cgi.pl


	
  1. #!/usr/bin/perl

  2. use FCGI;

  3. #perl -MCPAN -e ‘install FCGI’

  4. use Socket;

  5. #this keeps the program alive or something after exec’ing perl scripts

  6. END()

  7. {

  8. }

  9. BEGIN()

  10. {

  11. }

  12. *CORE::GLOBAL::exit

  13. =

  14. sub

  15. {

  16. die

  17. “fakeexit\nrc=”.shift().”\n”;

  18. };

  19. eval

  20. q{exit};

  21. if

  22. ($@)

  23. {

  24. exit

  25. unless

  26. $@

  27. =~

  28. /^fakeexit/;

  29. }

  30. ;

  31. &main;

  32. sub main {

  33. #$socket = FCGI::OpenSocket( “:3461″, 10 ); #use IP sockets

  34. $socket

  35. = FCGI::OpenSocket(

  36. “/var/run/nagios.sock”,

  37. 10

  38. );

  39. #use UNIX sockets – user running this script must have w access to the ‘nginx’ folder!!

  40. $request

  41. = FCGI::Request(

  42. \*STDIN,

  43. \*STDOUT,

  44. \*STDERR,

  45. \%ENV,

  46. $socket

  47. );

  48. if

  49. ($request)

  50. {request_loop()};

  51. FCGI::CloseSocket(

  52. $socket

  53. );

  54. }

  55. sub request_loop {

  56. while(

  57. $request->Accept()

  58. >=

  59. 0

  60. )

  61. {

  62. #processing any STDIN input from WebServer (for CGI-GET actions)

  63. $env

  64. =

  65. $request->GetEnvironment();

  66. $stdin_passthrough

  67. =”;

  68. $req_len

  69. =

  70. 0

  71. +

  72. $ENV{CONTENT_LENGTH};

  73. if

  74. ($ENV{REQUEST_METHOD}

  75. eq

  76. ‘GET’){

  77. $stdin_passthrough

  78. .=

  79. $ENV{‘QUERY_STRING’};

  80. }

  81. #running the cgi app

  82. if

  83. (

  84. (-x $ENV{SCRIPT_FILENAME})

  85. &&

  86. #can I execute this?

  87. (-s

  88. $ENV{SCRIPT_FILENAME})

  89. &&

  90. #Is this file empty?

  91. (-r $ENV{SCRIPT_FILENAME})

  92. #can I read this file?

  93. ){

  94. #http://perldoc.perl.org/perlipc.html#Safe-Pipe-Opens

  95. open

  96. $cgi_app,

  97. ‘-|’,

  98. $ENV{SCRIPT_FILENAME},

  99. $stdin_passthrough

  100. or

  101. print(“Content-type: text/plain\r\n\r\n”);

  102. print

  103. “Error: CGI app returned no output – Executing $ENV{SCRIPT_FILENAME} failed !\n”;

  104. if

  105. ($cgi_app)

  106. {print

  107. <$cgi_app>;

  108. close

  109. $cgi_app;}

  110. }

  111. else

  112. {

  113. print(“Content-type: text/plain\r\n\r\n”);

  114. print

  115. “Error: No such CGI app – $req_len – $ENV{CONTENT_LENGTH} – $ENV{REQUEST_METHOD} – $ENV{SCRIPT_FILENAME} may not exist or is not executable by this process.\n”;

  116.     }

  117.  }

  118. }

  119. #设置权限chmod +x perl-cgi.pl 即可!

   以下是我的nginx.conf文件server内容


	
  1. server

  2. {

  3. listen 80;

  4. server_name 192.168.2.79;

  5. root /usr/local/nagios/share;

  6. index index.php;

  7. auth_basic “You Name”;

  8.  

  9. auth_basic_user_file /usr/local/nagios/etc/htpasswd;

  10. log_format nagios ‘$remote_addr – $remote_user [$time_local] “$request” ‘

  11. ‘$status $body_bytes_sent “$http_referer” ‘

  12. ‘”$http_user_agent” $http_x_forwarded_for’;

  13. access_log /usr/local/nginx/nagios.log nagios;

  14. location ~ .*\.(php|php5)?$

  15. {

  16. #fastcgi_pass unix:/tmp/php-cgi.sock;

  17. fastcgi_pass 127.0.0.1:9000;

  18.  

  19. fastcgi_index index.php;

  20. include fcgi.conf;

  21. }

  22. location ~ \.cgi$ {

  23. root /usr/local/nagios/sbin;

  24. rewrite ^/nagios/cgi-bin/(.*)\.cgi /$1.cgi break;

  25. fastcgi_index index.cgi;

  26. fastcgi_pass unix:/var/run/nagios.sock;

  27.  

  28. fastcgi_param SCRIPT_FILENAME /usr/local/nagios/sbin$fastcgi_script_name;

  29. fastcgi_param QUERY_STRING $query_string;

  30.  

  31. fastcgi_param REMOTE_ADDR $remote_addr;

  32. fastcgi_param REMOTE_PORT $remote_port;

  33. fastcgi_param REQUEST_METHOD $request_method;

  34. fastcgi_param REQUEST_URI $request_uri;

  35. fastcgi_param REMOTE_USER $remote_user;

  36. #默认没有红色这一句,如果没有,后面nagios页面会报错,具体错误后面给出。

  37. #fastcgi_param SCRIPT_NAME $fastcgi_script_name;

  38. fastcgi_param SERVER_ADDR $server_addr;

  39. fastcgi_param SERVER_NAME $server_name;

  40. fastcgi_param SERVER_PORT $server_port;

  41. fastcgi_param SERVER_PROTOCOL $server_protocol;

  42. fastcgi_param SERVER_SOFTWARE nginx;

  43. fastcgi_param CONTENT_LENGTH $content_length;

  44. fastcgi_param CONTENT_TYPE $content_type;

  45. fastcgi_param GATEWAY_INTERFACE CGI/1.1;

  46. fastcgi_param HTTP_ACCEPT_ENCODING gzip,deflate;

  47. fastcgi_param HTTP_ACCEPT_LANGUAGE zh-cn;

  48. }

   fcgi.conf文件内容如下


	
  1. fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;

  2. fastcgi_param  SERVER_SOFTWARE    nginx;

  3.  

  4. fastcgi_param  QUERY_STRING       $query_string;

  5. fastcgi_param  REQUEST_METHOD     $request_method;

  6. fastcgi_param  CONTENT_TYPE       $content_type;

  7. fastcgi_param  CONTENT_LENGTH     $content_length;

  8.  

  9. fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;

  10. fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;

  11. fastcgi_param  REQUEST_URI        $request_uri;

  12. fastcgi_param  DOCUMENT_URI       $document_uri;

  13. fastcgi_param  DOCUMENT_ROOT      $document_root;

  14. fastcgi_param  SERVER_PROTOCOL    $server_protocol;

  15.  

  16. fastcgi_param  REMOTE_ADDR        $remote_addr;

  17. fastcgi_param  REMOTE_PORT        $remote_port;

  18. fastcgi_param  SERVER_ADDR        $server_addr;

  19. fastcgi_param  SERVER_PORT        $server_port;

  20. fastcgi_param  SERVER_NAME        $server_name;

  21.  

  22. # PHP only, required if PHP was built with –enable-force-cgi-redirect

  23. fastcgi_param  REDIRECT_STATUS    200;

以上可以把nginx中fastcGI部分整合到fcgi.conf中


	
  1. 启动nginx,确定有fastcGI 9000端口。 后台启动screen   ./perl-cgi.pl >/dev/null

  2. ctrl +a +d 退出screen

  3. 然后chmod  777   /var/run/nagios.sock  ;并且重启service nagios restart

六、安装完毕,然后访问:

   直接访问 http://192.168.2.79   会弹出登录框 ,输入用户名nagiosadmin 和密码,会看到nagios默认监控localhost!页面如下:而且点击详细服务关闭报警,也不报错:

   自此,nginx+nagios 环境搭建完毕!剩下的就是具体监控客户端的配置啦!文章后续给出!

   本文环境已经测试通过,搭建的时候,也遇到了好多例如403、502、504等等系列问题,一点一滴的解决。一件事只有你真正做过,你才会感觉到学到很多东西。如下的报错:


	
  1. #It seems that you have chosen to not use the authentication functionality of the CGIs.

  2.  

  3. I don't want to be personally responsible for what may happen as a result of allowing

  4.  

  5. unauthorized users to issue commands to Nagios,so you'll have to disable this safeguard if you

  6.  

  7. are really stubborn and want to invite trouble.

  8.  

  9. #It appears as though you do not have permission to view information for any of the hosts you requested...

  10.  

  11. If you believe this is an error, check the HTTP server authentication requirements for accessing this CGI  

  12. and check the authorization options in your CGI configuration file

  13.  

  14. 可参照修改:http://hi.baidu.com/shengit/blog/item/b21b770965c6e8de62d986c0.html

本文参考文章链接


	
  1. http://bbs.linuxtone.org/thread-4441-1-1.html

  2.  

  3. http://bbs.linuxtone.org/thread-7404-1-1.html

  4.  

  5. http://www.comeonsa.com/

本文出自 “吴光科-专注自动化运维” 博客,请务必保留此出处http://wgkgood.blog.51cto.com/1192594/523150

(责任编辑:IT)