Search in sources :

Example 6 with NotificationChannelServiceClient

use of com.google.cloud.monitoring.v3.NotificationChannelServiceClient 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)

Aggregations

NotificationChannelServiceClient (com.google.cloud.monitoring.v3.NotificationChannelServiceClient)6 NotificationChannel (com.google.monitoring.v3.NotificationChannel)4 NotificationChannelName (com.google.monitoring.v3.NotificationChannelName)2 BeforeClass (org.junit.BeforeClass)2 AlertPolicyServiceClient (com.google.cloud.monitoring.v3.AlertPolicyServiceClient)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 AlertPolicy (com.google.monitoring.v3.AlertPolicy)1 IOException (java.io.IOException)1 ParseException (org.apache.commons.cli.ParseException)1