forked from awsdocs/aws-doc-sdk-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDescribeAlarmsForMetric.php
More file actions
103 lines (89 loc) · 3.12 KB
/
DescribeAlarmsForMetric.php
File metadata and controls
103 lines (89 loc) · 3.12 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
<?php
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
// snippet-start:[cloudwatch.php.describe_alarms_metric.complete]
// snippet-start:[cloudwatch.php.describe_alarms_metric.import]
require 'vendor/autoload.php';
use Aws\CloudWatch\CloudWatchClient;
use Aws\Exception\AwsException;
// snippet-end:[cloudwatch.php.describe_alarms_metric.import]
/* ////////////////////////////////////////////////////////////////////////////
* Purpose: Provides a list of alarms that match the specified
* metric in Amazon CloudWatch.
*
* Inputs:
* - $cloudWatchClient: An initialized CloudWatch client.
* - $metricName: The name of the metric, for example, BucketSizeBytes.
* - $namespace: The related namespace for the metric, for example, AWS/S3.
* - $dimensions: Any related dimensions, if the metric requires them to
* be specified.
*
* Returns: Information about any matching alarms found;
* otherwise, the error message.
* ///////////////////////////////////////////////////////////////////////// */
// snippet-start:[cloudwatch.php.describe_alarms_metric.main]
function describeAlarmsForMetric(
$cloudWatchClient,
$metricName,
$namespace,
$dimensions
) {
try {
$result = $cloudWatchClient->describeAlarmsForMetric([
'MetricName' => $metricName,
'Namespace' => $namespace,
'Dimensions' => $dimensions
]);
$message = '';
if (isset($result['@metadata']['effectiveUri'])) {
$message .= 'At the effective URI of ' .
$result['@metadata']['effectiveUri'] . ":\n\n";
if (
(isset($result['MetricAlarms'])) and
(count($result['MetricAlarms']) > 0)
) {
$message .= 'Matching alarms for ' . $metricName . ":\n\n";
foreach ($result['MetricAlarms'] as $alarm) {
$message .= $alarm['AlarmName'] . "\n";
}
} else {
$message .= 'No matching alarms found for ' . $metricName . '.';
}
} else {
$message .= 'No matching alarms found for ' . $metricName . '.';
}
return $message;
} catch (AwsException $e) {
return 'Error: ' . $e->getAwsErrorMessage();
}
}
function describeTheAlarmsForMetric()
{
$metricName = 'BucketSizeBytes';
$namespace = 'AWS/S3';
$dimensions = [
[
'Name' => 'StorageType',
'Value' => 'StandardStorage'
],
[
'Name' => 'BucketName',
'Value' => 'amzn-s3-demo-bucket'
]
];
$cloudWatchClient = new CloudWatchClient([
'profile' => 'default',
'region' => 'us-east-1',
'version' => '2010-08-01'
]);
echo describeAlarmsForMetric(
$cloudWatchClient,
$metricName,
$namespace,
$dimensions
);
}
// Uncomment the following line to run this code in an AWS account.
// describeTheAlarmsForMetric();
// snippet-end:[cloudwatch.php.describe_alarms_metric.main]
// snippet-end:[cloudwatch.php.describe_alarms_metric.complete]