使用 Amazon CloudWatch 警报Amazon适 SDK for PHP 发工具包版本 3 - Amazon适用于 PHP 的开发工具包
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 Amazon Web Services 服务入门

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

使用 Amazon CloudWatch 警报Amazon适 SDK for PHP 发工具包版本 3

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

以下示例演示如何:

的所有示例代码Amazon适用 SDK for PHP 开发工具包版本 3(位于 GitHub 上)。

Credentials

运行示例代码之前,请配置您的Amazon凭证,如的凭证Amazon适 SDK for PHP 发工具包版本 3。然后导入AmazonSDK for PHP,如的基本使用模式Amazon适 SDK for PHP 发工具包版本 3

描述警报

导入

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