nagios进程监控check_ps.sh
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目录。具体用法如下:
# ./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告警。也可以只对输出值做监控,不做告警。如下:
# /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 = "--vertical-label "percent" -u 100 -l 0 -r --title "CPU/Memory Usage for $hostname / $servicedesc" ";
$opt = "--vertical-label "minutes" -u 100 -l 0 -r --title "cputime for $hostname / $servicedesc" ";
$def ="DEF:cpu=$rrdfile:$DS:AVERAGE " ;
$def .="DEF:memory=$rrdfile:$DS:AVERAGE " ;
$def ="DEF:cputime=$rrdfile:$DS:AVERAGE " ; #该句前面没有'.'连接符,官方上传的有,会报错
$def .= "COMMENT:"\t\t\tLAST\t\t\tAVERAGE\t\t\tMAX\n" " ;
$def .= "COMMENT:"\t\t\tLAST\t\t\tAVERAGE\t\t\tMAX\n" " ;
$def .= "LINE2:cpu#E80C3E:"CPU\t\t" " ;
$def .= "GPRINT:cpu:LAST:"%6.2lf %%\t\t" " ;
$def .= "GPRINT:cpu:AVERAGE:"%6.2lf \t\t" " ;
$def .= "GPRINT:cpu:MAX:"%6.2lf \n" " ;
$def .= "LINE2:memory#008000:"Memory\t" " ;
$def .= "GPRINT:memory:LAST:"%6.2lf %%\t\t" " ;
$def .= "GPRINT:memory:AVERAGE:"%6.2lf \t\t" " ;
$def .= "GPRINT:memory:MAX:"%6.2lf \n" " ;
$def .= "AREA:cputime#E80C3E:"CPUTime\t" " ;
$def .= "GPRINT:cputime:LAST:"%6.2lf min\t\t" " ;
$def .= "GPRINT:cputime:AVERAGE:"%6.2lf min\t\t" " ;
$def .= "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 127320.00.0 103244 796 pts/1 S+ 10:30 0:00 _ grep nginx
root 313020.00.770836 27992 ? Ss Oct17 0:07 nginx: master process /App/nginx/sbin/nginx -c /App/nginx/conf/nginx.conf
www 326102.61.496804 55452 ? S Dec0788:56_ nginx: worker process
www 326112.51.496804 55508 ? S Dec0787:17_ nginx: worker process
www 326122.81.496276 55108 ? S Dec0796:41_ nginx: worker process
www 326132.71.496980 55800 ? S Dec0792: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求和操作,在此不再赘述。
页:
[1]