Search in sources :

Example 1 with CloudWatchException

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

the class EnableAlarmActions method enableActions.

// snippet-start:[cloudwatch.java2.enable_alarm_actions.main]
public static void enableActions(CloudWatchClient cw, String alarm) {
    try {
        EnableAlarmActionsRequest request = EnableAlarmActionsRequest.builder().alarmNames(alarm).build();
        cw.enableAlarmActions(request);
        System.out.printf("Successfully enabled actions on alarm %s", alarm);
    } catch (CloudWatchException e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}
Also used : EnableAlarmActionsRequest(software.amazon.awssdk.services.cloudwatch.model.EnableAlarmActionsRequest) CloudWatchException(software.amazon.awssdk.services.cloudwatch.model.CloudWatchException)

Example 2 with CloudWatchException

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

the class PutMetricData method putMetData.

// snippet-start:[cloudwatch.java2.put_metric_data.main]
public static void putMetData(CloudWatchClient cw, Double dataPoint) {
    try {
        Dimension dimension = Dimension.builder().name("UNIQUE_PAGES").value("URLS").build();
        // Set an Instant object
        String time = ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_INSTANT);
        Instant instant = Instant.parse(time);
        MetricDatum datum = MetricDatum.builder().metricName("PAGES_VISITED").unit(StandardUnit.NONE).value(dataPoint).timestamp(instant).dimensions(dimension).build();
        PutMetricDataRequest request = PutMetricDataRequest.builder().namespace("SITE/TRAFFIC").metricData(datum).build();
        cw.putMetricData(request);
    } catch (CloudWatchException e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
    System.out.printf("Successfully put data point %f", dataPoint);
}
Also used : CloudWatchException(software.amazon.awssdk.services.cloudwatch.model.CloudWatchException) PutMetricDataRequest(software.amazon.awssdk.services.cloudwatch.model.PutMetricDataRequest) Instant(java.time.Instant) MetricDatum(software.amazon.awssdk.services.cloudwatch.model.MetricDatum) Dimension(software.amazon.awssdk.services.cloudwatch.model.Dimension)

Example 3 with CloudWatchException

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

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

the class DescribeSubscriptionFilters method describeFilters.

// snippet-start:[cloudwatch.java2.describe_subscription_filters.main]
public static void describeFilters(CloudWatchLogsClient logs, String logGroup) {
    try {
        boolean done = false;
        String newToken = null;
        while (!done) {
            DescribeSubscriptionFiltersResponse response;
            if (newToken == null) {
                DescribeSubscriptionFiltersRequest request = DescribeSubscriptionFiltersRequest.builder().logGroupName(logGroup).limit(1).build();
                response = logs.describeSubscriptionFilters(request);
            } else {
                DescribeSubscriptionFiltersRequest request = DescribeSubscriptionFiltersRequest.builder().nextToken(newToken).logGroupName(logGroup).limit(1).build();
                response = logs.describeSubscriptionFilters(request);
            }
            for (SubscriptionFilter filter : response.subscriptionFilters()) {
                System.out.printf("Retrieved filter with name %s, " + "pattern %s " + "and destination arn %s", filter.filterName(), filter.filterPattern(), filter.destinationArn());
                System.out.println("");
            }
            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 : SubscriptionFilter(software.amazon.awssdk.services.cloudwatchlogs.model.SubscriptionFilter) CloudWatchException(software.amazon.awssdk.services.cloudwatch.model.CloudWatchException) DescribeSubscriptionFiltersResponse(software.amazon.awssdk.services.cloudwatchlogs.model.DescribeSubscriptionFiltersResponse) DescribeSubscriptionFiltersRequest(software.amazon.awssdk.services.cloudwatchlogs.model.DescribeSubscriptionFiltersRequest)

Example 5 with CloudWatchException

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

the class GetMetricData method getMetData.

// snippet-start:[cloudwatch.java2.get_metric_alarm.main]
public static void getMetData(CloudWatchClient cw) {
    try {
        // Set the date
        Instant start = Instant.parse("2019-10-23T10:12:35Z");
        Instant endDate = Instant.now();
        Metric met = Metric.builder().metricName("DiskReadBytes").namespace("AWS/EC2").build();
        MetricStat metStat = MetricStat.builder().stat("Minimum").period(60).metric(met).build();
        MetricDataQuery dataQUery = MetricDataQuery.builder().metricStat(metStat).id("foo2").returnData(true).build();
        List<MetricDataQuery> dq = new ArrayList();
        dq.add(dataQUery);
        GetMetricDataRequest getMetReq = GetMetricDataRequest.builder().maxDatapoints(100).startTime(start).endTime(endDate).metricDataQueries(dq).build();
        GetMetricDataResponse response = cw.getMetricData(getMetReq);
        List<MetricDataResult> data = response.metricDataResults();
        for (int i = 0; i < data.size(); i++) {
            MetricDataResult item = (MetricDataResult) data.get(i);
            System.out.println("The label is " + item.label());
            System.out.println("The status code is " + item.statusCode().toString());
        }
    } catch (CloudWatchException e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}
Also used : CloudWatchException(software.amazon.awssdk.services.cloudwatch.model.CloudWatchException) GetMetricDataRequest(software.amazon.awssdk.services.cloudwatch.model.GetMetricDataRequest) Instant(java.time.Instant) MetricStat(software.amazon.awssdk.services.cloudwatch.model.MetricStat) ArrayList(java.util.ArrayList) MetricDataResult(software.amazon.awssdk.services.cloudwatch.model.MetricDataResult) Metric(software.amazon.awssdk.services.cloudwatch.model.Metric) MetricDataQuery(software.amazon.awssdk.services.cloudwatch.model.MetricDataQuery) GetMetricDataResponse(software.amazon.awssdk.services.cloudwatch.model.GetMetricDataResponse)

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