LAMP坏境和LNMP环境安装Nagios4.1.1和基本配置
----------------------------------------以下内容为笔者生产环境的监控,安装都是经过一步步测试的--------------------------------
环境:LAMP :CentOS 6.8 、Apache 2.4、 Mysql 5.6 、 PHP 5.6.22
LNMP: CentOS 6.8 、Nginx 1.10、 Mysql 5.6 、 PHP 5.6.22
perl cgi软件:FCGI-ProcManger0.19、 FCGI-0.74、 IO-1.25、IO-All0.62、perl-fcgi.zip
软件:Nagios4.1.1 、NRPE2.15、Nagios-plugins2.14、pnp4nagios0.6.25、NSClient++-Stable
安装环境确认:
首先确认已经安装好LAMP环境或者LNMP环境,然后在按照需要看对应环境下的安装说明。
我这里使用的是LAMP、LNMP一键安装包,安装的时候可以按需求安装对应软件的版本。wget http://soft.vpser.net/lnmp/lnmp1.3-full.tar.gz
其他软件请自行到Nagios官网去下载https://www.nagios.org/downloads/nagios-core, 或者百度搜索软件名称就会有了。我这里就不提供连接了
===================================LAMP坏境下安装Nagios4.1.1=======================================
服务器端安装:
安装Nagios-4.1.1
1、新建nagios用户
useradd -s /sbin/nologin nagios
2、解压nagios4.1.1软件,并且安装
tar zxvf nagios-4.1.1.tat.gz
cd nagios-4.1.1/
./configure --prefix=/usr/local/nagios --with-nagios-user=nagios --with-nagios-group=nagios
make all && make install && make install-init && make install-commandmode && make install-config
3、添加nagios开机自动启动
chkconfig --add nagios
chkconfig --level 35 nagios on
chkconfig --list nagios
安装nagios-plugin插件
4、解压nagios-plugin软件,并且安装
tar zxvf nagios-plugin-2.1.4.tar.gz
./configure --prefix=/usr/local/nagios && make && make install
5、修改安装后的nagios目录所有者
chown –R nagios.nagios /usr/local/nagios
安装nrpe插件
6、解压nrpe软件,并且安装 (这里只需要安装nrpe其中一个check_nrpe的插件即可)
tar zxvf nrpe-2.15.tar.gz && cd nrpe.2.15
./configure && make all && make install-plugin
7、添加nrpe的命令到nagios的command.cfg文件里
vim /usr/local/nagios/etc/objects/commands.cfg 添加如下:
#'check_nrpe ' command definition
define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}
apache支持nagios配置
8、修改apache文件vim /usr/local/apache/conf/httpd.conf
添加或者修改:
User nagios
Group nagios
LoadModule actions_module modules/mod_actions.so
LoadModule cgid_module modules/mod_cgid.so
AddType application/x-httpd-php .php
DirectoryIndex index.html 后面添加 index.php
文件最后添加如下内容:
#setting for nagios
ScriptAlias /nagios/cgi-bin "/usr/local/nagios/sbin"
<Directory "/usr/local/nagios/sbin">
AuthType Basic
Options ExecCGI
AllowOverride None
Order allow,deny
Allow from all
AuthName "Nagios Access"
AuthUserFile /usr/local/nagios/etc/htpasswd //用于此目录访问身份验证的文件
Require valid-user
</Directory>
Alias /nagios "/usr/local/nagios/share"
<Directory "/usr/local/nagios/share">
AuthType Basic
Options None
AllowOverride None
Order allow,deny
Allow from all
AuthName "nagios Access"
AuthUserFile /usr/local/nagios/etc/htpasswd
Require valid-user
</Directory>
9、创建nagios web帐号
/usr/local/apache2/bin/htpasswd -c /usr/local/nagios/etc/htpasswd nagiosadmin (这是nagios默认的帐号,也可以使用其他名称,,但要在cgi.cfg去添加自己的帐号)
10、启动、停止Nagios
Service nagios restart 重启nagios
Service httpd restart 重启apache
11、web登陆nagios
http://xxx.xxx.xxx.xxx/nagios,帐号密码为第五步设置的帐号密码
至此Nagios的服务器端就安装好了!
Nagios监控Linux客户端
Linux客户端只需要安装nagios-plugins和nrpe即可,步骤如下:
1、新建nagios用户
useradd -s /sbin/nologin nagios
2、解压nagios-plugin软件,并且安装
yum -y install openssl openssl-devel
tar zxvf nagios-plugin-2.1.4.tar.gz
./configure --prefix=/usr/local/nagios && make && make install
3、修改安装后的nagios目录所有者
chown –R nagios.nagios /usr/local/nagios
4、安装nrpe监控远程linux插件
tar zxvf nrpe-2.15.tar.gz && cd nrpe-2.15
./configure && make all && make install-plugin && make install-daemon && make install-daemon-config && make install xinetd
5、安装xinetd服务,nrpe依靠xinetd服务
yum -y install xinetd
6、修改nrpe配置文件
vim /etc/xinetd.d/nrpe
Only_from 后面增加nagios服务器的IP ,逗号隔开不要空格
vim /usr/local/nagios/etc/nrpe.cfg
allowed_hosts后面增加nagios服务器的IP ,逗号隔开不要空格
7、启动nrpe和xinetd
/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg –d
service xinetd start
8、nagios服务器端检查和被监控linux主机的通讯
/usr/local/nagios/libexec/check_nrpe -H 192.168.16.222 (出现NRPE v2.15表示正常)
NRPE v2.15
至此nagios的客户端就安装完毕!
Nagios监控Window客户端
1、安装NSClient++
双击运行,一直下一步,直到出现输入allowed hosts 和NSClient passwd
输入对应nagios的服务器的IP,以及设置一个NSClient 的密码
Modules to load 勾选里面 除了Enable WMI checks不勾选之外,其他都勾选,下一步安装完成。
2、配置NSClient++
进入NSClient的安装目录,打开NSC.ini文件,
修改[modules]内容:除了;CheckWMI.dll 和 ;RemoteConfiguration.dll 其他的都去掉前面的分号。注意同样的就不需要重复打开了
修改[Settings]内容:修改allowed_hosts= nagios的ip password= 安装NSClient设置的密码
修改[NSClient]内容:修改port=12489
保存即可!
3、启动NSClient
cmd——>services.msc——>NSClient++ 启动该服务,并且设置为自动,登陆选项选择允许交互打勾。
4、nagios服务器端检查和被监控lwindows主机的通讯
/usr/local/nagios/libexec/check_nt -H 192.168.16.200 -p 12489 -s 123456 -v UPTIME (-p 指定端口 -s 指定NSClient的密码 -v 为命令 。还有些有-l 参数)
System Uptime - 36 day(s) 14 hour(s) 50 minute(s)
至此nagios监控windows主机配置完成!
PNP4Nagios0.6.25安装(本例使用Bulk Mode with NPCD模式)
1、安装依赖包
yum install rrdtool perl-Time-HiRes rrdtool-perl php-gd
2、安装PNP4
cd pnp4nagios-0.6.25
./configure --with-nagios-user=nagios --with-nagios-group=nagios --with-httpd-conf=/usr/local/apache/conf
make all && make fullinstall
3、npcd启动和添加自启动
service npcd restart && chkconfig --add npcd && chkconfig --level 35 npcd on
4、修改pnp4nagios.conf文件htpass.user(以nagios web的帐号文件为准)
并且把在httpd.conf 添加Include pnp4nagios.conf,(如果是LAMP1.3环境,只需要mv到vhost目录下即可)
5、 添加pnp4的perfdata service 和host
vim /usr/local/nagios/etc/nagios.cfg
修改成process_performance_data=1
增加以下内容
# service performance data
#
service_perfdata_file=/usr/local/pnp4nagios/var/service-perfdata
service_perfdata_file_template=DATATYPE::SERVICEPERFDATA\tTIMET::$TIMET$\tHOSTNAME::$HOSTNAME$\tSERVICEDESC::$SERVICEDESC$\tSERVICEPERFDATA::$SERVICEPERFDATA$\tSERVICECHECKCOMMAND::$SERVICECHECKCOMMAND$\tHOSTSTATE::$HOSTSTATE$\tHOSTSTATETYPE::$HOSTSTATETYPE$\tSERVICESTATE::$SERVICESTATE$\tSERVICESTATETYPE::$SERVICESTATETYPE$
service_perfdata_file_mode=a
service_perfdata_file_processing_interval=15
service_perfdata_file_processing_command=process-service-perfdata-file
#
# host performance data starting with Nagios 3.0
#
host_perfdata_file=/usr/local/pnp4nagios/var/host-perfdata
host_perfdata_file_template=DATATYPE::HOSTPERFDATA\tTIMET::$TIMET$\tHOSTNAME::$HOSTNAME$\tHOSTPERFDATA::$HOSTPERFDATA$\tHOSTCHECKCOMMAND::$HOSTCHECKCOMMAND$\tHOSTSTATE::$HOSTSTATE$\tHOSTSTATETYPE::$HOSTSTATETYPE$
host_perfdata_file_mode=a
host_perfdata_file_processing_interval=15
host_perfdata_file_processing_command=process-host-perfdata-file
6、添加perfdata命令
vim /usr/local/nagios/etc/objects/commands.cfg
增加以下内容
define command{
command_name process-service-perfdata-file
command_line /bin/mv /usr/local/pnp4nagios/var/service-perfdata /usr/local/pnp4nagios/var/spool/service-perfdata.$TIMET$
}
define command{
command_name process-host-perfdata-file
command_line /bin/mv /usr/local/pnp4nagios/var/host-perfdata /usr/local/pnp4nagios/var/spool/host-perfdata.$TIMET$
}
7、 重启下npcd 、nagios、apache
service npcd restart 或者
/usr/local/pnp4nagios/bin/npcd -d -f /usr/local/pnp4nagios/etc/npcd.cfg
service nagios restart
lnmp httpd restart
8、 检查数据是否存在
ls -la /usr/local/pnp4nagios/var/service-perfdata
9、验证pnp4在bulk+npcd模式下是否正确
下载验证脚本:wget http://verify.pnp4nagios.org/verify_pnp_config
perl verify_pnp_config --mode bulk+npcd --config=/usr/local/nagios/etc/nagios.cfg --pnpcfg=/usr/local/pnp4nagios/etc
(无CTRIX为正常)
10、添加PNP主机和服务到nagios
vim /usr/local/nagios/etc/objects/templates.cfg
define host{
name host-pnp
register 0
action_url /pnp4nagios/graph?host=$HOSTNAME$&srv=_HOST_
}
define service {
name srv-pnp
action_url /pnp4nagios/index.php/graph?host=$HOSTNAME$&srv=$SERVICEDESC$
register 0
}
11、在监控的主机和服务上use PNP的主机和服务
例如:
vi /usr/local/nagios/etc/objects/localhost.cfg
至此,nagios下的pnp4安装完成!
===================================LNMP坏境下安装Nagios4.1.1=======================================
一、Nagios的服务器端、linux客户端、windows客户端安装nagios程序、插件以及PNP4Nagios的配置和LAMP环境的一样,请参照上面步骤!不同的只有配置nginx的虚拟主机支持perl cgi 和nginx配置pnp4nagios的虚拟主机而已。
二、安装perl fastcgi模块
1、 安装FCGI模块
tar xvzf FCGI-0.74.tar.gz && cd FCGI-0.74
perl Makefile.Pl && make && make install
2、 安装安装FCGI-ProcManager模块
tar xvzf FCGI-ProcManager-0.19.tar.gz && cd FCGI-ProcManager-0.19
perl Makefile.Pl && make && make install
3、 安装IO和IO-ALL模块
tar zxvf IO-1.25.tar.gz && cd IO-1.25
perl Makefile.Pl && make && make install
tar zxvf IO-All-0.62.tar.gz && cd IO-All-0.62
perl Makefile.Pl && make && make install
4、 Nginx支持perl脚本
这个脚本的目的就是产生一个PERL的FastCGI接口,让Nginx可以以CGI方式处理Perl
unzip perl-fcgi.zip
mv perl-fcgi.pl /usr/local/nginx/
chmod 755 /usr/local/nginx/perl-fcgi.pl
perl-fcgi.pl内容如下:
#!/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 "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
#
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.");
}
}
}
5、 建立一个CGI启动/停止脚本 名称为:start.per_cgi.sh
这个SHELL脚本只是为了方便管理上面的Perl脚本。脚本中的www为nginx的运行用户,请据自己的实际情况调整。
注意事项:不能用root用户执行(会提示). 要用与Nginx相同身份的用户执行。否则可能会在Nginx Log中提示 Permision Denied。
#!/bin/bash
#set -x
dir=/usr/local/nginx
stop ()
{
#pkill -f $dir/perl-fcgi.pl
kill $(cat $dir/logs/perl-fcgi.pid)
rm $dir/logs/perl-fcgi.pid 2>/dev/null
rm $dir/logs/perl-fcgi.sock 2>/dev/null
echo "stop perl-fcgi done"
}
start ()
{
rm $dir/now_start_perl_fcgi.sh 2>/dev/null
chown www.www $dir/logs
echo "$dir/perl-fcgi.pl -l $dir/logs/perl-fcgi.log -pid $dir/logs/perl-fcgi.pid -S $dir/logs/perl-fcgi.sock" >>$dir/now_start_perl_fcgi.sh
chown www.www $dir/now_start_perl_fcgi.sh
chmod u+x $dir/now_start_perl_fcgi.sh
sudo -u www $dir/now_start_perl_fcgi.sh
echo "start perl-fcgi done"
}
case $1 in
stop)
stop
;;
start)
start
;;
restart)
stop
start
;;
esac
修改此脚本权限 chmod 755 /usr/local/webserver/nginx/start_perl_cgi.sh
启动脚本 /usr/local/webserver/nginx/start_perl_cgi.sh start
正常情况下在/usr/local/webserver/nginx/logs下生成perl-fcgi.sock这个文件,如果没有生成,请检查下上面的
三、Nginx配置Nagios
A、在WEB主目录下创建一个软链
ln -s /usr/local/nagios/share/ /home/wwwroot/nagios
B、Nginx配置Nagios的vhost
server {
listen 80;
server_name 192.168.16.200;
index index.html index.htm index.php;
root /home/wwwroot;
auth_basic "Nagios Access";
auth_basic_user_file /usr/local/nagios/etc/htpasswd;
location ~ .*\.(php|php5)?$ {
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ .*\.(cgi|pl)?$ {
gzip off;
root /home/wwwroot/nagios/sbin;
#root /usr/local/nagios/sbin;
rewrite ^/nagios/cgi-bin/(.*)\.cgi /$1.cgi break;
fastcgi_pass unix:/usr/local/nginx/logs/perl-fcgi.sock;
fastcgi_param SCRIPT_FILENAME /usr/local/nagios/sbin$fastcgi_script_name;
fastcgi_index index.cgi;
fastcgi_read_timeout 60;
fastcgi_param REMOTE_USER $remote_user;
include fastcgi_params;
auth_basic "Nagios Access";
auth_basic_user_file /usr/local/nagios/etc/htpasswd;
}
location ^~ /pnp4nagios {
alias /usr/local/pnp4nagios/share;
index index.php;
try_files $uri $uri/ @pnp4nagios;
}
location @pnp4nagios {
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(.*);
fastcgi_param PATH_INFO $fastcgi_path_info;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /usr/local/pnp4nagios/share/index.php;
}
access_log /home/wwwlogs/access_nagios.log;
}
红色字体的为PNP4Nagios的配置。
四、重启nagios和lnmp服务
service nagios restart
lnmp restart
记得把start_perl_cgi.sh加入开机启动
五、解决Ngios web端服务命令502 nginx错误
关键是提交的时候使用了post,估计是nginx下不支持这个post,到nagios源代码目录的cgi目录,vi 打开cmd.c文件,搜索post,只有一个,修改为get,然后当前目录重新make,拷贝新make出来的cmd.cgi到安装目录替换掉旧的(/usr/local/nagios/sbin/),这个时候重新执行commit命令时,会告诉你Error: Could not open command file ‘/usr/local/nagios/var/rw/nagios.cmd’ for update!,有这个错误信息后,搜索下,得到答案:http://blog.sina.com.cn/s/blog_6eee530801010ade.html
(本次测试做了红字部分就已经正常,余下步骤未测试!)
修改nagios的启动脚本/etc/init.d/nagios
在case语句 start) 最后一行加入chmod -R 777 $NagiosVarDir/rw
将nagios和www的用户放到一个组。
修改后,需要重启nginx哦。