使用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|linuxjishu/13830.html target=_blank class=infotextkey>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
添加:
最后,在监控端定制监控服务(service.cfg)即可开始监控服务了。