在Amazon SDK for PHP版本 3 中使用亚马逊 CloudWatch 警报 - Amazon SDK for PHP
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

在Amazon SDK for PHP版本 3 中使用亚马逊 CloudWatch 警报

Amazon CloudWatch 警报会在您指定的时间段内监视单个指标。它在多个时间段内根据相对于给定阈值的指标值,执行一项或多项操作。

以下示例演示如何:

的所有示例代码都可以在此Amazon SDK for PHP处找到 GitHub

凭证

运行示例代码之前,请配置您的 Amazon 凭证,如 凭证 中所述。然后导入 Amazon 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();