xuesn 发表于 2018-11-9 06:34:10

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  把下面内容写进脚本,并授执行权限
  


[*]#!/usr/bin/perl
[*]#
[*]#   author      Daniel Dominik Rudnicki
[*]#   thanks to:Piotr Romanczuk
[*]#   email       daniel@sardzent.org
[*]#   version   0.4.3
[*]#   webpage   http://www.nginx.eu/
[*]#
[*]#   BASED @ http://wiki.codemongers.com/NginxSimpleCGI
[*]#
[*]#
[*]# use strict;
[*]use FCGI;
[*]use Getopt::Long;
[*]use IO::All;
[*]use Socket;
[*]
[*]sub init {
[*]    GetOptions( "h" => \$help,
[*]            "verbose!"=>\$verbose,
[*]            "pid=s" => \$filepid,
[*]            "l=s" => \$logfile,
[*]            "S:s"   => \$unixsocket,
[*]            "P:i"   => \$unixport) or usage();
[*]      usage() if $help;
[*]
[*]    print " Starting Nginx-fcgi\n" if $verbose;
[*]    print " Running with $> UID" if $verbose;
[*]    print " Perl $]" if $verbose;
[*]
[*]#   if ( $> == "0" ) {
[*]#       print "\n\tERROR\tRunning as a root!\n";
[*]#       print "\tSuggested not to do so !!!\n\n";
[*]#       exit 1;
[*]#   }
[*]
[*]      if ( ! $logfile ) {
[*]      print "\n\tERROR\t log file must declared\n"
[*]            . "\tuse $0 with option -l filename\n\n";
[*]      exit 1;
[*]    }
[*]    print " Using log file $logfile\n" if $verbose;
[*]    "\n\n" >> io($logfile);
[*]    addlog($logfile, "Starting Nginx-cfgi");
[*]    addlog($logfile, "Running with $> UID");
[*]    addlog($logfile, "Perl $]");
[*]    addlog($logfile, "Testing socket options");
[*]
[*]    if ( ($unixsocket && $unixport) || (!($unixsocket) && !($unixport)) ) {
[*]      print "\n\tERROR\tOnly one option can be used!\n";
[*]      print "\tSuggested (beacuse of speed) is usage UNIX socket -S \n\n";
[*]      exit 1;
[*]    }
[*]
[*]    if ($unixsocket) {
[*]      print " Daemon listening at UNIX socket $unixsocket\n" if $versbose;
[*]      addlog($logfile, "Deamon listening at UNIX socket $unixsocket");
[*]    } else {
[*]      print " Daemon listening at TCP/IP socket *:$unixport\n" if $verbose;
[*]      #
[*]      addlog($logfile, "Daemon listening at TCP/IP socket *:$unixport");
[*]    }
[*]
[*]    if ( -e $filepid ) {
[*]      print "\n\tERROR\t PID file $filepid already exists\n\n";
[*]      addlog($logfile, "Can not use PID file $filepid, already exists.");
[*]      exit 1;
[*]    }
[*]
[*]    if ( $unixsocket ) {
[*]      print " Creating UNIX socket\n" if $verbose;
[*]      $socket = FCGI::OpenSocket( $unixsocket, 10 );
[*]      if ( !$socket) {
[*]            print " Couldn't create socket\n";
[*]            addlog($logfile, "Couldn't create socket");
[*]            exit 1;
[*]      }
[*]      print " Using UNIX socket $unixsocket\n" if $verbose;
[*]    } else {
[*]      print " Creating TCP/IP socket\n" if $verbose;
[*]      $portnumber = ":".$unixport;
[*]      $socket = FCGI::OpenSocket( $unixport, 10 );
[*]      if ( !$socket ) {
[*]            print " Couldn't create socket\n";
[*]            addlog($logfile, "Couldn't create socket");
[*]            exit 1;
[*]      }
[*]      print " Using port $unixport\n" if $verbose;
[*]    }
[*]    addlog($logfile, "Socket created");
[*]
[*]    if ( ! $filepid ) {
[*]      print "\n\tERROR\t PID file must declared\n"
[*]            . "\tuse $0 with option -pid filename\n\n";
[*]      exit 1;
[*]    }
[*]    print " Using PID file $filepid\n" if $verbose;
[*]    addlog($logfile, "Using PID file $filepid");
[*]
[*]    my $pidnumber = $$;
[*]    $pidnumber > io($filepid);
[*]    print " PID number $$\n" if $verbose;
[*]    addlog($logfile, "PID number $pidnumber");
[*]
[*]}
[*]
[*]sub addzero {
[*]    my ($date) = shift;
[*]    if ($date < 10) {
[*]      return &quot;0$date&quot;;
[*]    }
[*]       return $date;
[*]}
[*]
[*]sub logformat {
[*]    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$iddst) = localtime(time);
[*]    my $datestring;
[*]    $year += 1900;
[*]    $mon++;
[*]    $mon= addzero($mon);
[*]    $mday = addzero($mday);
[*]    $min= addzero($min);
[*]    $datestring = &quot;$year-$mon-$mday $hour:$min&quot;;
[*]    return($datestring);
[*]}
[*]
[*]sub addlog {
[*]    my ($log_file, $log_message) = @_;
[*]    my $curr_time = logformat();
[*]    my $write_message = &quot;[$curr_time]   $log_message&quot;;
[*]    $write_message >> io($log_file);
[*]    &quot;\n&quot; >> io($log_file);
[*]}
[*]
[*]sub printerror {
[*]    my $message = @_;
[*]    print &quot;\n   Nginx FastCGI\tERROR\n&quot;
[*]      . &quot;\t $message\n\n&quot;;
[*]    exit 1;
[*]}
[*]
[*]sub usage {
[*]    print &quot;\n   Nginx FastCGI \n&quot;
[*]      . &quot;\n\tusage: $0 [-h] -S string -P int\n&quot;
[*]      . &quot;\n\t-h\t\t: this (help) message&quot;
[*]      . &quot;\n\t-S path\t\t: path for UNIX socket&quot;
[*]      . &quot;\n\t-P port\t\t: port number&quot;
[*]      . &quot;\n\t-p file\t\t: path for pid file&quot;
[*]      . &quot;\n\t-l file\t\t: path for logfile&quot;
[*]      . &quot;\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&quot;;
[*]    exit 1;
[*]}
[*]
[*]
[*]init;
[*]#
[*]END() { } BEGIN() { }
[*]*CORE::GLOBAL::exit = sub { die &quot;fakeexit\nrc=&quot;.shift().&quot;\n&quot;; }; eval q{exit};
[*]if ($@) {
[*]    exit unless $@ =~ /^fakeexit/;
[*]} ;
[*]
[*]# fork part
[*]my $pid = fork();
[*]
[*]if( $pid == 0 ) {
[*]    &main;
[*]    exit 0;
[*]}
[*]
[*]print &quot; Forking worker process with PID $pid\n&quot; if $verbose;
[*]addlog($logfile, &quot;Forking worker process with PID $pid&quot;);
[*]print &quot; Update PID file $filepid\n&quot; if $verbose;
[*]addlog($logfile, &quot;Update PID file $filepid&quot;);
[*]$pid > io($filepid);
[*]print &quot; Worker process running.\n&quot; if $verbose;
[*]addlog ($logfile, &quot;Parent process $$ is exiting&quot;);
[*]exit 0;
[*]
[*]sub main {
[*]    $request = FCGI::Request( \*STDIN, \*STDOUT, \*STDERR, \%req_params, $socket );
[*]    if ($request) { request_loop()};
[*]      FCGI::CloseSocket( $socket );
[*]}
[*]
[*]sub request_loop {
[*]    while( $request->Accept() >= 0 ) {
[*]      # processing any STDIN input from WebServer (for CGI-POST actions)
[*]      $stdin_passthrough = '';
[*]      $req_len = 0 + $req_params{'CONTENT_LENGTH'};
[*]      if (($req_params{'REQUEST_METHOD'} eq 'POST') && ($req_len != 0) ){
[*]            while ($req_len) {
[*]                $stdin_passthrough .= getc(STDIN);
[*]                $req_len--;
[*]            }
[*]      }
[*]
[*]      # running the cgi app
[*]      if ( (-x $req_params{SCRIPT_FILENAME}) &&
[*]            (-s $req_params{SCRIPT_FILENAME}) &&
[*]            (-r $req_params{SCRIPT_FILENAME})
[*]      ){
[*]            foreach $key ( keys %req_params){
[*]                $ENV{$key} = $req_params{$key};
[*]            }
[*]            if ( $verbose ) {
[*]                addlog($logfile, &quot;running $req_params{SCRIPT_FILENAME}&quot;);
[*]            }
[*]            # http://perldoc.perl.org/perlipc.html#Safe-Pipe-Opens
[*]            #
[*]            open $cgi_app, '-|', $req_params{SCRIPT_FILENAME}, $stdin_passthrough or print(&quot;Content-type: text/plain\r\n\r\n&quot;); print &quot;Error: CGI app returned no output - Executing $req_params{SCRIPT_FILENAME} failed !\n&quot;; # addlog($logfile, &quot;Error: CGI app returned no output - Executing $req_params{SCRIPT_FILENAME} failed !&quot;);
[*]
[*]            if ($cgi_app) {
[*]                print ;
[*]                close $cgi_app;
[*]            }
[*]      } else {
[*]            print(&quot;Content-type: text/plain\r\n\r\n&quot;);
[*]            print &quot;Error: No such CGI app - $req_params{SCRIPT_FILENAME} may not exist or is not executable by this process.\n&quot;;
[*]            addlog($logfile, &quot;Error: No such CGI app - $req_params{SCRIPT_FILENAME} may not exist or is not executable by this process.&quot;);
[*]      }
[*]    }
[*]}
  

  

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创建虚拟主机
  以下是我的虚拟主机配置
  


[*]server
[*]{
[*]listen 80;
[*]server_name www.nagios.com;
[*]root /usr/local/nagios/share;
[*]index index.php;
[*]auth_basic &quot;lihp&quot;;
[*]auth_basic_user_file /usr/local/nginx/conf/htpasswd;
[*]
[*]
[*]#access_log /usr/local/nginx/logs/nagios.log nagios;
[*]location ~ \.cgi$ {
[*]root /usr/local/nagios/sbin;
[*]rewrite ^/nagios/cgi-bin/(.*)\.cgi /$1.cgi break;
[*]fastcgi_index index.cgi;
[*]fastcgi_pass unix:/usr/local/nginx/logs/nginx-fcgi.sock;
[*]fastcgi_param SCRIPT_FILENAME /usr/local/nagios/sbin$fastcgi_script_name;
[*]fastcgi_param QUERY_STRING $query_string;
[*]fastcgi_param REMOTE_ADDR $remote_addr;
[*]fastcgi_param REMOTE_PORT $remote_port;
[*]fastcgi_param REQUEST_METHOD $request_method;
[*]fastcgi_param REQUEST_URI $request_uri;
[*]fastcgi_param REMOTE_USER $remote_user;
[*]#fastcgi_param SCRIPT_NAME $fastcgi_script_name;
[*]fastcgi_param SERVER_ADDR $server_addr;
[*]fastcgi_param SERVER_NAME $server_name;
[*]fastcgi_param SERVER_PORT $server_port;
[*]fastcgi_param SERVER_PROTOCOL $server_protocol;
[*]fastcgi_param SERVER_SOFTWARE nginx;
[*]fastcgi_param CONTENT_LENGTH $content_length;
[*]fastcgi_param CONTENT_TYPE $content_type;
[*]fastcgi_param GATEWAY_INTERFACE CGI/1.1;
[*]fastcgi_param HTTP_ACCEPT_ENCODING gzip,deflate;
[*]fastcgi_param HTTP_ACCEPT_LANGUAGE zh-cn;
[*]}
[*]location ~ .*\.(php|php5)?$
[*]{
[*]#fastcgi_pass unix:/tmp/php-cgi.sock;
[*]fastcgi_pass 127.0.0.1:9000;
[*]fastcgi_index index.php;
[*]include fcgi.conf;
[*]}
[*]}
  

  最后重读配置:
  

/usr/local/nginx/sbin/nginx -s>然后绑定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错误的问题!


页: [1]
查看完整版本: Nginx平台安装Nagios监控服务(0608更新)