需要的包有:
- FCGI-0.74.tar.gz 462a77a0072480fea791a4d3095eb486
- FCGI-ProcManager-0.24.tar.gz 0aa184b1e5339e0ad92f4c1439406304
- nagios-3.3.1.tar.gz c935354ce0d78a63bfabc3055fa77ad5
- nagios-plugins-1.4.15.tar.gz 56abd6ade8aa860b38c4ca4a6ac5ab0d
- nconf-1.3.0-0.tgz 643b512dfa26676f652d966af37f62bd
- ndoutils-1.4b9.tar.gz 659b759a5eb54b84eb44a29f26b603bc
- npc-2.0.4.tar.gz 7b30302c544f10ed73cff406fda14499
前置条件:
rrdtool:http://blog.iyunv.com/liv2005/article/details/7180438
nginx+php+mysql:http://blog.iyunv.com/liv2005/article/details/7181508
Cacti 0.8.7i: http://blog.iyunv.com/liv2005/article/details/7181670
tar -zxvf nagios-3.3.1.tar.gz
cd nagios
./configure --with-nagios-user=web --with-nagios-group=web --with-gd-lib=/usr/local/libgd/lib --with-gd-inc=/usr/local/libgd/include
make all
make install
make install-init
make install-config
make install-commandmode
cd ..
ln -s /usr/local/lib/libiconv.so.2.5.0 /usr/lib64/libiconv.so.2
ln -s /usr/local/nagios/share /home/web/cacti/nagios
ln -s /usr/local/nagios/sbin /usr/local/nagios/share/cgi-bin
tar -zxvf nagios-plugins-1.4.15.tar.gz
cd nagios-plugins-1.4.15
./configure --with-nagios-user=web --with-nagios-group=web
make
make install
cd ..
#########nagios安装完成...真的....但要小小的配置一下,才能在nginx里开起来########
#为了运行nagios里的cgi部分,需要安装perl fcgi模块,并且让nginx支持cgi的调用
#参考URL:http://wiki.nginx.org/NginxSimpleCGI
tar -zxvf FCGI-0.74.tar.gz
cd FCGI-0.74
perl Makefile.PL
make
make install
cd ..
tar -zxvf FCGI-ProcManager-0.24.tar.gz
cd FCGI-ProcManager-0.24
perl Makefile.PL
make
make install
cd ..
#将以下内容保存为cgiwrap-fcgi.pl
#!/usr/bin/perl
use FCGI;
use Socket;
use FCGI::ProcManager;
sub shutdown { FCGI::CloseSocket($socket); exit; }
sub restart { FCGI::CloseSocket($socket); &main; }
use sigtrap 'handler', \&shutdown, 'normal-signals';
use sigtrap 'handler', \&restart, 'HUP';
require 'syscall.ph';
use POSIX qw(setsid);
END() { }
BEGIN() { }
{
no warnings;
*CORE::GLOBAL::exit = sub { die "fakeexit\nrc=" . shift() . "\n"; };
};
eval q{exit};
if ($@) {
exit unless $@ =~ /^fakeexit/;
}
&main;
sub daemonize() {
chdir '/' or die "Can't chdir to /: $!";
defined( my $pid = fork ) or die "Can't fork: $!";
exit if $pid;
setsid() or die "Can't start a new session: $!";
umask 0;
}
sub main {
$proc_manager = FCGI::ProcManager->new( {n_processes => 5} );
$socket = FCGI::OpenSocket( "/tmp/cgiwrap-dispatch.sock", 10 )
; #use UNIX sockets - user running this script must have w access to the 'nginx' folder!!
$request =
FCGI::Request( \*STDIN, \*STDOUT, \*STDERR, \%req_params, $socket,
&FCGI::FAIL_ACCEPT_ON_INTR );
$proc_manager->pm_manage();
if ($request) { request_loop() }
FCGI::CloseSocket($socket);
}
sub request_loop {
while ( $request->Accept() >= 0 ) {
$proc_manager->pm_pre_dispatch();
#processing any STDIN input from WebServer (for CGI-POST actions)
$stdin_passthrough = '';
{ no warnings; $req_len = 0 + $req_params{'CONTENT_LENGTH'}; };
if ( ( $req_params{'REQUEST_METHOD'} eq 'POST' ) && ( $req_len != 0 ) ) {
my $bytes_read = 0;
while ( $bytes_read < $req_len ) {
my $data = '';
my $bytes = read( STDIN, $data, ( $req_len - $bytes_read ) );
last if ( $bytes == 0 || !defined($bytes) );
$stdin_passthrough .= $data;
$bytes_read += $bytes;
}
}
#running the cgi app
if (
( -x $req_params{SCRIPT_FILENAME} ) && #can I execute this?
( -s $req_params{SCRIPT_FILENAME} ) && #Is this file empty?
( -r $req_params{SCRIPT_FILENAME} ) #can I read this file?
) {
pipe( CHILD_RD, PARENT_WR );
pipe( PARENT_ERR, CHILD_ERR );
my $pid = open( CHILD_O, "-|" );
unless ( defined($pid) ) {
print("Content-type: text/plain\r\n\r\n");
print "Error: CGI app returned no output - Executing $req_params{SCRIPT_FILENAME} failed !\n";
next;
}
$oldfh = select(PARENT_ERR);
$| = 1;
select(CHILD_O);
$| = 1;
select($oldfh);
if ( $pid > 0 ) {
close(CHILD_RD);
close(CHILD_ERR);
print PARENT_WR $stdin_passthrough;
close(PARENT_WR);
$rin = $rout = $ein = $eout = '';
vec( $rin, fileno(CHILD_O), 1 ) = 1;
vec( $rin, fileno(PARENT_ERR), 1 ) = 1;
$ein = $rin;
$nfound = 0;
while ( $nfound = select( $rout = $rin, undef, $ein = $eout, 10 ) ) {
die "$!" unless $nfound != -1;
$r1 = vec( $rout, fileno(PARENT_ERR), 1 ) == 1;
$r2 = vec( $rout, fileno(CHILD_O), 1 ) == 1;
$e1 = vec( $eout, fileno(PARENT_ERR), 1 ) == 1;
$e2 = vec( $eout, fileno(CHILD_O), 1 ) == 1;
if ($r1) {
while ( $bytes = read( PARENT_ERR, $errbytes, 4096 ) ) {
print STDERR $errbytes;
}
if ($!) {
$err = $!;
die $!;
vec( $rin, fileno(PARENT_ERR), 1 ) = 0
unless ( $err == EINTR or $err == EAGAIN );
}
}
if ($r2) {
while ( $bytes = read( CHILD_O, $s, 4096 ) ) {
print $s;
}
if ( !defined($bytes) ) {
$err = $!;
die $!;
vec( $rin, fileno(CHILD_O), 1 ) = 0
unless ( $err == EINTR or $err == EAGAIN );
}
}
last if ( $e1 || $e2 );
}
close CHILD_RD;
close PARENT_ERR;
waitpid( $pid, 0 );
} else {
foreach $key ( keys %req_params ) {
$ENV{$key} = $req_params{$key};
}
# cd to the script's local directory
if ( $req_params{SCRIPT_FILENAME} =~ /^(.*)\/[^\/] +$/ ) {
chdir $1;
}
close(PARENT_WR);
#close(PARENT_ERR);
close(STDIN);
close(STDERR);
#fcntl(CHILD_RD, F_DUPFD, 0);
syscall( &SYS_dup2, fileno(CHILD_RD), 0 );
syscall( &SYS_dup2, fileno(CHILD_ERR), 2 );
#open(STDIN, "<&CHILD_RD");
exec( $req_params{SCRIPT_FILENAME} );
die("exec failed");
}
} 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";
}
}
}
#放到nginx那里去
dos2unix cgiwrap-fcgi.pl; chmod u+x cgiwrap-fcgi.pl;
cp cgiwrap-fcgi.pl /usr/local/nginx/sbin/
#放入后台执行该pl,并且添加为开机自启动,或者加在nginx启动的时候,可以考虑修改fastcgi_nginx_ctl.sh的start函数和stop函数
#启动:
/usr/local/nginx/sbin/cgiwrap-fcgi.pl &> /home/web/logs/perl-fcgi.log &
#关闭:
kill `ps -ef | grep "perl-fcgi-pm" | grep -v grep | awk '{print $2}'`
for i in `ps -ef | grep "perl-fcgi" | grep -v grep | awk '{print $2}'`; do kill -9 $i; done;
#手动在nginx.conf里添加如下内容
location ~ .*\.cgi$ {
gzip off; #gzip makes scripts feel slower since they have to complete before getting gzipped
fastcgi_pass unix:/tmp/cgiwrap-dispatch.sock;
fastcgi_index index.cgi;
include fcgi.conf;
auth_basic "Nagios Login";
auth_basic_user_file /usr/local/nagios/etc/nagios.htpasswd;
}
#找一台有apache的服务器,生成nagios页面的验证信息,不然很多页面没有权限访问
./htpasswd -c nagios.htpasswd nagios
New password: nagios
Re-type new password: nagios
Adding password for user nagios
#将该文件放到/usr/local/nagios/etc/nagios.htpasswd#如果没有apache的机器,将如下内容保存为nagios.htpasswd,用户名密码都是nagios,弱口令哦,呵呵
nagios:e9hfl5LNHrFYo
vi /usr/local/nagios/etc/cgi.cfg
#修改以下项的nagiosadmin为nagios
authorized_for_all_host_commands=nagiosadmin
authorized_for_all_hosts=nagiosadmin
authorized_for_all_service_commands=nagiosadmin
authorized_for_all_services=nagiosadmin
authorized_for_configuration_information=nagiosadmin
authorized_for_system_commands=nagiosadmin
authorized_for_system_information=nagiosadmin启动nagios:
/etc/init.d/nagios start
#==============至此,nagios安装完毕 , 可以访问 http://ip/nagios 试试看了====================
############下面安装的ndoutils组件,可以将nagios的数据写入数据库,然后由Cacti的npc组件在Cacti的页面中展示,即所谓的整合,其实有些坑爹###############
tar -zxvf ndoutils-1.4b9.tar.gz
cd ndoutils-1.4b9
./configure --with-mysql=/usr/local/mysql --enable-mysql --with-ndo2db-user=web --with-ndo2db-group=web
vi include/config.h
#手动将261 262行,写上mysql头文件的绝对路径,前面的井号别删哈...
#include </usr/local/mysql/include/mysql/mysql.h>
#include </usr/local/mysql/include/mysql/errmsg.h>
make
cd src/
cp ndomod-3x.o ndo2db-3x log2ndo file2sock /usr/local/nagios/bin/
cd ../config
cp ndo2db.cfg-sample ndo2db.cfg
cp ndomod.cfg-sample ndomod.cfg
vi ndo2db.cfg
#修改以下参数
db_name=cacti
db_prefix=npc_
db_user=cactiuser
db_pass=cactiuser
#复制到nagios目录里
cp ndo2db.cfg ndomod.cfg /usr/local/nagios/etc/
vi /usr/local/nagios/etc/nagios.cfg
#添加一行,使nagios能调用组件,将数据写入数据库
broker_module=/usr/local/nagios/bin/ndomod-3x.o config_file=/usr/local/nagios/etc/ndomod.cfg
vi /etc/init.d/nagios
#使启动和关闭nagios的时候,组件一并启动或关闭
#在start函数结束前添加:
/usr/local/nagios/bin/ndo2db-3x -c /usr/local/nagios/etc/ndo2db.cfg
#在stop函数结束前添加:
killall ndo2db-3x;
rm -f /usr/local/nagios/var/ndo2db.lock;
rm -f /usr/local/nagios/var/ndo.sock;
#为了运行不出错,要建个软连接
ln -s /usr/local/mysql/lib/mysql/libmysqlclient.so.16.0.0 /lib64/libmysqlclient.so.16
cd /usr/local/
chown web.web nagios/ -R
chmod +x nagios/ -R
vi /etc/init.d/nagios
#注释掉“su - …… nagios.log”这一行,因为nagios会尝试使用web进行登录,touch日志文件,此时会报错,而且这个touch没啥实际意义,因为我们的nagios.log可以放到其他路径下。
#没啥要改的了,启动!
/etc/init.d/nagios start
#加入到rc.local或chkconfig设置为开机自启动
########nagios装完了,真的,可以看看nagios的日志和/var/log/message里有报错没有,有报错的话,自己想办法搞定吧...########
########装npc,让cacti能显示nagios数据############
tar -zxvf npc-2.0.4.tar.gz
mv npc /home/web/cacti/plugins/
cd /home/web/cacti/plugins
chown web.web npc/ -R
chmod u+x npc/ -R
vi /home/web/cacti/include/plugins.php
#修改$plugins_system中的数组,添加npc
#然后登陆cacti界面来install,enable;然后到setting里随便点点之类的。
#由于npc的表缺少long_output这个字段,所以需要重新建立,应该是npc-2.0.4的bug,不过这玩意已经没新版本了,出现问题可以理解……
#已测必须添加的字段:
alter table npc_hostchecks add long_output TEXT NOT NULL default '' after output;
alter table npc_hoststatus add long_output TEXT NOT NULL default '' after output;
alter table npc_servicechecks add long_output TEXT NOT NULL default '' after output;
alter table npc_servicestatus add long_output TEXT NOT NULL default '' after output;
#未测,网上说还需要添加的字段:
alter table npc_eventhandlers add long_output TEXT NOT NULL default '' after output;
alter table npc_notifications add long_output TEXT NOT NULL default '' after output;
alter table npc_statehistory add long_output TEXT NOT NULL default '' after output;
alter table npc_systemcommands add long_output TEXT NOT NULL default '' after output;
#参考 https://trac.assembla.com/npc/wiki/QuickStartGuide
#注意:如果能显示npc的界面,数据库里有数据,但界面上不显示,检查一下php是否支持pdo_mysql,php的编译参数可能需要添加--with-pdo-mysql;如果是按照我之前nginx的安装脚本装的php,那就已经包含了。
#==============至此,与cacti集成完毕====================
扩展:
nconf的安装,什么?不想装? 那nagios手工改配置文件去吧...烦死你....
#校验nagios的配置是否正确:
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
#确认nagios.log和message里没有报错信息,就可以继续安装nagios其他组件了
#安装nconf
cd /usr/local/nagios
tar -zxvf nconf-1.3.0-0.tgz
chown web.web nconf/ -R
chmod +x nconf/ -R
ln -s /usr/local/nagios/nconf /home/web/cacti/nconf
#访问 http://ip/nconf/INSTALL.php 进行在线配置,配置完了,记得执行删除操作
cd /usr/local/nagios/nconf
rm -rf INSTALL UPDATE INSTALL.php UPDATE.php
#然后可访问http://ip/nconf/ 进行正常使用了,用户admin,密码为刚才界面上随便点点时设置的密码
#参考 http://www.nconf.org/dokuwiki/doku.php?id=nconf:help:documentation:start:installation&s[]=install
#==============至此,nconf安装完毕====================
|