I'm able to retrieve metrics information for included metrics however for custom metrics, for example CPUUtilization on a per/instance basis, I cannot get the info. I'm not sure if I'm using the correct NameSpace, or perhaps I need to include additional information in the Dimensions field. Anyone know how to do this?
Here is my code:
<?php
date_default_timezone_set('America/New_York');
require 'vendor/autoload.php';
use Aws\CloudWatch\CloudWatchClient;
$cloudWatchClient = CloudWatchClient::factory(array(
'profile' => 'my_profile',
'region' => 'us-east-1',
));
$dimensions = array(
array('Name' => 'InstanceId', 'Value' => 'i-0c0f546e4d1ef25e1'),
array('Name' => 'ImageId', 'Value' => 'ami-9cdee48b'),
array('Name' => 'AutoScalingGroupName', 'Value' => 'lc-wordpress-asg'),
);
$cpuResult = $cloudWatchClient->getMetricStatistics(array(
'Namespace' => 'AWS/EC2',
'MetricName' => 'CPUUtilization',
'Dimensions' => $dimensions,
'StartTime' => strtotime('-5 minutes'),
'EndTime' => strtotime('now'),
'Period' => 300,
'Statistics' => array('Average'),
));
var_dump($cpuResult);
$memoryResult = $cloudWatchClient->getMetricStatistics(array(
'Namespace' => 'Linux System',
'MetricName' => 'MemoryUtilization',
'Dimensions' => $dimensions,
'StartTime' => strtotime('-5 hours'),
'EndTime' => strtotime('now'),
'Period' => 60,
'Statistics' => array('Average'),
));
var_dump($memoryResult);
?>
Running this code produces the following output. As you can see CPUUtilization returns the desired results however MemoryUtilization returns an empty array.
[ec2-user@ip-10-0-52-163 php-sdk]$ php get-cloudwatch-metrics.php
object(Guzzle\Service\Resource\Model)#100 (2) {
["structure":protected]=>
NULL
["data":protected]=>
array(3) {
["Datapoints"]=>
array(0) {
}
["Label"]=>
string(14) "CPUUtilization"
["ResponseMetadata"]=>
array(1) {
["RequestId"]=>
string(36) "63dc311e-bdaf-11e6-8143-f96eefa160b8"
}
}
}
object(Guzzle\Service\Resource\Model)#102 (2) {
["structure":protected]=>
NULL
["data":protected]=>
array(3) {
["Datapoints"]=>
array(0) {
}
["Label"]=>
string(17) "MemoryUtilization"
["ResponseMetadata"]=>
array(1) {
["RequestId"]=>
string(36) "63dec890-bdaf-11e6-b04d-0707ed181fab"
}
}
}
Note, I'm using the typical AWS custom metric Perl scripts to send custom metrics to CloudWatch.
* * * * * ~/aws-scripts-mon/mon-put-instance-data.pl --mem-util --mem-used --mem-avail --disk-space-util --disk-path=/ --from-cron --aggregated --auto-scaling
ImageIdandAutoScalingGroupNamein your dimensions? It should be sufficient to useInstanceId. - John Rotenstein