3

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?

1
  • TLDR Version of answer is I was missing combiner --combiner='AND', I think I could also have done something like --condition-filter="resource.type=\"global\" AND metric.type=\"logging.googleapis.com/user/vpc_firewall_changes\"". And docs got me on an incorrect 1min formatting, when it should be 1m which I had tried but with other errors. The flag Channel Name: my-channel threw me off on notification-channels, docs do mention ID but missed that and thought all flags would work. Commented Jan 13, 2020 at 18:39

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the help.
0

Thank you @maxim, That helped me a a lot! Here is what i used to deploy log based metric that listens for a syslog message:

Create Log-Based Metric:

gcloud logging metrics create "S3-SQL-backup-failed-test2" \
    --description "The backup has failed go over the logs to find out why (tag: backup-to-s3)." \
    --log-filter "resource.type=gce_instance AND 
resource.labels.instance_id=${INSTANCE_ID} AND 
logName=projects/${PROJECT_ID}/logs/syslog AND
'SQL backup has failed failure occurred during the dump file exists validation'"

Create Monitor Email:

gcloud alpha monitoring channels create --display-name="${MONITOR_EMAIL_NAME}" /
    --type=email /
    --channel-labels=email_address="${MONITOR_EMAIL}"

Create Alert based on metric

gcloud alpha monitoring policies create \
    --notification-channels="${CHANNEL}" \
    --aggregation='{"alignmentPeriod": "60s","perSeriesAligner": "ALIGN_MEAN"}' \
    --condition-display-name='S3-SQL-backup-failed-test2'\
    --condition-filter='resource.type="gce_instance" AND metric.type="logging.googleapis.com/user/S3-SQL-backup-failed"' \
    --duration='0s' \
    --display-name='Alert-S3-SQL-backup-failed-test2' \
    --if='> 0' \
    --combiner='OR' \
    --trigger-count="1"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.