Build nginx + nagios monitoring platform in CentOS

Source: Internet
Author: User
Tags install perl

With the increasing availability and Stability of linux servers, enterprises have increasingly strict requirements on online services. Therefore, special monitoring systems are required to monitor the normal operation of services, if any problem occurs, send an email and send an alarm to the linux SA. Most of our nagios and cacti monitoring systems are built based on apache. Today we will talk about a new platform to build the nginx platform's nagios monitoring!

I. system running environment

 
 
  1. Server System Version: Centos 5.3x64

  2. Nginx version: nginx-0.8.54

  3. Nagios version: nagios-3.2.3

Ii. download the required package

 
 
  1. # All source code packages required for installation: download from the site integration: (the following package address has expired. Please download the corresponding package from the official website)

  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 installation see: http://blog.mgcrazy.com /? P = 792 [install nginx]

  10. After nginx is installed.

3. Officially install nagios

 
 
  1. # Create a nagios user and group and add www to the nagios Management Group:

  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 installation is complete!

Verify that the program is correctly installed. Switch the directory to the installation path (/usr/local/nagios) to check whether the five directories including etc, bin, sbin, share, and var exist, if yes, it indicates that the program has been correctly installed to the system. Next, compile and install the nagios plug-in 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. # Plug-ins have been installed!

  5. # Ls/usr/local/nagios/libexec: The installed plug-in file is displayed, that is, all the plug-ins are installed in the libexec directory.

4. Create a nagiosadmin user

 
 
  1. # Use the WEB interface for logging on to Nagios.

  2. # Write down your logon password. you will use it later.

  3. Here we need to use the htpasswd of apche to execute the following command on an apache machine.

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

  5. Enter the same password twice.

  6. Chkconfig-add nagios

  7. # Add to service, and set chkconfig nagios-level 35 on to start upon startup.

  8. # Disable selinux and iptables, or change the iptables policy to access nagios

  9. # Start nagios

  10. /Etc/init. d/nagios start

5. Install Perl FCGI

 
 
  1. # Install php refer to http://chinaapp.sinaapp.com/thread-627-1-1.html

  2. # Installing FCGI

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

  4. Make & make install

  5. # Install FCGI-ProcManager:

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

  7. Perl Makefile. PL & make install

  8. # The installation is complete.

  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. # Avoid Image Display failures.

Next, configure the cgi script and nginx configuration file:
New file under cd/usr/local/nagios/bin/and write the following content to 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 ");

  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 ");

  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. # Set the permission chmod + x perl-cgi.pl!

The following is my nginx. conf file server content

 
 
  1. Server

  2. {

  3. Listen 80;

  4. Server_name 192.168.2.79;

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

  6. Index. php;

  7. Auth_basic "You Name ";

  8. Auth_basic_user_file/usr/local/nagios/etc/htpasswd;

  9. Log_format nagios '$ remote_addr-$ remote_user [$ time_local] "$ request "'

  10. '$ Status $ body_bytes_sent' $ http_referer "'

  11. '"$ Http_user_agent" $ http_x_forwarded_for ';

  12. Access_log/usr/local/nginx/nagios. log nagios;

  13. Location ~ . * \. (Php | php5 )? $

  14. {

  15. # Fastcgi_pass unix:/tmp/php-cgi.sock;

  16. Fastcgi_pass 127.0.0.1: 9000;

  17. Fastcgi_index index. php;

  18. Fcinclude gi. conf;

  19. }

  20. Location ~ \. Cgi $ {

  21. Root/usr/local/nagios/sbin;

  22. Rewrite ^/nagios/cgi-bin/(. *) \. cgi/$ 1.cgi break;

  23. Fastcgi_index index. cgi;

  24. Fastcgi_pass unix:/var/run/nagios. sock;

  25. Fastcgi_param SCRIPT_FILENAME/usr/local/nagios/sbin $ fastcgi_script_name;

  26. Fastcgi_param QUERY_STRING $ query_string;

  27. Fastcgi_param REMOTE_ADDR $ remote_addr;

  28. Fastcgi_param REMOTE_PORT $ remote_port;

  29. Fastcgi_param REQUEST_METHOD $ request_method;

  30. Fastcgi_param REQUEST_URI $ request_uri;

  31. Fastcgi_param REMOTE_USER $ remote_user;

  32. # There is no red sentence by default. If not, an error will be reported on the nagios page later. The specific error is shown later.

  33. # Fastcgi_param SCRIPT_NAME $ fastcgi_script_name;

  34. Fastcgi_param SERVER_ADDR $ server_addr;

  35. Fastcgi_param SERVER_NAME $ server_name;

  36. Fastcgi_param SERVER_PORT $ server_port;

  37. Fastcgi_param SERVER_PROTOCOL $ server_protocol;

  38. Fastcgi_param SERVER_SOFTWARE nginx;

  39. Fastcgi_param CONTENT_LENGTH $ content_length;

  40. Fastcgi_param CONTENT_TYPE $ content_type;

  41. Fastcgi_param GATEWAY_INTERFACE CGI/1.1;

  42. Fastcgi_param HTTP_ACCEPT_ENCODING gzip, deflate;

  43. Fastcgi_param HTTP_ACCEPT_LANGUAGE zh-cn;

  44. }

The content of the fcgi. conf file is as follows:

 
 
  1. fastcgi_param GATEWAY_INTERFACE CGI/1.1;

  2. fastcgi_param SERVER_SOFTWARE nginx;

  3. fastcgi_param QUERY_STRING $query_string;

  4. fastcgi_param REQUEST_METHOD $request_method;

  5. fastcgi_param CONTENT_TYPE $content_type;

  6. fastcgi_param CONTENT_LENGTH $content_length;

  7. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

  8. fastcgi_param SCRIPT_NAME $fastcgi_script_name;

  9. fastcgi_param REQUEST_URI $request_uri;

  10. fastcgi_param DOCUMENT_URI $document_uri;

  11. fastcgi_param DOCUMENT_ROOT $document_root;

  12. fastcgi_param SERVER_PROTOCOL $server_protocol;

  13. fastcgi_param REMOTE_ADDR $remote_addr;

  14. fastcgi_param REMOTE_PORT $remote_port;

  15. fastcgi_param SERVER_ADDR $server_addr;

  16. fastcgi_param SERVER_PORT $server_port;

  17. fastcgi_param SERVER_NAME $server_name;

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

  19. fastcgi_param REDIRECT_STATUS 200;

The above code can integrate fastcGI in nginx into fcgi. conf.

 
 
  1. Start nginx and check that port fastcGI 9000 is available. Start screen./perl-cgi.pl>/dev/null in the background

  2. Ctrl + a + d Exit screen

  3. Then chmod 777/var/run/nagios. sock; and restart service nagios restart

6. After installation is complete, access:

Access http: // 192.168.2.79 directly. The logon window is displayed. Enter the username nagiosadmin and password. The default monitoring localhost of nagios is displayed! The page is as follows: Click Detail service to close the alarm, and no error is reported:

Since then, the nginx + nagios environment has been set up! The rest is the configuration of the specific monitoring client! This article will be provided later!

The environment in this article has been tested, and many series of problems, such as 403, 502, and 504, were encountered during the setup. Only when you have done one thing can you feel that you have learned a lot. The following error is reported:

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

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

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

  4. Are really stubborn and want to invite trouble.

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

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

  7. And check the authorization options in your CGI configuration file

  8. See modify: http://hi.baidu.com/shengit/blog/item/b21b770965c6e8de62d986c0.html

References

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

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

  3. http://www.comeonsa.com/

This article from the "Wu guangke-Focus on automated O & M" blog, please be sure to keep this source http://wgkgood.blog.51cto.com/1192594/523150


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.