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");
}
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);
}
}
}
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();
}
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;
}
Aggregations