示例#1
0
 /**
  * 创建 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;
 }
示例#2
0
文件: RrdTest.php 项目: blar/rrd
 public function testCreateAndUpdate()
 {
     $rrdFile = __DIR__ . "/speed.rrd";
     $rrd = new Rrd($rrdFile);
     $timestamp = new DateTimeImmutable('2016-01-01 13:37:42');
     if (file_exists($rrd->getFileName())) {
         unlink($rrd->getFileName());
     }
     if (!file_exists($rrd->getFileName())) {
         $creator = new RRDCreator();
         $creator->setFileName($rrd->getFileName());
         $creator->setStart($timestamp);
         $creator->setStep(60);
         $dataSource = new RrdDataSource();
         $dataSource->setName('downstream');
         $dataSource->setType(RrdDataSource::TYPE_COUNTER);
         $creator->addDataSource($dataSource);
         $dataSource = new RrdDataSource();
         $dataSource->setName('upstream');
         $dataSource->setType(RrdDataSource::TYPE_COUNTER);
         $creator->addDataSource($dataSource);
         $archive = new RrdArchive();
         $archive->setConsolidation(RrdArchive::CONSOLIDATION_AVERAGE);
         $archive->setSteps(60);
         $archive->setRows(60 * 24);
         $creator->addArchive($archive);
         $archive = new RrdArchive();
         $archive->setConsolidation(RrdArchive::CONSOLIDATION_AVERAGE);
         $archive->setSteps(60 * 60);
         $archive->setRows(72);
         $creator->addArchive($archive);
         $creator->save();
     }
     $rrd->setDateTime($timestamp->add(new DateInterval('PT1M')));
     $rrd->update(['downstream' => 1024, 'upstream' => 768]);
     $rrd->setDateTime($timestamp->add(new DateInterval('PT2M')));
     $rrd->update(['downstream' => 1024, 'upstream' => 768]);
     $this->assertSame('2016-01-01 13:39:42', $rrd->getDateTime()->format('Y-m-d H:i:s'));
     $this->assertSame('2016-01-01 13:39:42', $rrd->getLastUpdate()['timestamp']->format('Y-m-d H:i:s'));
 }