当前位置: > Linux服务器 > 监控工具 >

使用nagios监控某进程的运行状态

时间:2014-10-26 01:22来源:linux.it.net.cn 作者:it

使用nagios监控被监控服务器上的某一程序,脚本通过查看进程状态返回状态值,并传送给nagios,来达到监控目的。
nagios每个插件监控会返回以下状态值,我只用到了 0 、1、 2

 

复制代码代码如下:

#!/bin/bash -x
# Exit values:
# ------------
#    0  OK
#    1  Warning
#    2  Cirital
#    3  Unknown
#    Others Unknown

##-------
dfs_process=`ps -ef | grep './dfs client' | grep -v grep`
if [ -z "$dfs_process" ];then
        echo "Critical the program of gather is down !!!"
        exit 2
fi

process_ppid=`echo $dfs_process|awk '{print $3}'`
process_pid=`echo $dfs_process|awk '{print $2}'`

if [ $process_ppid = 1 ];then
        echo 'The process of dfs put back'
        exit 1
else
        while [ $process_ppid != 1 ]
                do
                old_process_ppid=$process_ppid
                process_ppid=`ps -ef | grep $process_ppid | grep -v $process_pid | grep -v grep | awk '{print $3}'`
                process_pid=`ps -ef | grep $old_process_ppid| grep -v $process_pid | grep -v grep|awk '{print $2}'`
                done
        dfs_pprocess=`ps -ef | grep "$process_pid     1" | grep -v grep`
        echo "process of './dfs client' OK ! - Parent process is `echo $dfs_pprocess | awk '{print $8,$9,$10}'`"
        exit 0

fi

然后,打开被监控端nrpe的配置文件
# vi /usr/local/nagios/etc/nrpe.cfg
添加:
 

复制代码代码如下:
command[check_dfs]=/usr/local/nagios/libexec/check_dfs.sh

最后,在监控端定制监控服务(service.cfg)即可开始监控服务了。

(责任编辑:IT)
------分隔线----------------------------