Search in sources :

Example 11 with AlertPolicy

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());
    }
}
Also used : Aggregation(com.google.monitoring.v3.Aggregation) AlertPolicy(com.google.monitoring.v3.AlertPolicy) UpdateAlertPolicyRequest(com.google.monitoring.v3.UpdateAlertPolicyRequest) AlertPolicyServiceClient(com.google.cloud.monitoring.v3.AlertPolicyServiceClient) Duration(com.google.protobuf.Duration)

Example 12 with AlertPolicy

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());
    }
}
Also used : Aggregation(com.google.monitoring.v3.Aggregation) AlertPolicy(com.google.monitoring.v3.AlertPolicy) UpdateAlertPolicyRequest(com.google.monitoring.v3.UpdateAlertPolicyRequest) AlertPolicyServiceClient(com.google.cloud.monitoring.v3.AlertPolicyServiceClient) Duration(com.google.protobuf.Duration)

Example 13 with AlertPolicy

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;
}
Also used : AlertPolicy(com.google.monitoring.v3.AlertPolicy) ListAlertPoliciesPagedResponse(com.google.cloud.monitoring.v3.AlertPolicyServiceClient.ListAlertPoliciesPagedResponse)

Example 14 with AlertPolicy

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();
}
Also used : JsonArray(com.google.gson.JsonArray) NotificationChannel(com.google.monitoring.v3.NotificationChannel) JsonPrimitive(com.google.gson.JsonPrimitive) AlertPolicy(com.google.monitoring.v3.AlertPolicy) FileWriter(java.io.FileWriter) JsonObject(com.google.gson.JsonObject)

Example 15 with AlertPolicy

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);
}
Also used : NotificationChannel(com.google.monitoring.v3.NotificationChannel) AlertPolicy(com.google.monitoring.v3.AlertPolicy) BufferedReader(java.io.BufferedReader) JsonObject(com.google.gson.JsonObject) FileReader(java.io.FileReader)

Aggregations

AlertPolicy (com.google.monitoring.v3.AlertPolicy)33 Test (org.junit.Test)16 AbstractMessage (com.google.protobuf.AbstractMessage)11 AlertPolicyServiceClient (com.google.cloud.monitoring.v3.AlertPolicyServiceClient)9 ListAlertPoliciesPagedResponse (com.google.cloud.monitoring.v3.AlertPolicyServiceClient.ListAlertPoliciesPagedResponse)8 ArrayList (java.util.ArrayList)7 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)5 ListAlertPoliciesRequest (com.google.monitoring.v3.ListAlertPoliciesRequest)5 NotificationChannel (com.google.monitoring.v3.NotificationChannel)5 StatusRuntimeException (io.grpc.StatusRuntimeException)5 JsonObject (com.google.gson.JsonObject)4 CreateAlertPolicyRequest (com.google.monitoring.v3.CreateAlertPolicyRequest)4 ListAlertPoliciesResponse (com.google.monitoring.v3.ListAlertPoliciesResponse)4 ProjectName (com.google.monitoring.v3.ProjectName)4 Aggregation (com.google.monitoring.v3.Aggregation)3 FolderName (com.google.monitoring.v3.FolderName)3 OrganizationName (com.google.monitoring.v3.OrganizationName)3 UpdateAlertPolicyRequest (com.google.monitoring.v3.UpdateAlertPolicyRequest)3 Duration (com.google.protobuf.Duration)3 JsonArray (com.google.gson.JsonArray)2