简体   繁体   English

通过实例ID获取EC2带宽使用情况

[英]Get EC2 Bandwidth Usage By Instance ID

How do I get the instance bandwidth usage for NetworkIn and NetworkOut for an EC2 instance based on the instance ID using the PHP SDK. 如何使用PHP SDK根据实例ID获取EC2实例的NetworkIn和NetworkOut的实例带宽使用情况。

So far what I have is... 到目前为止,我所拥有的是...

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

I have a PHP cron job running every hour and I need to be able to get the bandwidth in and out for a specific EC2 instance to record in an internal database. 我有一个每小时运行的PHP cron作业,我需要能够获取和输出特定EC2实例的带宽以记录在内部数据库中。

What I have above I have been able to piece together from the SDK documentation but from here I am kinda stumped. 我上面已经可以从SDK文档中整理出来,但是从这里开始我有些困惑。

I believe what I need is cloudwatch so would rather it be able to be done through this. 我相信我需要的是cloudwatch,因此希望可以通过此方法完成。 I know that I can install a small program onto each server to report the bandwidth usage to a file on the server that I then SFTP into to download to our database but would rather it be done externally of any settings within the instance itself so that an instance admin can't cause issues with the bandwidth reporting. 我知道我可以在每台服务器上安装一个小程序,以将带宽使用情况报告给服务器上的一个文件,然后通过SFTP将该文件下载到我们的数据库中,但是宁愿在实例本身内部进行任何设置以在外部进行操作实例管理员不能引起带宽报告问题。

Managed to get it working with... 设法使其与...合作

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

If you're trying to calculate your bandwidth charge the same way AWS would, a better and more conclusive way would be to use VPC Flow Logs. 如果您尝试以与AWS相同的方式来计算带宽费用,则更好和更确定的方法是使用VPC流日志。 You can subscribe your ENI to VPC flow logs (should be pretty cheap, they only charge for CloudWatch Logs costs, flow logs is free) then use the AWS SDK to pull from CloudWatch with GetLogEvents, and then sum up the bytes total. 您可以为ENI订阅VPC流日志(应该很便宜,它们只收取CloudWatch Logs费用,流日志是免费的),然后使用AWS SDK从CloudWatch获取GetLogEvents,然后对字节总数求和。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM