Search in sources :

Example 16 with CloudWatchException

use of software.amazon.awssdk.services.cloudwatch.model.CloudWatchException in project aws-doc-sdk-examples by awsdocs.

the class PutMetricAlarm method putMetricAlarm.

// snippet-start:[cloudwatch.java2.put_metric_alarm.main]
public static void putMetricAlarm(CloudWatchClient cw, String alarmName, String instanceId) {
    try {
        Dimension dimension = Dimension.builder().name("InstanceId").value(instanceId).build();
        PutMetricAlarmRequest request = PutMetricAlarmRequest.builder().alarmName(alarmName).comparisonOperator(ComparisonOperator.GREATER_THAN_THRESHOLD).evaluationPeriods(1).metricName("CPUUtilization").namespace("AWS/EC2").period(60).statistic(Statistic.AVERAGE).threshold(70.0).actionsEnabled(false).alarmDescription("Alarm when server CPU utilization exceeds 70%").unit(StandardUnit.SECONDS).dimensions(dimension).build();
        cw.putMetricAlarm(request);
        System.out.printf("Successfully created alarm with name %s", alarmName);
    } catch (CloudWatchException e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}
Also used : CloudWatchException(software.amazon.awssdk.services.cloudwatch.model.CloudWatchException) PutMetricAlarmRequest(software.amazon.awssdk.services.cloudwatch.model.PutMetricAlarmRequest) Dimension(software.amazon.awssdk.services.cloudwatch.model.Dimension)

Example 17 with CloudWatchException

use of software.amazon.awssdk.services.cloudwatch.model.CloudWatchException in project aws-doc-sdk-examples by awsdocs.

the class PutTargets method putCWTargets.

// snippet-start:[cloudwatch.java2.put_targets.main]
public static void putCWTargets(CloudWatchEventsClient cwe, String ruleName, String functionArn, String targetId) {
    try {
        Target target = Target.builder().arn(functionArn).id(targetId).build();
        PutTargetsRequest request = PutTargetsRequest.builder().targets(target).rule(ruleName).build();
        PutTargetsResponse response = cwe.putTargets(request);
        System.out.printf("Successfully created CloudWatch events target for rule %s", ruleName);
    } catch (CloudWatchException e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}
Also used : Target(software.amazon.awssdk.services.cloudwatchevents.model.Target) CloudWatchException(software.amazon.awssdk.services.cloudwatch.model.CloudWatchException) PutTargetsResponse(software.amazon.awssdk.services.cloudwatchevents.model.PutTargetsResponse) PutTargetsRequest(software.amazon.awssdk.services.cloudwatchevents.model.PutTargetsRequest)

Aggregations

CloudWatchException (software.amazon.awssdk.services.cloudwatch.model.CloudWatchException)17 Instant (java.time.Instant)2 Dimension (software.amazon.awssdk.services.cloudwatch.model.Dimension)2 Metric (software.amazon.awssdk.services.cloudwatch.model.Metric)2 ArrayList (java.util.ArrayList)1 DeleteAlarmsRequest (software.amazon.awssdk.services.cloudwatch.model.DeleteAlarmsRequest)1 DescribeAlarmsRequest (software.amazon.awssdk.services.cloudwatch.model.DescribeAlarmsRequest)1 DescribeAlarmsResponse (software.amazon.awssdk.services.cloudwatch.model.DescribeAlarmsResponse)1 DisableAlarmActionsRequest (software.amazon.awssdk.services.cloudwatch.model.DisableAlarmActionsRequest)1 EnableAlarmActionsRequest (software.amazon.awssdk.services.cloudwatch.model.EnableAlarmActionsRequest)1 GetMetricDataRequest (software.amazon.awssdk.services.cloudwatch.model.GetMetricDataRequest)1 GetMetricDataResponse (software.amazon.awssdk.services.cloudwatch.model.GetMetricDataResponse)1 ListMetricsRequest (software.amazon.awssdk.services.cloudwatch.model.ListMetricsRequest)1 ListMetricsResponse (software.amazon.awssdk.services.cloudwatch.model.ListMetricsResponse)1 MetricAlarm (software.amazon.awssdk.services.cloudwatch.model.MetricAlarm)1 MetricDataQuery (software.amazon.awssdk.services.cloudwatch.model.MetricDataQuery)1 MetricDataResult (software.amazon.awssdk.services.cloudwatch.model.MetricDataResult)1 MetricDatum (software.amazon.awssdk.services.cloudwatch.model.MetricDatum)1 MetricStat (software.amazon.awssdk.services.cloudwatch.model.MetricStat)1 PutMetricAlarmRequest (software.amazon.awssdk.services.cloudwatch.model.PutMetricAlarmRequest)1