forked from awsdocs/aws-doc-sdk-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetMetricStatistics.php
More file actions
173 lines (154 loc) · 4.9 KB
/
Copy pathGetMetricStatistics.php
File metadata and controls
173 lines (154 loc) · 4.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
// snippet-start:[cloudwatch.php.get_metric_stats.complete]
// snippet-start:[cloudwatch.php.get_metric_stats.import]
require 'vendor/autoload.php';
use Aws\CloudWatch\CloudWatchClient;
use Aws\Exception\AwsException;
// snippet-end:[cloudwatch.php.get_metric_stats.import]
/* ////////////////////////////////////////////////////////////////////////////
* Purpose: Provides statistical information for a specified metric
* in Amazon CloudWatch.
*
* Inputs:
* - $cloudWatchClient: An initialized CloudWatch client.
* - $namespace: The metric's namespace.
* - $metricName: The metric's name.
* - $dimensions: Any required dimensions for the specified metric.
* - $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.
*
* Returns: Statistical information for the specific metric;
* otherwise, the error message.
* ///////////////////////////////////////////////////////////////////////// */
// snippet-start:[cloudwatch.php.get_metric_stats.main]
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
]);
$message = '';
if (isset($result['@metadata']['effectiveUri'])) {
$message .= 'For the effective URI at ' .
$result['@metadata']['effectiveUri'] . "\n\n";
if (
(isset($result['Datapoints'])) and
(count($result['Datapoints']) > 0)
) {
$message .= "Datapoints found:\n\n";
foreach ($result['Datapoints'] as $datapoint) {
foreach ($datapoint as $key => $value) {
$message .= $key . ' = ' . $value . "\n";
}
$message .= "\n";
}
} else {
$message .= 'No datapoints found.';
}
} else {
$message .= 'No datapoints found.';
}
return $message;
} catch (AwsException $e) {
return 'Error: ' . $e->getAwsErrorMessage();
}
}
function getTheMetricStatistics()
{
// 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';
$cloudWatchClient = new CloudWatchClient([
'profile' => 'default',
'region' => 'us-east-1',
'version' => '2010-08-01'
]);
echo getMetricStatistics(
$cloudWatchClient,
$namespace,
$metricName,
$dimensions,
$startTime,
$endTime,
$period,
$statistics,
$unit
);
// Another example: average number of bytes of standard storage in the
// specified Amazon S3 bucket each day for the past 3 days.
/*
$namespace = 'AWS/S3';
$metricName = 'BucketSizeBytes';
$dimensions = [
[
'Name' => 'StorageType',
'Value'=> 'StandardStorage'
],
[
'Name' => 'BucketName',
'Value' => 'amzn-s3-demo-bucket'
]
];
$startTime = strtotime('-3 days');
$endTime = strtotime('now');
$period = 86400; // Seconds. (1 day = 86400 seconds.)
$statistics = array('Average');
$unit = 'Bytes';
$cloudWatchClient = new CloudWatchClient([
'profile' => 'default',
'region' => 'us-east-1',
'version' => '2010-08-01'
]);
echo getMetricStatistics($cloudWatchClient, $namespace, $metricName,
$dimensions, $startTime, $endTime, $period, $statistics, $unit);
*/
}
// Uncomment the following line to run this code in an AWS account.
// getTheMetricStatistics();
// snippet-end:[cloudwatch.php.get_metric_stats.main]
// snippet-end:[cloudwatch.php.get_metric_stats.complete]