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"));
}
}
}
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;
}
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());
}
}
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"));
}
}
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()));
}
Aggregations