Search in sources :

Example 6 with AlertPolicyServiceClient

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

the class AlertSample method listAlertPolicies.

// [START monitoring_alert_list_policies]
private static void listAlertPolicies(String projectId) throws IOException {
    try (AlertPolicyServiceClient client = AlertPolicyServiceClient.create()) {
        ListAlertPoliciesPagedResponse response = client.listAlertPolicies(ProjectName.of(projectId));
        System.out.println("Alert Policies:");
        for (AlertPolicy policy : response.iterateAll()) {
            System.out.println(String.format("\nPolicy %s\nalert-id: %s", policy.getDisplayName(), policy.getName()));
            int channels = policy.getNotificationChannelsCount();
            if (channels > 0) {
                System.out.println("notification-channels:");
                for (int i = 0; i < channels; i++) {
                    System.out.println("\t" + policy.getNotificationChannels(i));
                }
            }
            if (policy.hasDocumentation() && policy.getDocumentation().getContent() != null) {
                System.out.println(policy.getDocumentation().getContent());
            }
        }
    }
}
Also used : AlertPolicy(com.google.monitoring.v3.AlertPolicy) AlertPolicyServiceClient(com.google.cloud.monitoring.v3.AlertPolicyServiceClient) ListAlertPoliciesPagedResponse(com.google.cloud.monitoring.v3.AlertPolicyServiceClient.ListAlertPoliciesPagedResponse)

Example 7 with AlertPolicyServiceClient

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

the class AlertSample method replaceChannels.

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

Example 8 with AlertPolicyServiceClient

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

the class GetAlertPolicy method getAlertPolicy.

public static void getAlertPolicy(String alertPolicyName) throws ApiException, IOException {
    // once, and can be reused for multiple requests.
    try (AlertPolicyServiceClient alertPolicyServiceClient = AlertPolicyServiceClient.create()) {
        // Gets a single alerting policy
        AlertPolicy alertPolicy = alertPolicyServiceClient.getAlertPolicy(alertPolicyName);
        System.out.format("alert policy retrieved successfully:%s", alertPolicy.getName());
    }
}
Also used : AlertPolicy(com.google.monitoring.v3.AlertPolicy) AlertPolicyServiceClient(com.google.cloud.monitoring.v3.AlertPolicyServiceClient)

Example 9 with AlertPolicyServiceClient

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

the class ListAlertPolicy method listAlertPolicy.

public static void listAlertPolicy(String projectId) throws ApiException, IOException {
    // once, and can be reused for multiple requests.
    try (AlertPolicyServiceClient alertPolicyServiceClient = AlertPolicyServiceClient.create()) {
        // Lists the existing alerting policies for the project.
        ListAlertPoliciesRequest listAlertPoliciesRequest = ListAlertPoliciesRequest.newBuilder().setName(ProjectName.of(projectId).toString()).build();
        // process response
        AlertPolicyServiceClient.ListAlertPoliciesPagedResponse response = alertPolicyServiceClient.listAlertPolicies(listAlertPoliciesRequest);
        // List all the policy.
        response.iterateAll().forEach(alertPolicy -> System.out.format("success! alert policy %s is policyId %s%n", alertPolicy.getDisplayName(), alertPolicy.getName()));
    }
}
Also used : ListAlertPoliciesRequest(com.google.monitoring.v3.ListAlertPoliciesRequest) AlertPolicyServiceClient(com.google.cloud.monitoring.v3.AlertPolicyServiceClient)

Example 10 with AlertPolicyServiceClient

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

the class CreateAlertPolicy method createAlertPolicy.

public static void createAlertPolicy(String projectId, String alertPolicyName) throws ApiException, IOException {
    // once, and can be reused for multiple requests.
    try (AlertPolicyServiceClient alertPolicyServiceClient = AlertPolicyServiceClient.create()) {
        ProjectName name = ProjectName.of(projectId);
        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 object
        AlertPolicy.Condition alertPolicyCondition = AlertPolicy.Condition.newBuilder().setDisplayName(alertPolicyName).setConditionThreshold(metricThreshold).build();
        // Build an alert policy
        AlertPolicy alertPolicy = AlertPolicy.newBuilder().setDisplayName(alertPolicyName).addConditions(alertPolicyCondition).setCombiner(AlertPolicy.ConditionCombinerType.AND).build();
        // Create an alert policy
        AlertPolicy actualAlertPolicy = alertPolicyServiceClient.createAlertPolicy(name, alertPolicy);
        System.out.format("alert policy created:%s", actualAlertPolicy.getName());
    }
}
Also used : Aggregation(com.google.monitoring.v3.Aggregation) ProjectName(com.google.monitoring.v3.ProjectName) AlertPolicy(com.google.monitoring.v3.AlertPolicy) AlertPolicyServiceClient(com.google.cloud.monitoring.v3.AlertPolicyServiceClient) Duration(com.google.protobuf.Duration)

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