使用CloudWatch鬧鐘 - AWS SDK for Java 2.x

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

使用CloudWatch鬧鐘

建立警示

要根據CloudWatch指標創建警報,請調用PutMetricAlarmRequest填充了警報條件 CloudWatchClient的putMetricAlarm方法。

匯入

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.cloudwatch.CloudWatchClient; import software.amazon.awssdk.services.cloudwatch.model.Dimension; import software.amazon.awssdk.services.cloudwatch.model.PutMetricAlarmRequest; import software.amazon.awssdk.services.cloudwatch.model.ComparisonOperator; import software.amazon.awssdk.services.cloudwatch.model.Statistic; import software.amazon.awssdk.services.cloudwatch.model.StandardUnit; import software.amazon.awssdk.services.cloudwatch.model.CloudWatchException;

Code

public static void putMetricAlarm(CloudWatchClient cw, String alarmName, String instanceId) { try { Dimension dimension = Dimension.builder() .name("InstanceId") .value(instanceId).build(); PutMetricAlarmRequest request = PutMetricAlarmRequest.builder() .alarmName(alarmName) .comparisonOperator( ComparisonOperator.GREATER_THAN_THRESHOLD) .evaluationPeriods(1) .metricName("CPUUtilization") .namespace("AWS/EC2") .period(60) .statistic(Statistic.AVERAGE) .threshold(70.0) .actionsEnabled(false) .alarmDescription( "Alarm when server CPU utilization exceeds 70%") .unit(StandardUnit.SECONDS) .dimensions(dimension) .build(); cw.putMetricAlarm(request); System.out.printf( "Successfully created alarm with name %s", alarmName); } catch (CloudWatchException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }

請參閱(詳見)的完整實例 GitHub。

列出警示

若要列出您已建立的CloudWatch警示,請使用可用來設定結果選項的describeAlarms方法來呼叫方法。 CloudWatchClient DescribeAlarmsRequest

匯入

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.cloudwatch.CloudWatchClient; import software.amazon.awssdk.services.cloudwatch.model.CloudWatchException; import software.amazon.awssdk.services.cloudwatch.model.DescribeAlarmsRequest; import software.amazon.awssdk.services.cloudwatch.model.DescribeAlarmsResponse; import software.amazon.awssdk.services.cloudwatch.model.MetricAlarm;

Code

public static void desCWAlarms( CloudWatchClient cw) { try { boolean done = false; String newToken = null; while(!done) { DescribeAlarmsResponse response; if (newToken == null) { DescribeAlarmsRequest request = DescribeAlarmsRequest.builder().build(); response = cw.describeAlarms(request); } else { DescribeAlarmsRequest request = DescribeAlarmsRequest.builder() .nextToken(newToken) .build(); response = cw.describeAlarms(request); } for(MetricAlarm alarm : response.metricAlarms()) { System.out.printf("\n Retrieved alarm %s", alarm.alarmName()); } if(response.nextToken() == null) { done = true; } else { newToken = response.nextToken(); } } } catch (CloudWatchException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } System.out.printf("Done"); }

可以通過調用MetricAlarms返回的來獲取警報列表describeAlarmsDescribeAlarmsResponse

結果可能會分頁。若要擷取下一批次結果,請在回應物件上呼叫 nextToken,並使用字符值來建置新的請求物件。然後以新的請求再次呼叫 describeAlarms 方法。

注意

您也可以使用 CloudWatchClient的describeAlarmsForMetric方法擷取特定量度的警示。其用法類似於 describeAlarms

請參閱(詳見)的完整實例 GitHub。

刪除警示

要刪除CloudWatch警報,請使用DeleteAlarmsRequest包含要刪除的一個或多個警報名稱來調用的deleteAlarms方法。 CloudWatchClient

匯入

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.cloudwatch.CloudWatchClient; import software.amazon.awssdk.services.cloudwatch.model.CloudWatchException; import software.amazon.awssdk.services.cloudwatch.model.DeleteAlarmsRequest;

Code

public static void deleteCWAlarm(CloudWatchClient cw, String alarmName) { try { DeleteAlarmsRequest request = DeleteAlarmsRequest.builder() .alarmNames(alarmName) .build(); cw.deleteAlarms(request); System.out.printf("Successfully deleted alarm %s", alarmName); } catch (CloudWatchException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }

請參閱(詳見)的完整實例 GitHub。

其他資訊