最近有个下有通道老出问题一个星期两次有次还是周末,这严重影响了公司的业务和形象,同时也影响了我个人的形象,于是决定监控这条通道!
对于这个通道做的事被动调用文件,同时载入日志,由于这个通道的数据量比较大,在数据库里的形式和其他通道有所区别,于是决定采取监控日志的方法,
php里面有个filectime()和filemtime()函数可以获取文件的最近更改时间,可惜不支持远程,我监控程序都跑在备份服务器上的.
于是只好使用fsockopen()函数打开后获取HEAD信息里面的Last modified信息
知道这个思路之后就很简单了
封装成一个函数如下:
- //check log_file status
//@param $logfile is the path in the master server. eg.'syncsms/log/sync_***_****.txt'
//@param $interTime is the max time betwin the file last change time and now.
//author:zhwq1986@hotmail.com
//date:2009/11/9
function getLogStat($host,$logfile,$interTime = 3600)
{
$fp = fsockopen($host, 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)
/n";
return array('flag'=>false,'content'=>"connect error");
} else {
$out = "HEAD /".$logfile." HTTP/1.1/r/n";
$out .= "Host: $host/r/n";
$out .= "Connection: Close/r/n/r/n";
fwrite($fp, $out);
while (!feof($fp)) {
$target = fgets($fp, 128);
if(preg_match('/(?<=/bLast-Modified:).+/i',$target,$match))
$lastTime = strtotime($match[0]);
if(preg_match('/(?<=/bHTTP//1/.1).+/i',$target,$match))
$return_status = $match[0];
}
fclose($fp);
}
$minute = $interTime/60;
if(($lastTime+$interTime)
return array('flag'=>false,'content'=>"the channel is no change in $minute minute!",'status'=>$return_status);
else
return array('flag'=>true,'content'=>'everything is ok!');
}