如何通过PHP SDK访问AWS CloudWatch自定义指标
我能够为包含的度量检索度量信息,但是对于自定义度量,例如基于每个/实例的CPUUtilization,我无法获取信息。我不确定是否使用了正确的名称空间,或者可能需要在“维度”字段中包含其他信息。有人知道怎么做吗 这是我的密码:如何通过PHP SDK访问AWS CloudWatch自定义指标,php,amazon-web-services,amazon-ec2,aws-sdk,amazon-cloudwatch,Php,Amazon Web Services,Amazon Ec2,Aws Sdk,Amazon Cloudwatch,我能够为包含的度量检索度量信息,但是对于自定义度量,例如基于每个/实例的CPUUtilization,我无法获取信息。我不确定是否使用了正确的名称空间,或者可能需要在“维度”字段中包含其他信息。有人知道怎么做吗 这是我的密码: <?php date_default_timezone_set('America/New_York'); require 'vendor/autoload.php'; use Aws\CloudWatch\CloudWatchClient; $cloudWat
<?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);
?>
注意,我正在使用典型的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
我想出来了。快速检查一下我的自定义度量实例脚本(将度量放入CloudWatch)会显示一个名称空间“System/Linux”,但在CloudWatch控制台中它会显示“Linux系统”。将代码切换到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);
输出如下:
[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"
}
}
}
我想出来了。快速检查一下我的自定义度量实例脚本(将度量放入CloudWatch)会显示一个名称空间“System/Linux”,但在CloudWatch控制台中它会显示“Linux系统”。将代码切换到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);
输出如下:
[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"
}
}
}
我以前也有同样的问题, 问题在于维度(我应该使用ClusterName)
我以前也有同样的问题, 问题在于维度(我应该使用ClusterName)
为什么要在维度中包含
ImageId
和AutoScalingGroupName
?使用InstanceId
就足够了。是的,我添加了额外的信息,希望能有所帮助,但运气不好。仅使用实例id就可以生成相同的空数组。还尝试修改开始和结束时间和周期,但仍然没有成功。不过,我在CloudWatch(控制台)中有大量的内存利用信息,所以数据就在那里,只是无法通过这个SDK代码访问它……嗨,John。感谢您的帮助,我找到了答案,请参见下面的答案。为什么要在维度中包含ImageId
和AutoScalingGroupName
?使用InstanceId
就足够了。是的,我添加了额外的信息,希望能有所帮助,但运气不好。仅使用实例id就可以生成相同的空数组。还尝试修改开始和结束时间和周期,但仍然没有成功。不过,我在CloudWatch(控制台)中有大量的内存利用信息,所以数据就在那里,只是无法通过这个SDK代码访问它……嗨,John。谢谢你的帮助,我想出来了,请看下面的答案。