本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
使用CloudWatch鬧鐘
建立警示
要根據CloudWatch指標創建警報,請調用PutMetricAlarmRequestputMetricAlarm
方法。
匯入
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); } }
請參閱(詳見)的完整實例
列出警示
若要列出您已建立的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
返回的來獲取警報列表describeAlarms
。DescribeAlarmsResponse
結果可能會分頁。若要擷取下一批次結果,請在回應物件上呼叫 nextToken
,並使用字符值來建置新的請求物件。然後以新的請求再次呼叫 describeAlarms
方法。
注意
您也可以使用 CloudWatchClient的describeAlarmsForMetric
方法擷取特定量度的警示。其用法類似於 describeAlarms
。
請參閱(詳見)的完整實例
刪除警示
要刪除CloudWatch警報,請使用DeleteAlarmsRequestdeleteAlarms
方法。 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); } }
請參閱(詳見)的完整實例
其他資訊
-
PutMetricAlarm在 Amazon CloudWatch API 參考資料中
-
DescribeAlarms在 Amazon CloudWatch API 參考資料中
-
DeleteAlarms在 Amazon CloudWatch API 參考資料中