redhat--nagios插件--check_traffic.sh

****在被监控主机安装nrpe****

(1)在被监控主机上,增加用户和密码

useradd nagios

passwd nagios

(2)安装nagios插件

tar zxf nagios-plugins-1.4.15.tar.gz(nagios-plugins-1.4.15放任意位置,一般习惯放在/opt目录下)

cd nagios-plugins-1.4.15

./configure --prefix=/usr/local/nagios

make

make install

chown nagios:nagios /usr/local/nagios/

chown nagios:nagios /usr/local/nagios/libexec/ -R

(3.1)安装nrpe

tar zxf nrpe-2.12.tar.gz(nrpe-2.12.tar.gz放在任意位置,一般习惯放在/opt目录下)

cd nrpe-2.12

./configure(会默认安装在/usr/local/nagios目录下)

make all

make install-plugin

make install-daemon

make install-daemon-config

(3.2)修改nrpe.conf文件

修改以下内容,将nagios监控主机的ip添加到allowed_hosts字段

allowed_hosts=127.0.0.1,监控主机Ip

否则在监控主机上运行/usr/local/nagios/libexec/check_nrpe -H 远端被监控机ip

时会报以下错误CHECK_NRPE: Error - Could not complete SSL handshake.

(3.3)安装xinetd脚本

在nrpe-2.12目录下执行

make install-xinetd

编辑xinetd脚本

vi /etc/xinetd.d/nrpe

在only_from后面增加监控主机的地址,以空格间隔

only_from       = 127.0.0.1,监控主机Ip

编辑/etc/services文件,增加nrpe服务

vi /etc/services

# Local services

nrpe            5666/tcp                        # nrpe

(3.4)重启xinetd服务

/etc/init.d/xinetd restart

(3.5)启动nrpe服务

检查nrpe服务是否启动

#ps aux|grep nrpe

如果nrpe没有启动,启动nrpe服务

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

再次查看nrpe是否已经启动,端口是否正常监听

netstat -at|grep nrpe

tcp        0      0 *:nrpe                      *:*                      LISTEN 

netstat -ant | grep 5666

tcp        0      0 0.0.0.0:5666                0.0.0.0:*                LISTEN

(3.6)测试nrpe是否正常工作

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

NRPE v2.12(正确的返回值)

(3.7)查看nrpe的监控命令

cd /usr/local/nagios/etc/

vi nrpe.cfg

找到可以监控的命令

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

(3.8)在监控主机上检查nrpe是否可以练到远端的nrpe

在监控主机上运行

#/usr/local/nagios/libexec/check_nrpe -H 远端监控主机ip

NRPE v2.12(正确的返回值)

这样远端nrpe安装成功,并保证监控主机可以连接远端的nrpe

****安装check_traffic插件****

 (1)下载chech_traffic.sh

chech_traffic.sh并不是nagios-plugins自带插件,所以需要自行下载

下载后,将chech_traffic.sh放在/usr/local/nagios/libexec/chech_traffic.sh

修改chech_traffic.sh的权限为777,用户及用户组为nagios

chmod 777 chech_traffic.sh

choen nagios:nagios chech_traffic.sh

(2)安装并配置chech_traffic.sh的依赖服务

脚本准本好以后

在执行

#/usr/local/nagios/libexec/chech_traffic.sh  -V 2c -C public -H ***.***.***.*** -I 2 -w 200,300 -c 400,500 -K -B

提示以下错误:

which: no snmpwalk in (/opt/jdk1.6.0_25/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/dell/srvadmin/bin:/opt/dell/srvadmin/sbin:/root/bin)

Can not found command snmpwalk in you system PATH: /opt/jdk1.6.0_25/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/dell/srvadmin/bin:/opt/dell/srvadmin/sbin:/root/bin, pleas check it

是因为chech_traffic.sh依赖snmp服务

(2.1)安装snmp

如果/etc/下已有snmp文件夹,说明snmp已经安装

若果没有,需要安装snmp

因为是redhat系统,首先选择rpm安装

下载对应版本的snmp

rpm -ivh net-snmp-5.3.2.2-7.el5.x86_64.rpm

提示:libsensors.so.3()(64bit) is needed by package 1:net-snmp-5.3.2.2-7.el5.x86_64

依赖libsensors.so.3()(依赖这个库是因为snmp来依赖其他的一些服务

选择用yum安装,解决依赖库的问题

yum install net-snmp* -y

得知安装net-snmp依赖以下:

 

lm_sensors*;beecrypt*;elfutils*

下载相应的rpm包,或是使用yum安装以上,再使用

rpm -ivh net-snmp-5.3.2.2-7.el5.x86_64.rpm 安装,即可安装成功

安装成功后,会有/etc/snmp文件

(2.2)配置snmp.conf

修改以下:

# group context sec.model sec.level prefix read write notif 

access notConfigGroup "" any noauth exact mib2 none none 

##去掉下面此行行首注释#。 

view mib2 included .iso.org.dod.internet.mgmt.mib-2 fc 

备注:有些情况是将snmp.conf里面的以下字段也进行了修改,添加了被监控主机ip

# sec.name source community
com2sec notConfigUser 被监控机ip public

修改了以后再被监控上运行

 

/usr/local/nagios/libexec/chech_traffic.sh  -V 2c -C public -H ***.***.***.*** -L 

提示noresponse form ....

于是就将com2sec notConfigUser 被监控机ip public的被监控ip去掉以后,就可以正常运行了

(3)执行check_traffic.sh

/usr/local/nagios/libexec/chech_traffic.sh  -V 2c -C public -H ***.***.***.*** -I 2 -w 200,300 -c 400,500 -K -B

-I 2 监控的网卡(2是网卡多对应的index值)

-w 200,300 warning的范围

-c 400,500 critical的范围

-K -B是单位

-H 被监测流量的主机ip

使用/usr/local/nagios/libexec/chech_traffic.sh  -V 2c -C public -H ***.***.***.*** -L

可以查看主机的网卡对应的index值

Interface index 1 orrresponding to lo

Interface index 2 orrresponding to eth0

Interface index 3 orrresponding to eth1

使用ifconfig,如果你监控的网卡是eth1,那么-I的参数就是3,即,-I 3

如果执行

#/usr/local/nagios/libexec/chech_traffic.sh  -V 2c -C public -H ***.***.***.*** -I 2 -w 200,300 -c 400,500 -K -B

OK - It's the first time for this plugins run. We'll get the data from the next time

说明check_traffic.sh可以在被测机上正确执行了

(4)将check_traffic.sh命令添加到nrpe.conf中

command[check_traffic]=/usr/local/nagios/libexec/check_traffic.sh -V 2c -C public -H ***.***.***.*** -I 2 -w 200,300 -c 400,500 -K -B

保存,重启nrpe服务

在被监控机上执行

/usr/local/nagios/libexec/check_nrpe -H ***.***.***.*** -c check_traffic

可能会提示:

SERVICE ALERT: ***.***.***.***;;traffic;UNKNOWN;SOFT;3;Unknown - Read or Write File /var/tmp/check_traffic_***.***.***.***;_11.hist_dat_root__32 Error with user uid=517(nagios) gid=517(nagios) groups=517(nagios)

是因为以非nagios用户身份,手动测试执行过该脚本(也就是command[check_traffic]=/usr/local/nagios/libexec/check_traffic.sh -V 2c -C public -H ***.***.***.*** -I 2 -w 200,300 -c 400,500 -K -B这个操作),请在正式使用该脚本前,删除/var/tmp下对应测试生成的/var/tmp/check_traffic_${Host}_${Interface}.hist_dat文件,否则会造成nagios用户无法读写该文件的错误

删除/var/tmp/check_traffic_${Host}_${Interface}.hist_dat文件后

再次执行

/usr/local/nagios/libexec/check_nrpe -H localhost -c check_traffic

可以看见流量的统计结果

(5)在监控主机上验证的被监控上的check_traffic.sh是否可以成功执行

在监控主机上执行/usr/local/nagios/libexec/check_nrpe -H ***.***.***.*** -c check_traffic

得到流量统计结果,可以将check_traffic配到监控主机的服务里面,在nagios上可以监控到远端主机的流量了

以上check_traffic.sh只能监控到某一个网卡的全部流量,一旦nagios流量过高,出现警告,如何排查那个进程所占用的流量

可以使用nethogs工具,可以监控某块网卡上每个进程的流量,可以使用nethogs工具自行写个脚本,监控你所关心的进程流量,配合check_traffic.sh插件使用

 

 

 

 

posted on 2013-07-04 13:22  龙沙宝石  阅读(3680)  评论(0编辑  收藏  举报

导航