nagios进程监控check_ps.sh

2013年12月9日 发表评论 阅读评论

nagios-plugins自带的有一个check_procs插件。用来做进程监控,不过该插件只能做已存在的进程fork超出多少个子进程的监控告警。而对进程是否存在不能做监控。对进程的监控可以借助check_ps.sh这个脚本去实现,而且其还可以配合pnp4nagios方便的出图。

使用方法是,打开check_ps.sh在nagios上的项目页 ,下载check_ps.sh。将其放到/usr/local/nagios/libexec目录。将check_ps.php放到/usr/local/pnp4nagios/share/templates目录。具体用法如下:

[root@jjh-cc libexec]# ./check_ps.sh -h
check_ps.sh -p firefox [-w 10] [-c 20] [-t cpu]
Options:
  -p/--process)
     You need to provide a string for which the ps output is then
     then "greped".
  -w/--warning)
     Defines a warning level for a target which is explained
     below. Default is: off
  -c/--critical)
     Defines a critical level for a target which is explained
     below. Default is: off
  -t/--target)
     A target can be defined via -t. Choose between cpu and mem.
     Default is: mem

默认告警阀值是对内存的,可以通过-t参数设置为对cpu告警。也可以只对输出值做监控,不做告警。如下:

[root@cc libexec]# /usr/local/nagios/libexec/check_ps.sh -p 'JjhControlServerMain'
OK - Process: JjhControlServerMain, User: www, CPU: 1.0%, RAM: 31.2%, Start: Nov30, CPU Time: 8049 min | 'cpu'=1.0 'memory'=31.2 'cputime'=8049

上面是监控的一个java程序及其输出。配合check_ps.php模板,输出的图样如下:

不过官方上传的模板写的有一个小错误,多了一个连接符。导致模板使用时会报错:Notice: Undefined offset: 2  。正确的模板代码如下:

<?php
$opt[1] = "--vertical-label "percent" -u 100 -l 0 -r --title "CPU/Memory Usage for $hostname / $servicedesc" ";
$opt[2] = "--vertical-label "minutes" -u 100 -l 0 -r --title "cputime for $hostname / $servicedesc" ";
$def[1] =  "DEF:cpu=$rrdfile:$DS[1]:AVERAGE " ;
$def[1] .=  "DEF:memory=$rrdfile:$DS[2]:AVERAGE " ;
$def[2] =  "DEF:cputime=$rrdfile:$DS[3]:AVERAGE " ;      #该句前面没有'.'连接符,官方上传的有,会报错
$def[1] .= "COMMENT:"\t\t\tLAST\t\t\tAVERAGE\t\t\tMAX\n" " ;
$def[2] .= "COMMENT:"\t\t\tLAST\t\t\tAVERAGE\t\t\tMAX\n" " ;
$def[1] .= "LINE2:cpu#E80C3E:"CPU\t\t" " ;
$def[1] .= "GPRINT:cpu:LAST:"%6.2lf %%\t\t" " ;
$def[1] .= "GPRINT:cpu:AVERAGE:"%6.2lf \t\t" " ;
$def[1] .= "GPRINT:cpu:MAX:"%6.2lf \n" " ;
$def[1] .= "LINE2:memory#008000:"Memory\t" " ;
$def[1] .= "GPRINT:memory:LAST:"%6.2lf %%\t\t" " ;
$def[1] .= "GPRINT:memory:AVERAGE:"%6.2lf \t\t" " ;
$def[1] .= "GPRINT:memory:MAX:"%6.2lf \n" " ;
$def[2] .= "AREA:cputime#E80C3E:"CPUTime\t" " ;
$def[2] .= "GPRINT:cputime:LAST:"%6.2lf min\t\t" " ;
$def[2] .= "GPRINT:cputime:AVERAGE:"%6.2lf min\t\t" " ;
$def[2] .= "GPRINT:cputime:MAX:"%6.2lf min\n" " ;
?>

该脚本有一个缺点:对于多进程的进程名的监控输出结果不准确。如nginx进程,其可能有多个子进程,我们想要输出所有nginx进程一共使用的CPU和MEM的多少,该脚本就不对了。具体如下:

root@test:#./check_ps.sh -p nginx
OK - Process: nginx, User: root, CPU: 0.0%, RAM: 0.7%, Start: Oct17, CPU Time: 7 min | 'cpu'=0.0 'memory'=0.7 'cputime'=7
root@test:#ps auxf|grep nginx
root     12732  0.0  0.0 103244   796 pts/1    S+   10:30   0:00                      _ grep nginx
root     31302  0.0  0.7  70836 27992 ?        Ss   Oct17   0:07 nginx: master process /App/nginx/sbin/nginx -c /App/nginx/conf/nginx.conf
www      32610  2.6  1.4  96804 55452 ?        S    Dec07  88:56  _ nginx: worker process
www      32611  2.5  1.4  96804 55508 ?        S    Dec07  87:17  _ nginx: worker process
www      32612  2.8  1.4  96276 55108 ?        S    Dec07  96:41  _ nginx: worker process
www      32613  2.7  1.4  96980 55800 ?        S    Dec07  92:02  _ nginx: worker process
root@test:#./check_ps.sh -p 'nginx: worker process'
OK - Process: nginx: worker process, User: www, CPU: 2.6%, RAM: 1.4%, Start: Dec07, CPU Time: 5338 min | 'cpu'=2.6 'memory'=1.4 'cputime'=5338

不过既然是一个shell脚本,想要实现上面对类nginx这样的情况的监控,稍加修改脚本即可实现。只不过多一个sum求和操作,在此不再赘述。




本站的发展离不开您的资助,金额随意,欢迎来赏!

You can donate through PayPal.
My paypal id: itybku@139.com
Paypal page: https://www.paypal.me/361way

分类: 开源软件 标签:
  1. 本文目前尚无任何评论.
  1. 本文目前尚无任何 trackbacks 和 pingbacks.