AWS SDK for PHP버전 3에서 Amazon CloudWatch 알람 사용하기 - AWS SDK for PHP

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

AWS SDK for PHP버전 3에서 Amazon CloudWatch 알람 사용하기

Amazon CloudWatch 알람은 지정한 기간 동안 단일 지표를 감시합니다. 기간 수에 대한 주어진 임계값과 지표 값을 비교하여 하나 이상의 작업을 수행합니다.

다음 예제에서는 다음과 같은 작업을 하는 방법을 보여줍니다.

의 모든 예제 코드는 여기에서 확인할 수 GitHub 있습니다. AWS SDK for PHP

보안 인증 정보

예제 코드를 실행하기 전에 보안 인증에 설명된 대로 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();