/monitoring-dashboard-samples

Primary LanguagePythonApache License 2.0Apache-2.0

Cloud Monitoring Dashboard Samples

This repository contains samples that may be used with the Cloud Monitoring Dashboards API.

How to use the samples

  1. Check it out from GitHub. For example, you can do it in Cloud Shell by clicking the button below:

    Open this project in Cloud Shell

  2. Use the gcloud monitoring dashboards create command to create a dashboard. Make sure you replace the [file-name.json] in the command:

gcloud monitoring dashboards create --config-from-file=[file_name.json]

Alternatively, you can run the following command via curl. Use the projects.dashboards.create to call the Dashboards API with the sample JSON and create a new dashboard. Make sure you replace the [project-id] and [file-name.json] in the command:

curl -X POST -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
https://monitoring.googleapis.com/v1/projects/[project-id]/dashboards -d @[file-name.json]

Steps to get the JSON configuration

If you want to export a dashboard to a JSON file and share it, you can use the following steps.

  1. Create the dashboard in the Monitoring UI (or even better, share one that you’ve already developed and used).

  2. Open the dashboard and copy the dashboard ID from the URL (the string is in the URL after the /monitoring/dashboards/custom/ ˆprefix). For example, 10768789961894600977 is the dashboard ID in the URL below:

    https://console.cloud.google.com/monitoring/dashboards/custom/10768789961894600977?project=sd-uxr-001&timeDomain=1h

  3. Use these commands to export the JSON configuration for your dashboard.

    First, set your environment variables, replacing the following values with your values:

    • DASH_ID with the dashboard ID copied from step #2
    • YOUR_PROJECT_ID with your project id that contains the dashboard
    • FILE_NAME with the output filename for the dashboard JSON config
    export DASH_ID=<YOUR_DASH_ID>
    
    export PROJECT_NUMBER=$(gcloud projects describe <YOUR_PROJECT_ID> 
        --format="value(projectNumber)")
    
    export FILE_NAME=<DASH_FILE_NAME>
  4. Use the gcloud command line to export the dashboard to a JSON file.

    gcloud monitoring dashboards describe \
    projects/$PROJECT_NUMBER/dashboards/$DASH_ID --format=json > $FILE_NAME   
  5. Open the exported JSON file in an editor and remove the etag / name fields. Alternatively, you can use the sed command on a Linux or MAC terminal, for example:

    sed -i '/"name":/d' $FILE_NAME
    sed -i '/"etag":/d' $FILE_NAME

    MAC version of the sed commands. Note the '' addition.

    sed -i '' '/"etag":/d' $FILE_NAME
    sed -i '' '/"name":/d' $FILE_NAME