Nginx平台安装Nagios监控服务(0608更新)

简介:

安装环境:centos5.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 all
make install
make install-init
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       daniel@sardzent.org  
  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\n" if $verbose;  
  28.     print " Running with $> UID" if $verbose;  
  29.     print " Perl $]" if $verbose;  
  30.  
  31. #   if ( $> == "0" ) {  
  32. #       print "\n\tERROR\tRunning as a root!\n";  
  33. #       print "\tSuggested not to do so !!!\n\n";  
  34. #       exit 1;  
  35. #   }  
  36.  
  37.         if ( ! $logfile ) {  
  38.         print "\n\tERROR\t log file must declared\n" 
  39.             . "\tuse $0 with option -l filename\n\n";  
  40.         exit 1;  
  41.     }  
  42.     print " Using log file $logfile\n" if $verbose;  
  43.     "\n\n" >> 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 "\n\tERROR\tOnly one option can be used!\n";  
  51.         print "\tSuggested (beacuse of speed) is usage UNIX socket -S \n\n";  
  52.         exit 1;  
  53.     }  
  54.  
  55.     if ($unixsocket) {  
  56.         print " Daemon listening at UNIX socket $unixsocket\n" if $versbose;  
  57.         addlog($logfile, "Deamon listening at UNIX socket $unixsocket");  
  58.     } else {  
  59.         print " Daemon listening at TCP/IP socket *:$unixport\n" if $verbose;  
  60.         #  
  61.         addlog($logfile, "Daemon listening at TCP/IP socket *:$unixport");  
  62.     }  
  63.  
  64.     if ( -e $filepid ) {  
  65.         print "\n\tERROR\t PID file $filepid already exists\n\n";  
  66.         addlog($logfile, "Can not use PID file $filepid, already exists.");  
  67.         exit 1;  
  68.     }  
  69.  
  70.     if ( $unixsocket ) {  
  71.         print " Creating UNIX socket\n" if $verbose;  
  72.         $socket = FCGI::OpenSocket( $unixsocket, 10 );  
  73.         if ( !$socket) {  
  74.             print " Couldn't create socket\n";  
  75.             addlog($logfile, "Couldn't create socket");  
  76.             exit 1;  
  77.         }  
  78.         print " Using UNIX socket $unixsocket\n" if $verbose;  
  79.     } else {  
  80.         print " Creating TCP/IP socket\n" if $verbose;  
  81.         $portnumber = ":".$unixport;  
  82.         $socket = FCGI::OpenSocket( $unixport, 10 );  
  83.         if ( !$socket ) {  
  84.             print " Couldn't create socket\n";  
  85.             addlog($logfile, "Couldn't create socket");  
  86.             exit 1;  
  87.         }  
  88.         print " Using port $unixport\n" if $verbose;  
  89.     }  
  90.     addlog($logfile, "Socket created");  
  91.  
  92.     if ( ! $filepid ) {  
  93.         print "\n\tERROR\t PID file must declared\n" 
  94.             . "\tuse $0 with option -pid filename\n\n";  
  95.         exit 1;  
  96.     }  
  97.     print " Using PID file $filepid\n" if $verbose;  
  98.     addlog($logfile, "Using PID file $filepid");  
  99.  
  100.     my $pidnumber = $$;  
  101.     $pidnumber > io($filepid);  
  102.     print " PID number $$\n" 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.     "\n" >> io($log_file);  
  133. }  
  134.  
  135. sub printerror {  
  136.     my $message = @_;  
  137.     print "\n   Nginx FastCGI\tERROR\n" 
  138.         . "\t $message\n\n";  
  139.     exit 1;  
  140. }  
  141.  
  142. sub usage {  
  143.     print "\n   Nginx FastCGI \n" 
  144.         . "\n\tusage: $0 [-h] -S string -P int\n" 
  145.         . "\n\t-h\t\t: this (help) message" 
  146.         . "\n\t-S path\t\t: path for UNIX socket" 
  147.         . "\n\t-P port\t\t: port number" 
  148.         . "\n\t-p file\t\t: path for pid file" 
  149.         . "\n\t-l file\t\t: path for logfile" 
  150.         . "\n\n\texample: $0 -S /var/run/nginx-perl_cgi.sock -l /var/log/nginx/nginx-cfgi.log -pid /var/run/nginx-fcgi.pid\n\n";  
  151.     exit 1;  
  152. }  
  153.  
  154.  
  155. init;  
  156. #  
  157. END() { } BEGIN() { }  
  158. *CORE::GLOBAL::exit = sub { die "fakeexit\nrc=".shift()."\n"; }; 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\n" if $verbose;  
  172. addlog($logfile, "Forking worker process with PID $pid");  
  173. print " Update PID file $filepid\n" if $verbose;  
  174. addlog($logfile, "Update PID file $filepid");  
  175. $pid > io($filepid);  
  176. print " Worker process running.\n" 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\n\r\n"); print "Error: CGI app returned no output - Executing $req_params{SCRIPT_FILENAME} failed !\n"# addlog($logfile, "Error: CGI app returned no output - Executing $req_params{SCRIPT_FILENAME} failed !");  
  212.               
  213.             if ($cgi_app) {   
  214.                 print <$cgi_app>;   
  215.                 close $cgi_app;   
  216.             }  
  217.         } else {  
  218.             print("Content-type: text/plain\r\n\r\n");  
  219.             print "Error: No such CGI app - $req_params{SCRIPT_FILENAME} may not exist or is not executable by this process.\n";  
  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/images /usr/local/nagios/share/nagios/images
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

解决了map和trends错误的问题!

本文转自运维笔记博客51CTO博客,原文链接http://blog.51cto.com/lihuipeng/565412如需转载请自行联系原作者


lihuipeng

相关文章
|
1月前
|
应用服务中间件 nginx
Nginx安装nginx-rtmp-module模块
【2月更文挑战第4天】 nginx中的模块虽然就是类似插件的概念,但是它无法像VsCode那样轻松的安装扩展。 nginx要安装其它模块必须同时拿到nginx源代码和模块源代码,然后手动编译,将模块打到nginx中,最终生成一个名为nginx的可执行文件。
71 6
|
2月前
|
负载均衡 Ubuntu 应用服务中间件
|
2月前
|
缓存 负载均衡 应用服务中间件
如何在 CentOS 7 上为 NGINX 安装开源 HTTP 加速器:Varnish
如何在 CentOS 7 上为 NGINX 安装开源 HTTP 加速器:Varnish
67 1
如何在 CentOS 7 上为 NGINX 安装开源 HTTP 加速器:Varnish
|
22天前
|
网络协议 应用服务中间件 网络安全
linxu安装nginx
linxu安装nginx
48 0
|
30天前
|
NoSQL 关系型数据库 MySQL
Docker安装详细步骤及相关环境安装配置(mysql、jdk、redis、自己的私有仓库Gitlab 、C和C++环境以及Nginx服务代理)
Docker安装详细步骤及相关环境安装配置(mysql、jdk、redis、自己的私有仓库Gitlab 、C和C++环境以及Nginx服务代理)
187 0
|
1月前
|
负载均衡 应用服务中间件 nginx
|
1月前
|
应用服务中间件 nginx Windows
windows下快速安装nginx 并配置开机自启动
windows下快速安装nginx 并配置开机自启动
windows下快速安装nginx 并配置开机自启动
|
1月前
|
Kubernetes 负载均衡 应用服务中间件
Ingress Nginx 安装【亲测可用】
Ingress Nginx 安装【亲测可用】
103 2
|
1月前
|
安全 应用服务中间件 nginx
|
1月前
|
域名解析 网络协议 应用服务中间件
nginx-ingress通过ipv6暴露服务,并在nginx ingress日志中记录客户端真实ipv6的ip地址
本文主要通过阿里云提供的clb和nlb来实现,建议是提前创建好双栈的vpc和vsw(使用clb可以不用双栈vpc和vsw)
175 1