示例#1
0
 /**
  * 处理业务
  * @see PHPServerWorker::dealProcess()
  */
 public function dealProcess($recv_str)
 {
     if ($this->isReloading) {
         return;
     }
     $data = JMProtocol::bufferToData($recv_str);
     switch ($data['sub_cmd']) {
         // 开发环境如果没有inotify扩展,则用这个进程监控文件更新
         case self::CMD_TELL_INCLUDE_FILES:
             if (PHPServerConfig::get('ENV') !== 'dev') {
                 return;
             }
             clearstatcache();
             if ($files = json_decode($data['body'], true)) {
                 foreach ($files as $file) {
                     if (!isset($this->filesToInotify[$file])) {
                         $stat = @stat($file);
                         $mtime = isset($stat['mtime']) ? $stat['mtime'] : 0;
                         $this->filesToInotify[$file] = $mtime;
                     }
                 }
             }
             break;
     }
 }
示例#2
0
 /**
  * 发送数据到监控进程
  * @param int $cmd
  * @param mix $data
  * @param string $address
  */
 protected static function sendData($cmd, $data, $address = 'udp://0.0.0.0:10203')
 {
     if (class_exists('JMProtocol')) {
         $packge = new JMProtocol();
         $packge->header['sub_cmd'] = $cmd;
         $packge->body = json_encode($data);
         $socket = stream_socket_client($address);
         $buffer = $packge->getBuffer();
         $buffer_len = strlen($buffer);
         if ($buffer_len <= self::MAX_UDP_PACKAGE_SIZE) {
             stream_socket_sendto($socket, $buffer);
         } else {
             $chunk_count = ceil($buffer_len / self::MAX_UDP_PACKAGE_SIZE);
             $chunk_count = $chunk_count == 1 ? 2 : $chunk_count;
             foreach (array_chunk($data, ceil(count($data) / $chunk_count)) as $chunk_data) {
                 self::sendData($cmd, $chunk_data);
             }
         }
     }
 }
示例#3
0
 /**
  * 初始化
  */
 public function __construct($buffer = null)
 {
     if ($buffer) {
         $data = self::bufferToData($buffer);
         $this->body = $data['body'];
         unset($data['body']);
         $this->header = $data;
     } else {
         if (self::$seriesId >= 65535) {
             self::$seriesId = 0;
         } else {
             $this->header['series_id'] = self::$seriesId++;
         }
     }
 }
示例#4
0
 /**
  * 处理业务逻辑 查询log 查询统计信息
  * @see PHPServerWorker::dealProcess()
  */
 public function dealProcess($recv_str)
 {
     $pack_data = new JMProtocol($recv_str);
     $cmd = $pack_data->header['cmd'];
     $sub_cmd = $pack_data->header['sub_cmd'];
     switch ($cmd) {
         case self::CMD_REPORT_IP:
             $body_data = json_decode($pack_data->body, true);
             $pack_data->header['cmd'] = self::CMD_REPORT_IP_RESULT;
             $pack_data->body = '';
             stream_socket_sendto($this->udpBroadSocket, $pack_data->getBuffer(), 0, $this->currentClientAddress);
             return;
         case self::CMD_PROVIDER:
             switch ($sub_cmd) {
                 case self::SUB_CMD_GET_ST_AND_MODULES:
                     $module_interface = json_decode($pack_data->body, true);
                     $module = isset($module_interface['module']) ? $module_interface['module'] : '';
                     $interface = isset($module_interface['interface']) ? $module_interface['interface'] : '';
                     $time = isset($module_interface['time']) ? $module_interface['time'] : '';
                     $pack_data->body = json_encode(array('modules' => $this->getModules($module), 'statistic' => $this->getStatistic($module, $interface, $time)));
                     $this->sendToClient($pack_data->getBuffer());
                     return;
                 case self::SUB_CMD_GET_LOGS:
                     $log_param = json_decode($pack_data->body, true);
                     $module = isset($log_param['module']) ? $log_param['module'] : '';
                     $interface = isset($log_param['interface']) ? $log_param['interface'] : '';
                     $start_time = isset($log_param['start_time']) ? $log_param['start_time'] : '';
                     $end_time = isset($log_param['end_time']) ? $log_param['end_time'] : '';
                     $code = isset($log_param['code']) ? $log_param['code'] : '';
                     $msg = isset($log_param['msg']) ? $log_param['msg'] : '';
                     $pointer = isset($log_param['pointer']) ? $log_param['pointer'] : '';
                     $count = isset($log_param['count']) ? $log_param['count'] : 10;
                     $pack_data->body = json_encode($this->getStasticLog($module, $interface, $start_time, $end_time, $code, $msg, $pointer, $count));
                     $this->sendToClient($pack_data->getBuffer());
                     return;
             }
             return;
     }
 }
示例#5
0
 protected function getStasticLog($module, $interface, $start_time, $end_time = '', $code = '', $msg = '', $pointer = '', $count = 10)
 {
     $pack = new JMProtocol();
     $pack->header['cmd'] = self::CMD_PROVIDER;
     $pack->header['sub_cmd'] = self::SUB_CMD_GET_LOGS;
     $ip_list = !empty($_GET['ip']) && is_array($_GET['ip']) ? $_GET['ip'] : $this->phpServerIpList;
     $pointer_list = !empty($_GET['pointer']) && is_array($_GET['pointer']) ? $_GET['pointer'] : array();
     $port = PHPServerConfig::get('workers.StatisticProvider.port');
     $request_buffer_array = array();
     foreach ($ip_list as $key => $ip) {
         $pointer = isset($pointer_list[$key]) ? $pointer_list[$key] : 0;
         $pack->body = json_encode(array('module' => $module, 'interface' => $interface, 'start_time' => $start_time, 'end_time' => $end_time, 'code' => $code, 'msg' => $msg, 'pointer' => $pointer, 'count' => $count));
         $request_buffer_array["{$ip}:{$port}"] = $pack->getBuffer();
     }
     $read_buffer_array = $this->multiRequest($request_buffer_array);
     ksort($read_buffer_array);
     foreach ($read_buffer_array as $address => $buf) {
         list($ip, $port) = explode(':', $address);
         $pack = new JMProtocol($buf);
         $body_data = json_decode($pack->body, true);
         $log_data = isset($body_data['data']) ? $body_data['data'] : '';
         $point = isset($body_data['pointer']) ? $body_data['pointer'] : 0;
         $read_buffer_array[$address] = array('pointer' => $point, 'data' => $log_data);
     }
     return $read_buffer_array;
 }