Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2112064
  • 博文数量: 317
  • 博客积分: 5670
  • 博客等级: 大校
  • 技术积分: 3677
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-10 17:51
文章分类

全部博文(317)

文章存档

2016年(2)

2015年(44)

2014年(68)

2013年(42)

2012年(23)

2011年(51)

2010年(67)

2009年(17)

2008年(3)

分类: 系统运维

2010-01-29 10:44:10

9、定义监控的项目,也叫服务,创建services.cfg
vi  /usr/local/nagios/etc/services.cfg
#监控主机是否存活
define service{
        host_name               nagios-server
        service_description     check-host-alive
        check_command           check-host-alive
        max_check_attempts      5
        normal_check_interval   5
        retry_check_interval    2
        check_period            24x7
        notification_interval   10
        notification_period     24x7
        notification_options    w,u,c,r
        contact_groups          sagroup
        }
#监控主机的web服务
define service{
        host_name               nagios-server
        service_description     check_tcp 80
        check_period            24x7
        max_check_attempts      4
        normal_check_interval   3
        retry_check_interval    2
        contact_groups          sagroup
        notification_interval   10
        notification_period     24x7
        notification_options    w,u,c,r
        check_command           check_tcp!80
        }
#监控主机的cpu负载情况
define service{
        host_name               nagios-server
        service_description     cpu load
        check_command           check_nrpe!check_load
        check_period            24x7
        max_check_attempts      4
        normal_check_interval   3
        retry_check_interval    2
        contact_groups          sagroup
        notification_interval   10
        notification_period     24x7
        notification_options    w,u,c,r
        }
#监控主机的进程数
define service{
        host_name               nagios-server
        service_description     total-procs
        check_command           check_nrpe!check_total_procs
        check_period            24x7
        max_check_attempts      4
        normal_check_interval   3
        retry_check_interval    2
        contact_groups          sagroup
        notification_interval   10
        notification_period     24x7
        notification_options    w,u,c,r
        }
 
说明:
host_name:必须是主机配置文件hosts.cfg中定义的主机。
check_command:在commands.cfg文件中定义或在nrpe.cfg里面定义的命令;
max_check_attempts: 最大重试次数,一般设置为4次左右;
normal_check_interval retry_check_interval检查间隔的单位是分钟。
notification_interval  通知间隔指探测到故障后,每隔多长时间发送一次报警信息,单位是分钟。
notification_options通知选项跟联系人配置文件相同。
contact_groups:配置文件contactgroup.cfg定义的组名称。
注意:check_command选项后面跟的命令一定要在commands.cfg里有定义;
如果要监控其他的主机的信息,可以通过复制并修改想应的选项来进行添加;
通过复制添加下面这两台服务器的监控项目:
win2003linux
 
四、安装nrpe
tar  xvf  nrpe-2.12.tar.gz
cd  nrpe-2.12.
./configure  --prefix=/usr/local/nrpe
make
make install
#复制文件,因为在nrpe安装目录/usr/local/nrpe/libexec里只有cneck_nrpe这一个文件,而在nagios/libexec里却没有,还有一个就是nrpe.cfg文件里面默认定义的那几个命令后面的路径是放在/usr/local/nrpe/libexec的目录里面,也要把那几个文件复制过来,如果不复制过来的话必须要修改nrpe.cfg里面定义的命令的路径,免得在services.cfg里面定义check_command时提示找不到命令;现在把下面的文件复制过来:
   cp /usr/local/nrpe/libexec/check_nrpe  /usr/local/nagios/libexec
   cp /usr/local/nagios/libexec/check_disk  /usr/local/nrpe/libexec
   cp /usr/local/nagios/libexec/check_load  /usr/local/nrpe/libexec
   cp /usr/local/nagios/libexec/check_ping  /usr/local/nrpe/libexec
   cp /usr/local/nagios/libexec/check_procs  /usr/local/nrpe/libexec
cp /usr/local/nagios/libexec/check_users  /usr/local/nrpe/libexec
 
#修改nrpe配置文件,只把改过的地方写出来
vi  /usr/local/nrpe/etc/nrpe.cfg
server_address=192.168.0.10      //以单独的守护进程运行
allowed_hosts=127.0.0.1,192.168.0.10  //设置允许nagios监控服务器可以访问
command[check_users]=/usr/local/nrpe/libexec/check_users -w 5 -c 10
command[check_load]=/usr/local/nrpe/libexec/check_load -w 15,10,5 -c 30,25,20
#command[check_hda1]=/usr/local/nrpe/libexec/check_disk -w 20 -c 10 -p /dev/hda1   //注释掉
command[check_df]=/usr/local/nrpe/libexec/check_disk -w 20 -c 10              //添加这一行,监控整个磁盘利用率
command[check_zombie_procs]=/usr/local/nrpe/libexec/check_procs -w 5 -c 10 -s z
command[check_total_procs]=/usr/local/nrpe/libexec/check_procs -w 150 -c 200
command[check_ips]=/usr/local/nrpe/libexec/ip_conn.sh 8000 10000     //监控ip连接数
 
说明:
     command[check_users]=/usr/local/nrpe/libexec/check_users –w 5 –c 10在默认情况下check_users的插件是放在/usr/local/nrpe/libexec/目录里面,而目录里面在默认情况下是没有这一个文件的,所以说要从/usr/local/nagios/libexec/目录下拷贝一个过来;或者说的它后面的它改成:  command[check_users]=/usr/local/nagios/libexec/check_users –w 5 –c 10 这样的话就可以了,要不然在引用check_users的时候会提示没有那命令;
ps
:我这里为了方便,就是从/usr/local/nagios/libexec下把那几个文件拷贝过来;
     在上面的nrpe.cfg配置文件里面,在中括号 “ [ ] “里面部分是命令名,也就是check_nrep –c 后面可以接的内容,等号=后面的就是实际执行的插件程序的路径;从上往下分别是检测登录用户数,cpu使用率,磁盘的容量,僵尸进程,总进程,连接数;
     要是还要添加其它监控项目,不要忘记了在这里定义相应的命令;例:如果要监控主机的swap分区使用情况,当空闲空间小于20%时为警告状态,当空闲空间小于10%时为严重状态。需要在nrpe.cnf里面添加下面的命令:/usr/local/nagios/libexec/check_swap -w 20% -c 10% 如还有其它的,添加相就应的就可以了;关于命令用法可以能过/usr/local/nagios/libexec/check_swap –h这样的命令来查询;
     command[check_ips]=/usr/local/nrpe/libexec/ip_conn.sh 8000 10000 ip连接数,ip_conn.sh脚本需要自己写,下面给出脚本的内容:
#!/bin/sh
#if [ $#-ne 2 ]
#then
# echo "usage:$0 -w num1 -c num2"
#exit 3
#fi
 
 
ip_conns=`netstat -an |grep tcp |grep est |wc -l`
 
        if [ $ip_conns -lt $1 ]
        then
        echo "ok -connectcounts is $ip_conns"
        exit 0
        fi
 
        if [ $ip_conns -gt $1 -a $ip_conns -lt $2 ]
        then
        echo "warning -connectcounts is $ip_conns"
        exit 1
        fi
 
        if [ $ip_conns -gt $2 ]
        then
        echo "critical -connectcounts is $ip_conns"
        exit 2
        fi
     我在nrpe配置文件nrpe.cfg把脚本所需的两个参数写上了,因此这个脚本就不需判断两个参数输入值的情况。只要当前ip连接数大于8000,系统就发warning报警,超过10000,则发“critical”报警信息。把这个脚本放在目录/usr/local/nrpe/libexec下,并给于执行权限;
注:脚本来自田逸的《开源监控利器nagios
 
修改/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$
        }
添加check_nrpe的命令支持,要不是加的话,在”check_cmmmands   check_nrpe!check_nrpe”这样的情况下的时候,会提示没有check_nrpe这一个命令。
五、     被监控主机设置
这一部分内容参考yahoon小屋的nagios全攻略()----监控linux上的本地信息,地址是:http://yahoon.blog.51cto.com/13184/41893
nrpe的工作原理图:
       nrpe总共由两部分组成:
check_nrpe 插件,位于在监控主机上
nrpe daemon,运行在远程的linux主机上(通常就是被监控机)
按照上图,整个的监控过程如下:
nagios需要监控某个远程linux主机的服务或者资源情况时
1.nagios会运行check_nrpe这个插件,告诉它要检查什么.
2.check_nrpe插件会连接到远程的nrpe daemon,所用的方式是ssl
3.nrpe daemon会运行相应的nagios插件来执行检查
4.nrpe daemon将检查的结果返回给check_nrpe插件,插件将其递交给nagios做处理.
注意:nrpe daemon需要nagios插件安装在远程的linux主机上,否则,daemon不能做任何的监控.
 
      
1linux 主机
       1)添加用户
       #  useradd  nagios  -s /sbin/nologin
       2) 安装nagios插件
tar xvf  nagios-plugins-1.4.14.tar.gz
cd nagios-plugins-1.4.14
./configure  --prefix=/usr/local/nagios   
make
make install
修改目录权限:
chown –R  nagios:nagios  /usr/local/nagios
chown –R  nagios:nagios  /usr/local/nagios/libexec
2) 安装nrpe
tar  xvf  nrpe-2.12.tar.gz
cd  nrpe nrpe-2.12.
./configure  --prefix=/usr/local/nagios    //也把它放到nagios这一个目录
make  all
make  install-plugin    //安装check_nrpe这个插件
make install-daemon     //安装deamon
make install-daemon-config    //安装配置文件
注意:安装nrpe时,要先安装SSL这一个工具,因为监控服务器与被监控主机之间通讯时是通过SSL的方式来进行的。
3)修改nrpe.cfg配置文件
allowed_hosts=127.0.0.1,192.168.0.10    //允许监控服务器访问,中间用逗号隔开;
server_address=192.168.0.10      //以单独的守护进程运行
# 修改NRPE的监控命令,添加相应的命令;
# 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_df]=/usr/local/nagios/libexec/check_disk -w 20 -c 10
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 20% -c 10%
command[check_tcp]=/usr/local/nagios/libexec/check_tcp -p 80
4)启动nrpe服务
# /usr/local/nagios/bin/nrpe –c /usr/local/nagios/etc/nrpe.cfg -d
5) 在本机测试一下nrpe.cfg里面定义的命令有没有错误
/usr/local/nagios/libexec/check_nrpe -H localhost
/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_df
/usr/local/nagios/libexec/check_nrpe -H localhost  –c  check_zombie_procs
/usr/local/nagios/libexec/check_nrpe -H localhost  –c  check_total_procs
/usr/local/nagios/libexec/check_nrpe -H localhost  –c  check_swap
/usr/local/nagios/libexec/check_nrpe -H localhost  –c  check_tcp
6)在监控服务器上修改/usr/local/nagios/etc/services.cfg文件,添加相对应的监控项目;
2windows主机
       如果被监控主机是windows系统的话,需要安装nsclient++-0.3.6-win32.msi   这一个客户端工具;下载地址:
       它的工作原理图如下:
      
 
 
可以看到它是通过监控服务器上的check_nt这一个插件来检查WINDOWS机器的服务,当监控服务器将监控请求发给NSClient后,通过NSClient客户端检查监控项目,所有的监控都是由NSClient来完成;
       双击安装,完成后进到安装目录,我的是安装在F:\Program Files\NSClient++,用记事本打开NSC.ini文件,修改如下:
[modules]部分的所在模块前面的注释“;”去掉,只保留CheckWMI.dllRemoteConfiguration.dll这两个不用去掉注释;
[Settings] 部分找到”allowed_hosts”选项,将前面的 “;”注释去掉,并在后面加上监控服务器的IP,这里也可以写成一个子网,如:192.168.0.0/24的形式,表示所有来自这个子网的机器都可以访问。
修改后如下:allowed_hosts=127.0.0.1/32,192.168.0.10 ;在多个IP之间用逗号相隔。
[NSClient] 部分找到“port=12489”这一行,并把前面的注释去掉;这是NSClinet的默认监听端口。
 
打开CMD ,进入到NSClient的安装目录,如下:
F:\Program Files\NSClient++> dir
2009-10-10  20:54              .
2009-10-10  20:54              ..
2009-06-14  13:20           442,368 nsclient++.exe
2009-06-14  13:24           278,528 nstray.exe
2009-10-10  23:08            13,087 NSC.ini
2009-06-14  13:13            43,129 changelog.txt
2008-06-15  18:20             8,804 counters.defs
2008-06-15  18:20            18,351 license.txt
2009-10-10  20:54              modules
2009-10-10  20:54              scripts
2009-10-14  20:00            26,592 nsclient.log
               7 个文件        830,859 字节
               4 个目录  3,589,242,880 可用字节
//依次执行下面的命令:
F:\Program Files\NSClient++> nsclient++.exe  /install
e NSClient++.cpp(212) Service installation failed: Service already installed!
F:\Program Files\NSClient++> nsclient++.exe  /install SysTray   //注意大小写;
Service SysTray installed...
l NSClient++.cpp(224) Service installed!
F:\Program Files\NSClient++>nsclient++.exe /start    //启动服务
接下来在运行里面输入:services.msc 打开服务管理;
看到下图时就表明已经安装上了:
NSClinet++上面点右键属性,打开服务,如下图:服务已经启动了
点登录,在登录登录身份下面勾选允许服务与桌面交互,如下图:
 
到这里就已经在WINDOWS主机上安装好了,如果你有防火墙的话请打开TCP12489端口,否则nagios检查此服务的时候会报socket 超时错误。
这样被监控的主机就搞定了,现在回到nagios-server 监控服务器上做下面三个步骤:
1安装监控windows的插件(已经默认安装了check_nt)
2)定义命令
#'check_nt' command definition
define command{
        command_name    check_nt
        command_line    $USER1$/check_nt -H $HOSTADDRESS$ -p 12489 -v $ARG1$ $ARG2$
        }
//这一部分在3.20里面默认就已经有了,不用再添加;
3)定义要监控的项目
修改/usr/local/nagios/etc/services.cfg文件,增加windows 被监控主机的监控项目;
详情请参阅前面第三步内容;
关于check_nt的用法可以使用下面命令查看帮助:
# /usr/local/nagios/libexec/check_nt  -h
下面给出一些常用的参数:
1)监控windows服务器运行的时间
check_command           check_nt!UPTIME
2)监控Windows服务器的CPU负载,如果5分钟超过80%则是warning,如果5分钟超过90%则是critical
check_command           check_nt!CPULOAD!-l 5,80,90
3)监控Windows服务器的内存使用情况,如果超过了80%则是warning,如果超过90%则是critical.
check_command           check_nt!MEMUSE!-w 80 -c 90
4)监控Windows服务器C:\盘的使用情况,如果超过80%已经使用则是warning,超过90%则是critical
check_command           check_nt!USEDDISKSPACE!-l c -w 80 -c 90
:-l后面接的参数用来指定盘符
5)监控Windows服务器D:\盘的使用情况,如果超过80%已经使用则是warning,超过90%则是critical
check_command           check_nt!USEDDISKSPACE!-l d -w 80 -c 90
6)监控Windows服务器的W3SVC服务的状态,如果服务停止了,则是critical
check_command           check_nt!SERVICESTATE!-d SHOWALL -l W3SVC
7)监控Windows服务器的Explorer.exe进程的状态,如果进程停止了,则是critical
check_command           check_nt!PROCSTATE!-d SHOWALL -l Explorer.exe
 
这一部分被控主机设置这一部分是参考yahoon的小屋   原文:http://yahoon.blog.51cto.com/13184/41897
阅读(2323) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~