AWS SDK for PHP
Developer Guide

The AWS Documentation website is getting a new look!
Try it now and let us know what you think. Switch to the new look >>

You can return to the original look by selecting English in the language selector above.

Getting Metrics from Amazon CloudWatch with AWS SDK for PHP Version 3

Metrics are data about the performance of your systems. You can enable detailed monitoring of some resources, such as your Amazon EC2 instances, or of your own application metrics.

The following examples show how to:

All the example code for the AWS SDK for PHP Version 3 is available here on GitHub.

Credentials

Before running the example code, configure your AWS credentials, as described in Credentials for the AWS SDK for PHP Version 3. Then import the AWS SDK for PHP, as described in Basic Usage Patterns of the AWS SDK for PHP Version 3.

List Metrics

Imports

require 'vendor/autoload.php'; use Aws\CloudWatch\CloudWatchClient; use Aws\Exception\AwsException;

Sample Code

$client = new Aws\CloudWatch\CloudWatchClient([ 'profile' => 'default', 'region' => 'us-west-2', 'version' => '2010-08-01' ]); try { $result = $client->listMetrics(); var_dump($result); } catch (AwsException $e) { // output error message if fails error_log($e->getMessage()); }

Retrieve Alarms for a Metric

Imports

require 'vendor/autoload.php'; use Aws\CloudWatch\CloudWatchClient; use Aws\Exception\AwsException;

Sample Code

$client = new Aws\CloudWatch\CloudWatchClient([ 'profile' => 'default', 'region' => 'us-west-2', 'version' => '2010-08-01' ]); try { $result = $client->describeAlarmsForMetric(array( // MetricName is required 'MetricName' => 'ApproximateNumberOfMessagesVisible', // Namespace is required 'Namespace' => 'AWS/SQS', )); var_dump($result); } catch (AwsException $e) { // output error message if fails error_log($e->getMessage()); }

Get Metric Statistics

Imports

require 'vendor/autoload.php'; use Aws\CloudWatch\CloudWatchClient; use Aws\Exception\AwsException;

Sample Code

$client = new Aws\CloudWatch\CloudWatchClient([ 'profile' => 'default', 'region' => 'us-west-2', 'version' => '2010-08-01' ]); try { $result = $client->getMetricStatistics(array( 'Namespace' => 'string', 'MetricName' => 'CloudWatchTests', //StartTime : mixed type: string (date format)|int (unix timestamp)|\DateTime 'StartTime' => strtotime('-1 days'), //EndTime : mixed type: string (date format)|int (unix timestamp)|\DateTime 'EndTime' => strtotime('now'), //The granularity, in seconds, of the returned datapoints. Period must be at least 60 seconds and must be a multiple of 60. The default value is 60 'Period' => 3000, 'Statistics' => array('Maximum', 'Minimum'), )); var_dump($result); } catch (AwsException $e) { // output error message if fails error_log($e->getMessage()); }