use of com.google.cloud.monitoring.v3.AlertPolicyServiceClient in project java-docs-samples by GoogleCloudPlatform.
the class AlertSample method listAlertPolicies.
// [START monitoring_alert_list_policies]
private static void listAlertPolicies(String projectId) throws IOException {
try (AlertPolicyServiceClient client = AlertPolicyServiceClient.create()) {
ListAlertPoliciesPagedResponse response = client.listAlertPolicies(ProjectName.of(projectId));
System.out.println("Alert Policies:");
for (AlertPolicy policy : response.iterateAll()) {
System.out.println(String.format("\nPolicy %s\nalert-id: %s", policy.getDisplayName(), policy.getName()));
int channels = policy.getNotificationChannelsCount();
if (channels > 0) {
System.out.println("notification-channels:");
for (int i = 0; i < channels; i++) {
System.out.println("\t" + policy.getNotificationChannels(i));
}
}
if (policy.hasDocumentation() && policy.getDocumentation().getContent() != null) {
System.out.println(policy.getDocumentation().getContent());
}
}
}
}
use of com.google.cloud.monitoring.v3.AlertPolicyServiceClient in project java-docs-samples by GoogleCloudPlatform.
the class AlertSample method replaceChannels.
// [END monitoring_alert_restore_policies]
// [START monitoring_alert_replace_channels]
private static void replaceChannels(String projectId, String alertPolicyId, String[] channelIds) throws IOException {
AlertPolicy.Builder policyBuilder = AlertPolicy.newBuilder().setName(AlertPolicyName.of(projectId, alertPolicyId).toString());
for (String channelId : channelIds) {
policyBuilder.addNotificationChannels(NotificationChannelName.of(projectId, channelId).toString());
}
try (AlertPolicyServiceClient client = AlertPolicyServiceClient.create()) {
AlertPolicy result = client.updateAlertPolicy(FieldMask.newBuilder().addPaths("notification_channels").build(), policyBuilder.build());
System.out.println(String.format("Updated %s", result.getName()));
}
}
use of com.google.cloud.monitoring.v3.AlertPolicyServiceClient in project java-monitoring by googleapis.
the class GetAlertPolicy method getAlertPolicy.
public static void getAlertPolicy(String alertPolicyName) throws ApiException, IOException {
// once, and can be reused for multiple requests.
try (AlertPolicyServiceClient alertPolicyServiceClient = AlertPolicyServiceClient.create()) {
// Gets a single alerting policy
AlertPolicy alertPolicy = alertPolicyServiceClient.getAlertPolicy(alertPolicyName);
System.out.format("alert policy retrieved successfully:%s", alertPolicy.getName());
}
}
use of com.google.cloud.monitoring.v3.AlertPolicyServiceClient in project java-monitoring by googleapis.
the class ListAlertPolicy method listAlertPolicy.
public static void listAlertPolicy(String projectId) throws ApiException, IOException {
// once, and can be reused for multiple requests.
try (AlertPolicyServiceClient alertPolicyServiceClient = AlertPolicyServiceClient.create()) {
// Lists the existing alerting policies for the project.
ListAlertPoliciesRequest listAlertPoliciesRequest = ListAlertPoliciesRequest.newBuilder().setName(ProjectName.of(projectId).toString()).build();
// process response
AlertPolicyServiceClient.ListAlertPoliciesPagedResponse response = alertPolicyServiceClient.listAlertPolicies(listAlertPoliciesRequest);
// List all the policy.
response.iterateAll().forEach(alertPolicy -> System.out.format("success! alert policy %s is policyId %s%n", alertPolicy.getDisplayName(), alertPolicy.getName()));
}
}
use of com.google.cloud.monitoring.v3.AlertPolicyServiceClient in project java-monitoring by googleapis.
the class CreateAlertPolicy method createAlertPolicy.
public static void createAlertPolicy(String projectId, String alertPolicyName) throws ApiException, IOException {
// once, and can be reused for multiple requests.
try (AlertPolicyServiceClient alertPolicyServiceClient = AlertPolicyServiceClient.create()) {
ProjectName name = ProjectName.of(projectId);
String metricType = "compute.googleapis.com/instance/cpu/utilization";
String resourceType = "gce_instance";
// A Filter that identifies which time series should be compared with the threshold
String metricFilter = "metric.type=" + '"' + metricType + '"' + " AND " + "resource.type=" + '"' + resourceType + '"';
// Build Duration
Duration aggregationDuration = Duration.newBuilder().setSeconds(60).build();
// Build Aggregation
Aggregation aggregation = Aggregation.newBuilder().setAlignmentPeriod(aggregationDuration).setCrossSeriesReducer(Aggregation.Reducer.REDUCE_MEAN).setPerSeriesAligner(Aggregation.Aligner.ALIGN_MAX).build();
// Build MetricThreshold
AlertPolicy.Condition.MetricThreshold metricThreshold = AlertPolicy.Condition.MetricThreshold.newBuilder().setComparison(ComparisonType.COMPARISON_GT).addAggregations(aggregation).setFilter(metricFilter).setDuration(aggregationDuration).build();
// Construct Condition object
AlertPolicy.Condition alertPolicyCondition = AlertPolicy.Condition.newBuilder().setDisplayName(alertPolicyName).setConditionThreshold(metricThreshold).build();
// Build an alert policy
AlertPolicy alertPolicy = AlertPolicy.newBuilder().setDisplayName(alertPolicyName).addConditions(alertPolicyCondition).setCombiner(AlertPolicy.ConditionCombinerType.AND).build();
// Create an alert policy
AlertPolicy actualAlertPolicy = alertPolicyServiceClient.createAlertPolicy(name, alertPolicy);
System.out.format("alert policy created:%s", actualAlertPolicy.getName());
}
}
Aggregations