Search in sources :

Example 1 with DescribeAlarmsResponse

use of software.amazon.awssdk.services.cloudwatch.model.DescribeAlarmsResponse in project aws-doc-sdk-examples by awsdocs.

the class DescribeAlarms method desCWAlarms.

// snippet-start:[cloudwatch.java2.describe_alarms.main]
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");
}
Also used : MetricAlarm(software.amazon.awssdk.services.cloudwatch.model.MetricAlarm) CloudWatchException(software.amazon.awssdk.services.cloudwatch.model.CloudWatchException) DescribeAlarmsRequest(software.amazon.awssdk.services.cloudwatch.model.DescribeAlarmsRequest) DescribeAlarmsResponse(software.amazon.awssdk.services.cloudwatch.model.DescribeAlarmsResponse)

Example 2 with DescribeAlarmsResponse

use of software.amazon.awssdk.services.cloudwatch.model.DescribeAlarmsResponse in project aws-sdk-java-v2 by aws.

the class CloudWatchIntegrationTest method cleanupAlarms.

/**
 * Cleans up any existing alarms before and after running the test suite
 */
@AfterClass
public static void cleanupAlarms() {
    if (cloudwatch != null) {
        DescribeAlarmsResponse describeResult = cloudwatch.describeAlarms(DescribeAlarmsRequest.builder().build());
        Collection<String> toDelete = new LinkedList<>();
        for (MetricAlarm alarm : describeResult.metricAlarms()) {
            if (alarm.metricName().startsWith(CloudWatchIntegrationTest.class.getName())) {
                toDelete.add(alarm.alarmName());
            }
        }
        if (!toDelete.isEmpty()) {
            DeleteAlarmsRequest delete = DeleteAlarmsRequest.builder().alarmNames(toDelete).build();
            cloudwatch.deleteAlarms(delete);
        }
    }
}
Also used : MetricAlarm(software.amazon.awssdk.services.cloudwatch.model.MetricAlarm) DeleteAlarmsRequest(software.amazon.awssdk.services.cloudwatch.model.DeleteAlarmsRequest) DescribeAlarmsResponse(software.amazon.awssdk.services.cloudwatch.model.DescribeAlarmsResponse) LinkedList(java.util.LinkedList) AfterClass(org.junit.AfterClass)

Example 3 with DescribeAlarmsResponse

use of software.amazon.awssdk.services.cloudwatch.model.DescribeAlarmsResponse in project aws-cloudwatch-exporter by asserts.

the class AlarmFetcherTest method sendAlarmsForRegions.

@Test
public void sendAlarmsForRegions() {
    expect(accountIDProvider.getAccountId()).andReturn("123456789").anyTimes();
    expect(scrapeConfigProvider.getScrapeConfig()).andReturn(scrapeConfig);
    expect(scrapeConfig.getRegions()).andReturn(ImmutableSet.of("region")).anyTimes();
    expect(awsClientProvider.getCloudWatchClient("region")).andReturn(cloudWatchClient).anyTimes();
    Capture<RateLimiter.AWSAPICall<DescribeAlarmsResponse>> callbackCapture = Capture.newInstance();
    MetricAlarm alarm = MetricAlarm.builder().alarmName("alarm1").stateValue("ALARM").stateUpdatedTimestamp(now).threshold(10.0).comparisonOperator(ComparisonOperator.GREATER_THAN_THRESHOLD).namespace("AWS/RDS").build();
    DescribeAlarmsResponse response = DescribeAlarmsResponse.builder().metricAlarms(ImmutableList.of(alarm)).build();
    expect(alarmMetricConverter.extractMetricAndEntityLabels(alarm)).andReturn(ImmutableMap.of("label1", "value1"));
    DescribeAlarmsRequest request = DescribeAlarmsRequest.builder().stateValue(StateValue.ALARM).nextToken(null).build();
    expect(cloudWatchClient.describeAlarms(request)).andReturn(response);
    cloudWatchClient.close();
    expect(rateLimiter.doWithRateLimit(eq("CloudWatchClient/describeAlarms"), anyObject(SortedMap.class), capture(callbackCapture))).andReturn(response);
    SortedMap<String, String> labels = new TreeMap<>(new ImmutableMap.Builder<String, String>().put("account_id", "123456789").put("label1", "value1").put("alertname", "alarm1").put("namespace", "AWS/RDS").put("metric_namespace", "AWS/RDS").put("metric_operator", ">").put("region", "region").put("state", "ALARM").put("threshold", "10.0").put("timestamp", now.toString()).build());
    alertsProcessor.sendAlerts(ImmutableList.of(labels));
    replayAll();
    testClass.sendAlarmsForRegions();
    assertEquals(response, callbackCapture.getValue().makeCall());
    verifyAll();
}
Also used : MetricAlarm(software.amazon.awssdk.services.cloudwatch.model.MetricAlarm) SortedMap(java.util.SortedMap) DescribeAlarmsRequest(software.amazon.awssdk.services.cloudwatch.model.DescribeAlarmsRequest) DescribeAlarmsResponse(software.amazon.awssdk.services.cloudwatch.model.DescribeAlarmsResponse) TreeMap(java.util.TreeMap) Test(org.junit.jupiter.api.Test)

Example 4 with DescribeAlarmsResponse

use of software.amazon.awssdk.services.cloudwatch.model.DescribeAlarmsResponse in project aws-cloudwatch-exporter by asserts.

the class AlarmFetcher method getAlarms.

private List<Map<String, String>> getAlarms(String region) {
    List<Map<String, String>> labelsList = new ArrayList<>();
    String[] nextToken = new String[] { null };
    try (CloudWatchClient cloudWatchClient = awsClientProvider.getCloudWatchClient(region)) {
        do {
            DescribeAlarmsResponse response = rateLimiter.doWithRateLimit("CloudWatchClient/describeAlarms", ImmutableSortedMap.of(SCRAPE_REGION_LABEL, region, SCRAPE_OPERATION_LABEL, "CloudWatchClient/describeAlarms"), () -> cloudWatchClient.describeAlarms(DescribeAlarmsRequest.builder().stateValue(StateValue.ALARM).nextToken(nextToken[0]).build()));
            if (response.hasMetricAlarms()) {
                labelsList.addAll(response.metricAlarms().stream().map(metricAlarm -> this.processMetricAlarm(metricAlarm, region)).collect(Collectors.toList()));
            }
            if (response.hasCompositeAlarms()) {
            // TODO: Implement this case
            }
            nextToken[0] = response.nextToken();
        } while (nextToken[0] != null);
    } catch (Exception e) {
        log.error("Failed to build resource metric samples", e);
    }
    return labelsList;
}
Also used : CloudWatchClient(software.amazon.awssdk.services.cloudwatch.CloudWatchClient) ArrayList(java.util.ArrayList) DescribeAlarmsResponse(software.amazon.awssdk.services.cloudwatch.model.DescribeAlarmsResponse) TreeMap(java.util.TreeMap) Map(java.util.Map) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap)

Aggregations

DescribeAlarmsResponse (software.amazon.awssdk.services.cloudwatch.model.DescribeAlarmsResponse)4 MetricAlarm (software.amazon.awssdk.services.cloudwatch.model.MetricAlarm)3 TreeMap (java.util.TreeMap)2 DescribeAlarmsRequest (software.amazon.awssdk.services.cloudwatch.model.DescribeAlarmsRequest)2 ImmutableSortedMap (com.google.common.collect.ImmutableSortedMap)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 SortedMap (java.util.SortedMap)1 AfterClass (org.junit.AfterClass)1 Test (org.junit.jupiter.api.Test)1 CloudWatchClient (software.amazon.awssdk.services.cloudwatch.CloudWatchClient)1 CloudWatchException (software.amazon.awssdk.services.cloudwatch.model.CloudWatchException)1 DeleteAlarmsRequest (software.amazon.awssdk.services.cloudwatch.model.DeleteAlarmsRequest)1