public function testMetricOperations() { $namespace = 'AWSSDKPHP'; $metricName = 'CloudWatchTests'; $dimensions = array(array('Name' => 'Prefix', 'Value' => $this->getResourcePrefix())); self::log('Put some data to a metric.'); $this->cloudwatch->putMetricData(array('Namespace' => $namespace, 'MetricData' => array(array('MetricName' => $metricName, 'Timestamp' => time(), 'Value' => rand(1, 20) + rand(1, 59) / 100, 'Unit' => Unit::KILOBYTES, 'Dimensions' => $dimensions)))); self::log('Make sure the metric exists.'); $found = false; foreach ($this->cloudwatch->getIterator('ListMetrics', array('Namespace' => $namespace)) as $metric) { if ($found = $metric['MetricName'] == $metricName && $metric['Dimensions'] == $dimensions) { break; } } if (!$found) { $this->markTestSkipped('The CloudWatch metric you created has not yet been picked up by CloudWatch. This ' . 'can take up to 15 minutes to occur. Please run this test again later.'); } self::log('Verify the statistics of the data that has been put.'); $result = $this->cloudwatch->getMetricStatistics(array('Namespace' => $namespace, 'MetricName' => $metricName, 'Dimensions' => $dimensions, 'StartTime' => strtotime('-1 days'), 'EndTime' => strtotime('now'), 'Period' => 3000, 'Statistics' => array(Statistic::MAXIMUM, Statistic::MINIMUM))); $min = min($result->getPath('Datapoints/*/Minimum')); $max = max($result->getPath('Datapoints/*/Maximum')); $this->assertGreaterThan(1, $min); $this->assertLessThan(22, $max); }
public function getConsumedCapacity($indexName = self::PRIMARY_INDEX, $period = 60, $num_of_period = 5, $timeshift = -300) { $cloudwatch = new CloudWatchClient(["profile" => $this->config['profile'], "region" => $this->config['region'], "version" => "2010-08-01"]); $end = time() + $timeshift; $end -= $end % $period; $start = $end - $num_of_period * $period; $requestArgs = ["Namespace" => "AWS/DynamoDB", "Dimensions" => [["Name" => "TableName", "Value" => $this->tableName]], "MetricName" => "ConsumedReadCapacityUnits", "StartTime" => date('c', $start), "EndTime" => date('c', $end), "Period" => 60, "Statistics" => ["Sum"]]; if ($indexName != self::PRIMARY_INDEX) { $requestArgs['Dimensions'][] = ["Name" => "GlobalSecondaryIndexName", "Value" => $indexName]; } $result = $cloudwatch->getMetricStatistics($requestArgs); $total_read = 0; $total_count = 0; foreach ($result['Datapoints'] as $data) { $total_count++; $total_read += $data['Sum']; } $readUsed = $total_count ? $total_read / $total_count / 60 : 0; $requestArgs['MetricName'] = 'ConsumedWriteCapacityUnits'; $result = $cloudwatch->getMetricStatistics($requestArgs); $total_write = 0; $total_count = 0; foreach ($result['Datapoints'] as $data) { $total_count++; $total_write += $data['Sum']; } $writeUsed = $total_count ? $total_write / $total_count / 60 : 0; return [$readUsed, $writeUsed]; }
require 'vendor/autoload.php'; use Aws\CloudWatch\CloudWatchClient; use Symfony\Component\Yaml\Yaml; try { $file = realpath(dirname(__FILE__)) . '/config/config.yml'; if ($file && file_exists($file)) { $config = Yaml::parse(file_get_contents($file)); } else { die('No config.yml was found. Use config/config.yml.sample as base example'); } $client = new CloudWatchClient(['region' => $config['amazon']['amazonRegion'], 'version' => $config['amazon']['amazonVersion'], 'credentials' => ['key' => $config['amazon']['amazonKey'], 'secret' => $config['amazon']['amazonSecret']]]); $date = new DateTime(null, new DateTimeZone('America/Toronto')); $endTime = $date->getTimestamp(); $date = $date->sub(new DateInterval('P1D')); $startTime = $date->getTimestamp(); $metrics = $client->getMetricStatistics(['Namespace' => 'AWS/RDS', 'MetricName' => 'FreeStorageSpace', 'DBInstanceIdentifier' => $config['amazon']['amazonRDSdb'], 'StartTime' => $startTime, 'EndTime' => $endTime, 'Period' => 3600 * 6, 'Statistics' => ['Maximum'], 'Unit' => 'Bytes']); if (is_object($metrics)) { $dataList = $metrics['Datapoints']; $x = sizeof($dataList); if ($x > 0) { foreach ($dataList as $data) { $size = number_format($data['Maximum'] / 1073741824, 2) . ' GB'; $pct = number_format($size * 100 / 307.2, 2); echo "[{$x}] " . $size . " or {$pct}% of space still available" . PHP_EOL; // Space available to use $x--; } } } } catch (Aws\CloudWatch\Exception\CloudWatchException $e) { echo $e->getMessage();