Search in sources :

Example 1 with DescribeAlarmsRequest

use of software.amazon.awssdk.services.cloudwatch.model.DescribeAlarmsRequest 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 DescribeAlarmsRequest

use of software.amazon.awssdk.services.cloudwatch.model.DescribeAlarmsRequest 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)

Aggregations

DescribeAlarmsRequest (software.amazon.awssdk.services.cloudwatch.model.DescribeAlarmsRequest)2 DescribeAlarmsResponse (software.amazon.awssdk.services.cloudwatch.model.DescribeAlarmsResponse)2 MetricAlarm (software.amazon.awssdk.services.cloudwatch.model.MetricAlarm)2 SortedMap (java.util.SortedMap)1 TreeMap (java.util.TreeMap)1 Test (org.junit.jupiter.api.Test)1 CloudWatchException (software.amazon.awssdk.services.cloudwatch.model.CloudWatchException)1