在Nginx中搭建Nagios监控平台

时间:2021-10-29 22:50:35
在Nginx中搭建Nagios监控平台

本文只做Nginx下Nagiox安装的说明,其它关于Nagios监控的详细配置请参考我的另一篇文章[Ubuntu 10.04下构建Nagios监控平台]

Nagios依赖PHP环境和perl环境。由于Nginx不支持Perl的CGI,这里就需要先来搭建Perl环境。

一、给Nginx加上对Perl的CGI支持

让Nginx支持Perl的CGI方法有好几种,基本原理都是通过Perl的FCGI模块实现。具体哪一个更好,目前没时间验证。下面的方法就是其中一种:

1、安装FCGI模块

1
2
3
4
5
6
7
tar xvzf FCGI-0.73.tar.gz
cd FCGI-0.73
perl Makefile.PL
make
make install
cd ..

2、安装FCGI-ProcManager模块

1
2
3
4
5
6
7
tar xvzf FCGI-ProcManager-0.19.tar.gz
cd FCGI-ProcManager-0.19
perl Makefile.PL
make
make install
cd ..

3、安装IO和IO::ALL模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
tar zxvf IO-1.25.tar.gz
cd IO-1.25
perl Makefile.PL
make
make install
cd ..
tar zxvf IO-All-0.41.tar.gz
cd IO-All-0.41
perl Makefile.PL
make
make install
cd ..

4、下载Perl脚本

这个脚本的目的就是产生一个PERL的FastCGI接口,让Nginx可以以CGI方式处理Perl。

1
2
3
unzip perl-fcgi.zip
cp perl-fcgi.pl /usr/local/webserver/nginx/

注:建议把这个脚本放在Nginx安装目录。

修改脚本权限

1
chmod 755 /usr/local/webserver/nginx/perl-fcgi.pl

5、建立一个CGI启动/停止脚本

这个SHELL脚本只是为了方便管理上面的Perl脚本。脚本中的www为nginx的运行用户,请据自己的实际情况调整。

注意事项:不能用root用户执行(会提示). 要用与Nginx相同身份的用户执行。否则可能会在Nginx Log中提示 Permision Denied。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
vi /usr/local/webserver/nginx/start_perl_cgi.sh
#!/bin/bash
#set -x
dir=/usr/local/webserver/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-data.www-data $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

修改SHELL脚本权限

1
chmod 755 /usr/local/webserver/nginx/start_perl_cgi.sh

启动脚本

1
/usr/local/webserver/nginx/start_perl_cgi.sh start
 

正常情况下在/usr/local/webserver/nginx/logs下生成perl-fcgi.sock这个文件,如果没有生成,请检查下上面的步聚。在Nginx中搭建Nagios监控平台
必须保证perl-fcgi.pl存在,执行start_perl_cgi.sh 才能执行成功  最好用sudo执行

保证能正常使用 在rc.local中 加入 启动 php功能模块组件 还有 启动peri_cgi支持模块

启动 php 功能模块
 sudo /usr/local/php/sbin/php-fpm
启动peri_cgi支持模块 
 sudo /etc/nginx/start_perl_cgi.sh start

还需要启动 nginx 和nagios 不过这两个默认是随系统一起启动的

二、安装Nagios

A、安装前准备

安装的机器上必须有一个WEB服务,本文是在Nginx环境上安装的。

下载nagios主程序和相关插件程序包

安装GD库(Nagios中的statusmap和trends模块必须)

1
apt-get install libgd2-noxpm libgd2-noxpm-dev

B、Nagios监控端安装

1、创建Nagios用户及组

建立Nagios账号

1
/usr/sbin/useradd -m -s /sbin/nologin nagios

2、创建一个名为nagcmd的用户组,用于从web接口执行外部命令。将Nagios用户和Nginx用户加入组中。

1
2
3
groupadd nagcmd
usermod -a -G nagcmd nagios
usermod -a -G nagcmd www

注:上面的www是Nginx用户所属的组,如有不同请自行调整。

3、编译安装Nagios

1
2
3
4
5
6
7
8
9
10
11
12
tar zxvf nagios-3.2.3.tar.gz
cd nagios-3.2.3
./configure --with-command-group=nagcmd
make
make all
make install
make install-init
make install-config
make install-commandmode
#这里是在Nginx下运行Nagios,这一步就不用做了
make install-webconf
cd ..

注:

make install 用于安装主要的程序、CGI及HTML文件
make install-init 用于生成init启动脚本
make install-config 用于安装示例配置文件
make install-commandmode 用于设置相应的目录权限
make install-webconf 用于安装Apache配置文件

4、验证程序是否被正确安装

切换目录到安装路径,这里是/usr/local/nagios,看是否存在etc、bin、 sbin、 share、 var这五个目录,如果存在则可以表明程序被正确的安装到系统了。

1
2
ls  /usr/local/nagios/
bin/     etc/     sbin/    share/   var/

注;bin–Nagios执行程序所在目录,其中的nagios文件即为主程序。
etc–Nagios配置文件位置
sbin–Nagios cgi文件所在目录,也就是执行外部命令所需文件所在的目录
Share–Nagios网页文件所在的目录
var–Nagios日志文件、spid 等文件所在的目录
var/archives–日志归档目录
var/rw–用来存放外部命令文件

5、配置NGINX

1)、配置Nagios Web界面登陆帐号及密码

1
htpasswd -c /usr/local/nagios/etc/nagiospasswd mike

如果你没有htpasswd(这个工具由Apache安装包所提供),可在线生成需要加密数据。

a)、访问http://www.4webhelp.net/us/password.php生成需要加密数据

b)、创建加密验证文件

1
2
3
4
vi /usr/local/nagios/etc/nagiospasswd
#加入生成的加密数据,冒号前是用户名,后面是加密后的密码
mike:25JB.R7mXY96o

修改Nagios配置文件,给新增的用户增加访问权限

1
2
3
4
5
6
7
8
9
10
vi /usr/local/nagios/etc/cgi.cfg
#以下几项中分别加入新增的用户,多用户用逗号分隔。
authorized_for_system_information=nagiosadmin,mike
authorized_for_configuration_information=nagiosadmin,mike
authorized_for_system_commands=nagiosadmin,mike
authorized_for_all_services=nagiosadmin,mike
authorized_for_all_hosts=nagiosadmin,mike
authorized_for_all_service_commands=nagiosadmin,mike
authorized_for_all_host_commands=nagiosadmin,mike

2)、修改NGINX配置,以支持WEB方式访问Nagios

正确:

server

{

listen       8008;

#    server_name  192.168.1.108;

index index.html index.htm index.php;

root  /usr/share/nginx/www;

#    auth_basic "Nagios Access";

auth_basic_user_file /usr/local/nagios/etc/nagiospasswd;

location ~ .*\.(php|php5)?$ {

#root html;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

include fastcgi_params;

}

location ~ .*\.(cgi|pl)?$ {

auth_basic "Nagios Access";

auth_basic_user_file /usr/local/nagios/etc/nagiospasswd;

gzip off;

root   /usr/local/nagios/sbin;  #这个目录下必须有ndex.cgi

rewrite ^/nagios/cgi-bin/(.*)\.cgi /$1.cgi break;

fastcgi_pass  unix:/etc/nginx/logs/perl-fcgi.sock; #注意这文件路径要对

fastcgi_param SCRIPT_FILENAME /usr/local/nagios/sbin$fastcgi_script_name;

fastcgi_index index.cgi,index.pl;

fastcgi_param  REMOTE_USER        $remote_user;

fastcgi_param HTTP_ACCEPT_LANGUAGE en_US;

include fastcgi_params;

fastcgi_read_timeout   60;

include /etc/nginx/fastcgi_params; #这个目录也要注意

}

#    log_format  www  '$remote_addr - $remote_user [$time_local] "$request" '

#               '$status $body_bytes_sent "$http_referer" '

#

#               '"$http_user_agent" $http_x_forwarded_for';

#    access_log  /etc/nginx/logs/access_www.log  wwwlogs;

}

方法二:以http://ip/nagios方式访问

在WEB主目录下创建一个软链

1
ln -s  /usr/local/nagios/share/  /data0/htdocs/www/nagios

NGINX配置片断如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
server
  {
    listen       80;
    server_name  192.168.1.108;
    index index.html index.htm index.php;
    root  /data0/htdocs/www;
    auth_basic "Nagios Access";
    auth_basic_user_file /usr/local/nagios/etc/nagiospasswd;
    location ~ .*\.(php|php5)?$
    {     
      #fastcgi_pass  unix:/tmp/php-cgi.sock;
      fastcgi_pass  127.0.0.1:9000;
      fastcgi_index index.php;
      include fcgi.conf;
    }
    location ~ .*\.(cgi|pl)?$
    {
    gzip off;
    root   /usr/local/nagios/sbin;
    rewrite ^/nagios/cgi-bin/(.*)\.cgi /$1.cgi break;
    fastcgi_pass  unix:/usr/local/webserver/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 fcgi.conf;
    auth_basic "Nagios Access";
    auth_basic_user_file /usr/local/nagios/etc/nagiospasswd;
    }
    log_format  wwwlogs  '$remote_addr - $remote_user [$time_local] "$request" '
               '$status $body_bytes_sent "$http_referer" '
               '"$http_user_agent" $http_x_forwarded_for';
    access_log  /data1/logs/access_www.log  wwwlogs;
  }

注:如果你的fastcgi的配置文件中没有配置REMOTE_USER参数,一定要在nginx.conf中加上下面这个fastcgi的参数定义。

1
fastcgi_param  REMOTE_USER        $remote_user;

如果没有这个fastcgi的参数定义,Nagios就不能正确验证你的登陆信息。网上大多数文章解决Nginx下Nagios登陆验证失败的方法都是在Nagios的cgi.cfg配置文件(nagios/etc/cgi.cfg)中关掉验证(use_authentication=0)或设置一个缺省的登陆用户(default_user_name=test),这两种方法都是不安全的。

6、编译并安装Nagios插件

由于Nagios主程序只是提供一个运行框架,其具体监控是靠运行在其下的插件完成的,所以Nagios插件是必须安装的。

1
2
3
4
5
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
 

验证Nagios插件是否正确安装

1
ls /usr/local/nagios/libexec

显示安装的插件文件,即所有的插件都安装在libexec这个目录下。

7、启动服务

启动前先检查下配置文件是否正确

1
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

如果没有报错,可以启动Nagios服务

1
/usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg

查看Nagios运行状态

1
/usr/local/nagios/bin/nagiostats

8、安装NRPE

由于Nagios只能监测自己所在的主机的一些本地情况,例如,cpu负载、内存使用、硬盘使用等等。如果想要监测被监控的服务器上的这些本地情况,就要用到NRPE。NRPE(Nagios Remote Plugin Executor)是Nagios的一个扩展,它被用于被监控的服务器上,向Nagios监控平台提供该服务器的一些本地的情况。NRPE可以称为Nagios的Linux客户端。

由于NRPE是通过SSL方式在监控和被监控主机上进行数据传输的,所以必须先安装ssl相关的软件包。

1
apt-get install libssl-dev libssl0.9.8

编译安装NRPE

1
2
3
4
5
6
7
tar zxvf nrpe-2.12.tar.gz
cd nrpe-2.12
./configure                            
 #checking for SSL libraries... configure: error: Cannot find ssl libraries . 解决:./configure --with-ssl-lib=/usr/lib/x86_64-linux-gnu ,原因是找不到 libssl.so文件
 
make all
make install-plugin
make install-daemon 
make install-daemon-config
  

注:监控主机上只需要make install-plugin这一步就可以了。监控机上只要有一个check_nrpe插件用于连接被监控端nrpe的daemon就行了。发现不执行make install-daemon 和make install-daemon-config /usr/local/nagios/etc/目录下不会产生nrpe.cfg,就没有办法启动nrpe

安装xinetd
xinetd默认是已经安装好的 如果没有需要 sudo apt-get install xinetd

cd nrpe-2.12
 make install-xinetd 
可以看到创建了这个文件/etc/xinetd.d/nrpe

vi /etc/xinetd.d/nrpe
# default: on
# description: NRPE (Nagios Remote Plugin Executor)
service nrpe
{
        flags           = REUSE
        socket_type     = stream
        port            = 5666
        wait            = no
        user            = nagios
        group           = nagios
        server          = /usr/local/nagios/bin/nrpe
        server_args     = -c /usr/local/nagios/etc/nrpe.cfg --inetd
        log_on_failure  += USERID
        disable         = no
        only_from       = 127.0.0.1在后面增加监控主机的地址0.111,以空格间隔
}
改后
     only_from       = 127.0.0.1 192.168.0.140
编辑/etc/services文件,增加NRPE服务
vi /etc/services
增加如下
# Local services
nrpe            5666/tcp                        # nrpe
重启xinetd服务
[root@dbpi nrpe-2.8.1]# service xinetd restart
Stopping xinetd: [  OK  ]
Starting xinetd: [  OK  ]
 
查看NRPE是否已经启动
[root@dbpi nrpe-2.8.1]# netstat -at|grep nrpe
tcp        0      0 *:nrpe                  *:*                     LISTEN   
[root@dbpi nrpe-2.8.1]# netstat -an|grep 5666
tcp        0      0 0.0.0.0:5666            0.0.0.0:*               LISTEN  
可以看到5666端口已经在监听了
 
5.测试NRPE是否则正常工作
之前我们在安装了check_nrpe这个插件用于测试,现在就是用的时候.执行
/usr/local/nagios/libexec/check_nrpe -H localhost
会返回当前NRPE的版本
[root@dbpi nrpe-2.8.1]# /usr/local/nagios/libexec/check_nrpe -H localhost
NRPE v2.8.1

也就是在本地用check_nrpe连接nrpe daemon是正常的

注:为了后面工作的顺利进行,注意本地防火墙要打开5666能让外部的监控机访问
/usr/local/nagios/libexec/check_nrpe –h查看这个命令的用法
可以看到用法是check_nrpe –H 被监控的主机 -c要执行的监控命令
注意:-c后面接的监控命令必须是nrpe.cfg文件中定义的.也就是NRPE daemon只运行nrpe.cfg中所定义的命令
查看NRPE的监控命令
cd /usr/local/nagios/etc
vi nrpe.cfg
找到下面这段话
# The following examples use hardcoded command arguments...
command[check_users]=/usr/local/nagios/libexec/check_users -w 5 -c 10
command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20
command[check_hda1]=/usr/local/nagios/libexec/check_disk -w 20 -c 10 -p /dev/hda1
command[check_zombie_procs]=/usr/local/nagios/libexec/check_procs -w 5 -c 10 -s Z
command[check_total_procs]=/usr/local/nagios/libexec/check_procs -w 150 -c 200
红色部分是命令名,也就是check_nrpe 的-c参数可以接的内容,等号=后面是实际执行的插件程序(这与commands.cfg中定义命令的形式十分相似,只不过是写在了一行).也就是说check_users就是等号后面/usr/local/nagios/libexec/check_users -w 5 -c 10的简称.
我们可以很容易知道上面这5行定义的命令分别是检测登陆用户数,cpu负载,hda1的容量,僵尸进程,总进程数.各条命令具体的含义见插件用法(执行”插件程序名 –h”)
由于-c后面只能接nrpe.cfg中定义的命令,也就是说现在我们只能用上面定义的这五条命令.我们可以在本机实验一下.执行
/usr/local/nagios/libexec/check_nrpe -H localhost -c check_users
/usr/local/nagios/libexec/check_nrpe -H localhost -c check_load
/usr/local/nagios/libexec/check_nrpe -H localhost -c check_hda1
/usr/local/nagios/libexec/check_nrpe -H localhost -c check_zombie_procs
/usr/local/nagios/libexec/check_nrpe -H localhost -c check_total_procs

启动NRPE

1
/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d

验证NRPE是否正确安装

1
/usr/local/nagios/libexec/check_nrpe -H localhost

注:如果成功,会返回NRPE的版本号。

在运行nagios的监控主机上
之前已经将nagios运行起来了,现在要做的事情是:
– 安装check_nrpe插件
– 在commands.cfg中创建check_nrpe的命令定义,因为只有在commands.cfg中定义过的命令才能在services.cfg中使用
–      创建对被监控主机的监控项目
在commands.cfg中增加对check_nrpe的定义
vi /usr/local/nagios/etc/objects/commands.cfg
在最后面增加如下内容
########################################################################
#
# 2007.9.5 add by yahoon
# NRPE COMMAND
#
########################################################################
# 'check_nrpe ' command definition
define command{
        command_name check_nrpe
        command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
        }
意义如下
command_name check_nrpe
定义命令名称为check_nrpe,在services.cfg中要使用这个名称.
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
这是定义实际运行的插件程序.这个命令行的书写要完全按照check_nrpe这个命令的用法.不知道用法的就用check_nrpe –h查看
-c后面带的$ARG1$参数是传给nrpe daemon执行的检测命令,之前说过了它必须是nrpe.cfg中所定义的那5条命令中的其中一条.在services.cfg中使用check_nrpe的时候要用!带上这个参数
下面就可以在services.cfg中定义对dbpi主机cpu负载的监控
define service{
        host_name               dbpi
被监控的主机名,这里注意必须是linux且运行着nrpe,而且必须是hosts.cfg中定义的
        service_description     check-load
        监控项目的名称
        check_command           check_nrpe!check_load
        监控命令是check_nrpe,是在commands.cfg中定义的,带的参数是check_load,是在nrpe.cfg中定义的
        max_check_attempts      5
        normal_check_interval   3
        retry_check_interval    2
        check_period            24x7
        notification_interval   10
        notification_period     24x7
        notification_options    w,u,c,r
        contact_groups          sagroup
        }
像这样将其余四个监控项目加进来.
之前我们说过了,今天还有一个任务是要监控dbpi的swap使用情况.但是很遗憾,在nrpe.cfg中默认没有定义这个监控功能的命令.怎么办?手动在nrpe.cfg中添加,也就是自定义NRPE命令.
现在我们要监控swap分区,如果空闲空间小于20%则为警告状态—warning;如果小于10%则为严重状态—critical.我们可以查得需要使用check_swap插件,完整的命令行应该是下面这样.
/usr/local/nagios/libexec/check_swap -w 20% -c 10%
在被监控机上增加check_swap命令的定义
vi /usr/local/nagios/etc/nrpe.cfg
增加下面这一行
command[check_swap]=/usr/local/nagios/libexec/check_swap -w 20% -c 10%
我们知道check_swap现在就可以作为check_nrpe的-c的参数使用了
修改了配置文件,当然要重启.但是
如果你是以独立的daemon运行的nrpe,那么需要手动重启.
如果你是在xinetd或者inetd下面运行的,则不需要.
由于我们是xinetd下运行的,所以不需要重启服务
在监控机上增加这个监控项目
define service{
        host_name               dbpi
        service_description     check-swap
        check_command           check_nrpe!check_swap
        max_check_attempts      5
        normal_check_interval   3
        retry_check_interval    2
        check_period            24x7
        notification_interval   10
        notification_period     24x7
        notification_options    w,u,c,r
        contact_groups          sagroup
        }
所有的配置文件已经修改好了,现在重启nagios.杀掉nagios进程,然后再重启.等上一会你就可以看到下面这个画面了
在Nginx中搭建Nagios监控平台
基本上nagios的主要功能就有这些,nagios的使用关键在于如何活用那些丰富的插件.nagios可以说是一个对于linux/unix环境支持十分好的程序.对于被监控主机是windows系列相关的文章比较少.我就专门花一章来讲述.
有了下一篇,大家就可以功德圆满了.
写到这里,有几个我在安装和使用的几个小知识点,也可以说是小问题附在此处,欢迎大家批评指教.一般的附录都是在文章最后,可下一篇是windows相关了,与我要说的这几个问题没什么联系正所谓打铁趁热,我就在这里一气呵成,大家也容易看.
附录:
1.重启nagios的方法
之前我说重启nagios的时候都是用的杀进程的方式,其实也可以不这么做.如果在安装nagios的时候安装了启动脚本就可以使用/etc/init.d/nagios restart 还可以带的参数有stop, start,status
如果报错了,有可能是脚本里面的路径设置错误,解决办法
vi /etc/init.d/nagios
将prefix=/usr/local/nagiosaa改为安装的目录/etc/init.d/nagios
注:在nagios安装的时候说是将脚本安装到了/etc/rc.d/init.d,其实这和/etc/init.d是一个目录
2.不以xinetd的方式运行nrpe
因为我们按照nrpe的安装文档安装下来,nrpe是在xinetd下面运行的,个人比较喜欢像nagios那样以单独的daemon来运行.这样比较好控制.
方法:
编辑 /etc/services将nrpe注释掉
# Local services
#nrpe           5666/tcp                        # nrpe
编辑 nrpe.cfg,增加监控主机的地址
# NOTE: This option is ignored if NRPE is running under either inetd or xinetd
allowed_hosts=127.0.0.1,192.168.0.111
注意两个地址以逗号隔开
以单独的daemon启动nrpe
[root@dbpi etc]# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
查看
[root@dbpi etc]# ps -ef|grep nrpe
nagios   22125     1  0 14:04 ?        00:00:00 [nrpe]
[root@dbpi nagios]# netstat -an|grep 5666
tcp        0      0 0.0.0.0:5666            0.0.0.0:*               LISTEN  
说明已经正常启动了
在/etc/rc.d/rc.local里面加入下面一行就实现开机启动nrpe了
/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg –d
同理要开机运行nagios就在/etc/rc.d/rc.local里面增加下面这行
/usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg
3.有关于check_load的用法及意义
这个插件是用来检测系统当前的cpu负载,使用的方法为
check_load [-r] -w WLOAD1,WLOAD5,WLOAD15 -c CLOAD1,CLOAD5,CLOAD15
在unix里面负载的均值通常表示是1分钟,5分钟,15分钟内平均有多少进程处于等待状态.
例如check_load -w 15,10,5 -c 30,25,20这个命令的意义如下
当1分钟多于15个进程等待,5分钟多于10个,15分钟多于5个则为warning状态
当1分钟多于30个进程等待,5分钟多于25个,15分钟多于20个则为critical状态

C、Nagios被控端安装配置

1、创建Nagios用户及组

建立Nagios账号

1
/usr/sbin/useradd -m -s /sbin/nologin nagios

2、编译并安装Nagios插件

1
2
3
4
5
6
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
cd ..            //如果安装出现错误可看日志中解决办法

验证程序是否被正确安装:

1
ls /usr/local/nagios/libexec

显示安装的插件文件,即所有的插件都安装在libexec这个目录下。

3、安装NRPE

1
2
3
4
5
6
7
8
tar zxvf nrpe-2.12.tar.gz
cd nrpe-2.12
./configure
make all
make install-plugin
make install-daemon
make install-daemon-config
cd ..       //如果安装出现错误可看日志中解决办法

4、启动NRPE

1
/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d

验证NRPE是否正确安装

1
/usr/local/nagios/libexec/check_nrpe -H localhost

注:如果成功,会返回NRPE的版本号。

5、修改NRPE配置文件,让监控主机可以访问被监控主机的NRPE。

缺省NRPE配置文件中只允许本机访问NRPE的Daemon

1
2
3
4
vi /usr/local/nagios/etc/nrpe.cfg
#缺省为127.0.0.1,只能本机访问
allowed_hosts=127.0.0.1,192.168.0.140监控端ip

6、重启nrpe的方法

1
2
killall nrpe
/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
添加一台监控机,监控主机上操作:
在监控机操作/usr/local/nagios/etc/objects/commands.cfg 添加nrpe的定义
# 'check_nrpe ' command definition
define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}
监控主机上操作:
增加如下文件的目的为实时更新相关服务监控信息(数据显示的比较慢)
 
增加192.168.0.104.cfg 为要监控主机的主机信息和监控服务 192.168.0.104为被监控端ip
 
在/usr/local/nagios/etc/objects/ 目录下新建文件:192.168.0.104.cfg
把192.168.0.104.cfg添加到nagios主机:
# echo "cfg_file=/usr/local/nagios/etc/objects/1192.168.0.104.cfg" >>  /usr/local/nagios/etc/nagios.cfg
 vi /usr/local/nagios/etc/nagios.cfg
cfg_file=/usr/local/nagios/etc/objects/commands.cfg
cfg_file=/usr/local/nagios/etc/objects/contacts.cfg
cfg_file=/usr/local/nagios/etc/objects/timeperiods.cfg
cfg_file=/usr/local/nagios/etc/objects/templates.cfg
#cfg-file=/usr/local/nagios/etc/objects/hosts.cfg
#cfg-file=/usr/local/nagios/etc/objects/services.cfg
# Definitions for monitoring the local (Linux) host
cfg_file=/usr/local/nagios/etc/objects/localhost.cfg
cfg_file=/usr/local/nagios/etc/objects/192.168.0.104.cfg
# Definitions for monitoring a Windows machine
如果要再添加新机器方法炮制就可以了
vi /usr/local/nagios/etc/object/192.168.0.104.cfg
define host{
        use linux-server
        host_name 104
        alias test
        address 192.168.0.104
}
define service{
        use                             generic-service         ; Name of service template to use
        host_name                       104
        service_description             PING
        check_command                   check_nrpe!check_ping
        }
define service{
        use generic-service
        host_name 104
        service_description CPU Load
        check_command check_nrpe!check_load
}
define service{
        use generic-service
        host_name 104
        service_description Current Users
         check_command    check_nrpe!check_users
}
define service{
        use generic-service
        host_name 104
        service_description Disk Free Space /
        check_command check_nrpe!check_disk_root
}
define service{
        use generic-service
        host_name 104
        service_description Total Processes
        check_command check_nrpe!check_total_procs
}
define service{
        use generic-service
        host_name 104
        service_description Zombie Processes
        check_command check_nrpe!check_zombie_procs
}
define service{
        use generic-service
        host_name 104
        service_description Swap Usage
        check_command      check_nrpe!check_swap
 }
define service{
        use     generic-service
        host_name       104
        service_description     data1
        check_command          check_nrpe!check_disk_data1
        max_check_attempts 5
        normal_check_interval 1
}
重启监控机nagios
# /etc/init.d/nagios restart
检查服务端配置文件是否正确(包括你添加的192.168.0.104.cfg文件中的错误)
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg 
在被监控主机上设置如下:
vi /usr/local/nagios/etc/nrpe.cfg
allowed_hosts=127.0.0.1,192.168.0.140
command[check_users]=/usr/local/nagios/libexec/check_users -w 1 -c 1
command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20
#command[check_hda1]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/hda1
command[check_zombie_procs]=/usr/local/nagios/libexec/check_procs -w 5 -c 10 -s Z
command[check_total_procs]=/usr/local/nagios/libexec/check_procs -w 150 -c 200
command[check_swap]=/usr/local/nagios/libexec/check_swap -w 30 -c 20
command[check_disk_root]=/usr/local/nagios/libexec/check_disk -w 20 -c 10 /
command[check_disk_data1]=/usr/local/nagios/libexec/check_disk -w 20 -c 10 /data1
command[check_ping]=/usr/local/nagios/libexec/check_ping -w 100.0,20%  -c 500.0,60%
killall nrpe

/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d,

在监控主机上执行(执行此条目的为强制更新 相关服务数据)
/usr/local/nagios/libexec/check_nrpe -H  被监控IP -c 执行的监控服务(被监控主机libexec下的,也就是被监控主机/usr/local/nagios/etc/nrpe.cfg中所定义的监控服务)
执行一条 nagios服务  就会显示一条

要监控几个服务 就执行几个服务 
 
服务端:
监控端需要配置被监控端的配置文件,其中包括 host和services,如192.168.0.104.cfg
commands.cfg中配置check_nrpe使用命令,这个命令用于连接被监控端的nrpe.cfg
被监控端:
首先 libexec下要有nrpe.cfg中定义服务执行的插件,

原理:
服务器端加载被监控端的配置文件,通过用check_nrpe去连接被监控端的nrpe.cfg,被监控端执行nrpe.cfg中服务命令执行格式,
 例如:
服务端添加被监控端的配置文件
     define host{

        use linux-server
        host_name 104
        alias test
        address 192.168.0.104
}
define service{
        use generic-service
        host_name 104
        service_description Swap Usage
        check_command      check_nrpe!check_swap 

}                  
被监控端:

nrpe.cfg

command[check_swap]=/usr/local/nagios/libexec/check_swap -w 30 -c 20   

三、参考文档

http://www.google.com
http://www.linuxidc.com/Linux/2011-05/36092.htm
http://bbs.linuxtone.org/forum.php?mod=viewthread&tid=7404&page=1
http://www.xtgly.com/2010/09/21/centos-5-5-nginx-nagios监控端和被控端安装配置指南.htm
http://www.xtgly.com/2010/09/20/nginx-fastcgi-perl-pl、cgi支持.htm

原文出处:http://www.mike.org.cn/articles/nginx-install-nagios-monitor-platform/

http://www.open-open.com/lib/view/1328680567780