简体   繁体   English

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

[英]How can I access AWS CloudWatch Custom Metrics via PHP SDK

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. 我能够检索所包含指标的指标信息,但是对于自定义指标,例如基于每个/实例的CPUUtilization,我无法获取信息。 I'm not sure if I'm using the correct NameSpace, or perhaps I need to include additional information in the Dimensions field. 我不确定我是否使用了正确的NameSpace,或者我需要在Dimensions字段中包含其他信息。 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. 正如您所看到的,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"
    }
  }
}

Note, I'm using the typical AWS custom metric Perl scripts to send custom metrics to CloudWatch. 请注意,我使用典型的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

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". 快速检查将指标放入CloudWatch的自定义指标实例脚本会显示“System / Linux”的命名空间,但在CloudWatch控制台中,它会显示“Linux System”。 Switching the code to System/Linux produces the expected results! 将代码切换到System / Linux会产生预期的结果!

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

I used to have same problem, Problem was in dimensions (I should use ClusterName) 我以前有同样的问题,问题在于维度(我应该使用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')]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AWS Cloudwatch PHP SDK返回未排序的指标统计信息 - AWS Cloudwatch PHP SDK returning unsorted metrics statistics 如何从Amazon Cloudwatch获取冰川存储指标? - How can I get glacier storage metrics from Amazon Cloudwatch? 如何通过PHP访问AWS S3? - How do I access AWS S3 via PHP? 什么是“私有 Amazon CloudWatch URL”?AWS SDK for PHP 是否需要 OpenSSL? - What are "private Amazon CloudWatch URLs" & do I need OpenSSL for the AWS SDK for PHP? 如何通过PHP SDK删除facebook页面上的照片? - How can I Delete Photo On facebook Pages via PHP SDK? 适用于PHP的AWS开发工具包生成CloudWatch事件(putEvents)的示例 - Example for AWS SDK for PHP to generate CloudWatch event (putEvents) 如何访问自定义字段Facebook PHP SDK - How to access custom fields Facebook PHP SDK 使用Facebook Graph API,我可以访问相册的JSOD,但不能通过PHP SDK通过api调用 - Using Facebook Graph API I can access the JSOD for my album but not through an api call via PHP SDK 我可以更改提供商以使用 aws / aws-sdk-php 吗? - Can I change the provider to use aws / aws-sdk-php? 如何通过适用于PHP的AWS开发工具包修改运输订单状态? - How to modify Shipping Order Status via AWS SDK for PHP?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM