Get notified when Google Cloud external static IPs are not in use, so you can free them up and save money.
I was reviewing my Google Cloud Platform (GCP) bill and noticed the following:
That’s my fault, not Google’s. I reserved a static IP but forgot to release it after deleting the VM. I don’t think it’s just me.
I looked around the GCP console and couldn’t find an option to alert me if a static IP is not used. It may be a request regarding a product feature.
But I have no intention of repeating my mistakes.
Thanks to gcloud CLI provided by the Google Cloud SDK , you can do almost anything within your scripts. I thought I would write a script that would run daily and notify me when a static IP is not in use. There are multiple ways to receive notifications, but the first and most convenient method is email. However, since I don’t have a mail server running, I had to find an alternative.
Within a few minutes of searching, I found Pushbullet . This is a notification system that can be used to push alerts to Chrome, Firefox, Safari, Opera, Android, iOS, and Windows. Almost everything. What else do I need?
We can assume that Pushbullet is a prerequisite to receive notifications if the mail server is not running on your server or is located where you want to run the gcloud command.
If you’re moving to Pushbullet, create a free account, set up where you want to receive notifications, and go to Settings to generate an access token.
Assume that you have an access token in place and the gcloud command is working on your server.
Here’s a small script. Create a file with the following content. For example, <span class="s1">gcp-unused-ip-monitor.sh</span> .
#!/bin/bash
if gcloud compute addresses list | grep EXTERNAL | grep RESERVED ; then
curl -u $Access_TOKEN: https://api.pushbullet.com/v2/pushes -d type=note -d title="Google Cloud IP NOT_INUSE" -d body="Some external IP is not in use"
else :
fiDon’t forget to update $Access_TOKEN with your actual value.
Save the file and give it executable permissions to make it executable.
chmod 755 gcp-unused-ip-monitor.shVerify alerts by running the script manually. If there are any unused IPs, an alert will be displayed.
The notification above is from Chrome with Pushbullet set as an alert. However, as mentioned above, alerts can be pushed to your mobile device or another browser. Choose the one you like.
Now we have a working script. Then I need to run this automatically every day.
To schedule a script to run daily, use Crontab, available for UNIX-based operating systems.
Edit crontab with crontab -e and add the following
0 0 * * * $PATH/gcp-unused-ip-monitor.sh > /dev/nullUpdate $PATH to the path where your script is located. The above cron entry will run the script every midnight. You can adjust the execution time according to your preference.
How easy is it?
Once you receive the alert, you can go to the GCP console and check and release the NOT IN USE IP.
I hope this little script can save you some money.




![How to set up a Raspberry Pi web server in 2021 [Guide]](https://i0.wp.com/pcmanabu.com/wp-content/uploads/2019/10/web-server-02-309x198.png?w=1200&resize=1200,0&ssl=1)











































