Skip to content

Instantly share code, notes, and snippets.

@masayuki5160
Last active June 23, 2017 04:01
Show Gist options
  • Select an option

  • Save masayuki5160/c57f10f5d4497b8b40f1 to your computer and use it in GitHub Desktop.

Select an option

Save masayuki5160/c57f10f5d4497b8b40f1 to your computer and use it in GitHub Desktop.
AWS SDK For PHPをつかってCloudWatch APIへアクセス
<?php
// Include the SDK using the Composer autoloader
require 'vendor/autoload.php';
use Aws\CloudWatch\CloudWatchClient;
$client = CloudWatchClient::factory(array(
'key' => 'keyをね',
'secret' => '秘密のやつをここに',
'region' => 'ap-northeast-1'
));
$dimensions = array(
array('Name' => 'InstanceId', 'Value' => 'i-hogehoge(コンソールにのってるID)'),// HOGE SERVER
array('Name' => 'InstanceId', 'Value' => 'i-hogehoge(コンソールにのってるID)'),// HOGEHOGE SERVER
);
$result = $client->getMetricStatistics(array(
'Namespace' => 'AWS/EC2',
'MetricName' => 'CPUUtilization',
'Dimensions' => $dimensions,
'StartTime' => strtotime('-1 days'),
'EndTime' => strtotime('now'),
'Period' => 3000,
'Statistics' => array('Maximum', 'Minimum','Average'),
));
var_dump($result);
$ pwd
/home/ec2-user/aws-php-sample
$ php cloudwatch.php
object(Guzzle\Service\Resource\Model)#79 (2) {
["structure":protected]=>
NULL
["data":protected]=>
array(3) {
["Datapoints"]=>
array(29) {
[0]=>
array(5) {
["Timestamp"]=>
string(20) "2014-05-25T03:49:00Z"
["Unit"]=>
string(7) "Percent"
["Minimum"]=>
string(3) "0.0"
["Maximum"]=>
string(4) "3.33"
["Average"]=>
string(6) "1.2992"
}
[1]=>
array(5) {
["Timestamp"]=>
string(20) "2014-05-25T02:09:00Z"
["Unit"]=>
string(7) "Percent"
["Minimum"]=>
string(3) "0.0"
["Maximum"]=>
string(4) "3.33"
["Average"]=>
....
[28]=>
array(5) {
["Timestamp"]=>
string(20) "2014-05-24T21:59:00Z"
["Unit"]=>
string(7) "Percent"
["Minimum"]=>
string(3) "0.0"
["Maximum"]=>
string(4) "3.33"
["Average"]=>
string(6) "1.1998"
}
}
["Label"]=>
string(14) "CPUUtilization"
["ResponseMetadata"]=>
array(1) {
["RequestId"]=>
string(36) "f7c2f31c-e403-11e3-85ff-1fb877523246"
}
}
}
@masayuki5160
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment