Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a monitoring-alerting-samples #29

Open
hurier-m opened this issue Mar 10, 2021 · 3 comments
Open

Create a monitoring-alerting-samples #29

hurier-m opened this issue Mar 10, 2021 · 3 comments

Comments

@hurier-m
Copy link
Contributor

This repository was really helpful for me to backup and restore Cloud Monitoring dashboards.

I'd like to have a similar repository for sharing alerting policies, following the same structure.

I already adapted the script (i.e., like alerting.sh) and tested it for AI/ML alert policies.

#!/usr/bin/env bash

set -eE

ACTION=${1:?"action [import | export ] is required as argument \$1"}

alerting_export () {
    # action is $1
    alert_id=${2:?"alert id is required as argument \$2"}
    project_id=${3:?"project id is required as argument \$3"}
    output_file=${4:?"output file name is requred as argument \$4"}

    project_number=$(gcloud projects describe "${project_id}" --format="value(projectNumber)")

    echo "Exporting alerting \"$alert_id\" from project \"$project_id\" to file \"${output_file}\""

    gcloud alpha monitoring policies describe \
        "projects/${project_number}/alertPolicies/${alert_id}" --format=json \
        | jq  'del(.name, .creationRecord, .mutationRecord, .notificationChannels)' \
        | jq  'del(.conditions[].name)' \
        > "${output_file}"

    echo "Export finished"
}

alerting_import () {
    # action is $1
    project_id=${2:?"project id is required as argument \$2"}
    input_file=${3:?"input file is requred as argument \$3"}

    gcloud alpha monitoring policies create \
        --project "${project_id}" \
        --policy-from-file "${input_file}"
}

if [ "$ACTION" = "export" ]; then
    alerting_export "$@"
elif [ "$ACTION" = "import" ]; then
    alerting_import "$@"
else
    echo "unknown action: ${ACTION}"
    exit 1
fi

Note: this script doesn't export and create notification channels.

@joyw-2018
Copy link
Contributor

Here are some samples on the GCP document site: https://cloud.google.com/monitoring/alerts/policies-in-json#policy_samples

@joyw-2018
Copy link
Contributor

@mmarquezv
Copy link

This repository was really helpful for me to backup and restore Cloud Monitoring dashboards.

I'd like to have a similar repository for sharing alerting policies, following the same structure.

I already adapted the script (i.e., like alerting.sh) and tested it for AI/ML alert policies.

#!/usr/bin/env bash

set -eE

ACTION=${1:?"action [import | export ] is required as argument \$1"}

alerting_export () {
    # action is $1
    alert_id=${2:?"alert id is required as argument \$2"}
    project_id=${3:?"project id is required as argument \$3"}
    output_file=${4:?"output file name is requred as argument \$4"}

    project_number=$(gcloud projects describe "${project_id}" --format="value(projectNumber)")

    echo "Exporting alerting \"$alert_id\" from project \"$project_id\" to file \"${output_file}\""

    gcloud alpha monitoring policies describe \
        "projects/${project_number}/alertPolicies/${alert_id}" --format=json \
        | jq  'del(.name, .creationRecord, .mutationRecord, .notificationChannels)' \
        | jq  'del(.conditions[].name)' \
        > "${output_file}"

    echo "Export finished"
}

alerting_import () {
    # action is $1
    project_id=${2:?"project id is required as argument \$2"}
    input_file=${3:?"input file is requred as argument \$3"}

    gcloud alpha monitoring policies create \
        --project "${project_id}" \
        --policy-from-file "${input_file}"
}

if [ "$ACTION" = "export" ]; then
    alerting_export "$@"
elif [ "$ACTION" = "import" ]; then
    alerting_import "$@"
else
    echo "unknown action: ${ACTION}"
    exit 1
fi

Note: this script doesn't export and create notification channels.

Thanks for this script. It was very useful for my alerts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants