2018-10-11 14:50:03 -07:00
< ? php
2020-05-08 16:08:11 -07:00
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
2024-02-05 10:49:20 -07:00
2019-01-30 18:45:43 -08:00
// snippet-start:[cloudwatch.php.get_metric_stats.complete]
// snippet-start:[cloudwatch.php.get_metric_stats.import]
2018-10-11 14:50:03 -07:00
require 'vendor/autoload.php' ;
2024-02-05 10:49:20 -07:00
2019-01-30 18:45:43 -08:00
use Aws\CloudWatch\CloudWatchClient ;
2018-10-11 14:50:03 -07:00
use Aws\Exception\AwsException ;
2024-02-05 10:49:20 -07:00
2019-01-30 18:45:43 -08:00
// snippet-end:[cloudwatch.php.get_metric_stats.import]
2024-02-05 10:49:20 -07:00
2020-05-08 16:08:11 -07:00
/* ////////////////////////////////////////////////////////////////////////////
* Purpose: Provides statistical information for a specified metric
* in Amazon CloudWatch.
*
* Inputs:
2020-05-18 11:12:48 -07:00
* - $cloudWatchClient: An initialized CloudWatch client.
2020-05-08 16:08:11 -07:00
* - $namespace: The metric's namespace.
* - $metricName: The metric's name.
* - $dimensions: Any required dimensions for the specified metric.
2020-05-15 10:57:38 -07:00
* - $startTime: The datapoints' start time.
* - $endTime: The datapoints' end time.
* - $period: The time period to report datapoints for.
* - $statistics: The datapoints' measurement type.
* - $unit: The datapoints' unit of measurement.
2020-05-08 16:08:11 -07:00
*
* Returns: Statistical information for the specific metric;
* otherwise, the error message.
* ///////////////////////////////////////////////////////////////////////// */
2024-02-05 10:49:20 -07:00
2019-01-30 19:17:27 -08:00
// snippet-start:[cloudwatch.php.get_metric_stats.main]
2020-05-08 16:08:11 -07:00
function getMetricStatistics (
$cloudWatchClient ,
$namespace ,
$metricName ,
$dimensions ,
$startTime ,
$endTime ,
$period ,
$statistics ,
$unit
) {
try {
$result = $cloudWatchClient -> getMetricStatistics ([
'Namespace' => $namespace ,
'MetricName' => $metricName ,
'Dimensions' => $dimensions ,
'StartTime' => $startTime ,
'EndTime' => $endTime ,
'Period' => $period ,
'Statistics' => $statistics ,
'Unit' => $unit
]);
2024-02-05 10:49:20 -07:00
2020-05-08 16:08:11 -07:00
$message = '' ;
2024-02-05 10:49:20 -07:00
2020-05-15 10:57:38 -07:00
if ( isset ( $result [ '@metadata' ][ 'effectiveUri' ])) {
$message .= 'For the effective URI at ' .
$result [ '@metadata' ][ 'effectiveUri' ] . " \n \n " ;
2024-02-05 10:49:20 -07:00
2020-05-15 10:57:38 -07:00
if (
( isset ( $result [ 'Datapoints' ])) and
( count ( $result [ 'Datapoints' ]) > 0 )
2020-05-08 16:08:11 -07:00
) {
2020-05-15 10:57:38 -07:00
$message .= " Datapoints found: \n \n " ;
2024-02-05 10:49:20 -07:00
2020-05-15 10:57:38 -07:00
foreach ( $result [ 'Datapoints' ] as $datapoint ) {
foreach ( $datapoint as $key => $value ) {
$message .= $key . ' = ' . $value . " \n " ;
}
2024-02-05 10:49:20 -07:00
2020-05-15 10:57:38 -07:00
$message .= " \n " ;
}
} else {
$message .= 'No datapoints found.' ;
2020-05-08 16:08:11 -07:00
}
} else {
2020-05-15 10:57:38 -07:00
$message .= 'No datapoints found.' ;
2020-05-08 16:08:11 -07:00
}
2024-02-05 10:49:20 -07:00
2020-05-08 16:08:11 -07:00
return $message ;
} catch ( AwsException $e ) {
return 'Error: ' . $e -> getAwsErrorMessage ();
2024-02-05 10:49:20 -07:00
}
2020-05-08 16:08:11 -07:00
}
2024-02-05 10:49:20 -07:00
2020-05-08 16:08:11 -07:00
function getTheMetricStatistics ()
{
2020-05-15 10:57:38 -07:00
// Average number of Amazon EC2 vCPUs every 5 minutes within
// the past 3 hours.
$namespace = 'AWS/Usage' ;
$metricName = 'ResourceCount' ;
$dimensions = [
[
'Name' => 'Service' ,
'Value' => 'EC2'
],
[
'Name' => 'Resource' ,
'Value' => 'vCPU'
],
[
'Name' => 'Type' ,
'Value' => 'Resource'
],
[
'Name' => 'Class' ,
'Value' => 'Standard/OnDemand'
]
];
$startTime = strtotime ( '-3 hours' );
$endTime = strtotime ( 'now' );
$period = 300 ; // Seconds. (5 minutes = 300 seconds.)
$statistics = [ 'Average' ];
$unit = 'None' ;
2024-02-05 10:49:20 -07:00
2020-05-15 10:57:38 -07:00
$cloudWatchClient = new CloudWatchClient ([
'profile' => 'default' ,
'region' => 'us-east-1' ,
'version' => '2010-08-01'
]);
2024-02-05 10:49:20 -07:00
2020-05-15 10:57:38 -07:00
echo getMetricStatistics (
$cloudWatchClient ,
$namespace ,
$metricName ,
$dimensions ,
$startTime ,
$endTime ,
$period ,
$statistics ,
$unit
);
2024-02-05 10:49:20 -07:00
2020-05-15 10:57:38 -07:00
// Another example: average number of bytes of standard storage in the
// specified Amazon S3 bucket each day for the past 3 days.
2024-02-05 10:49:20 -07:00
2020-05-15 10:57:38 -07:00
/*
2020-05-08 16:08:11 -07:00
$namespace = 'AWS/S3';
$metricName = 'BucketSizeBytes';
$dimensions = [
[
2020-05-15 10:57:38 -07:00
'Name' => 'StorageType',
2020-05-08 16:08:11 -07:00
'Value'=> 'StandardStorage'
],
[
'Name' => 'BucketName',
2020-05-15 10:57:38 -07:00
'Value' => 'my-bucket'
2020-05-08 16:08:11 -07:00
]
];
$startTime = strtotime('-3 days');
$endTime = strtotime('now');
$period = 86400; // Seconds. (1 day = 86400 seconds.)
$statistics = array('Average');
$unit = 'Bytes';
2024-02-05 10:49:20 -07:00
2020-05-08 16:08:11 -07:00
$cloudWatchClient = new CloudWatchClient([
'profile' => 'default',
'region' => 'us-east-1',
'version' => '2010-08-01'
]);
2024-02-05 10:49:20 -07:00
2020-05-08 16:08:11 -07:00
echo getMetricStatistics($cloudWatchClient, $namespace, $metricName,
$dimensions, $startTime, $endTime, $period, $statistics, $unit);
2020-05-15 10:57:38 -07:00
*/
2020-05-08 16:08:11 -07:00
}
2024-02-05 10:49:20 -07:00
2020-05-08 16:08:11 -07:00
// Uncomment the following line to run this code in an AWS account.
// getTheMetricStatistics();
2019-01-30 18:45:43 -08:00
// snippet-end:[cloudwatch.php.get_metric_stats.main]
// snippet-end:[cloudwatch.php.get_metric_stats.complete]