1
votes

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
2
Why are you including ImageId and AutoScalingGroupName in your dimensions? It should be sufficient to use InstanceId. - John Rotenstein
Yeah, I added that additional info hoping it would help, no luck. Using just instance id alone produces the same empty array. Also tried modifying start and end times and period, still no luck. I do have plenty of MemoryUtilization info in CloudWatch however (in Console), so the data is there, just can't get to it via this SDK code... - ElasticThoughts
Hi John. Appreciate your help, I figured it out, see answer below. - ElasticThoughts

2 Answers

4
votes

I figured it out. A quick check of my custom metric instance scripts that put metrics to CloudWatch show a namespace of "System/Linux", however in the CloudWatch Console it displays "Linux System". Switching the code to System/Linux produces the expected results!

$dimensions = array(
    array('Name' => 'InstanceId', 'Value' => 'i-0c0f546e4d1ef25e1'),
);

$memoryResult = $cloudWatchClient->getMetricStatistics(array(
    'Namespace'  => 'System/Linux',
    'MetricName' => 'MemoryUtilization',
    'Dimensions' => $dimensions,
    'StartTime'  => strtotime('-5 minutes'),
    'EndTime'    => strtotime('now'),
    'Period'     => 60,
    'Statistics' => array('Average'),
));

var_dump($memoryResult);

Output below:

[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(5) {
      [0]=>
      array(3) {
        ["Average"]=>
        string(16) "50.1385233069785"
        ["Unit"]=>
        string(7) "Percent"
        ["Timestamp"]=>
        string(20) "2016-12-10T23:07:00Z"
      }
      [1]=>
      array(3) {
        ["Average"]=>
        string(16) "38.5987663771093"
        ["Unit"]=>
        string(7) "Percent"
        ["Timestamp"]=>
        string(20) "2016-12-10T23:08:00Z"
      }
      [2]=>
      array(3) {
        ["Average"]=>
        string(16) "41.5588687016341"
        ["Unit"]=>
        string(7) "Percent"
        ["Timestamp"]=>
        string(20) "2016-12-10T23:09:00Z"
      }
      [3]=>
      array(3) {
        ["Average"]=>
        string(16) "45.4270517993215"
        ["Unit"]=>
        string(7) "Percent"
        ["Timestamp"]=>
        string(20) "2016-12-10T23:10:00Z"
      }
      [4]=>
      array(3) {
        ["Average"]=>
        string(16) "46.4784461685095"
        ["Unit"]=>
        string(7) "Percent"
        ["Timestamp"]=>
        string(20) "2016-12-10T23:06:00Z"
      }
    }
    ["Label"]=>
    string(17) "MemoryUtilization"
    ["ResponseMetadata"]=>
    array(1) {
      ["RequestId"]=>
      string(36) "07c17abc-bf2e-11e6-a0ce-0f6cd308d7b2"
    }
  }
}
0
votes

I used to have same problem, Problem was in dimensions (I should use ClusterName)

 $dimensions = array(array('Name' => 'ClusterName', 'Value' => env('AWS_CLUSTER_NAME')));
 $metrics = $cloudWatch->GetMetricStatistics(['Namespace' => 'AWS/ECS',
                                              'MetricName' => "MemoryUtilization",
                                              'Period' => 60,
                                              'Dimensions' => $dimensions,
                                              'StartTime' => strtotime('-24 hours'),
                                              'EndTime' =>  strtotime('now'),
                                              'Statistics' => array('Maximum', 'Minimum', 'Average')]);