CloudWatch 使用第 3 AWS SDK for PHP 版在 Amazon 中發布自定義指標 - AWS SDK for PHP

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

CloudWatch 使用第 3 AWS SDK for PHP 版在 Amazon 中發布自定義指標

指標是有關您系統效能的資料。警示會在您指定的期間,監看單一指標。警示會根據在數個期間與指定閾值相關的指標值,來執行一個或多個動作。

下列範例示範如何:

所有的範例程式碼都可以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();