AWS SDK for PHP バージョン 3 CloudWatch を使用した Amazon でのカスタムメトリクスの公開 - AWS SDK for PHP

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

AWS SDK for PHP バージョン 3 CloudWatch を使用した Amazon でのカスタムメトリクスの公開

メトリクスとは、システムのパフォーマンスに関するデータです。1 つのアラームで、指定した期間中、1 つのメトリクスを監視します。このアラームは、複数の期間にわたる一定のしきい値とメトリクスの値の関係性に基づき、1 つ以上のアクションを実行します。

以下の例では、次の方法を示しています。

  • を使用してメトリクスデータを公開しますPutMetricData

  • を使用してアラームを作成しますPutMetricAlarm

のすべてのサンプルコードAWS SDK for PHPは、 にあります GitHub

認証情報

サンプルコードを実行する前に、AWS の認証情報を設定します (認証情報 を参照)。AWS SDK for PHP からのインポート (基本的な使用法 を参照)。

メトリクスデータを発行する

インポート

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

サンプルコード

function putMetricData( $cloudWatchClient, $cloudWatchRegion, $namespace, $metricData ) { try { $result = $cloudWatchClient->putMetricData([ 'Namespace' => $namespace, 'MetricData' => $metricData ]); if (isset($result['@metadata']['effectiveUri'])) { if ( $result['@metadata']['effectiveUri'] == 'https://monitoring.' . $cloudWatchRegion . '.amazonaws.com' ) { return 'Successfully published datapoint(s).'; } else { return 'Could not publish datapoint(s).'; } } else { return 'Error: Could not publish datapoint(s).'; } } catch (AwsException $e) { return 'Error: ' . $e->getAwsErrorMessage(); } } function putTheMetricData() { $namespace = 'MyNamespace'; $metricData = [ [ 'MetricName' => 'MyMetric', 'Timestamp' => 1589228818, // 11 May 2020, 20:26:58 UTC. 'Dimensions' => [ [ 'Name' => 'MyDimension1', 'Value' => 'MyValue1' ], [ 'Name' => 'MyDimension2', 'Value' => 'MyValue2' ] ], 'Unit' => 'Count', 'Value' => 1 ] ]; $cloudWatchRegion = 'us-east-1'; $cloudWatchClient = new CloudWatchClient([ 'profile' => 'default', 'region' => $cloudWatchRegion, 'version' => '2010-08-01' ]); echo putMetricData( $cloudWatchClient, $cloudWatchRegion, $namespace, $metricData ); } // Uncomment the following line to run this code in an AWS account. // putTheMetricData();

アラームの作成

インポート

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();