com.amazonaws.services.cloudwatch.model.MetricAlarm Java Examples

The following examples show how to use com.amazonaws.services.cloudwatch.model.MetricAlarm. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DescribeAlarms.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {

        final AmazonCloudWatch cw =
            AmazonCloudWatchClientBuilder.defaultClient();

        boolean done = false;
        DescribeAlarmsRequest request = new DescribeAlarmsRequest();

        while(!done) {

            DescribeAlarmsResult response = cw.describeAlarms(request);

            for(MetricAlarm alarm : response.getMetricAlarms()) {
                System.out.printf("Retrieved alarm %s", alarm.getAlarmName());
            }

            request.setNextToken(response.getNextToken());

            if(response.getNextToken() == null) {
                done = true;
            }
        }
    }
 
Example #2
Source File: AwsCloudWatchService.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
private Stream<List<String>> getExistingCloudWatchAlarms(String regionName, AwsCredentialView credentialView, List<String> alarmNames) {
    Stream<List<String>> filteredAlarmNamesStream;
    try {
        DescribeAlarmsRequest request = new DescribeAlarmsRequest().withAlarmNames(alarmNames).withMaxRecords(maxBatchsize);
        AmazonCloudWatchClient amazonCloudWatchClient = awsClient.createCloudWatchClient(credentialView, regionName);
        List<String> filteredAlarmNames = amazonCloudWatchClient.describeAlarms(request).getMetricAlarms().stream()
                .map(MetricAlarm::getAlarmName)
                .collect(Collectors.toList());
        filteredAlarmNamesStream = Stream.of(filteredAlarmNames);
        LOGGER.debug("Checking cloudwatch alarms [{}] for existence and found [{}]", alarmNames, filteredAlarmNames);
    } catch (AmazonCloudWatchException acwe) {
        LOGGER.error("Unable to describe cloudwatch alarms falling back to delete all alarms indivdually [{}]: {}", alarmNames, acwe.getLocalizedMessage());
        filteredAlarmNamesStream = alarmNames.stream()
                .map(alarmName -> List.of(alarmName));
    }
    return filteredAlarmNamesStream;
}
 
Example #3
Source File: GetMetricStaticticsCloudwatchTest.java    From aws-mock with MIT License 5 votes vote down vote up
/**
 * Test GetMetricAlarm.
 */
@Test(timeout = TIMEOUT_LEVEL1)
public final void GetMetricAlarm() {
    log.info("Start GetMetricAlarm Cloudwatch test");

    MetricAlarm metricAlarm = describerAlarmsTest();

    Assert.assertNotNull("metricAlarm should not be null", metricAlarm);
    Assert.assertNotNull("metricAlarm Name should not be null", metricAlarm.getAlarmName());
}
 
Example #4
Source File: CloudWatchBaseTest.java    From aws-mock with MIT License 5 votes vote down vote up
/**
 * describerAlarmsTest to get the data points
 *
 * @return MetricAlarm
 */
protected final MetricAlarm describerAlarmsTest() {
	MetricAlarm metricAlarm = null;
    DescribeAlarmsRequest describeAlarmsRequest = new DescribeAlarmsRequest();
    DescribeAlarmsResult result = amazonCloudWatchClient.describeAlarms(describeAlarmsRequest);
    
    if (result != null && !result.getMetricAlarms().isEmpty()) {
    	metricAlarm = result.getMetricAlarms().get(0);
    }

    return metricAlarm;
}
 
Example #5
Source File: AwsCloudWatchServiceTest.java    From cloudbreak with Apache License 2.0 4 votes vote down vote up
@Test
void testDeleteCloudWatchAlarmsForSystemFailuresWhenOneAlarmHasAlreadyBeenDeleted() {
    String alarm1Name = "i-1-Status-Check-Failed-System";
    String alarm2Name = "i-2-Status-Check-Failed-System";
    String alarm3Name = "i-3-Status-Check-Failed-System";
    String instanceId1 = "i-1";
    String instanceId2 = "i-2";
    String instanceId3 = "i-3";
    MetricAlarm alarm1 = mock(MetricAlarm.class);
    MetricAlarm alarm2 = mock(MetricAlarm.class);
    DescribeAlarmsResult describeAlarmsResult = mock(DescribeAlarmsResult.class);
    AmazonCloudWatchClient cloudWatchClient = mock(AmazonCloudWatchClient.class);
    AwsCredentialView credentialView = mock(AwsCredentialView.class);
    CloudStack stack = mock(CloudStack.class);
    Group group = mock(Group.class);
    CloudInstance instance1 = mock(CloudInstance.class);
    CloudInstance instance2 = mock(CloudInstance.class);
    CloudInstance instance3 = mock(CloudInstance.class);
    List<Group> groups = List.of(group);
    when(stack.getParameters()).thenReturn(PARAMETERS);
    when(stack.getGroups()).thenReturn(groups);
    when(group.getInstances()).thenReturn(List.of(instance1, instance2, instance3));
    when(instance1.getInstanceId()).thenReturn(instanceId1);
    when(instance2.getInstanceId()).thenReturn(instanceId2);
    when(instance3.getInstanceId()).thenReturn(instanceId3);
    when(awsClient.createCloudWatchClient(eq(credentialView), eq(REGION))).thenReturn(cloudWatchClient);
    when(cloudWatchClient.describeAlarms(any())).thenReturn(describeAlarmsResult);
    // alarm 3 was already deleted
    when(describeAlarmsResult.getMetricAlarms()).thenReturn(List.of(alarm1, alarm2));
    when(alarm1.getAlarmName()).thenReturn(alarm1Name);
    when(alarm2.getAlarmName()).thenReturn(alarm2Name);

    underTest.deleteCloudWatchAlarmsForSystemFailures(stack, REGION, credentialView);

    ArgumentCaptor<DescribeAlarmsRequest> captorDescribe = ArgumentCaptor.forClass(DescribeAlarmsRequest.class);
    ArgumentCaptor<DeleteAlarmsRequest> captorDelete = ArgumentCaptor.forClass(DeleteAlarmsRequest.class);
    verify(cloudWatchClient, times(1)).describeAlarms(captorDescribe.capture());
    verify(cloudWatchClient, times(1)).deleteAlarms(captorDelete.capture());
    assertEquals(List.of(alarm1Name, alarm2Name, alarm3Name), captorDescribe.getValue().getAlarmNames());

    // only delete alarms that were not already deleted
    assertEquals(List.of(alarm1Name, alarm2Name), captorDelete.getValue().getAlarmNames());
}