|
@@ -14,23 +14,17 @@ Base::run_util();
|
|
|
$monitor = new Monitor();
|
|
|
|
|
|
while (true) {
|
|
|
- /*$monitor->readOne();
|
|
|
- usleep(100);*/
|
|
|
$monitor->run_loop();
|
|
|
}
|
|
|
|
|
|
class Monitor
|
|
|
{
|
|
|
private $_ptr_pos = 0;
|
|
|
-
|
|
|
private $_mark = "]";
|
|
|
-
|
|
|
private $date_now;
|
|
|
-
|
|
|
private $input_fname;
|
|
|
-
|
|
|
+ private $_fsize;
|
|
|
private $_fd_input;
|
|
|
-
|
|
|
private $_fd_log;
|
|
|
|
|
|
public function __construct()
|
|
@@ -58,7 +52,7 @@ class Monitor
|
|
|
|
|
|
if (@feof($this->_fd_input)) {
|
|
|
if (!$this->switchLogFile()) {
|
|
|
- $this->setInputFd();
|
|
|
+ $this->checkStat();
|
|
|
}
|
|
|
|
|
|
usleep(1000);
|
|
@@ -71,7 +65,10 @@ class Monitor
|
|
|
if ($line == "") continue;
|
|
|
if (strpos($line, $this->_mark) === 0) continue;
|
|
|
|
|
|
- $this->log($line);
|
|
|
+ if ($this->DealLine($line) === -1) {
|
|
|
+ @fseek($this->_fd_input, $this->_ptr_pos);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
@fseek($this->_fd_input, $this->_ptr_pos);
|
|
|
$this->addMark();
|
|
|
@fgets($this->_fd_input);
|
|
@@ -82,6 +79,67 @@ class Monitor
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private function DealLine($str)
|
|
|
+ {
|
|
|
+ $str = trim($str);
|
|
|
+ $pattern = "/User_Path:/";
|
|
|
+ $matched = preg_match_all($pattern, $str);
|
|
|
+ if ($matched)
|
|
|
+ {
|
|
|
+ $data = Parser::parse($str);
|
|
|
+ if (!empty($data)) {
|
|
|
+ $this->log(date("Y-m-d h:i:s"). " deal a record: {$str}");
|
|
|
+ $mod = Model();
|
|
|
+ $insert = $this->formatData($data);
|
|
|
+ $result = $mod->table('user_path')->insert($insert);
|
|
|
+ if ($result) {
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+ return -1; //未成功插入数据库
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ private function formatData(array $data)
|
|
|
+ {
|
|
|
+ $ret = [];
|
|
|
+ if (isset($data['session_id'])) {
|
|
|
+ $ret['session_id'] = $data['session_id'];
|
|
|
+ }
|
|
|
+ if (isset($data['member_id'])) {
|
|
|
+ $ret['member_id'] = intval($data['member_id']);
|
|
|
+ }
|
|
|
+ if (isset($data['time'])) {
|
|
|
+ $ret['time'] = strtotime($data['time']);
|
|
|
+ }
|
|
|
+ if (isset($data['thread_id'])) {
|
|
|
+ $ret['thread_id'] = intval($data['thread_id']);
|
|
|
+ }
|
|
|
+ if (isset($data['act'])) {
|
|
|
+ $ret['act'] = $data['act'];
|
|
|
+ }
|
|
|
+ if (isset($data['op'])) {
|
|
|
+ $ret['op'] = $data['op'];
|
|
|
+ }
|
|
|
+ if (isset($data['data'])) {
|
|
|
+ $ret['data'] = $data['data'];
|
|
|
+ }
|
|
|
+ if (isset($data['from_act'])) {
|
|
|
+ $ret['from_act'] = $data['from_act'];
|
|
|
+ }
|
|
|
+ if (isset($data['from_op'])) {
|
|
|
+ $ret['from_op'] = $data['from_op'];
|
|
|
+ }
|
|
|
+ if (isset($data['from_data'])) {
|
|
|
+ $ret['from_data'] = $data['from_data'];
|
|
|
+ }
|
|
|
+ if (isset($data['type'])) {
|
|
|
+ $ret['type'] = $data['type'];
|
|
|
+ }
|
|
|
+ return $ret;
|
|
|
+ }
|
|
|
+
|
|
|
private function addMark()
|
|
|
{
|
|
|
if (@flock($this->_fd_input, LOCK_EX)) {
|
|
@@ -92,6 +150,15 @@ class Monitor
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private function checkStat()
|
|
|
+ {
|
|
|
+ $size_now = @fstat($this->_fd_input)['size'];
|
|
|
+ if ($size_now != $this->_fsize)
|
|
|
+ {
|
|
|
+ $this->setInputFd();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
private function resetPtrPos()
|
|
|
{
|
|
|
$this->setPtrPos(0);
|
|
@@ -116,12 +183,14 @@ class Monitor
|
|
|
private function setInputFd()
|
|
|
{
|
|
|
$date = date("Ymd");
|
|
|
- $file_name = BASE_DATA_PATH. DS. 'log'. DS. $date. ".log";
|
|
|
+ $file_name = BASE_DATA_PATH. DS. 'log'. DS. $date. "_userpath.log";
|
|
|
+ file_exists($file_name) or die("{$file_name} 不存在!");
|
|
|
@fclose($this->_fd_input);
|
|
|
$fd = @fopen($file_name, "r+");
|
|
|
if ($fd) {
|
|
|
$this->input_fname = $file_name;
|
|
|
$this->_fd_input = $fd;
|
|
|
+ $this->_fsize = @fstat($this->_fd_input)['size'];
|
|
|
$this->resetPtrPos();
|
|
|
return true;
|
|
|
}
|
|
@@ -133,4 +202,53 @@ class Monitor
|
|
|
//$msg .= "\r\n";
|
|
|
@fwrite($this->_fd_log, $msg);
|
|
|
}
|
|
|
+}
|
|
|
+
|
|
|
+class Parser
|
|
|
+{
|
|
|
+ public static function parse($str)
|
|
|
+ {
|
|
|
+ $pairs = self::matchPairs($str);
|
|
|
+ if ($pairs) {
|
|
|
+ $params = [];
|
|
|
+
|
|
|
+ $pattern_time_format = "/\d{4}\-\d{2}\-\d{2}\s+\d{2}\:\d{2}\:\d{2}/";
|
|
|
+ $matched = preg_match_all($pattern_time_format, $str, $matches);
|
|
|
+ if ($matched) {
|
|
|
+ if (isset($matches[0][0])) {
|
|
|
+ $params['time'] = $matches[0][0];
|
|
|
+ }
|
|
|
+ unset($matches);
|
|
|
+ }
|
|
|
+
|
|
|
+ $pattern_thead_id = "/^\[\d+/";
|
|
|
+ $matched = preg_match_all($pattern_thead_id, $str, $matches);
|
|
|
+ if ($matched) {
|
|
|
+ if (isset($matches[0][0])) {
|
|
|
+ $params['thread_id'] = str_replace("[","", $matches[0][0]);
|
|
|
+ }
|
|
|
+ unset($matches);
|
|
|
+ }
|
|
|
+
|
|
|
+ foreach ($pairs as $pair)
|
|
|
+ {
|
|
|
+ $kv = explode("=", $pair);
|
|
|
+ if ($kv[0] != "") {
|
|
|
+ $params[$kv[0]] = $kv[1];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return $params;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static function matchPairs($str)
|
|
|
+ {
|
|
|
+ $pattern = "/(\w+=[^\&]+)/i";
|
|
|
+ $matched = preg_match_all($pattern, $str, $matches);
|
|
|
+ if ($matched) {
|
|
|
+ return $matches[0];
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
}
|