Php 按实例ID获取EC2带宽使用率
如何使用PHPSDK根据实例ID获取EC2实例的NetworkIn和NetworkOut的实例带宽使用情况 到目前为止我所拥有的是Php 按实例ID获取EC2带宽使用率,php,amazon-web-services,amazon-ec2,amazon-cloudwatch,Php,Amazon Web Services,Amazon Ec2,Amazon Cloudwatch,如何使用PHPSDK根据实例ID获取EC2实例的NetworkIn和NetworkOut的实例带宽使用情况 到目前为止我所拥有的是 <?php require_once("../aws/Sdk.php"); use Aws\CloudWatch\CloudWatchClient; $client = CloudWatchClient::factory(array( 'profile' => 'default', 'region' => 'ap-southe
<?php
require_once("../aws/Sdk.php");
use Aws\CloudWatch\CloudWatchClient;
$client = CloudWatchClient::factory(array(
'profile' => 'default',
'region' => 'ap-southeast-2'
));
$dimensions = array(
array('Name' => 'Prefix', 'Value' => ""),
);
$result = $client->getMetricStatistics(array(
'Namespace' => 'AWSSDKPHP',
'MetricName' => 'NetworkIn',
'Dimensions' => $dimensions,
'StartTime' => strtotime('-1 hour'),
'EndTime' => strtotime('now'),
'Period' => 3000,
'Statistics' => array('Maximum', 'Minimum'),
));
设法让它与
<?php
require '../../aws.phar';
use Aws\CloudWatch\CloudWatchClient;
$cw = CloudWatchClient::factory(array(
'key' => 'your-key-here',
'secret' => 'your-secret-here',
'region' => 'your-region-here',
'version' => 'latest'
));
$metrics = $cw->listMetrics(array('Namespace' => 'AWS/EC2'));
//print_r($metrics);
$statsyo = $cw->getMetricStatistics(array(
'Namespace' => 'AWS/EC2',
'MetricName' => 'NetworkIn',
'Dimensions' => array(array('Name' => 'InstanceId', 'Value' => 'your-instance-id-here')),
'StartTime' => strtotime("2017-01-23 00:00:00"),
'EndTime' => strtotime("2017-01-23 23:59:59"),
'Period' => 86400,
'Statistics' => array('Average'),
'Unit' => 'Bytes'
));
echo($statsyo);
如果您试图以AWS相同的方式计算带宽费用,那么更好、更确切的方法是使用VPC流量日志。您可以将您的ENI订阅到VPC流量日志(应该非常便宜,他们只收取CloudWatch日志的费用,流量日志是免费的),然后使用AWS SDK从CloudWatch获取GetLogEvents,然后汇总字节总数。您的意思是说CloudWatch NetworkIn和NetworkOut统计数据不准确吗?CloudWatch NetworkIn和NetworkOut将包括一些未计费或计量的数据传输类别。以前在本页上有描述,但现在在“数据传输”下找不到。windows授权、DNS等类别不应该对您从CloudWatch获取的数据传输数量产生巨大影响