修改现有警报AWS

时间:2016-02-19 09:39:45

标签: aws-sdk amazon-cloudwatch

我想知道如何阅读和修改所有警报?我目前正面临阅读下一组警报的问题。第一组包含前50个。

    DescribeAlarmsRequest describeAlarmsRequest = new DescribeAlarmsRequest();

    DescribeAlarmsResult alarmsResult = cloudWatch.describeAlarms(describeAlarmsRequest);


    System.out.println(alarmsResult.getMetricAlarms().size());
    System.out.println(alarmsResult.getNextToken());
    DescribeAlarmsRequest describeAlarmsRequest1 = new DescribeAlarmsRequest();
    describeAlarmsRequest1.setNextToken(alarmsResult.getNextToken());
    DescribeAlarmsResult alarmsResult1 = cloudWatch.describeAlarms(describeAlarmsRequest1);
    System.out.println(alarmsResult1.getMetricAlarms().size());

1 个答案:

答案 0 :(得分:1)

我是按照以下方式做到的,并且有效。

public class Alarms {
   private static AmazonCloudWatchClient cloudWatch;
   private static AmazonSNSClient client;
   private static ClientConfiguration clientConfiguration;
   private static final String AWS_KEY = "";
   private static final String AWS_SECRET_KEY = "";

static {

    BasicAWSCredentials credentials = new BasicAWSCredentials(AWS_KEY,AWS_SECRET_KEY);
    cloudWatch = new AmazonCloudWatchClient(credentials);
    clientConfiguration = new ClientConfiguration();
    clientConfiguration.setConnectionTimeout(10000);
    clientConfiguration.setSocketTimeout(30000);
    clientConfiguration.setMaxErrorRetry(5);
    client = new AmazonSNSClient(credentials, clientConfiguration);
}

public static void main(String args[]) {

    cloudWatch.setEndpoint("monitoring.us-east-1.amazonaws.com");

    DescribeAlarmsRequest describeAlarmsRequest = new DescribeAlarmsRequest();
    //describeAlarmsRequest.setStateValue(StateValue.OK);
    DescribeAlarmsResult alarmsResult = cloudWatch.describeAlarms(describeAlarmsRequest);
    List<MetricAlarm> metricAlarmList = new ArrayList<>();
    metricAlarmList.addAll(alarmsResult.getMetricAlarms());
    do {
        describeAlarmsRequest.withNextToken(alarmsResult.getNextToken());
        alarmsResult = cloudWatch.describeAlarms(describeAlarmsRequest);
        metricAlarmList.addAll(alarmsResult.getMetricAlarms());
    } while (alarmsResult.getNextToken() != null);

    int i = metricAlarmList.size();
    System.out.println("size " + i);
    for(MetricAlarm alarm : metricAlarmList){
        System.out.println(i--);
        modifyalarm(alarm);
    }

}

private static void modifyalarm(MetricAlarm alarm) {
    Dimension instanceDimension = new Dimension();
    instanceDimension.setName("InstanceId");
    instanceDimension.setValue(alarm.getAlarmName());

    PutMetricAlarmRequest request = new PutMetricAlarmRequest()
            .withActionsEnabled(true).withAlarmName(alarm.getAlarmName())
            .withComparisonOperator(ComparisonOperator.GreaterThanOrEqualToThreshold)
            .withDimensions(Arrays.asList(instanceDimension))
            .withAlarmActions(getTopicARN())
            .withEvaluationPeriods(5)
            .withPeriod(60)
            .withThreshold(5.0D)
            .withStatistic(Statistic.Average)
            .withMetricName("StatusCheckFailed")
            .withNamespace("AWS/EC2");
    cloudWatch.putMetricAlarm(request);
}

private static String getTopicARN() {
    ListTopicsResult listTopicsResult = client.listTopics();
    String nextToken = listTopicsResult.getNextToken();
    List<Topic> topics = listTopicsResult.getTopics();
    String topicARN = "";

    while (nextToken != null) {
        listTopicsResult = client.listTopics(nextToken);
        nextToken = listTopicsResult.getNextToken();
        topics.addAll(listTopicsResult.getTopics());
    }

    for (Topic topic : topics) {
        if (topic.getTopicArn().contains("status-alarms")) {
            topicARN = topic.getTopicArn();
            break;
        }

    }
    return topicARN;
  }
}