如何使用PHP SDK根据实例ID获取EC2实例的NetworkIn和NetworkOut的实例带宽使用量。
到目前为止我得到的是..。
<?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 cron作业每小时运行一次,我需要能够获得进出特定EC2实例的带宽,以便记录在内部数据库中。
我已经能够从SDK文档中拼凑出上面所说的东西,但在这里我有点困惑。
我相信我需要的是cloudwatch,所以我更希望它能够通过这个来完成。我知道我可以在每个服务器上安装一个小程序,将带宽使用情况报告到服务器上的一个文件中,然后通过SFTP下载到我们的数据库中,但我更希望它是在实例本身的任何设置之外完成的,以便实例管理员不会导致带宽报告问题。
发布于 2017-01-24 21:59:55
设法让它与..。
<?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);发布于 2017-01-25 00:46:43
如果您尝试使用与AWS相同的方式来计算带宽费用,则使用VPC流日志将是更好、更有说服力的方法。您可以将您的弹性网卡订阅私有网络流日志(价格应该比较便宜,只收取CloudWatch日志费用,流日志是免费的),然后使用AWS SDK使用GetLogEvents从CloudWatch中拉取,然后将总字节数相加。
https://stackoverflow.com/questions/41828952
复制相似问题