getOption(self::OPTION_PERIOD); } /** * @return string */ public function getNamespace() { return $this->getOption(self::OPTION_NAMESPACE); } /** * @return array */ public function getDimensions() { return $this->getOption(self::OPTION_DIMENSIONS); } public function collect($force = false) { $active = $this->getPersistence()->get(self::class); if (!$active || $force) { $active = $this->getMetric(); $this->getPersistence()->set(self::class, $active, $this->getOption(self::OPTION_TTL)); } return $active; } private function getMetric() { $cloudWatchClient = $this->getAwsClient()->getCloudWatchClient(); $period = $this->getPeriod(); $interval = new DateInterval('PT' . $period . 'S'); $since = (new DateTime())->sub($interval); $result = $cloudWatchClient->getMetricStatistics([ 'Namespace' => $this->getNamespace(), 'MetricName' => 'CPUUtilization', 'StartTime' => $since, 'EndTime' => new DateTime(), 'Period' => $period, 'Statistics' => ['Average', 'Maximum'], 'Dimensions' => $this->getDimensions(), ]); $this->logInfo('RDS_METRICS:' . json_encode($result->toArray())); $data = $result->get('Datapoints'); $v = array_shift($data); return $v['Average']; } /** * Requires lib-generis-aws at least 0.9.3 * @return AwsClient */ public function getAwsClient() { if (!$this->awsClient) { $this->awsClient = $this->getServiceLocator()->get('generis/awsClient'); } return $this->awsClient; } }