截至 2024 年 7 月 31 日, AWS SDK for Java 1.x 已進入維護模式,並將end-of-support
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
使用 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; } }
警示清單可以通過呼叫getMetricAlarms
在DescribeAlarmsResult,會傳回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);
詳細資訊
-
正在建立Amazon CloudWatch警報中的Amazon CloudWatch使用者指南
-
PutMetricAlarm中的Amazon CloudWatchAPI 參考
-
DescribeAlarms中的Amazon CloudWatchAPI 參考
-
DeleteAlarms中的Amazon CloudWatchAPI 參考