wsaer 发表于 2015-11-22 16:24:02

用php自定义 nagios监控插件

  用来监控是哪个进程触发了指定预警的cpu占用率

#!/usr/local/php5/bin/php
<?php
//exec('ps -eo pcpu,pid,user,command | sort -k 1 -r | head -2',$info);
exec('top -n 1 -b | head -8',$tinfo);
$match = preg_split('/[\s\t]+/',trim($tinfo));
$pid = $match;
$user = $match;
$cpu = $match;
$mem = $match;
exec(&quot;ps -p {$pid} -o command&quot;,$info);
$cmd = $info;
$w = $argv;
$c = $argv;

if($cpu >= $c){
$stat = &quot;CRITICAL&quot;;
$code = 2;
}elseif($cpu >= $w){
$stat = &quot;WARNING&quot;;
$code = 1;
}else{
$stat = &quot;OK&quot;;
$code = 0;
}
echo &quot;WHOISTOP {$stat}: CPU({$cpu}%),MEM({$mem}),PID({$pid}),USER({$user}),CMD({$cmd})&quot;;
exit($code);
  


  使用:/path/to/check_whoistop 30 50



注意exit($code), 是nagios要求的脚本返回码,分别为0:ok, 1:warning,2,CRITICAL,3,unkown
页: [1]
查看完整版本: 用php自定义 nagios监控插件