使用 CloudWatch 警報 - AWS SDK for Java 1.x

截至 2024 年 7 月 31 日, AWS SDK for Java 1.x 已進入維護模式,並將end-of-support在 2025 年 12 月 31 日送達。我們建議您遷移至 AWS SDK for Java 2.x,以繼續接收新功能、可用性改進和安全性更新。

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

使用 CloudWatch 警報

建立警示

若要根據 CloudWatch 指標,請調用卓越亞馬遜關注客户端的putMetricAlarm方法並搭配PutMetricAlarmRequest充滿了報警條件。

匯入

import com.amazonaws.services.cloudwatch.AmazonCloudWatch; import com.amazonaws.services.cloudwatch.AmazonCloudWatchClientBuilder; import com.amazonaws.services.cloudwatch.model.ComparisonOperator; import com.amazonaws.services.cloudwatch.model.Dimension; import com.amazonaws.services.cloudwatch.model.PutMetricAlarmRequest; import com.amazonaws.services.cloudwatch.model.PutMetricAlarmResult; import com.amazonaws.services.cloudwatch.model.StandardUnit; import com.amazonaws.services.cloudwatch.model.Statistic;

Code

final AmazonCloudWatch cw = AmazonCloudWatchClientBuilder.defaultClient(); Dimension dimension = new Dimension() .withName("InstanceId") .withValue(instanceId); PutMetricAlarmRequest request = new PutMetricAlarmRequest() .withAlarmName(alarmName) .withComparisonOperator( ComparisonOperator.GreaterThanThreshold) .withEvaluationPeriods(1) .withMetricName("CPUUtilization") .withNamespace("{AWS}/EC2") .withPeriod(60) .withStatistic(Statistic.Average) .withThreshold(70.0) .withActionsEnabled(false) .withAlarmDescription( "Alarm when server CPU utilization exceeds 70%") .withUnit(StandardUnit.Seconds) .withDimensions(dimension); PutMetricAlarmResult response = cw.putMetricAlarm(request);

列出警示

若要列出 CloudWatch 警示,請呼叫卓越亞馬遜 CloudWatch 客户端的describeAlarms方法並搭配DescribeAlarmsRequest,您可以使用它來設置結果的選項。

匯入

import com.amazonaws.services.cloudwatch.AmazonCloudWatch; import com.amazonaws.services.cloudwatch.AmazonCloudWatchClientBuilder; import com.amazonaws.services.cloudwatch.model.DescribeAlarmsRequest; import com.amazonaws.services.cloudwatch.model.DescribeAlarmsResult; import com.amazonaws.services.cloudwatch.model.MetricAlarm;

Code

final AmazonCloudWatch cw = AmazonCloudWatchClientBuilder.defaultClient(); boolean done = false; DescribeAlarmsRequest request = new DescribeAlarmsRequest(); while(!done) { DescribeAlarmsResult response = cw.describeAlarms(request); for(MetricAlarm alarm : response.getMetricAlarms()) { System.out.printf("Retrieved alarm %s", alarm.getAlarmName()); } request.setNextToken(response.getNextToken()); if(response.getNextToken() == null) { done = true; } }

警示清單可以通過呼叫getMetricAlarmsDescribeAlarmsResult,會傳回describeAlarms

結果可能會分頁。若要檢索下一批次的結果,請呼叫setNextToken在原始請求對象上,返回值為DescribeAlarmsResult物件的getNextToken方法,並將修改的請求物件傳回另一個describeAlarms

注意

您也可以使用卓越亞馬遜 CloudWatch 客户端的describeAlarmsForMetric方法。其用法類似於 describeAlarms

刪除警示

要刪除 CloudWatch 警報,請調用卓越亞馬遜關注客户端的deleteAlarms方法並搭配DeleteAlarmsRequest,其包含一個或多個您所要刪除警示的名稱。

匯入

import com.amazonaws.services.cloudwatch.AmazonCloudWatch; import com.amazonaws.services.cloudwatch.AmazonCloudWatchClientBuilder; import com.amazonaws.services.cloudwatch.model.DeleteAlarmsRequest; import com.amazonaws.services.cloudwatch.model.DeleteAlarmsResult;

Code

final AmazonCloudWatch cw = AmazonCloudWatchClientBuilder.defaultClient(); DeleteAlarmsRequest request = new DeleteAlarmsRequest() .withAlarmNames(alarm_name); DeleteAlarmsResult response = cw.deleteAlarms(request);

詳細資訊