Search in sources :

Example 1 with MetricAlarm

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

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

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

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