翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。
AWS SDK for PHP バージョン 3 での Amazon CloudWatch アラームの使用
Amazon CloudWatch アラームは、指定した期間にわたって 1 つのメトリクスを監視します。このアラームは、複数の期間にわたる一定のしきい値とメトリクスの値の関係性に基づき、1 つ以上のアクションを実行します。
以下の例では、次の方法を示しています。
-
を使用してアラームを記述しますDescribeAlarms。
-
を使用してアラームを作成しますPutMetricAlarm。
-
を使用してアラームを削除しますDeleteAlarms。
のすべてのサンプルコードAWS SDK for PHPは、 にあります GitHub
認証情報
サンプルコードを実行する前に、AWS の認証情報を設定します (認証情報 を参照)。AWS SDK for PHP からのインポート (基本的な使用法 を参照)。
アラームの記述
インポート
require 'vendor/autoload.php';
use Aws\CloudWatch\CloudWatchClient;
use Aws\Exception\AwsException;
サンプルコード
function describeAlarms($cloudWatchClient)
{
try {
$result = $cloudWatchClient->describeAlarms();
$message = '';
if (isset($result['@metadata']['effectiveUri'])) {
$message .= 'Alarms at the effective URI of ' .
$result['@metadata']['effectiveUri'] . "\n\n";
if (isset($result['CompositeAlarms'])) {
$message .= "Composite alarms:\n";
foreach ($result['CompositeAlarms'] as $alarm) {
$message .= $alarm['AlarmName'] . "\n";
}
} else {
$message .= "No composite alarms found.\n";
}
if (isset($result['MetricAlarms'])) {
$message .= "Metric alarms:\n";
foreach ($result['MetricAlarms'] as $alarm) {
$message .= $alarm['AlarmName'] . "\n";
}
} else {
$message .= 'No metric alarms found.';
}
} else {
$message .= 'No alarms found.';
}
return $message;
} catch (AwsException $e) {
return 'Error: ' . $e->getAwsErrorMessage();
}
}
function describeTheAlarms()
{
$cloudWatchClient = new CloudWatchClient([
'profile' => 'default',
'region' => 'us-east-1',
'version' => '2010-08-01'
]);
echo describeAlarms($cloudWatchClient);
}
// Uncomment the following line to run this code in an AWS account.
// describeTheAlarms();
アラームの作成
インポート
require 'vendor/autoload.php';
use Aws\CloudWatch\CloudWatchClient;
use Aws\Exception\AwsException;
サンプルコード
function putMetricAlarm(
$cloudWatchClient,
$cloudWatchRegion,
$alarmName,
$namespace,
$metricName,
$dimensions,
$statistic,
$period,
$comparison,
$threshold,
$evaluationPeriods
) {
try {
$result = $cloudWatchClient->putMetricAlarm([
'AlarmName' => $alarmName,
'Namespace' => $namespace,
'MetricName' => $metricName,
'Dimensions' => $dimensions,
'Statistic' => $statistic,
'Period' => $period,
'ComparisonOperator' => $comparison,
'Threshold' => $threshold,
'EvaluationPeriods' => $evaluationPeriods
]);
if (isset($result['@metadata']['effectiveUri'])) {
if (
$result['@metadata']['effectiveUri'] ==
'https://monitoring.' . $cloudWatchRegion . '.amazonaws.com'
) {
return 'Successfully created or updated specified alarm.';
} else {
return 'Could not create or update specified alarm.';
}
} else {
return 'Could not create or update specified alarm.';
}
} catch (AwsException $e) {
return 'Error: ' . $e->getAwsErrorMessage();
}
}
function putTheMetricAlarm()
{
$alarmName = 'my-ec2-resources';
$namespace = 'AWS/Usage';
$metricName = 'ResourceCount';
$dimensions = [
[
'Name' => 'Type',
'Value' => 'Resource'
],
[
'Name' => 'Resource',
'Value' => 'vCPU'
],
[
'Name' => 'Service',
'Value' => 'EC2'
],
[
'Name' => 'Class',
'Value' => 'Standard/OnDemand'
]
];
$statistic = 'Average';
$period = 300;
$comparison = 'GreaterThanThreshold';
$threshold = 1;
$evaluationPeriods = 1;
$cloudWatchRegion = 'us-east-1';
$cloudWatchClient = new CloudWatchClient([
'profile' => 'default',
'region' => $cloudWatchRegion,
'version' => '2010-08-01'
]);
echo putMetricAlarm(
$cloudWatchClient,
$cloudWatchRegion,
$alarmName,
$namespace,
$metricName,
$dimensions,
$statistic,
$period,
$comparison,
$threshold,
$evaluationPeriods
);
}
// Uncomment the following line to run this code in an AWS account.
// putTheMetricAlarm();
アラームの削除
インポート
require 'vendor/autoload.php';
use Aws\CloudWatch\CloudWatchClient;
use Aws\Exception\AwsException;
サンプルコード
function deleteAlarms($cloudWatchClient, $alarmNames)
{
try {
$result = $cloudWatchClient->deleteAlarms([
'AlarmNames' => $alarmNames
]);
return 'The specified alarms at the following effective URI have ' .
'been deleted or do not currently exist: ' .
$result['@metadata']['effectiveUri'];
} catch (AwsException $e) {
return 'Error: ' . $e->getAwsErrorMessage();
}
}
function deleteTheAlarms()
{
$alarmNames = array('my-alarm');
$cloudWatchClient = new CloudWatchClient([
'profile' => 'default',
'region' => 'us-east-1',
'version' => '2010-08-01'
]);
echo deleteAlarms($cloudWatchClient, $alarmNames);
}
// Uncomment the following line to run this code in an AWS account.
// deleteTheAlarms();