示例#1
0
 /**
  * Return the system information about the $metric of an instance
  *
  * @param  string $id
  * @param  string $metric
  * @param  null|array $options
  * @return boolean|array
  */
 public function monitorInstance($id, $metric, $options = null)
 {
     if (empty($id) || empty($metric)) {
         return false;
     }
     if (!in_array($metric, $this->validMetrics)) {
         throw new Exception\InvalidArgumentException(sprintf('The metric "%s" is not valid', $metric));
     }
     if (!empty($options) && !is_array($options)) {
         throw new Exception\InvalidArgumentException('The options must be an array');
     }
     if (!empty($options) && (empty($options[Instance::MONITOR_START_TIME]) || empty($options[Instance::MONITOR_END_TIME]))) {
         throw new Exception\InvalidArgumentException(sprintf('The options array must contain: "%s" and "%s"', $options[Instance::MONITOR_START_TIME], $options[Instance::MONITOR_END_TIME]));
     }
     if (!isset($this->ec2Monitor)) {
         $this->ec2Monitor = new Ec2Monitor($this->accessKey, $this->accessSecret, $this->region);
         $this->ec2Monitor->setHttpClient($this->ec2->getHttpClient());
     }
     $param = array('MeasureName' => $this->mapMetrics[$metric], 'Statistics' => array('Average'), 'Dimensions' => array('InstanceId' => $id));
     if (!empty($options)) {
         $param['StartTime'] = $options[Instance::MONITOR_START_TIME];
         $param['EndTime'] = $options[Instance::MONITOR_END_TIME];
     }
     $this->resetError();
     try {
         $this->adapterResult = $this->ec2Monitor->getMetricStatistics($param);
     } catch (Ec2Exception\RunTimeException $e) {
         $this->setError($e);
         return false;
     }
     $monitor = array();
     $num = 0;
     $average = 0;
     if (!empty($this->adapterResult['datapoints'])) {
         foreach ($this->adapterResult['datapoints'] as $result) {
             $monitor['series'][] = array('timestamp' => $result['Timestamp'], 'value' => $result['Average']);
             $average += $result['Average'];
             $num++;
         }
     }
     if ($num > 0) {
         $monitor['average'] = $average / $num;
     }
     return $monitor;
 }