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