コード例 #1
0
ファイル: sw_update.class.php プロジェクト: nmred/swansoft
 /**
  * 更新 RRD 库 
  * 
  * @param string $monitor_id 
  * @static
  * @access public
  * @return void
  */
 public static function update($monitor_key, $data, $time)
 {
     // 更新 rrd 数据库
     if (!isset(self::$__redis)) {
         self::$__redis = \swan\redis\sw_redis::singleton();
     }
     $file_name = sw_create::create($monitor_key);
     $monitor_info = self::$__redis->get(SWAN_CACHE_MONITOR_PREFIX . $monitor_key);
     if (!$monitor_info) {
         throw new sw_exception('create rrd file faild. reason is get monitor info fail.');
     }
     $monitor_info = json_decode($monitor_info, true);
     $madapter_id = $monitor_info['madapter_id'];
     $metrics = array();
     foreach ($data as $metric_id => $value) {
         // 获取metric info
         $metric_info = self::$__redis->get(SWAN_CACHE_METRIC_PREFIX . $madapter_id . '_' . $metric_id);
         if (!$metric_info) {
             throw new sw_exception('create rrd file faild. reason is get monitor metric info fail.');
         }
         $metric_info = json_decode($metric_info, true);
         $metrics[$metric_info['metric_name']] = $value;
     }
     try {
         $updater = new \RRDUpdater($file_name);
         $updater->update($metrics, $time);
     } catch (\Exception $e) {
         throw new sw_exception($e);
     }
 }
コード例 #2
0
ファイル: sw_graph.class.php プロジェクト: nmred/swansoft
 /**
  * 绘制图片 
  * 
  * @param string $monitor_id 
  * @static
  * @access public
  * @return void
  */
 public static function graph($monitor_id, $metric_id, $options = array())
 {
     // 创建 rrd 数据库
     if (!isset(self::$__redis)) {
         self::$__redis = \swan\redis\sw_redis::singleton();
     }
     $monitor_info = self::$__redis->get(SWAN_CACHE_MONITOR_PREFIX . $monitor_id);
     if (!$monitor_info) {
         throw new sw_exception('graph rrd file faild. reason is get dm info fail.');
     }
     $monitor_info = json_decode($monitor_info, true);
     $madapter_id = $monitor_info['madapter_id'];
     // 获取metric info
     $metric_info = self::$__redis->get(SWAN_CACHE_METRIC_PREFIX . $madapter_id . '_' . $metric_id);
     if (!$metric_info) {
         throw new sw_exception('graph rrd file faild. reason is get monitor metric info fail.');
     }
     $metric_info = json_decode($metric_info, true);
     $graph_params = self::_get_graph_params($monitor_info, $metric_info, $options);
     $time_grid = isset($options['time_grid']) ? $options['time_grid'] : self::T_15_MIN;
     if (!array_key_exists($options['time_grid'], self::$__x_grid)) {
         $time_grid = self::T_15_MIN;
     }
     $out_file = PATH_SWAN_RRD_GRAPH . $time_grid . '/' . $monitor_id . '_' . $metric_id . '.png';
     try {
         $graph = new \RRDGraph($out_file);
         $graph->setOptions($graph_params);
         $graph->save();
     } catch (\Exception $e) {
         throw new sw_exception($e);
     }
     return $out_file;
 }
コード例 #3
0
ファイル: sw_create.class.php プロジェクト: nmred/swansoft
 /**
  * 创建 RRD 库 
  * 
  * @param string $monitor_id 
  * @static
  * @access public
  * @return void
  */
 public static function create($monitor_id, $force = false)
 {
     $file_name = PATH_SWAN_RRD . $monitor_id . '.rrd';
     if (file_exists($file_name) && !$force) {
         return $file_name;
     }
     // 创建 rrd 数据库
     if (!isset(self::$__redis)) {
         self::$__redis = \swan\redis\sw_redis::singleton();
     }
     $monitor_info = self::$__redis->get(SWAN_CACHE_MONITOR_PREFIX . $monitor_id);
     if (!$monitor_info) {
         throw new sw_exception('create rrd file faild. reason is get monitor info fail.');
     }
     $monitor_info = json_decode($monitor_info, true);
     $madapter_id = $monitor_info['madapter_id'];
     $madapter_info = self::$__redis->get(SWAN_CACHE_MADAPTER_PREFIX . $madapter_id);
     if (!$madapter_info) {
         throw new sw_exception('create rrd file faild. reason is get madapter info fail.');
     }
     $madapter_info = json_decode($madapter_info, true);
     $rrd_creater = new \RRDCreator($file_name, "now -10d", $madapter_info['steps']);
     // 获取 archive
     $archives = self::$__redis->get(SWAN_CACHE_MADAPTER_ARCHIVE_PREFIX . $madapter_id);
     if (!$archives) {
         throw new sw_exception('create rrd file faild. reason is get monitor archive fail.');
     }
     $archives = json_decode($archives, true);
     foreach ($archives as $archive) {
         $archive = self::$__cf_types[$archive['cf_type']] . ':' . $archive['xff'] . ':' . $archive['steps'] . ':' . $archive['rows'];
         $rrd_creater->addArchive($archive);
     }
     // 获取 metrics
     $metric_ids = self::$__redis->smembers(SWAN_CACHE_METRIC_IDS . $madapter_id);
     if (empty($metric_ids)) {
         throw new sw_exception('not exists metric this monitor. monitor:' . $monitor_id);
     }
     foreach ($metric_ids as $metric_id) {
         // 获取metric info
         $metric_info = self::$__redis->get(SWAN_CACHE_METRIC_PREFIX . $madapter_id . '_' . $metric_id);
         if (!$metric_info) {
             throw new sw_exception('create rrd file faild. reason is get monitor metric info fail.');
         }
         $metric_info = json_decode($metric_info, true);
         if (!$metric_info['tmax']) {
             $metric_info['tmax'] = $monitor_info['steps'] * 2;
         }
         $ds_data = $metric_info['metric_name'] . ':' . self::$__dst_types[$metric_info['dst_type']] . ':' . $metric_info['tmax'] . ':' . $metric_info['vmin'] . ':' . $metric_info['vmax'];
         $rrd_creater->addDataSource($ds_data);
     }
     try {
         $rrd_creater->save();
     } catch (\Exception $e) {
         throw new sw_exception($e);
     }
     return $file_name;
 }
コード例 #4
0
ファイル: sw_graph.class.php プロジェクト: nmred/swansoft
 /**
  * 初始化
  *
  * @return void
  */
 protected function _init()
 {
     $this->log('Start graph.', LOG_DEBUG);
     $array_config = array('proc_num');
     foreach ($array_config as $config_name) {
         if (!empty($this->__proc_config[$config_name])) {
             $var_name = '__' . $config_name;
             $this->{$var_name} = $this->__proc_config[$config_name];
         }
     }
     $this->__redis = \swan\redis\sw_redis::singleton();
 }
コード例 #5
0
 /**
  * 初始化
  *
  * @return void
  */
 protected function _init()
 {
     $this->log('Start push server worker.', LOG_DEBUG);
     $array_config = array();
     foreach ($array_config as $config_name) {
         if (!empty($this->__proc_config[$config_name])) {
             $var_name = '__' . $config_name;
             $this->{$var_name} = $this->__proc_config[$config_name];
         }
     }
     $this->__redis = \swan\redis\sw_redis::singleton();
     $this->__gmw_push = new sw_worker();
     $this->__gmw_push->set_log($this->__log);
     $this->__gmw_push->add_servers_by_config('gmw_push_server');
 }
コード例 #6
0
 /**
  * 重新更新配置 
  * 
  * @param int $interval 
  * @access protected
  * @return void
  */
 protected function _reconfig($interval)
 {
     // 缓存监控器数据
     $redis = \swan\redis\sw_redis::singleton();
     $monitor_data = array();
     try {
         $monitor_data = \lib\inner_client\sw_inner_client::call('user', 'dconfig.monitor');
         if (isset($monitor_data['data'])) {
             $monitor_data = $monitor_data['data'];
         } else {
             $monitor_data = array();
         }
     } catch (\swan\exception\sw_exception $e) {
         $this->log($e->getMessage(), LOG_INFO);
     }
     $old_monitor_ids = $redis->smembers(SWAN_CACHE_MONITOR_IDS);
     $monitor_ids = array();
     foreach ($monitor_data as $key => $value) {
         $cache_data = json_encode($value);
         $redis->set(SWAN_CACHE_MONITOR_PREFIX . $key, $cache_data, self::EXPIRE_TIME);
         $monitor_ids[] = $key;
         $redis->sadd(SWAN_CACHE_MONITOR_IDS, $key);
         $redis->expire(SWAN_CACHE_MONITOR_IDS, self::EXPIRE_TIME);
     }
     $del_monitor_ids = array_diff($old_monitor_ids, $monitor_ids);
     foreach ($del_monitor_ids as $key) {
         $redis->srem(SWAN_CACHE_MONITOR_IDS, $key);
         $redis->delete(SWAN_CACHE_MONITOR_PREFIX . $key);
     }
     // 缓存监控适配器相关数据
     try {
         $madapter_data = \lib\inner_client\sw_inner_client::call('user', 'dconfig.madapter');
         if (isset($madapter_data['data'])) {
             $madapter_data = $madapter_data['data'];
         } else {
             $madapter_data = array();
         }
     } catch (\swan\exception\sw_exception $e) {
         $this->log($e->getMessage(), LOG_INFO);
     }
     $old_madapter_ids = $redis->smembers(SWAN_CACHE_MADAPTER_IDS);
     $madapter_ids = array();
     foreach ($madapter_data as $madapter_id => $value) {
         if (isset($value['archives'])) {
             $cache_data = json_encode($value['archives']);
             $redis->set(SWAN_CACHE_MADAPTER_ARCHIVE_PREFIX . $madapter_id, $cache_data, self::EXPIRE_TIME);
         }
         if (isset($value['metrics'])) {
             $scache_id = SWAN_CACHE_METRIC_IDS . $madapter_id;
             $old_metric_ids = $redis->smembers($scache_id);
             $metric_ids = array();
             foreach ($value['metrics'] as $val) {
                 $cache_id = SWAN_CACHE_METRIC_PREFIX . $madapter_id . '_' . $val['metric_id'];
                 $cache_data = json_encode($val);
                 $redis->set($cache_id, $cache_data, self::EXPIRE_TIME);
                 $metric_ids[] = $val['metric_id'];
                 $redis->sadd($scache_id, $val['metric_id']);
                 $redis->expire($scache_id, self::EXPIRE_TIME);
             }
             $del_metric_ids = array_diff($old_metric_ids, $metric_ids);
             foreach ($del_metric_ids as $metric_id) {
                 $redis->srem($scache_id, $metric_id);
                 $redis->delete(SWAN_CACHE_METRIC_PREFIX . $madapter_id . '_' . $metric_id);
             }
         }
         if (isset($value['basic'])) {
             $cache_data = json_encode($value['basic']);
             $redis->set(SWAN_CACHE_MADAPTER_PREFIX . $madapter_id, $cache_data, self::EXPIRE_TIME);
             $madapter_ids[] = $madapter_id;
             $redis->sadd(SWAN_CACHE_MADAPTER_IDS, $madapter_id);
             $redis->expire(SWAN_CACHE_MADAPTER_IDS, self::EXPIRE_TIME);
         }
     }
     $del_madapter_ids = array_diff($old_madapter_ids, $madapter_ids);
     foreach ($del_madapter_ids as $madapter_id) {
         $redis->srem(SWAN_CACHE_MONITOR_IDS, $madapter_id);
         $redis->delete(SWAN_CACHE_MADAPTER_PREFIX . $madapter_id);
     }
 }
コード例 #7
0
ファイル: sw_smeta.class.php プロジェクト: nmred/swansoft
 /**
  * 处理收到的信息并转发出去
  *
  * @param string $data 收到的信息
  * @param string $client_key 客户端连接的 key
  * @return void
  */
 protected function _process_receive_data($data, $client_key)
 {
     $client_ip = $this->__buffer_event[$client_key][1];
     $client_port = $this->__buffer_event[$client_key][2];
     $client_name = "{$client_ip}:{$client_port}";
     $data = rtrim($data);
     $info = json_decode($data, true);
     if (!isset($info[1])) {
         $this->log('data format invalid.', LOG_INFO);
         return;
     }
     $redis = \swan\redis\sw_redis::singleton();
     list($device_id, $monitor_id, $metric_id) = explode('_', $info[0]);
     $monitor_info = $redis->get(SWAN_CACHE_MONITOR_PREFIX . $device_id . '_' . $monitor_id);
     $monitor_info = json_decode($monitor_info, true);
     if (!$monitor_info) {
         $this->log('store failed, get monitor info fail.: ' . $data, LOG_INFO);
         return;
     }
     $madapter_info = $redis->get(SWAN_CACHE_MADAPTER_PREFIX . $monitor_info['madapter_id']);
     $madapter_info = json_decode($madapter_info, true);
     if (!$madapter_info) {
         $this->log('store failed, get madapter info fail.: ' . $data, LOG_INFO);
         return;
     }
     if (!isset($madapter_info['store_type'])) {
         return;
     }
     switch ((int) $madapter_info['store_type']) {
         case 2:
             $this->__rrd_gmc->doBackground('rrd_store', $data);
             break;
         case 4:
             $this->__redis_gmc->doBackground('redis_store', $data);
             break;
         case 6:
             $this->__rrd_gmc->doBackground('rrd_store', $data);
             $this->__redis_gmc->doBackground('redis_store', $data);
             break;
     }
     $push_data = array('channel' => 'monitor', 'data' => json_decode($data, true));
     $this->__push_gmc->doBackground('push_server', json_encode($push_data));
 }
コード例 #8
0
 /**
  * 处理收到的信息并转发出去
  *
  * @param string $data 收到的信息
  * @param string $client_key 客户端连接的 key
  * @return void
  */
 public function process_receive_data($job)
 {
     $data = $job->workload();
     $this->log("log rrd_store " . $data, LOG_DEBUG);
     $data = rtrim($data);
     $data = json_decode($data, true);
     if (isset($data[0]) && isset($data[1]) && $data[0]) {
         $redis = \swan\redis\sw_redis::singleton();
         $redis->set(SWAN_MONITOR_REDIS_DATA . $data[0], json_encode($data[1]), self::EXPIRE_TIME);
     }
 }
コード例 #9
0
 /**
  * 初始化
  *
  * @return void
  */
 protected function _init()
 {
     $this->log('Start graph queue.', LOG_DEBUG);
     if (!empty($this->__proc_config['graph_interval'])) {
         $this->__graph_interval = $this->__proc_config['graph_interval'];
     }
     if (!empty($this->__proc_config['reconfig_interval'])) {
         $this->__loop_timeout = $this->__proc_config['reconfig_interval'];
     }
     $this->__event_base = new \EventBase();
     $this->__redis = \swan\redis\sw_redis::singleton();
     // 读取所有的入队列数据
     $this->_get_config_data();
     $this->_create_timer($this->__graph_interval);
 }