Centos6.4 Nginx环境下安装配置nagios


1.环境:
centos 6.4 X64
nginx 1.4.8
php 5.3.17
php和nginx提前安装这里不再详细介绍,如果比较懒可以下载lnmp一键安装包来安装。
所需软件安装包:
nagios 4.0.2 金山快盘附件:nagios-4.0.2.tar.gz(1.62MB)
nagios-plugins1.5 金山快盘附件:nagios-plugins-1.5.tar.gz(2.32MB)

FCGI-0.74.tar.gz 金山快盘附件:FCGI-0.74.tar.gz(95.31KB)
FCGI-ProcManager-0.24.tar.gz 金山快盘附件:FCGI-ProcManager-0.24.tar.gz(21.06KB)
IO-All-0.50.tar.gz 金山快盘附件:IO-All-0.50.tar.gz(82.08KB)
所有软件包都已上传到附件可下载。
2.建立用户:
[root@apabicentos2 ~]# useradd nagios -s /sbin/nologin
3.安装nagios
[root@apabicentos2 ~]# tar zxvf nagios-4.0.2.tar.gz
[root@apabicentos2 ~]# cd nagios-4.0.2
[root@apabicentos2 nagios-4.0.2]# ./configure --prefix=/usr/local/nagios
make all
make install
make install-init
make install-config
make install-commandmode
注:
(1)make install 安装主要的程序、CGI及HTML文件
(2)make install-commandmode 给外部命令访问nagios配置文件权限
(3)make install-config 把配置文件的例子复制到nagios的安装目录
4.安装nagios插件:
[root@apabicentos2 ~]# tar zxvf nagios-plugins-1.5.tar.gz
[root@apabicentos2 ~]# cd nagios-plugins-1.5
[root@apabicentos2 nagios-plugins-1.5]# ./configure --prefix=/usr/local/nagios
[root@apabicentos2 nagios-plugins-1.5]# make && make install
5.配置nagios开机启动
[root@apabicentos2 ~]# chkconfig nagios on
[root@apabicentos2 ~]# chkconfig --list nagios
nagios                 0:off        1:off        2:on        3:on        4:on        5:on        6:off
6.安装Perl fcgi模块,让Nginx支持CGI
tar -zxf FCGI-0.74.tar.gz
cd FCGI-0.74
perl Makefile.PL
make && make install
tar -zxf FCGI-ProcManager-0.24.tar.gz
cd FCGI-ProcManager-0.24
perl Makefile.PL
make && make install
tar zxf IO-All-0.50.tar.gz
cd IO-All-0.50
perl Makefile.PL
make && make install
7.建立nginx-fcgi脚本:
vim /usr/local/nginx/sbin/nginx-fcgi

#!/usr/bin/perl

#

#   author      Daniel Dominik Rudnicki

#   thanks to:  Piotr Romanczuk

#   email       mailto:daniel@sardzent.org]daniel@sardzent.org

#   version     0.4.3

#   webpage     http://www.nginx.eu/]http://www.nginx.eu/

#

#   BASED @ http://wiki.codemongers.com/NginxSimpleCGI]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 "0$date";

}

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 = "$year-$mon-$mday $hour:$min";

return($datestring);

}

 

sub addlog {

my ($log_file, $log_message) = @_;

my $curr_time = logformat();

my $write_message = "[$curr_time]   $log_message";

$write_message >> io($log_file);

"\n" >> io($log_file);

}

 

sub printerror {

my $message = @_;

print "\n   Nginx FastCGI\tERROR\n"

. "\t $message\n\n";

exit 1;

}

 

sub usage {

print "\n   Nginx FastCGI \n"

. "\n\tusage: $0 [-h] -S string -P int\n"

. "\n\t-h\t\t: this (help) message"

. "\n\t-S path\t\t: path for UNIX socket"

. "\n\t-P port\t\t: port number"

. "\n\t-p file\t\t: path for pid file"

. "\n\t-l file\t\t: path for logfile"

. "\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";

exit 1;

}

 

 

init;

#

END() { } BEGIN() { }

*CORE::GLOBAL::exit = sub { die "fakeexit\nrc=".shift()."\n"; }; eval q{exit};

if ($@) {

exit unless $@ =~ /^fakeexit/;

} ;

 

# fork part

my $pid = fork();

 

if( $pid == 0 ) {

&main;

exit 0;

}

 

print " Forking worker process with PID $pid\n" if $verbose;

addlog($logfile, "Forking worker process with PID $pid");

print " Update PID file $filepid\n" if $verbose;

addlog($logfile, "Update PID file $filepid");

$pid > io($filepid);

print " Worker process running.\n" if $verbose;

addlog ($logfile, "Parent process $$ is exiting");

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, "running $req_params{SCRIPT_FILENAME}");

}

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

#

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

if ($cgi_app) {

print <$cgi_app>;

close $cgi_app;

}

} else {

print("Content-type: text/plain\r\n\r\n");

print "Error: No such CGI app - $req_params{SCRIPT_FILENAME} may not exist or is not executable by this process.\n";

addlog($logfile, "Error: No such CGI app - $req_params{SCRIPT_FILENAME} may not exist or is not executable by this process.");

}

}

}

8.授予脚本可执行权限:
[root@apabicentos2 IO-All-0.50]# cd /usr/local/nginx/sbin/
[root@apabicentos2 sbin]# ls
nginx  nginx-fcgi  nginx.old
[root@apabicentos2 sbin]# chmod a+x 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
查看是否启动:
[root@apabicentos2 sbin]# ps -aux |grep fcgi
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ
root      6710  0.0  0.2 136404  4540 pts/2    S    17:42   0:00 /usr/bin/perl /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
root      6712  0.0  0.0 103172   848 pts/2    S+   17:42   0:00 grep fcgi

已经正常启动。
授予nginx-fcgi.sock 777权限。
[root@apabicentos2 sbin]# chmod -R 777 /usr/local/nginx/logs/nginx-fcgi.sock
9.设置登录密码保证安全:
[root@apabicentos2 sbin]# htpasswd -c /usr/local/nginx/conf/htpasswd nagios
New password:
Re-type new password:
Adding password for user nagios

10.在nginx里面设置一个虚拟主机:

server

{

listen       80;

server_name nagios.a.com;

index index.html index.htm index.php default.html default.htm default.php;

root /usr/local/nagios/share;                auth_basic "nagios";                  auth_basic_user_file /usr/local/nginx/conf/htpasswd;

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)?$

{

try_files $uri =404;

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

fastcgi_index index.php;

include fcgi.conf;

}

 

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$

{

expires      30d;

}

 

location ~ .*\.(js|css)?$

{

expires      12h;

}

 

access_log  /home/wwwlogs/nagios.a.com.log  nagios.a.com;

}

重新加载配置文件:
[root@apabicentos2 vhost]# /usr/local/nginx/sbin/nginx -s reload
11.如果图片加载不正常加个链接就好了:
[root@apabicentos2 vhost]# mkdir -p /usr/local/nagios/share/nagios
[root@apabicentos2 vhost]# ln -s /usr/local/nagios/share/images /usr/local/nagios/share/nagios/images
[root@apabicentos2 vhost]# ln -s /usr/local/nagios/share/stylesheets /usr/local/nagios/share/nagios/stylesheets

12.配置可加载GD动态库:
vim /etc/ld.so.conf
include ld.so.conf.d/*.conf
/usr/local/gd/lib     #加入GD动态库路径
然后手动运行一下:
ldconfig
解决了map和trends错误的问题!

175927b9ncqt82ns70cnbw


Whatever is worth doing is worth doing well.