This Python script checks the creation date of snapshots listed in a JSON file (snapshots.json
). If any snapshot is older than 30 days, it sends a POST request to a specified webhook URL, alerting you with the snapshot's resource ID.
- Python 3.x
requests
library
-
Clone or download this repository to your local machine.
-
Ensure you have Python 3 installed. You can download it from python.org.
-
Install the
requests
library if you don't have it already. You can install it using pip:pip install requests
-
Prepare the JSON File:
-
You can retrieve the JSON data using the
doctl
command line tool. Run the following command to get the list of snapshots and save it to a file namedsnapshots.json
:doctl compute snapshot list --output json > snapshots.json
-
Ensure the
snapshots.json
file is in the same directory as the script. The file should contain snapshot data in the following format:[ { "id": "129721643", "name": "opnsense-lon1-01-1680180362752", "resource_id": "348280077", "resource_type": "droplet", "regions": ["lon1", "ams3"], "min_disk_size": 50, "size_gigabytes": 2.96, "created_at": "2023-03-30T12:46:04Z" }, { "id": "141692420", "name": "outl-lon1-01 2023-10-04", "resource_id": "375470875", "resource_type": "droplet", "regions": ["lon1", "ams3"], "min_disk_size": 80, "size_gigabytes": 2.07, "created_at": "2023-10-04T04:56:58Z" } ]
-
-
Set the Webhook URL:
- Open the script file (
snapshot_alert.py
) and replace'https://example.com/webhook'
with your actual webhook URL.
- Open the script file (
-
Run the Script:
-
Execute the script using Python:
python snapshot_alert.py
-
-
Check the Output:
- The script will check each snapshot's
created_at
date. If a snapshot is older than 30 days, it will send an alert to the webhook and print the result.
- The script will check each snapshot's
The script performs the following steps:
-
Loads the JSON Data:
- Reads the
snapshots.json
file and parses its content.
- Reads the
-
Defines the Time Threshold:
- Calculates a date that is 30 days before the current date and time.
-
Iterates Over Snapshots:
- For each snapshot, it checks if the
created_at
date is older than the time threshold.
- For each snapshot, it checks if the
-
Sends Alerts:
- If a snapshot is older than 30 days, sends a POST request to the webhook URL with the snapshot's
resource_id
and an alert message.
- If a snapshot is older than 30 days, sends a POST request to the webhook URL with the snapshot's
Alert sent for snapshot 129721643
Alert sent for snapshot 141692420
If the alert fails to send, you will see:
Failed to send alert for snapshot 129721643: <HTTP status code>
This project is licensed under the MIT License. See the LICENSE file for details.
If you find any issues or have suggestions for improvements, feel free to open an issue or submit a pull request.
This script uses the following libraries:
- Requests: HTTP library for Python.
If you wish to learn more about DigitalOcean's services, you are welcome to reach out to the sales team at [email protected]. A global team of talented engineers will be happy to provide assistance.