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

The following examples show how to use com.amazonaws.services.cloudwatch.model.PutMetricAlarmResult. 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: CloudWatchClient.java    From titus-control-plane with Apache License 2.0 4 votes vote down vote up
@Override
public Observable<String> createOrUpdateAlarm(String policyRefId,
                                              String jobId,
                                              AlarmConfiguration alarmConfiguration,
                                              String autoScalingGroup,
                                              List<String> actions) {
    Dimension dimension = new Dimension();
    dimension.setName(AUTO_SCALING_GROUP_NAME);
    dimension.setValue(autoScalingGroup);

    String cloudWatchName = buildCloudWatchName(policyRefId, jobId);

    PutMetricAlarmRequest putMetricAlarmRequest = new PutMetricAlarmRequest();
    if (alarmConfiguration.getActionsEnabled().isPresent()) {
        putMetricAlarmRequest.setActionsEnabled(alarmConfiguration.getActionsEnabled().get());
    }
    putMetricAlarmRequest.setAlarmActions(actions);
    putMetricAlarmRequest.setAlarmName(cloudWatchName);
    putMetricAlarmRequest.setDimensions(Arrays.asList(dimension));
    putMetricAlarmRequest.setNamespace(alarmConfiguration.getMetricNamespace());
    putMetricAlarmRequest.setComparisonOperator(alarmConfiguration.getComparisonOperator().name());
    putMetricAlarmRequest.setStatistic(alarmConfiguration.getStatistic().name());
    putMetricAlarmRequest.setEvaluationPeriods(alarmConfiguration.getEvaluationPeriods());
    putMetricAlarmRequest.setPeriod(alarmConfiguration.getPeriodSec());
    putMetricAlarmRequest.setThreshold(alarmConfiguration.getThreshold());
    putMetricAlarmRequest.setMetricName(alarmConfiguration.getMetricName());

    return wrapWithExponentialRetry(String.format("createOrUpdateAlarm in policy %s for job %s", policyRefId, jobId),
            Observable.create(emitter ->
                    awsCloudWatch.putMetricAlarmAsync(putMetricAlarmRequest, new AsyncHandler<PutMetricAlarmRequest, PutMetricAlarmResult>() {
                        @Override
                        public void onError(Exception exception) {
                            createErrorCounter.increment();
                            emitter.onError(AutoScalePolicyException.errorCreatingAlarm(policyRefId, exception.getMessage()));
                        }

                        @Override
                        public void onSuccess(PutMetricAlarmRequest request, PutMetricAlarmResult putMetricAlarmResult) {
                            int httpStatusCode = putMetricAlarmResult.getSdkHttpMetadata().getHttpStatusCode();
                            log.info("Created Cloud Watch Alarm {} for {} - status {}", request, jobId, httpStatusCode);
                            // TODO : how to get ARN created by AWS for this resource ? returning cloudWatchName for now
                            createAlarmCounter.increment();
                            emitter.onNext(cloudWatchName);
                            emitter.onCompleted();
                        }
                    }), Emitter.BackpressureMode.NONE));
}
 
Example #2
Source File: PutMetricAlarm.java    From aws-doc-sdk-examples with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) {

        final String USAGE =
            "To run this example, supply an alarm name and instance id\n" +
            "Ex: DeleteAlarm <alarm-name> <instance-id>\n";

        if (args.length != 2) {
            System.out.println(USAGE);
            System.exit(1);
        }

        String alarmName = args[0];
        String instanceId = args[1];

        final AmazonCloudWatch cw =
            AmazonCloudWatchClientBuilder.defaultClient();

        Dimension dimension = new Dimension()
            .withName("InstanceId")
            .withValue(instanceId);

        PutMetricAlarmRequest request = new PutMetricAlarmRequest()
            .withAlarmName(alarmName)
            .withComparisonOperator(
                ComparisonOperator.GreaterThanThreshold)
            .withEvaluationPeriods(1)
            .withMetricName("CPUUtilization")
            .withNamespace("AWS/EC2")
            .withPeriod(60)
            .withStatistic(Statistic.Average)
            .withThreshold(70.0)
            .withActionsEnabled(false)
            .withAlarmDescription(
                "Alarm when server CPU utilization exceeds 70%")
            .withUnit(StandardUnit.Seconds)
            .withDimensions(dimension);

        PutMetricAlarmResult response = cw.putMetricAlarm(request);

        System.out.printf(
            "Successfully created alarm with name %s", alarmName);

    }
 
Example #3
Source File: NoopCloudWatch.java    From dynamodb-cross-region-library with Apache License 2.0 4 votes vote down vote up
@Override
public PutMetricAlarmResult putMetricAlarm(PutMetricAlarmRequest putMetricAlarmRequest) {
    return null;
}