Search in sources :

Example 1 with AlertPolicyServiceClient

use of com.google.cloud.monitoring.v3.AlertPolicyServiceClient 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 2 with AlertPolicyServiceClient

use of com.google.cloud.monitoring.v3.AlertPolicyServiceClient 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 3 with AlertPolicyServiceClient

use of com.google.cloud.monitoring.v3.AlertPolicyServiceClient in project java-monitoring by googleapis.

the class DeleteAlertPolicy method deleteAlertPolicy.

public static void deleteAlertPolicy(String alertPolicyId) throws ApiException, IOException {
    // once, and can be reused for multiple requests.
    try (AlertPolicyServiceClient alertPolicyServiceClient = AlertPolicyServiceClient.create()) {
        // Deletes an alerting policy
        alertPolicyServiceClient.deleteAlertPolicy(alertPolicyId);
        System.out.format("alert policy deleted successfully:%s", alertPolicyId);
    }
}
Also used : AlertPolicyServiceClient(com.google.cloud.monitoring.v3.AlertPolicyServiceClient)

Example 4 with AlertPolicyServiceClient

use of com.google.cloud.monitoring.v3.AlertPolicyServiceClient 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 5 with AlertPolicyServiceClient

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

AlertPolicyServiceClient (com.google.cloud.monitoring.v3.AlertPolicyServiceClient)10 AlertPolicy (com.google.monitoring.v3.AlertPolicy)8 Aggregation (com.google.monitoring.v3.Aggregation)3 Duration (com.google.protobuf.Duration)3 ListAlertPoliciesPagedResponse (com.google.cloud.monitoring.v3.AlertPolicyServiceClient.ListAlertPoliciesPagedResponse)2 UpdateAlertPolicyRequest (com.google.monitoring.v3.UpdateAlertPolicyRequest)2 NotificationChannelServiceClient (com.google.cloud.monitoring.v3.NotificationChannelServiceClient)1 ListAlertPoliciesRequest (com.google.monitoring.v3.ListAlertPoliciesRequest)1 NotificationChannel (com.google.monitoring.v3.NotificationChannel)1 ProjectName (com.google.monitoring.v3.ProjectName)1