Search code examples
google-cloud-platformgcloudstackdriver

Create a Stackdriver Alert Policy using gcloud? (without loading from file)


So I setup a notification channel with:

gcloud alpha monitoring channels create \
    --description='test notification' \
    --display-name='test_channel' \
    --type=email \
    [email protected]

But I can't seem to get an Alert Policy to work with the command line (trying to avoid using load from file as I would need to use a template). It seems like this should work:

gcloud alpha monitoring policies create \
    --notification-channels=test_channel \
    --aggregation='{"alignmentPeriod": "60s","perSeriesAligner": "ALIGN_MEAN"}' \
    --condition-display-name='CPU Utilization >0.80 for 10m'\
    --condition-filter='metric.type="appengine.googleapis.com/flex/instance/cpu/utilization" resource.type="gae_instance"' \
    --duration='1min' \
    --if='> 0.80' \
    --display-name='test alert'

But it returns:

Invalid value for [--aggregation]: Should only be specified if --condition-filter is also specified.

However, as you can see, there's a --condition-filter flag above. I tried reordering it so --condition-filter appears before --aggregation, but this is causing a duration error, although it already matches the documentation, and slight edits like =60s, =1min, or adding quotes around the time in --aggregation doesn't seem to help:

gcloud alpha monitoring policies create \
    --notification-channels=test_channel \
    --condition-filter='metric.type=appengine.googleapis.com/flex/instance/cpu/utilization resource.type=gae_instance' \
    --aggregation='{"alignmentPeriod": "20s","perSeriesAligner": "ALIGN_MEAN"}' \
    --condition-display-name='CPU Utilization >0.80 for 1m'\
    --duration='1min' \
    --if='> 0.80' \
    --display-name='test alert'

What is wrong? Why am I getting these errors?


Solution

  • After some deliberate trial an error, I managed to get this working with a few changes.

    First off, I highly recommend you to use the latest Cloud SDK version - update it to the latest if you haven't already with gcloud components update. In my case, I used the Cloud SDK v275.0.0.

    The first command snippet you provided worked correctly to create a notification channel, so that should be kept the same:

    gcloud alpha monitoring channels create \
        --description='test notification' \
        --display-name='test_channel' \
        --type=email \
        [email protected]
    

    In order to create a Stackdriver Monitoring policy though, I had to perform some changes:

    • The duration specified by the --duration flag doesn't seem to recognize the unit well; I had to change from --duration='1min' to --duration='1m' for it in order not to throw an error.

    • A combiner for the Alert Policy was needed, so I added one with the --combiner option set to AND in this case: --combiner='AND'.

    • The notification channel specified by the --notification-channels flag requires and ID or fully qualified identifier - it doesn't parse the display name, so I changed that to --notification-channels=13234113421234567.

      You can find the ID or fully qualified identifier of the notification channel you previously created with:

      gcloud alpha monitoring channels list \
         --filter='displayName="test_channel"' \
         --format='value(name)'
      

    At the end, the gcloud command to create the Alert Policy should look similar to this:

    gcloud alpha monitoring policies create \
        --notification-channels=13234113421234567 \
        --aggregation='{"alignmentPeriod": "60s","perSeriesAligner": "ALIGN_MEAN"}' \
        --condition-display-name='CPU Utilization >0.80 for 10m'\
        --condition-filter='metric.type="appengine.googleapis.com/flex/instance/cpu/utilization" resource.type="gae_instance"' \
        --duration='1m' \
        --if='> 0.80' \
        --display-name='test alert' \
        --combiner='AND'
    

    Note that this command is in an alpha state of development hence some features or functionalities may not be fully fleshed out and / or tested.