如何通过PHP SDK访问AWS CloudWatch自定义指标

时间:2016-12-09 01:36:07

标签: php amazon-web-services amazon-ec2 aws-sdk amazon-cloudwatch

我能够检索所包含指标的指标信息,但是对于自定义指标,例如基于每个/实例的CPUUtilization,我无法获取信息。我不确定我是否使用了正确的NameSpace,或者我需要在Dimensions字段中包含其他信息。有人知道怎么做吗?

这是我的代码:

<?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);
?>

运行此代码会产生以下输出。正如您所看到的,CPUUtilization返回所需的结果,但MemoryUtilization返回一个空数组。

[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"
    }
  }
}

注意,我使用典型的AWS自定义指标Perl脚本将自定义指标发送到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 个答案:

答案 0 :(得分:2)

我明白了。快速检查将指标放入CloudWatch的自定义指标实例脚本会显示“System / Linux”的命名空间,但在CloudWatch控制台中,它会显示“Linux System”。将代码切换到System / Linux会产生预期的结果!

[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"
    }
  }
}

以下输出:

_token

答案 1 :(得分:0)

我以前有同样的问题, 问题在于维度(我应该使用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')]);