nagios监控应用的进程。为什么要监控进程呢?这是因为对于一些应用,它通过程序本身来进行通讯,既不开启端口也没有输出,这让我们熟悉了nagios监控端口和输出的人无从下手,只好监控应用的进程了。在这儿我以keepalived为例,整理一下监控进程的方法。
我采用的是nrpe的方法进行的监控。当然,也可以采用nagios自己定义的command来进行监控。不过自定义command监控就涉及到了监控机与被控机连接信任的问题。多了几个配置步骤,而且使用自定义command的时候还要传递参数,稍微反锁了一点。有现成的nrpe简单的多。做法如下:
1、被监控机
编辑nrpe配置文件:
- vi /usr/local/nagios/etc/nrpe.cfg
在文件末尾添加:
- command[check_keepalived]=/usr/local/nagios/libexec/check_keepalived
在nagios插件目录下创建脚本文件:
- vi /usr/local/nagios/libexec/check_keepalived
添加如下内容:
- #!/bin/sh
- KPD=`ps aux | grep 'keepalived -D' |grep -v grep |wc -l`
- if [ $KPD != 2 ] //进程数的设定以自己系统当中的数量为准
- then
- echo "Critical!Keepalived process count is $KPD"
- exit 2
- fi
- echo "OK . Keepalived process count is $KPD"
- exit 0
保存退出。
更改属主和属组,增加可执行权限:
- chown nagios:nagios /usr/local/nagios/libexec/check_keepalived
- chmod +x /usr/local/nagios/libexec/check_keepalived
重启nrpe:
- killall -9 nrpe
- ps -ef | grep nrpe
- /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
- ps -ef | grep nrpe
2、nagios监控机设置
定义主机组---根据个人需求定义
- vi /usr/local/nagios/etc/objects/yidongIDC/hosts.cfg //这是我自己定义的配置文件,刚安装的nagios没有这个文件。需要自己创建,并且将配置文件添加到nagios.cfg主配置文件中。具体写法为:cfg_file=[配置文件全路径]。
添加如下内容(members中涉及到的主机已经在上面定义了):
- define hostgroup{
- hostgroup_name keepalived-servers
- alias The servers who have keepalived service
- members web110,web111,localhost,web115
- }
创建监控keepalived服务的配置文件
- vi /usr/local/nagios/etc/objects/yidongIDC/keepalived_monitor.cfg
添加如下内容:
- define service{
- use generic-service
- hostgroup_name keepalived-servers
- service_description CHECK_KEEPALIVED
- is_volatile 0
- max_check_attempts 2
- normal_check_interval 2
- retry_check_interval 1
- check_command check_nrpe!check_keepalived
- }
更该文件属主属组,更改权限
- chown nagios:nagios /usr/local/nagios/etc/objects/yidongIDC/keepalived_monitor.cfg
- chmod 755 /usr/local/nagios/etc/objects/yidongIDC/keepalived_monitor.cfg
重置nagios
- /etc/init.d/nagios reload
至此配置完成。监控效果如下图: