Search code examples
monitoringgoogle-compute-enginedisk

How can I monitor the free space on a persistent disk in Google Compute Engine?


I've created a persistent disk in Google Compute Engine and attached it to an instance. What I am wondering however, is how can I monitor the free space on the disk? Can't see it either in the Google Cloud Console or from the gcloud tool.


Solution

  • If you cannot use the Stackdriver Monitoring Agent, an alternative will be to use the gcloud command line tool to write to cloud logging.

    Note that this is using a beta command, so may change in the future.

    First, ensure that the logging beta commands are installed by typing:

    gcloud beta logging
    

    If it does not work, you can install with:

    sudo gcloud components install beta
    

    On your instance setup a cron job to run some script according to a schedule:

    sudo crontab -e
    

    Go to the bottom of the file and enter:

    0 * * * * gcloud beta logging write disk_usage $(df / |  awk '{print $5}' | cut -d '%' -f 1 | tail -n 1)
    

    This will run the command gcloud beta logging write disk_usage $(df / | awk '{print $5}' | cut -d '%' -f 1 | tail -n 1) once an hour, which will write the % of used space to the disk_usage log.

    Next go to Logging and from the first drop down choose Global and from the second choose disk_space.

    It is not a perfect solution, but it will work.