use of com.google.monitoring.v3.AlertPolicy in project java-monitoring by googleapis.
the class UpdateAlertPolicy method updateAlertPolicy.
public static void updateAlertPolicy(String alertPolicyName, String newPolicyName) throws ApiException, IOException {
// once, and can be reused for multiple requests.
try (AlertPolicyServiceClient alertPolicyServiceClient = AlertPolicyServiceClient.create()) {
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();
// build Alert policy condition
AlertPolicy.Condition alertPolicyCondition = AlertPolicy.Condition.newBuilder().setDisplayName(alertPolicyName).setConditionThreshold(metricThreshold).build();
// create alert policy for update
AlertPolicy alertPolicy = AlertPolicy.newBuilder().setName(alertPolicyName).setDisplayName(newPolicyName).addConditions(alertPolicyCondition).setCombiner(AlertPolicy.ConditionCombinerType.AND).build();
// create update alert policy request
UpdateAlertPolicyRequest updateAlertPolicyRequest = UpdateAlertPolicyRequest.newBuilder().setAlertPolicy(alertPolicy).build();
// process update policy request
AlertPolicy updatedAlertPolicy = alertPolicyServiceClient.updateAlertPolicy(updateAlertPolicyRequest);
System.out.print("alert policy updated successfully:" + updatedAlertPolicy.getName());
}
}
use of com.google.monitoring.v3.AlertPolicy in project java-monitoring by googleapis.
the class EnableDisableAlertPolicy method enableDisableAlertPolicy.
public static void enableDisableAlertPolicy(String alertPolicyId, boolean status, String displayName) throws ApiException, IOException {
// once, and can be reused for multiple requests.
try (AlertPolicyServiceClient alertPolicyServiceClient = AlertPolicyServiceClient.create()) {
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
AlertPolicy.Condition condition = AlertPolicy.Condition.newBuilder().setDisplayName(displayName).setConditionThreshold(metricThreshold).build();
// Create alert policy for update
AlertPolicy alertPolicy = AlertPolicy.newBuilder().setName(alertPolicyId).setDisplayName(displayName).addConditions(condition).setCombiner(AlertPolicy.ConditionCombinerType.AND).setEnabled(BoolValue.newBuilder().setValue((status == true) ? Boolean.TRUE : Boolean.FALSE).build()).build();
// Create update alert policy request
UpdateAlertPolicyRequest updateAlertPolicyRequest = UpdateAlertPolicyRequest.newBuilder().setAlertPolicy(alertPolicy).build();
// Process update policy request
AlertPolicy updatedAlertPolicy = alertPolicyServiceClient.updateAlertPolicy(updateAlertPolicyRequest);
System.out.format("alert policy enable disable status:%s%n", updatedAlertPolicy.getEnabled());
}
}
use of com.google.monitoring.v3.AlertPolicy in project java-docs-samples by GoogleCloudPlatform.
the class AlertSample method getAlertPolicies.
private List<AlertPolicy> getAlertPolicies(String projectId) {
List<AlertPolicy> alertPolicies = Lists.newArrayList();
ListAlertPoliciesPagedResponse response = alertPolicyClient.listAlertPolicies(ProjectName.of(projectId));
for (AlertPolicy policy : response.iterateAll()) {
alertPolicies.add(policy);
}
return alertPolicies;
}
use of com.google.monitoring.v3.AlertPolicy in project java-docs-samples by GoogleCloudPlatform.
the class AlertSample method writePoliciesBackupFile.
private void writePoliciesBackupFile(String projectId, String filePath, List<AlertPolicy> alertPolicies, List<NotificationChannel> notificationChannels) throws IOException {
JsonObject backupContents = new JsonObject();
backupContents.add("project_id", new JsonPrimitive(projectId));
JsonArray policiesJson = new JsonArray();
for (AlertPolicy policy : alertPolicies) {
policiesJson.add(gson.toJsonTree(policy));
}
backupContents.add("policies", policiesJson);
JsonArray notificationsJson = new JsonArray();
for (NotificationChannel channel : notificationChannels) {
notificationsJson.add(gson.toJsonTree(channel));
}
backupContents.add("notification_channels", notificationsJson);
FileWriter writer = new FileWriter(filePath);
writer.write(backupContents.toString());
writer.close();
}
use of com.google.monitoring.v3.AlertPolicy in project java-docs-samples by GoogleCloudPlatform.
the class AlertSample method restorePolicies.
// [END monitoring_alert_backup_policies]
// [START monitoring_alert_restore_policies]
void restorePolicies(String projectId, String filePath) throws IOException {
FileReader reader = new FileReader(filePath);
BufferedReader bufferedReader = new BufferedReader(reader);
JsonObject backupContent = getPolicyJsonContents(filePath, bufferedReader, gson);
String backupProjectId = backupContent.get("project_id").getAsString();
boolean isSameProject = projectId.equals(backupProjectId);
AlertPolicy[] policies = gson.fromJson(backupContent.get("policies"), AlertPolicy[].class);
List<NotificationChannel> notificationChannels = readNotificationChannelsJson(backupContent);
Map<String, String> restoredChannelIds = restoreNotificationChannels(projectId, notificationChannels, isSameProject);
List<AlertPolicy> policiesToRestore = reviseRestoredPolicies(policies, isSameProject, restoredChannelIds);
restoreRevisedPolicies(projectId, isSameProject, policiesToRestore);
}
Aggregations