Search in sources :

Example 16 with AlertPolicy

use of com.google.monitoring.v3.AlertPolicy in project java-docs-samples by GoogleCloudPlatform.

the class AlertSample method enablePolicies.

// [END monitoring_alert_replace_channels]
// [START monitoring_alert_enable_policies]
// [START monitoring_alert_disable_policies]
private static void enablePolicies(String projectId, String filter, boolean enable) throws IOException {
    try (AlertPolicyServiceClient client = AlertPolicyServiceClient.create()) {
        ListAlertPoliciesPagedResponse response = client.listAlertPolicies(ListAlertPoliciesRequest.newBuilder().setName(ProjectName.of(projectId).toString()).setFilter(filter).build());
        for (AlertPolicy policy : response.iterateAll()) {
            if (policy.getEnabled().getValue() == enable) {
                System.out.println(String.format("Policy %s is already %b.", policy.getName(), enable ? "enabled" : "disabled"));
                continue;
            }
            AlertPolicy updatedPolicy = AlertPolicy.newBuilder().setName(policy.getName()).setEnabled(BoolValue.newBuilder().setValue(enable)).build();
            AlertPolicy result = client.updateAlertPolicy(FieldMask.newBuilder().addPaths("enabled").build(), updatedPolicy);
            System.out.println(String.format("%s %s", result.getDisplayName(), result.getEnabled().getValue() ? "enabled" : "disabled"));
        }
    }
}
Also used : AlertPolicy(com.google.monitoring.v3.AlertPolicy) AlertPolicyServiceClient(com.google.cloud.monitoring.v3.AlertPolicyServiceClient) ListAlertPoliciesPagedResponse(com.google.cloud.monitoring.v3.AlertPolicyServiceClient.ListAlertPoliciesPagedResponse)

Example 17 with AlertPolicy

use of com.google.monitoring.v3.AlertPolicy in project java-docs-samples by GoogleCloudPlatform.

the class AlertSample method reviseRestoredPolicies.

private static List<AlertPolicy> reviseRestoredPolicies(AlertPolicy[] policies, boolean isSameProject, Map<String, String> restoredChannelIds) {
    List<AlertPolicy> newPolicies = Lists.newArrayListWithCapacity(policies.length);
    for (AlertPolicy policy : policies) {
        AlertPolicy.Builder policyBuilder = policy.toBuilder().clearNotificationChannels().clearMutationRecord().clearCreationRecord();
        // Update restored notification channel names.
        for (String channelName : policy.getNotificationChannelsList()) {
            String newChannelName = restoredChannelIds.get(channelName);
            if (!Strings.isNullOrEmpty(newChannelName)) {
                policyBuilder.addNotificationChannels(newChannelName);
            }
        }
        if (!isSameProject) {
            policyBuilder.clearName();
            policyBuilder.clearConditions();
            for (AlertPolicy.Condition condition : policy.getConditionsList()) {
                policyBuilder.addConditions(condition.toBuilder().clearName());
            }
        }
        newPolicies.add(policyBuilder.build());
    }
    return newPolicies;
}
Also used : AlertPolicy(com.google.monitoring.v3.AlertPolicy)

Example 18 with AlertPolicy

use of com.google.monitoring.v3.AlertPolicy in project java-security-private-ca by googleapis.

the class MonitorCertificateAuthority method createCaMonitoringPolicy.

// Creates a monitoring policy that notifies you 30 days before a managed CA expires.
public static void createCaMonitoringPolicy(String project) throws IOException {
    /* Initialize client that will be used to send requests. This client only needs to be created
    once, and can be reused for multiple requests. After completing all of your requests, call
    the `client.close()` method on the client to safely
    clean up any remaining background resources. */
    try (AlertPolicyServiceClient client = AlertPolicyServiceClient.create();
        NotificationChannelServiceClient notificationClient = NotificationChannelServiceClient.create()) {
        String policyName = "policy-name";
        /* Query which indicates the resource to monitor and the constraints.
      Here, the alert policy notifies you 30 days before a managed CA expires.
      For more info on creating queries, see: https://cloud.google.com/monitoring/mql/alerts */
        String query = "fetch privateca.googleapis.com/CertificateAuthority" + "| metric 'privateca.googleapis.com/ca/cert_chain_expiration'" + "| group_by 5m," + "[value_cert_chain_expiration_mean: mean(value.cert_chain_expiration)]" + "| every 5m" + "| condition val() < 2.592e+06 's'";
        // Create a notification channel.
        NotificationChannel notificationChannel = NotificationChannel.newBuilder().setType("email").putLabels("email_address", "java-docs-samples-testing@google.com").build();
        NotificationChannel channel = notificationClient.createNotificationChannel(ProjectName.of(project), notificationChannel);
        // Set the query and notification channel.
        AlertPolicy alertPolicy = AlertPolicy.newBuilder().setDisplayName(policyName).addConditions(Condition.newBuilder().setDisplayName("ca-cert-chain-expiration").setConditionMonitoringQueryLanguage(MonitoringQueryLanguageCondition.newBuilder().setQuery(query).build()).build()).setCombiner(ConditionCombinerType.AND).addNotificationChannels(channel.getName()).build();
        AlertPolicy policy = client.createAlertPolicy(ProjectName.of(project), alertPolicy);
        System.out.println("Monitoring policy successfully created !" + policy.getName());
    }
}
Also used : NotificationChannel(com.google.monitoring.v3.NotificationChannel) AlertPolicy(com.google.monitoring.v3.AlertPolicy) AlertPolicyServiceClient(com.google.cloud.monitoring.v3.AlertPolicyServiceClient) NotificationChannelServiceClient(com.google.cloud.monitoring.v3.NotificationChannelServiceClient)

Example 19 with AlertPolicy

use of com.google.monitoring.v3.AlertPolicy in project java-docs-samples by GoogleCloudPlatform.

the class AlertSample method enablePolicies.

// [END monitoring_alert_replace_channels]
// [START monitoring_alert_enable_policies]
// [START monitoring_alert_disable_policies]
void enablePolicies(String projectId, String filter, boolean enable) {
    ListAlertPoliciesPagedResponse response = alertPolicyClient.listAlertPolicies(ListAlertPoliciesRequest.newBuilder().setName(projectId).setFilter(filter).build());
    for (AlertPolicy policy : response.iterateAll()) {
        if (policy.getEnabled().getValue() == enable) {
            outputStream.println(String.format("Policy %s is already %b.", policy.getName(), enable));
            continue;
        }
        AlertPolicy updatedPolicy = AlertPolicy.newBuilder().setName(AlertPolicyName.of(projectId, policy.getName()).toString()).setEnabled(BoolValue.newBuilder().setValue(enable)).build();
        AlertPolicy result = alertPolicyClient.updateAlertPolicy(FieldMask.newBuilder().addPaths("enabled").build(), updatedPolicy);
        outputStream.println(String.format("%s %s", result.getName(), result.getEnabled().getValue() ? "Enabled" : "Disabled"));
    }
}
Also used : AlertPolicy(com.google.monitoring.v3.AlertPolicy) ListAlertPoliciesPagedResponse(com.google.cloud.monitoring.v3.AlertPolicyServiceClient.ListAlertPoliciesPagedResponse)

Example 20 with AlertPolicy

use of com.google.monitoring.v3.AlertPolicy in project java-docs-samples by GoogleCloudPlatform.

the class AlertSample method replaceChannels.

// [END monitoring_alert_restore_policies]
// [START monitoring_alert_replace_channels]
void replaceChannels(String projectId, String alertPolicyId, String[] channelIds) {
    AlertPolicy.Builder policyBuilder = AlertPolicy.newBuilder().setName(AlertPolicyName.of(projectId, alertPolicyId).toString());
    for (String channelId : channelIds) {
        policyBuilder.addNotificationChannels(NotificationChannelName.of(projectId, channelId).toString());
    }
    AlertPolicy result = alertPolicyClient.updateAlertPolicy(FieldMask.newBuilder().addPaths("notification_channels").build(), policyBuilder.build());
    outputStream.println(String.format("Updated %s", result.getName()));
}
Also used : AlertPolicy(com.google.monitoring.v3.AlertPolicy)

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