This document provides samples of alerting policies. The samples are written in JSON, and they use Monitoring filters. You can create policies in either JSON or YAML, regardless of whether you define the policy by using Monitoring filters or Prometheus Query Language (PromQL). Google Cloud CLI can read and write both JSON and YAML, while the REST API can read JSON.
To learn about alerting policies that use PromQL, see the following documents:
For information about how to configure alerting policy fields, see the following:
- Create metric-based alerting policies by using the Google Cloud console
- Create metric-based alerting policies by using the API
Generate YAML for existing policies
To generate YAML representations of your existing alerting policies, use the
gcloud monitoring policies list command to list your
policies and the gcloud monitoring policies describe
command to print the policy.
To generate YAML representations of your existing notification channels, use the
gcloud monitoring channels list command to list your
channels and the gcloud monitoring channels describe
command to print the channel configuration.
If you don't include the --format flag in the Google Cloud CLI commands, then,
the format defaults to YAML for both gcloud ... describe commands.
For example, the following gcloud monitoring policies describe command
retrieves a single policy named
projects/a-gcp-project/alertPolicies/12669073143329903307 and the redirect
(>) copies the output to the test-policy.yaml file:
gcloud monitoring policies describe projects/a-gcp-project/alertPolicies/12669073143329903307 > test-policy.yaml
Generate JSON for existing policies
To generate JSON representations of your existing alerting policies and notification channels, do any of the following:
Add the
--format=jsonflag to thegcloudCLI commands described in Generate YAML for existing policies. For example, to list policies, run the following command:gcloud monitoring policies list --format=jsonUse the APIs Explorer widget on the reference page for each API method:
For alerting policies, see the
alertPolicies.listandalertPolicies.getmethods.For notification channels, see the
notificationChannels.listandnotificationChannels.getmethods.
For more information, see APIs Explorer.
Policy samples
As shown in the backup/restore example, you can use saved policies to create new copies of those policies.
You can use a policy saved in one project to create a new, or similar, policy in another project. However, you must first make the following changes in a copy of the saved policy:
- Remove the following fields from any notification channels:
nameverificationStatus
- Create notification channels before referring to the channels in alerting policies (you need the new channel identifiers).
- Remove the following fields from any alerting policies you are recreating:
namecondition.namecreationRecordmutationRecord
The policies in this document are organized using the same terminology that Monitoring in the Google Cloud console uses, for example, “rate-of-change policy”, and there are two types of conditions:
- A threshold condition; almost all of the policy types mentioned in the UI are variants of a threshold condition
- An absence condition
In the samples that follow, these conditions correspond to conditionThreshold
and conditionAbsent. For more information, see the reference page for
Condition.
You can create many of these policies manually, by using the Google Cloud console, but some can be created only by using the Monitoring API. For more information, see [Creating an alerting policy (UI)][alert-ui] or Create alerting policies by using the API.
Metric-threshold policy
A metric-threshold policy detects when some value crosses a predetermined boundary. Threshold policies let you know that something is approaching an important point, so you can take some action. For example, the condition for a metric-threshold policy is met when available disk space becomes less than 10 percent of total disk space.
The following alerting policy uses the average CPU usage as an indicator of the health of a group of VMs. The policy's condition is met when the average CPU utilization of the VMs in a project, measured over 60-second intervals, exceeds a threshold of 90-percent utilization for 15 minutes (900 seconds):
{
"displayName": "Very high CPU usage",
"combiner": "OR",
"conditions": [
{
"displayName": "CPU usage is extremely high",
"conditionThreshold": {
"aggregations": [
{
"alignmentPeriod": "60s",
"crossSeriesReducer": "REDUCE_MEAN",
"groupByFields": [
"project"
],
"perSeriesAligner": "ALIGN_MAX"
}
],
"comparison": "COMPARISON_GT",
"duration": "900s",
"filter"