'unable to create a gcloud alert policy in command line with multiple conditions

I am trying to create a single alert policy for Cloud-Sql instance_state through gcloud with multiple conditions.

If the instance is in "RUNNABLE" OR "FAILED" state for more than 5 minutes, then a alert should be triggerred. I was able to create that in console and below is the screenshot: enter image description here

Now I try the same using the command line and give this gcloud command:

    gcloud alpha monitoring policies create \
         --display-name='Test Database State Alert ('$PROJECTID')' \
         --condition-display-name='Instance is not running for 5 minutes'\
         --notification-channels="x23234dfdfffffff" \
         --aggregation='{"alignmentPeriod": "60s","perSeriesAligner": "ALIGN_COUNT_TRUE"}' \
         --condition-filter='metric.type="cloudsql.googleapis.com/database/instance_state" AND resource.type="cloudsql_database" AND (metric.labels.state = "RUNNABLE")' 
OR 'metric.type="cloudsql.googleapis.com/database/instance_state" AND resource.type="cloudsql_database" AND (metric.labels.state = "FAILED")' \
         --duration='300s' \
         --if='> 0.0' \
         --trigger-count=1 \
         --combiner='OR' \
         --documentation='The rule "${condition.display_name}" has generated this alert for the "${metric.display_name}".' \
         --project="$PROJECTID" \
         --enabled

I am getting the error below in the OR part of the condition:

ERROR: (gcloud.alpha.monitoring.policies.create) unrecognized arguments:
  OR
  metric.type="cloudsql.googleapis.com/database/instance_state" AND resource.type="cloudsql_database" AND (metric.labels.state = "FAILED")

Even if i put ( ) over the condition still it fails, also the || operator also fails.

Can anyone please tell me the correct gcloud command for this? Also i want the structure of the alert policy to be similar to the one created in cloud-console as shown above

Thanks



Solution 1:[1]

Just stumbled upon this question.

From the docs gcloud alpha monitoring policies create, it appears that you can specify repeated (!) occurrences of:

[--aggregation=AGGREGATION --condition-display-name=CONDITION_DISPLAY_NAME --condition-filter=CONDITION_FILTER --duration=DURATION --if=IF_VALUE --trigger-count=TRIGGER_COUNT     | --trigger-percent=TRIGGER_PERCENT]

So I think you need to duplicate your --condition-filter with the --combiner="OR", i.e.

gcloud alpha monitoring policies create \
--display-name='Test Database State Alert ('$PROJECTID')' \
--notification-channels="x23234dfdfffffff" \
--aggregation='{"alignmentPeriod": "60s","perSeriesAligner": "ALIGN_COUNT_TRUE"}' \
--condition-display-name='RUNNABLE'\
--condition-filter='metric.type="cloudsql.googleapis.com/database/instance_state" AND resource.type="cloudsql_database" AND (metric.labels.state = "RUNNABLE")' 
--duration='300s' \
--if='> 0.0' \
--trigger-count=1 \
--aggregation='{"alignmentPeriod": "60s","perSeriesAligner": "ALIGN_COUNT_TRUE"}' \
--condition-display-name='FAILED'\
--condition-filter='metric.type="cloudsql.googleapis.com/database/instance_state" AND resource.type="cloudsql_database" AND (metric.labels.state = "FAILED")' \
--duration='300s' \
--if='> 0.0' \
--trigger-count=1 \
--combiner='OR' \
--documentation='The rule "${condition.display_name}" has generated this alert for the "${metric.display_name}".' \
--project="$PROJECTID" \
--enabled

Solution 2:[2]

I was able to use gcloud alpha monitoring policies conditions create to append additional conditions.

gcloud alpha monitoring policies create \
    --notification-channels=projects/qwiklabs-gcp-04-d822dd6cd419/notificationChannels/2510735656842641871 \
    --aggregation='{"alignmentPeriod": "60s","perSeriesAligner": "ALIGN_MEAN"}' \
    --condition-display-name='CPU Utilization >0.95 for 1m'\
    --condition-filter='metric.type="compute.googleapis.com/instance/cpu/utilization" resource.type="gce_instance"' \
    --duration='1m' \
    --if='> 0.95' \
    --display-name=' alert on spikes or consistantly high cpu' \
    --combiner='OR'

gcloud alpha monitoring policies list --format='value(name,displayName)'

gcloud alpha monitoring policies conditions create \
    projects/qwiklabs-gcp-04-d822dd6cd419/alertPolicies/1712202834227136574 \
    --aggregation='{"alignmentPeriod": "60s","perSeriesAligner": "ALIGN_MEAN"}' \
    --condition-display-name='CPU Utilization >0.80 for 10m'\
    --condition-filter='metric.type="compute.googleapis.com/instance/cpu/utilization" resource.type="gce_instance"' \
    --duration='10m' \
    --if='> 0.80' 

Duplicate --condition-filter clauses did not work for me. YMMV.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 DazWilkin
Solution 2 KCD